CP - Unit 1 - 2
CP - Unit 1 - 2
CP - Unit 1 - 2
Programming
Brief Facts About C++
• Evolved from C
• Designed and implemented by Bjarne Stroustrup at the Bell
Labs in the early 1980s
• “C with classes”
• Standardized by ISO in 1997
– Includes the C++ standard library
– Standard Template Library (STL)
• Part of the C++ standard library
• Readymade classes for data structures and algorithms
Filenames
• Can be different from the name of the class or the name of the function
in the file
• .cpp
– Extension for the C++ source code files
• .h
– Extension for C++ header files
– Usually, code for a data structure is put in a header file, and then it can be
added to a program with an include directive, e.g. #include
"BasicVector.h"
• Name of executable file
– In MSVS it’s the name of your project
– In g++ it’s either “a.out” or a name you specify
Simple C++ Example
/*
FILE: main.cpp
*/
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << endl;
return 0; //optional
}
Simple C++ Example
/*
FILE: main.cpp
*/
#include <iostream> Comment
#include <string>
using namespace std;
C++ header files from
int main() { C++ standard library
cout << "Enter your first name: ";
string name; Namespace for
cin >> name; C++ standard library
cout << "Hello, " << name << endl;
return 0; //optional
} “Entry” function
• cout
– #include <iostream>
– using namespace std;
– operator <<
– Represents standard output, usually the screen
cout << "Enter your first name: ";
• cin
– <iostream>
– operator >>
– Reads keyboard input until the first whitespace
string name;
cin >> name;
• endl
– Used to output a newline and flush output buffer
cout << "Hello, " << name << endl;
Variables and Data Types
Declaring a variable
– Allocates storage space in memory
– Not automatically initialized
int i; //i is declared but not initialized
int j = 0; //j is declared and initialized
int k(5); //k is declared and initialized
• Modulo operator
int m, n = 11;
m = n % 5;
cout << "m = " << m; //What prints?
Assignment Operator
• Assignment
int n = 10;
assignment operator
• Equality operators
– equality operator == (don’t confuse with = )
x == y; //x is equal to y
– operator !=
x != y; //x is not equal to y
• Relational operators
– operator <
x < y; //x is less than y
– operator >
x > y; //x is greater than y
– operator <=
x <= y; //x is less than or equal to y
– operator >=
x >= y; //x is greater than or equal to y
Increment and Decrement
• Post-increment operator
int n = 10;
n++; //returns n then adds 1 to it
• Pre-increment operator
int n = 10;
++n; //adds 1 to n then returns it
• Post-decrement operator
int n = 10;
n--; //returns n then subtracts 1 from it
• Pre-decrement operator
int n = 10;
--n; //subtracts 1 from n then returns it
Variables and I/O
• Values for the fundamental types can be input from the keyboard using
cin and output to the screen with cout
double x;
cout << "Enter a double: ";
cin >> x;
int n;
cout << "Enter an integer: ";
cin >> n;
char c;
cout << "Enter a char: ";
cin >> c;
cout << "double = " << x << " int = " << n;
Constants
i j result
0 0 1
int result = 0; 0 1 2
for( int i=0; i<3; ++i ) 0 2 3
for( int j=0; j<5; ++j 0 3 4
) 0 4 5
result++; 1 0 6
1 1 7
1 2 8
1 3 9
1 4 10
2 0 11
2 1 12
2 2 13
2 3 14
2 4 15
Nested Loop with Dependent Variable
i j result
0 0 1
int result = 0;
0 1 2
for( int i=0; i<3; ++i )
0 2 3
for( int j=i; j<5; ++j
0 3 4
)
0 4 5
result++;
1 1 6
1 2 7
1 3 8
1 4 9
2 2 10
2 3 11
2 4 12
How many iterations?
int result = 0;
for( int i=0; i<2; ++i )
for( int j=0; j<3; ++j )
result++;
Looping with while
int main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6)
{
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}
Looping with Do while
int main()
{
int i = 2; // Initialization expression
do
{
// loop body
printf( "Hello World\n");
// update expression
i++;
return 0;
}
Array
• Collection of elements of the same data type that are stored in adjacent
memory locations
• Creating an array
int myArray[5];//created but not initialized
• The first element in every array has the index 0
• Each element can be accessed by using its index
myArray[0] = 42;
cout << myArray[0];//what prints?
cout << myArray[3];//what prints?
• Initializing the array
for( int i=0; i<5; ++i )
myArray[i] = 0;
int nextArray[] = {0,0,0,0,0};//created + initialized
Using Arrays
• Common error
int myArray[5] = {0,0,0,0,0};
cout << myArray[5];
Class string
• C++ strings can be compared with ==, !=, <, >, <=, and
>=
string s1 = "Hello", s2 = "World";
if( s1 == s2 ) cout << "equal";
Functions and
Argument Passing
Functions