2 C++ Overview
2 C++ Overview
int main() {
cout << “Every age has a language of its own\n”;
return 0;
}
#include <iostream>
using
namespace std;
●
C++ compilers ignores two or more number of
space characters or new line feed.
int main() {
return 0;
}
●
The preprocessor directive #include tells the compiler to
insert another file into your source file. In effect, the #include
directive is replaced by the contents of the file indicated.
●
Here, includes the contents of the input/output stream header
<iostream>
●
.h extension is not used for C++ header files 6
int main() {
return 0;
}
●
The identifier cout is actually an object predefined in C++, corresponds to
standard output stream
●
The operator << directs the contents of the variable on its right to the
object on its left.
●
It directs the string constant (the output statement) to cout, which sends it
to the display device.
●
In C++, operators << is overloaded for “put to” operation. 7
int main() {
return 0;
}
●
What is the importance of main() function ???
int main() {
return 0;
}
●
The directive using namespace std; says that the program that
follow uses the std namespace.
●
Various program components such as cout object are declared
within this namespace.
●
If we didn’t use the using directive, we would need to add the std
name to them.
9
– std::cout << “Every age has a language of its own.”;
Dr. Alekha Kumar Mishra
Analyzing the simple program(5)
// A simple c++ program to display some text
#include <iostream>
using namespace std;
int main() {
return 0;
}
●
A Single line comment!!!
●
We can also use C's multiline comment in C++
10
int main() {
return 0;
}
●
Why return a 0 in main function ???
11
12
13
14
16
simple Structured/derived
address
float double long double
pointer reference
17
18
19
20
25
27
28
29
30
32
33
34
35
36
37
38
39
40
42
int main() {
float y = 23.1415;
cout.precision(1);
cout << y << endl; // Outputs 2e+01
cout.precision(2); 2e+01
cout << y << endl; // Outputs 23 23
cout.precision(3); 23.1
cout << y << endl; // Outputs 23.1 3.1416
double f =3.14159; 3.14159
cout << setprecision(5) << f << endl; 3.14159
cout << setprecision(9) << f << endl; 3.141590000
cout << fixed;
cout << setprecision(5) << f << endl;
cout << setprecision(9) << f << endl;
return 0;
} 43
45
47
48
50
namespace first {
int var = 5;
}
namespace second {
double var = 3.1416;
}
int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
52
namespace first {
int x = 5;
int y = 10; int main () {
} //using namespace first;
//using namespace second;
namespace second { using first::x;
double x = 3.1416; using second::y;
double y = 2.7183; cout << x << endl;
} cout << y << endl;
cout << first::y << endl;
cout << second::x << endl;
return 0;
}
53
// nested namespace
namespace Inner {
enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 };
} // end Inner namespace
} // end Example namespace 54
void Example::printValues() {
cout << "\nIn printValues:\n integer1 = " << integer1;
cout << "\nPI = " << PI << "\nE = " << E;
cout << "\ndoubleInUnnamed = " << doubleInUnnamed;
cout << "\n(global) integer1 = " << ::integer1;
cout << "\nFISCAL3 = " << Inner::FISCAL3 << endl;
} // end printValues
55
} // end main
56
57
58
ptr
59
60
61
62
ptr
63
64
A = new int*[5];
for (int i = 0; i < 5; i++) {
A[i] = new int[8];
for (int j = 0; j < 8; j++)
A[i][j] = i + j;
}
●
z = 9; // same as x = 9;
●
●
cout << x << endl; // prints 9
●
cout << z << endl; // prints 9
67
68
69
74
75
int main(){
part part1; //define a structure variable
part1.modelnumber = 6244; structure variable
part1.partnumber = 373; definition
part1.cost = 217.55F;
77
78
int main() {
//initialize variable
part part1 = { 6244, 373, 217.55F };
part part2;
//display first variable
cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
part2 = part1;
//assign first variable to second
//display second variable
cout << “Model “ << part2.modelnumber;
cout << “, part “ << part2.partnumber;
cout << “, costs $” << part2.cost << endl;
return 0; 79
}
Dr. Alekha Kumar Mishra
// demonstrates nested structures
#include <iostream>
using namespace std; Structures
struct Distance { Within
int feet;
float inches; Structures
};
struct Room {
Distance length;
Distance width;
}; int main() {
Room dining; //define a room
dining.length.feet = 13; //assign values to room
dining.length.inches = 6.5;
dining.width.feet = 10;
dining.width.inches = 0.0;