1 / 20

CS415 C++ Programming

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”

ohio
Download Presentation

CS415 C++ Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS415C++ Programming Takamitsu Kawai kawai@csee.wvu.edu 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University

  2. 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

  3. 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!

  4. 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)

  5. 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

  6. 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 }

  7. 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

  8. 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

  9. 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

  10. Operators and Priority

  11. Operators and Priority (cont’d)

  12. Operators and Priority (cont’d)

  13. Operators and Priority (cont’d)

  14. Operators and Priority (cont’d)

  15. Operators and Priority (cont’d)

  16. Operators and Priority (cont’d) ”::” is the highest priority ”,” is the lowest priority L/R: left associative R/L: right associative

  17. Return value of main() int main() { cout << ”Hello, world !\n”; return 0; } Operating System $status ex. % prog1 Hello, world ! % echo $status 0 (shell variable)

  18. 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”.

  19. 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; }

  20. 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; }

More Related