Vous êtes sur la page 1sur 20

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 80s
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 (contd)

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 (contd) 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 to the OS

return 0;

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

The C++ Data Types (contd)


Derived Types int* a; int& a; int a[10]; int func(void); struct union class enum pointers references arrays functions

structures unions classes enumerations

The C++ Data Types (contd)


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


Symbol Name :: :: scope resolution global Assoc. Ex. L/R L/R class_name::member ::name

. -> [] () () ++ --

member selection member selection subscripting function call value construction post increment post decrement

L/R L/R L/R L/R L/R R/L R/L

object.member pointer -> member pointer[expr] expr(expr_list) type(expr_list) lvalue ++ lvalue --

Operators and Priority (contd)


sizeof sizeof ++ -~ ! + sizeof expr sizeof(type) ++ lvalue -- lvalue ~ expr ! expr - expr + expr

size of object size of type pre increment pre decrement complement not unary minus unary plus

R/L R/L R/L R/L R/L R/L R/L R/L

Operators and Priority (contd)

& * new delete delete[] ()

address of dereference create destroy destroy array type cast

R/L R/L R/L R/L R/L R/L

& lvalue * expr new type delete pointer delete[] pointer (type) expr

Operators and Priority (contd)


.* ->*
* / %

member selection L/R member selection L/R


multiply divide modulo L/R L/R L/R

object.*pointer_to_member pointer ->*pointer_to_member expr * expr expr / expr expr % expr expr + expr expr - expr
expr << expr expr >> expr

+ << >>

add subtract
shift left shift right

L/R L/R
L/R L/R

Operators and Priority (contd)


< <= > >= == != & ^ | less than less than or equal greater than greater than or equal equal not equal bitwise AND bitwise exclusive OR bitwise inclusive OR L/R L/R L/R L/R L/R R/L L/R R/L L/R expr < expr expr <= expr expr > expr expr >= expr expr == expr expr != expr expr & expr expr ^ expr expr | expr

Operators and Priority (contd)


&& || ?: = *= /= %= += -= logical AND logical inclusive OR conditional expression simple assignment multiply and assign divide and assign modulo and assign add and assign subtract and assign L/R L/R L/R R/L R/L R/L R/L R/L R/L expr && expr expr || expr expr ? expr : expr lvalue = expr lvalue *= expr lvalue /= expr lvalue %= expr lvalue += expr lvalue -= expr

Operators and Priority (contd)


<<= >>= &= ^= |= throw , shift left and assign shift right and assin AND and assign exclusive OR and assign inclusive OR and assign throw exception comma R/L R/L R/L R/L R/L R/L L/R lvalue <<= expr lvalue >>= expr lvalue &= expr lvalue ^= expr lvalue |= expr throw expr expr,expr

:: 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; }

Vous aimerez peut-être aussi