cs116 senem kumova metİn. outline what is c++ ? some features of c++ input and output operations...
DESCRIPTION
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İNTRANSCRIPT
CS116 SENEM KUMOVA METİN
OutlineWhat is C++ ? Some features of C++Input and Output OperationsManipulatorsC++ header filesNamespaces and scope resolution operatorInline functionsReferences Default argumentsFunction overloadingFunction templates
CS116 SENEM KUMOVA METİN
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 CSupport for Data AbstractionSupport for Object-Oriented ProgrammingC++ supports these paradigms; it does not
require their use
CS116 SENEM KUMOVA METİN
Some features of C++C++ is by far the most popular OOPL
Information Hiding (public & private)
Encapsulation (functionsmethods)
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
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
Input and Output Operations (2/6)
#include <iostream> //header file without “.h” suffix
using namespace std;
main() {
cout << “Welcome to C++\n”;cout << “Welcome to C++"<< endl;
}
CS116 SENEM KUMOVA METİN
Input and Output Operations (3/6)#include <iostream> //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
Input and Output Operations (4/6)#include <iostream> 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
// A Simple Example
#include <iostream>using namespace std;
int main(){ int apples, oranges; // Declare two integer variables
apples = 5; oranges = 6; // Set initial values int fruit; // Declare a new variablefruit = 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
// A Simple Example
#include <iostream>using namespace std;
int main(){ int apples, oranges; // Declare two integer variables
int fruit; // ...then another one
cin>>apples; // Get initial value from keyboardcin>>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
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; // 19cout<<hex<<a; // 13
Manipulators without arguments require iostream file, manipulators with arguments require iomanip header file
CS116 SENEM KUMOVA METİN
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
Manipulators (3/4)#include<iostream>
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
Manipulators (4/4)#include <iostream> #include <iomanip>
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
C++ Header Files<iostream><iomanip><cmath><cstdlib><ctime><cstring><string> … (Chapter 15 p. 565-566 )
CS116 SENEM KUMOVA METİN
Namespaces & Scope Resolution Operator
#include <iostream>using namespace std;
main() {
cout << “Welcome to C++"<< endl;
}
#include <iostream>
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
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 declarationnamespace 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 namespacenamespace 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; }
Namespaces & Scope Resolution Operator// Declaring a namespace
#include <iostream>
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
Namespaces & Scope Resolution Operator// Declaring a namespace
#include <iostream> 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
Inline functionPlacing 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
Inline function
CS116 SENEM KUMOVA METİN
#include <iostream>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;
}
ReferencesThere 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 pointersCall by reference in C++ pointers
references
int x a variableint & y a reference to a variable
CS116 SENEM KUMOVA METİN
CS116 SENEM KUMOVA METİN 23
ReferencesReference is signaled by & and provides an
alternative name for storage
int x;int & ref =x;x=3;ref=3;
x … ref
3
References
CS116 SENEM KUMOVA METİN
#include <iostream>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;
References
CS116 SENEM KUMOVA METİN
#include <iostream>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; }
CS116 SENEM KUMOVA METİN 26
References : SWAP example#include<iostream>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; }
CS116 SENEM KUMOVA METİN 27
Default ArgumentsC++ 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);
Default Arguments
CS116 SENEM KUMOVA METİN
#include <iostream>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)<<en
dl;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;}
CS116 SENEM KUMOVA METİN 29
Function Overloading #include <iostream>using namespace std;
// C++ checks the number and type ofparameters to decide which overloaded function to use
int square( int a);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; }
CS116 SENEM KUMOVA METİN 30
Function Overloading #include <iostream>#include <iomanip>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; }
Function templates
template <class T>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; }
Function Template : Example 1#include <iostream> using namespace std;
template <typename T> 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
Function Template : Example 2void printArray( const int *, int ); void printArray( const double *,
int ); void printArray( const char *, int );
CS116 SENEM KUMOVA METİN
template<class T>void printArray( const T *, int );
Function Template : Example 2#include <iostream>using namespace std;
template< typename T>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