210 likes | 353 Views
CS415 C++ Programming. Takamitsu Kawai kawai@csee.wvu.edu 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University. What is C++ ?. C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80’s “C with class”
E N D
CS415C++ Programming Takamitsu Kawai kawai@csee.wvu.edu 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University
What is C++ ? • C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80’s • “C with class” • Translator from C++ to C (called “cfront”) existed
C++ Design Goals C++... • supports data abstraction • supports object-oriented programming • supports generic programming • is a better C But… NOT the best OOP language! Then, what is the best? -- There is NO “best” language!
C++ Design Goals (cont’d) C++... • supports object-oriented programming • Inheritance (class), polymorphism (virtual functions, operator overloading) • supports data abstraction • Class • supports generic programming • Parameterized types (templates)
C++ Design Goals (cont’d) Better “C” • As close to C as possible, but no closer • Runtime efficiency (as with C) • Compatibility with C libraries and traditional development tools
A Basic C++ Program #include <iostream> // for cout and endl. // #include <iostream.h> // also works, but obsolete using namespace std; // specify namespace. // “std” is the standard namespace int main() // program starts here { cout << ”Hello, world !” << endl; // same as // cout << ”Hello, world !\n”; return 0; // return to the OS }
The C++ Data Types Fundamental Data Types (The actual sizes depend on the compiler implementation. The following assumes typical 32bit microprocessor case) - Integers Type Size(bytes) Type Size char 1 int 4 short 2 long long 8 bool 4 signed/unsigned: signed char (-128 ~ 127) unsigned char (0 ~ 255) - Floating point numbers Type Size(bytes) float 4 double 8 long double 16
The C++ Data Types (cont’d) Derived Types int* a; pointers int& a; references int a[10]; arrays int func(void); functions struct structures union unions class classes enum enumerations
The C++ Data Types (cont’d) Type Conversion - Implicit type conversion ex. int i; float a=10.5; i=a; // i==10 - Explicit type conversion (cast) int i=5; j=2; float c; c=(float)i/(float)j; // c==2.5 c=float(i)/float(j); // also works
Operators and Priority (cont’d) ”::” is the highest priority ”,” is the lowest priority L/R: left associative R/L: right associative
Return value of main() int main() { cout << ”Hello, world !\n”; return 0; } Operating System $status ex. % prog1 Hello, world ! % echo $status 0 (shell variable)
Using streams “stream”: a general name of a flow of data cin, cout : standard input/output (cerr also exists.) cf. stdin, stdout, stderr #include <iostream> using namespace std; int main() { int a, b; cout << "Input two integers:"; cin >> a >> b; cout << a << " + " << b << " = " << a + b << '\n'; return 0; } Note: cout, cin, cerr are NOT keywords. They are defined in “iostream”.
Using file streams ifstream, ofstream : file input/output #include <fstream> using namespace std; int main() { int a, b; ifstream fin("input.dat"); ofstream fout("output.dat"); fin >> a >> b; fout << a << " + " << b << " = " << a + b << '\n'; fout.close(); fin.close(); return 0; }
Manipulators #include <iostream> #include <iomanip> using namespace std; int main() { int i = 123; cout << "Octal : " << oct << i << endl << "Decimal : " << dec << i << endl << "Hexadecimal: " << hex << i << endl; float f = 1.23456; cout << "Precision=1: " << setprecision(1) << f << endl << "Precision=2: " << setprecision(2) << f << endl << "Pricision=3: " << setprecision(3) << f << endl << "Width=4 Precision=1: " << setw(4) << setprecision(1) << f << endl << "Width=5 Precision=2: " << setw(5) << setprecision(2) << f << endl << "Width=6 Precision=3: “ << setw(6) << setprecision(3) << f << endl; return 0; }