SlideShare a Scribd company logo
Introduction to C++Dr. Darren Dancey
Hello World in C++#include <iostream>using namespace std;int main(){cout << "Hello World!" << endl;}C++
Hello World in Javaimport java.io.*;public class Helloworld{   public static  void main(Stringargs[])   {System.out.println("Hello World!");   }}Java
Much is the same…C++#include<iostream> usingnamespace std; intmain(char* args[], intargc){for(inti = 2; i < 100; i++){bool flag = true;	for (intj = 2; j <= i/2; j++ ){if (i%j == 0){				flag = false;break;			}		}if (flag == true){cout << i << endl;		}	}cin.get(); }
...javaJavapublic class week1{public static void main(Stringargs[]){for(inti = 2; i < 100; i++){booleanflag = true;			for (intj = 2; j <= i/2; j++ ){			if (i%j == 0){				flag = false;				break;			}		}		if (flag == true){System.out.println(i);		}	}}}
Compiling a C++ Program C:\Users\darren>cd %HOMEPATH%C:\Users\darren>notepad helloworld.cppC:\Users\darren>cl /EHschelloworld.cppMicrosoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86Copyright (C) Microsoft Corporation.  All rights reserved.helloworld.cppMicrosoft (R) Incremental Linker Version 8.00.50727.762Copyright (C) Microsoft Corporation.  All rights reserved./out:helloworld.exehelloworld.objC:\Users\darren>helloworldHello, World!C:\Users\darren>
Input and Output in C++Input and Output handled by a libraryI/O part of the C++ Standard LibraryStream Based I/OStream Based Abstraction provides a common interface to I/O for  harddisks, terminals, keyboards and the network
The coutObjectInstance of type ostreamDefined in Standard LibraryBound to Console (screen)Character Output StreamUses Operator overloading of << (left bit shift) to take arguments to printSimilar to cout << "Hello World!”cout.writeline(“Hello World”)
cout Examplescout << “Hello, World!”;cout << 5;cout << 5.1;intmyvar = 5 cout <<  myvar;string course = “SE5301”;cout << course;cout << course << “ is a “ << 5 << “star course”;
endl End LinePutting endl onto a stream moves to the next line.cout << “This is on line 1” << endl;cout << “This is on line 2” << endl;
cinPart of Standard LibarayInstance of istreamBound to the keyboardThe >> operator is overridden to provide input methodchar mychar;cin >> mychar;
cin examples	char mychar;cin >> mychar;intmyint;cin >> myint;	string mystr;cin >> mystr;
I/O Exampleint age; cout << "Please enter you age ";cin >> age;  if (age < 18){cout << "Being " << age;cout << " years old you are too young to vote" << endl;	}else{cout << "You are old enough to vote" << endl;	}cout << "Thank you for using vote-o-matic" << endl;
Pointers
Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;myvar1myvar2mydbl515.3
Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;int* ptrVar;ptrVar  = &myvar2;cout << *ptrVar ; // will print out 10 (value in myvar2)myvar1myvar2mydblptrVar5105.3AddressOf myvar2
Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;int* ptrVar ;ptrVar = &myvar2;*ptrVar = 20; cout << myvar2;  myvar1myvar2mydblptrVarmyvar25105.3AddressOf myvar220
Arrays in C++int myarray[5] = {10, 20, 30, 50, 5};	for (inti =0 ; i < 5; i++){cout << myarray[i] << " " ;	}cin.get();File:SimpleArrayTraversal
Pointer Arithmeticint myarray[5] = {10, 20, 30, 50, 5};int *ptr;ptr = &myarray[0];cout << "The value of *ptr is " << *ptr << endl;cout << "The value of *(ptr+2) is " << *(ptr+2) << endl;cout << "Array traversal with ptr" << endl;	for (inti = 0; i < 5; i++){cout << *(ptr+i) << endl;	}cout << "Array Traversal with moving ptr" << endl;	for (inti = 0; i < 5; i++){	cout << *ptr++ << endl;	}File:pointer Arithmetic
C/C++ can be tersewhile(*ptrString2++ = *ptrString1++);
PointersPointers are variables that hold a memory addressThe * operator get the value in the memory address *ptr get the value stored at the memory address in ptr.The & gets the memory address of a variableptr = &myvar get the memory address of myvar and stores it in ptr
Pointers and Pass by ReferenceC++ uses pass by value that means the parameters of a function are copies of the variables passed in.void myfunc (intfoo, int bar){…}Myfunc(a,b);The values in a and b are copied into foo and bar.
Why By ReferenceWant to avoid copying large amounts of data.Want the function to modify the value passed in.
Pointers as a SolutionMyfunc (int *foo, int *bar){ …}Myfunc(&a, &b)Still pass-by-value but pass in the value of the memory addresses of a and b.When the values pointed to by foo and bar are changed it will be changing a and b.
Objects in C++Dogage: Integerspeak()walk( location ) :string
Dog the .h fileclass dog{public:int age;	char* speak();moveTo(intx, inty);};C++
Dog the .cpp file#include "dog.h"#include <iostream>using namespace std;void Dog::speak(){cout << "Woof Woof!" << endl;}C++
Initializing a Dog C++#include <iostream>#include "dog.h"using namespace std;intmain(char* args[], intargc){cout << "Dog Program" << endl;	Dog fido;   //on stack    //intmyintfido.age = 5;fido.speak();cin.get();}
Scooby a Dynamic Dog C++Dog* scooby = new Dog();(*scooby).speak();  // these calls doscooby->speak();   //the same thingscooby->age = 6;
Objects and Pointersint myvar1= 5;int myvar2 = 1;double mydbl = 5.3;myvar1myvar2mydbl515.3
Pointersint myvar1= 5;int myvar2 = 1;double mydbl = 5.3;Dog* scooby = new Dog();scooby->age = 5; myvar1myvar2mydblscoobyage515.3Memoryaddress5
CallStackvs HeapCode example callStackExample
Directed Study/Further ReadingC++ Pointers http://www.cplusplus.com/doc/tutorial/pointers/Binky Videohttp://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/http://cslibrary.stanford.edu/104/

More Related Content

PPT
Savitch ch 08
PPT
Lecture 6
PPT
Advanced C programming
PPT
Savitch Ch 08
PPT
Fantom on the JVM Devoxx09 BOF
PPTX
Ponters
PDF
yield and return (poor English ver)
PDF
WebSummit 2015 - Gopher it
Savitch ch 08
Lecture 6
Advanced C programming
Savitch Ch 08
Fantom on the JVM Devoxx09 BOF
Ponters
yield and return (poor English ver)
WebSummit 2015 - Gopher it

What's hot (20)

PPT
Computer Programming- Lecture 5
PDF
Memory efficient pytorch
PPT
C pointers
PDF
Pointers & References in C++
PPTX
C++ Presentation
PPTX
Pointers in C
PDF
Java 5 New Feature
DOC
Logo tutorial
PPT
Buffer OverFlow
PDF
C++17 introduction - Meetup @EtixLabs
PDF
2 BytesC++ course_2014_c1_basicsc++
PPTX
Rust Intro
PDF
C++11
PPTX
C++ 11 Features
PPTX
Software Construction Assignment Help
PDF
Modern C++
PPT
Hooking signals and dumping the callstack
PDF
Lecturer23 pointersin c.ppt
PDF
C++11 & C++14
Computer Programming- Lecture 5
Memory efficient pytorch
C pointers
Pointers & References in C++
C++ Presentation
Pointers in C
Java 5 New Feature
Logo tutorial
Buffer OverFlow
C++17 introduction - Meetup @EtixLabs
2 BytesC++ course_2014_c1_basicsc++
Rust Intro
C++11
C++ 11 Features
Software Construction Assignment Help
Modern C++
Hooking signals and dumping the callstack
Lecturer23 pointersin c.ppt
C++11 & C++14
Ad

Similar to Cplusplus (20)

PDF
C++ L01-Variables
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
PPT
Chapter2
PPT
Lecture 3 c++
PPT
Lecture 2
PPT
C++ programming
PPTX
Cs1123 3 c++ overview
PPTX
Cs1123 11 pointers
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
PPTX
Testing lecture after lec 4
PDF
BASIC C++ PROGRAMMING
PPT
Pointer
PDF
Chapter 1.pdf
PPTX
Introduction to C++ lecture ************
PPTX
Intro to C++
PPT
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
PPT
Pengaturcaraan asas
PPTX
Presentation on C++ Programming Language
PPT
C++ Pointers And References
TXT
Advance C++notes
C++ L01-Variables
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
Chapter2
Lecture 3 c++
Lecture 2
C++ programming
Cs1123 3 c++ overview
Cs1123 11 pointers
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
Testing lecture after lec 4
BASIC C++ PROGRAMMING
Pointer
Chapter 1.pdf
Introduction to C++ lecture ************
Intro to C++
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
Pengaturcaraan asas
Presentation on C++ Programming Language
C++ Pointers And References
Advance C++notes
Ad

Recently uploaded (20)

PPT
Obstructive sleep apnea in orthodontics treatment
PPTX
anal canal anatomy with illustrations...
PPTX
NEET PG 2025 Pharmacology Recall | Real Exam Questions from 3rd August with D...
PPTX
ca esophagus molecula biology detailaed molecular biology of tumors of esophagus
PPT
ASRH Presentation for students and teachers 2770633.ppt
PPTX
Electromyography (EMG) in Physiotherapy: Principles, Procedure & Clinical App...
DOC
Adobe Premiere Pro CC Crack With Serial Key Full Free Download 2025
PPTX
Imaging of parasitic D. Case Discussions.pptx
PDF
شيت_عطا_0000000000000000000000000000.pdf
PPT
OPIOID ANALGESICS AND THEIR IMPLICATIONS
PPTX
Fundamentals of human energy transfer .pptx
PPTX
Transforming Regulatory Affairs with ChatGPT-5.pptx
PPTX
SKIN Anatomy and physiology and associated diseases
PDF
Human Health And Disease hggyutgghg .pdf
PPT
STD NOTES INTRODUCTION TO COMMUNITY HEALT STRATEGY.ppt
PPTX
15.MENINGITIS AND ENCEPHALITIS-elias.pptx
PPTX
History and examination of abdomen, & pelvis .pptx
PPTX
DENTAL CARIES FOR DENTISTRY STUDENT.pptx
PPT
1b - INTRODUCTION TO EPIDEMIOLOGY (comm med).ppt
DOCX
RUHS II MBBS Microbiology Paper-II with Answer Key | 6th August 2025 (New Sch...
Obstructive sleep apnea in orthodontics treatment
anal canal anatomy with illustrations...
NEET PG 2025 Pharmacology Recall | Real Exam Questions from 3rd August with D...
ca esophagus molecula biology detailaed molecular biology of tumors of esophagus
ASRH Presentation for students and teachers 2770633.ppt
Electromyography (EMG) in Physiotherapy: Principles, Procedure & Clinical App...
Adobe Premiere Pro CC Crack With Serial Key Full Free Download 2025
Imaging of parasitic D. Case Discussions.pptx
شيت_عطا_0000000000000000000000000000.pdf
OPIOID ANALGESICS AND THEIR IMPLICATIONS
Fundamentals of human energy transfer .pptx
Transforming Regulatory Affairs with ChatGPT-5.pptx
SKIN Anatomy and physiology and associated diseases
Human Health And Disease hggyutgghg .pdf
STD NOTES INTRODUCTION TO COMMUNITY HEALT STRATEGY.ppt
15.MENINGITIS AND ENCEPHALITIS-elias.pptx
History and examination of abdomen, & pelvis .pptx
DENTAL CARIES FOR DENTISTRY STUDENT.pptx
1b - INTRODUCTION TO EPIDEMIOLOGY (comm med).ppt
RUHS II MBBS Microbiology Paper-II with Answer Key | 6th August 2025 (New Sch...

Cplusplus

  • 1. Introduction to C++Dr. Darren Dancey
  • 2. Hello World in C++#include <iostream>using namespace std;int main(){cout << "Hello World!" << endl;}C++
  • 3. Hello World in Javaimport java.io.*;public class Helloworld{ public static void main(Stringargs[]) {System.out.println("Hello World!"); }}Java
  • 4. Much is the same…C++#include<iostream> usingnamespace std; intmain(char* args[], intargc){for(inti = 2; i < 100; i++){bool flag = true; for (intj = 2; j <= i/2; j++ ){if (i%j == 0){ flag = false;break; } }if (flag == true){cout << i << endl; } }cin.get(); }
  • 5. ...javaJavapublic class week1{public static void main(Stringargs[]){for(inti = 2; i < 100; i++){booleanflag = true; for (intj = 2; j <= i/2; j++ ){ if (i%j == 0){ flag = false; break; } } if (flag == true){System.out.println(i); } }}}
  • 6. Compiling a C++ Program C:\Users\darren>cd %HOMEPATH%C:\Users\darren>notepad helloworld.cppC:\Users\darren>cl /EHschelloworld.cppMicrosoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86Copyright (C) Microsoft Corporation. All rights reserved.helloworld.cppMicrosoft (R) Incremental Linker Version 8.00.50727.762Copyright (C) Microsoft Corporation. All rights reserved./out:helloworld.exehelloworld.objC:\Users\darren>helloworldHello, World!C:\Users\darren>
  • 7. Input and Output in C++Input and Output handled by a libraryI/O part of the C++ Standard LibraryStream Based I/OStream Based Abstraction provides a common interface to I/O for harddisks, terminals, keyboards and the network
  • 8. The coutObjectInstance of type ostreamDefined in Standard LibraryBound to Console (screen)Character Output StreamUses Operator overloading of << (left bit shift) to take arguments to printSimilar to cout << "Hello World!”cout.writeline(“Hello World”)
  • 9. cout Examplescout << “Hello, World!”;cout << 5;cout << 5.1;intmyvar = 5 cout << myvar;string course = “SE5301”;cout << course;cout << course << “ is a “ << 5 << “star course”;
  • 10. endl End LinePutting endl onto a stream moves to the next line.cout << “This is on line 1” << endl;cout << “This is on line 2” << endl;
  • 11. cinPart of Standard LibarayInstance of istreamBound to the keyboardThe >> operator is overridden to provide input methodchar mychar;cin >> mychar;
  • 12. cin examples char mychar;cin >> mychar;intmyint;cin >> myint; string mystr;cin >> mystr;
  • 13. I/O Exampleint age; cout << "Please enter you age ";cin >> age;  if (age < 18){cout << "Being " << age;cout << " years old you are too young to vote" << endl; }else{cout << "You are old enough to vote" << endl; }cout << "Thank you for using vote-o-matic" << endl;
  • 15. Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;myvar1myvar2mydbl515.3
  • 16. Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;int* ptrVar;ptrVar = &myvar2;cout << *ptrVar ; // will print out 10 (value in myvar2)myvar1myvar2mydblptrVar5105.3AddressOf myvar2
  • 17. Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;int* ptrVar ;ptrVar = &myvar2;*ptrVar = 20; cout << myvar2; myvar1myvar2mydblptrVarmyvar25105.3AddressOf myvar220
  • 18. Arrays in C++int myarray[5] = {10, 20, 30, 50, 5}; for (inti =0 ; i < 5; i++){cout << myarray[i] << " " ; }cin.get();File:SimpleArrayTraversal
  • 19. Pointer Arithmeticint myarray[5] = {10, 20, 30, 50, 5};int *ptr;ptr = &myarray[0];cout << "The value of *ptr is " << *ptr << endl;cout << "The value of *(ptr+2) is " << *(ptr+2) << endl;cout << "Array traversal with ptr" << endl; for (inti = 0; i < 5; i++){cout << *(ptr+i) << endl; }cout << "Array Traversal with moving ptr" << endl; for (inti = 0; i < 5; i++){ cout << *ptr++ << endl; }File:pointer Arithmetic
  • 20. C/C++ can be tersewhile(*ptrString2++ = *ptrString1++);
  • 21. PointersPointers are variables that hold a memory addressThe * operator get the value in the memory address *ptr get the value stored at the memory address in ptr.The & gets the memory address of a variableptr = &myvar get the memory address of myvar and stores it in ptr
  • 22. Pointers and Pass by ReferenceC++ uses pass by value that means the parameters of a function are copies of the variables passed in.void myfunc (intfoo, int bar){…}Myfunc(a,b);The values in a and b are copied into foo and bar.
  • 23. Why By ReferenceWant to avoid copying large amounts of data.Want the function to modify the value passed in.
  • 24. Pointers as a SolutionMyfunc (int *foo, int *bar){ …}Myfunc(&a, &b)Still pass-by-value but pass in the value of the memory addresses of a and b.When the values pointed to by foo and bar are changed it will be changing a and b.
  • 25. Objects in C++Dogage: Integerspeak()walk( location ) :string
  • 26. Dog the .h fileclass dog{public:int age; char* speak();moveTo(intx, inty);};C++
  • 27. Dog the .cpp file#include "dog.h"#include <iostream>using namespace std;void Dog::speak(){cout << "Woof Woof!" << endl;}C++
  • 28. Initializing a Dog C++#include <iostream>#include "dog.h"using namespace std;intmain(char* args[], intargc){cout << "Dog Program" << endl; Dog fido; //on stack //intmyintfido.age = 5;fido.speak();cin.get();}
  • 29. Scooby a Dynamic Dog C++Dog* scooby = new Dog();(*scooby).speak(); // these calls doscooby->speak(); //the same thingscooby->age = 6;
  • 30. Objects and Pointersint myvar1= 5;int myvar2 = 1;double mydbl = 5.3;myvar1myvar2mydbl515.3
  • 31. Pointersint myvar1= 5;int myvar2 = 1;double mydbl = 5.3;Dog* scooby = new Dog();scooby->age = 5; myvar1myvar2mydblscoobyage515.3Memoryaddress5
  • 32. CallStackvs HeapCode example callStackExample
  • 33. Directed Study/Further ReadingC++ Pointers http://www.cplusplus.com/doc/tutorial/pointers/Binky Videohttp://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/http://cslibrary.stanford.edu/104/