Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.

Similar presentations


Presentation on theme: "CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution."— Presentation transcript:

1 CS116 SENEM KUMOVA METİN

2 Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution operator Inline functions References Default arguments Function overloading Function templates CS116 SENEM KUMOVA METİN

3 What is C++? C++ is the work of Bjarne Stroustrup of AT&T Bell Labs. C++ is a mostly upward compatible extension of C that provides: A better C Support for Data Abstraction Support for Object-Oriented Programming C++ supports these paradigms; it does not require their use CS116 SENEM KUMOVA METİN

4 Some features of C++ C++ is by far the most popular OOPL Information Hiding (public & private) Encapsulation (functions  methods) Abstract Data Types (stacks) Inheritance (code reuse) vehicle  car  truck  bus Polymorphism (having many forms) two_dim  triangle ( area of triangle??)  square ( area of square??) CS116 SENEM KUMOVA METİN

5 Input and Output Operations (1/6) Input to a program is treated as a stream of consecutive bytes from keyboard, a disk file or some other source.. Output is treated as a stream of consecutive bytes to video display, disk file or some other destination.. cin  reads from standard input(keyboard) cout  writes to standart output (screen) CS116 SENEM KUMOVA METİN

6 Input and Output Operations (2/6) #include //header file without “.h” suffix using namespace std; main() { cout << “Welcome to C++\n”; cout << “Welcome to C++"<< endl; } CS116 SENEM KUMOVA METİN

7 Input and Output Operations (3/6) #include //header file without.h suffix using namespace std; main() { cout <<“Welcome”<<“ to C++”<<endl; cout <<“Wel”<<“co”<<“me to “ <<“C++"<< endl; cout <<“Did”<<endl<<“you” <<endl<<“understand?”<<endl; } CS116 SENEM KUMOVA METİN

8 Input and Output Operations (4/6) #include using namespace std; int main() { int value1, value2 = 0; cout << "enter integers: "; cin >> value1 >> value2; cout <<"\nYou entered "<< value1 << “and” <<value2<< endl; return 0; } CS116 SENEM KUMOVA METİN

9 // A Simple Example #include using namespace std; int main() { int apples, oranges; // Declare two integer variables apples = 5; oranges = 6; // Set initial values int fruit; // Declare a new variable fruit = apples + oranges; // Get the total fruit cout << endl; // Start output on a new line cout << “WE HAVE " << apples <<“ APPLES” <<endl; cout << “WE HAVE " << oranges << “ ORANGES“<<endl; cout << “WE HAVE " << fruit << “ FRUITS AT ALL…“<<endl; return 0; // Exit the program } CS116 SENEM KUMOVA METİN

10 // A Simple Example #include using namespace std; int main() { int apples, oranges; // Declare two integer variables int fruit; //...then another one cin>>apples;// Get initial value from keyboard cin>>oranges; // Get initial value from keyboard fruit = apples + oranges; // Get the total fruit cout << endl; // Start output on a new line cout << “WE HAVE " << apples <<“ APPLES” <<endl; cout << “WE HAVE " << oranges << “ ORANGES“<<endl; cout << “WE HAVE " << fruit << “ FRUITS AT ALL…“<<endl; return 0; // Exit the program } CS116 SENEM KUMOVA METİN

11 Manipulators (1/4) Input and the output can be formatted by manipulators Manipulators permanently change the state of the stream to which is applied (except setw) For example placing “hex” in the output stream causes all subsequent output of ints, shorts, longs to be written in hexadecimal format int a=19; cout<< a; // 19 cout<<hex<<a; // 13 Manipulators without arguments require iostream file, manipulators with arguments require iomanip header file CS116 SENEM KUMOVA METİN

12 Manipulators (2/4) endl  write a newline dec  input or output in decimal oct  input or output in octal hex  input or output in hexadecimal fixed  use fixed notation for floating point numbers: d.ddd scientific  use scientific notation for floating point numbers left  left justify right  right justify setfill(c)  make c to fill character setprecision(n)  set floating point precision to n setw(n)  set field width to n CS116 SENEM KUMOVA METİN Needs iomanip header file

13 Manipulators (3/4) #include using namespace std; main() { int i=19; cout<< "i= "<< i <<" (decimal)\n"; cout<< "i= "<< oct <<i <<" (octal)\n"; cout<< "i= "<< hex<< i <<" (hexadecimal)\n"; cout<< "i= "<< i <<" (? ? ?)\n"; cout<< "i= "<<dec<< i <<" (decimal)\n"; } CS116 SENEM KUMOVA METİN

14 Manipulators (4/4) #include using namespace std; int main() { int num1 = 12, num2 = 345, num3 = 6789; cout << endl; //Start on a new line cout << setw(6) << num1 << endl<< setw(6) << num2; cout << endl << setw(6) <<num3; //Output three values cout << endl; //Start on a new line return 0; //Exit program } CS116 SENEM KUMOVA METİN

15 C++ Header Files … (Chapter 15 p. 565-566 ) CS116 SENEM KUMOVA METİN

16 Namespaces & Scope Resolution Operator #include using namespace std; main() { cout << “Welcome to C++"<< endl; } #include main() { std::cout << “Welcome to C++"<<std:: endl; } CS116 SENEM KUMOVA METİN C++ provides namespaces to prevent name conflicts… Namespace std includes standard C++ definitions like cout, cin, endl

17 Namespaces & Scope Resolution Operator CS116 SENEM KUMOVA METİN namespace xxx { int flag; int s;} namespace yyy { int flag; int s;} main() { xxx::flag=3; // :: is the scope resolution operator yyy::flag=-1; } // with a using declaration namespace xxx { int flag; int s;} namespace yyy { int flag; int s;} using xxx::flag; main() { flag=3; // xxx::flag=3; xxx:: s=0; yyy::flag=-1; yyy:: s=1; } //using namespace  declaration covers entire namespace namespace xxx { int flag; int s;} namespace yyy { int flag; int s;} using namespace xxx; main() { flag=3; s=0; // xxx::s=0; yyy::flag=-1; yyy:: s=1; cout<<flag; cout<< yyy::flag; }

18 Namespaces & Scope Resolution Operator // Declaring a namespace #include namespace myStuff { int value = 0; } int main() { std::cout << "enter an integer: "; std::cin >> myStuff::value; std::cout << "\nYou entered " << myStuff::value << std::endl; return 0; } CS116 SENEM KUMOVA METİN

19 Namespaces & Scope Resolution Operator // Declaring a namespace #include namespace myStuff { int value = 0; } using namespace std; int main() { cout << "enter an integer: "; cin >> myStuff::value; cout << "\nYou entered " << myStuff::value << endl; return 0; } CS116 SENEM KUMOVA METİN

20 Inline function Placing the qualifier inline before function’s return type “advises” the compiler to generate a copy of the function’s in place to avoid a function call CS116 SENEM KUMOVA METİN

21 Inline function CS116 SENEM KUMOVA METİN #include using namespace std; inline double cube(double side) {return side*side*side; } main() {double sideValue; cout << “enter the side lenght of the cube”<<endl; cin>>sideValue; cout<< “Volume of the cube is ”<< cube(sideValue)<<endl; }

22 References There are two ways to call a function Call by value (send the value of the variable) Call by reference (send the adress/reference of the variable) Call by reference in C  pointers Call by reference in C++  pointers  references int x  a variable int & y  a reference to a variable CS116 SENEM KUMOVA METİN

23 23 References Reference is signaled by & and provides an alternative name for storage int x; int & ref =x; x=3; ref=3; x … ref 3

24 References CS116 SENEM KUMOVA METİN #include using namespace std; main() { int x=2; int &y=x; // You have to initialize the references cout<<x<<“ ” <<y<<endl; x++; cout<<x<<“ ” <<y<<endl; y++; cout<<x<<“ ” <<y<<endl;

25 References CS116 SENEM KUMOVA METİN #include using namespace std; int squareByValue (int); void squareByReference(int &); main() {int x=2, y=4; cout<<“before function call ”<<x<<endl; cout<<squareByValue(x)<<endl; cout<<“after function call ”<<x<<endl; cout<<“before function call ”<<y<<endl; squareByReference(y); cout<<“after function call ”<<y<<endl;} int squareByValue (int n) { n=n*n; return n;} void squareByReference(int & n) {n=n*n; }

26 CS116 SENEM KUMOVA METİN26 References : SWAP example #include using namespace std; void swap (int &, int &); void main() {int i=7 ; int j=-3; swap (i,j); cout <<i<<endl; cout <<j<<endl;} void swap (int & a, int & b) {int t= a; a=b; b=t;}

27 CS116 SENEM KUMOVA METİN27 Default Arguments C++ allows to specify default values for function parameters…. void f (int v, float s=3.14, char t=‘\n’, string msg =“Error!!”); Valid invocations: f(12, 3.5, ‘\t’, “OK”); f(12, 3.5, ‘\t’); f(12, 3.5); f(12);

28 Default Arguments CS116 SENEM KUMOVA METİN #include using namespace std; int boxVolume (int lenght=1, int height=1, int width=1); main() { cout <<“Default volume”<< boxVolume()<<endl; cout<<“lenght=10,height=width=1”<<boxVolume(10)<<endl; cout <<“lenght=10,height=20,width=1”<<boxVolume(10,20)<<endl; cout <<“lenght=10,height=20,width=30”<<boxVolume(10,20,30)<<endl; } int boxVolume (int lenght, int height, int width) {return lenght*height*width;}

29 CS116 SENEM KUMOVA METİN29 Function Overloading #include using namespace std; // C++ checks the number and type ofparameters to decide which overloaded function to use int square( int a); double square (double a); int main() { int x=8; double y=8; cout<<square(x); cout<<square (y); return 0;} int square(int a ) { return a*a; } double square(double a) { return a*a; }

30 CS116 SENEM KUMOVA METİN30 Function Overloading #include using namespace std; void print( int a); void print (double a); int main() { int x=8; double y=8; print(x); print(y); return 0;} void print (int a ) { cout <<a <<endl; } void print (double a) { cout <<showpoint<< a <<endl; }

31 Function templates template T GetMax (T a, T b) {if(a>b) return a; else return b; } CS116 SENEM KUMOVA METİN char GetMax (char a, char b) { if(a>b) return a; else return b; } float GetMax (float a, float b) { if(a>b) return a; else return b; } int GetMax (int a, int b) { if(a>b) return a; else return b; }

32 Function Template : Example 1 #include using namespace std; template T GetMax (T a, T b) { return (a>b?a:b); } void main () { int i=5, j=6, k; float l=10.0, m=5.3, n; k=GetMax(i,j); n=GetMax(l,m); cout << k << endl; cout << n << endl; } CS116 SENEM KUMOVA METİN

33 Function Template : Example 2 void printArray( const int *, int ); void printArray( const double *, int ); void printArray( const char *, int ); CS116 SENEM KUMOVA METİN template void printArray( const T *, int );

34 Function Template : Example 2 #include using namespace std; template void printArray( const T *array, int count ) {for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl;} // end function template printArray void main() {int a[ 5 ] = { 1, 2, 3, 4, 5 }; char b[ 4] = “C++"; cout << "Array a contains:" << endl; printArray( a, 5 ); cout << "Array b contains:" << endl; printArray( b, 4 );} CS116 SENEM KUMOVA METİN


Download ppt "CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution."

Similar presentations


Ads by Google