Beruflich Dokumente
Kultur Dokumente
Contents
Quick Revision Structures Functions Objects & Classes Arrays & Strings Operator overloading Inheritance Pointers Streams & Files
CECE Academic Team 12/3/2011
C++ (OOP)
Quick Revision
Basic Program Construction #include <iostream> using namespace std; int main() { --------------------------------return 0; }
Quick Revision
Output Using cout cout << CECE Academic Team \n;
Arithmetic Operators + - * / % ++ -- += -=
C++ (OOP)
12/3/2011
Quick Revision
Logical Operators && || !
1. 2. 3.
C++ (OOP)
For Loop
C++ (OOP)
12/3/2011
While Loop
C++ (OOP)
12/3/2011
Do Loop
C++ (OOP)
12/3/2011
Quick Revision
1.
2. 3.
C++ (OOP)
12/3/2011
If Statement
C++ (OOP)
12/3/2011
If .. Else Statement
C++ (OOP)
12/3/2011
Switch Statement
C++ (OOP)
12/3/2011
Structures
C++ (OOP)
12/3/2011
Structures
Definition: A structure is a collection of simple variables. The variables in a structure can be of different types.
C++ (OOP)
12/3/2011
C++ (OOP)
12/3/2011
C++ (OOP)
12/3/2011
Structures
Accessing Structure Members //give values to structure members part1.modelnumber = 6244; part1.partnumber = 373; part1.cost = 217.55; //display structure members cout << Model << part1.modelnumber; cout << , part << part1.partnumber; cout << , costs $ << part1.cost << endl;
Structures
Initializing Structure Members part part1 = { 6244, 373, 217.55 };
Arithmatic & Relational Opeartions part1+part2; //ILLEGAL Statement if( part1 == part2 ) //ILLEGAL Statement part1.cost+part2.cost; //Legal Statement
Structures
Structures Within Structures struct Distance { int feet; float inches; }; struct Room { Distance length; Distance width; };
Structures
Structures Within Structures Int main() { Room dining; dining.length.feet = 13; dining.length.inches = 6.5; dining.width.feet = 10; dining.width.inches = 0.0; Return 0; }
Structures
Initializing Nested Structures Room dining = { {13, 6.5}, {10, 0.0} };
Exercise write a program that obtains two time values from the user in 12:59:59 format, stores them in struct time variables, converts each one to seconds (type int), adds these quantities, converts the result back to hoursminutes-seconds, stores the result in a time structure, and finally displays the result in 12:59:59 format.
CECE Academic Team 12/3/2011
C++ (OOP)
Enumerations
C++ (OOP)
12/3/2011
Enumerations
Enumerated types work when you know in advance a finite (usually short) list of values that a data type can take on.
C++ (OOP)
12/3/2011
Enumerations Declaration
C++ (OOP)
12/3/2011
Enumerations
Example enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int main() { days_of_week day1, day2; //define variables of type days_of_week day1 = Mon; day2 = Thu; int diff = day2 - day1; //can do integer arithmetic cout << Days between = << diff << endl; if(day1 < day2) //can do comparisons cout << day1 comes before day2 \n; return 0; }
C++ (OOP)
12/3/2011
Functions
C++ (OOP)
12/3/2011
Functions
A function groups a number of program statements into a unit and gives it a name. This unit can then be invoked from other parts of the program. The most important reason to use functions is to aid in the conceptual organization & clarity of a program. Another reason is to reduce program size. Any sequence of instructions that appears in a program more than once is a candidate for being made into a function. The functions code is stored in only one place in memory, even though the function is executed many times in the course of the program.
CECE Academic Team 12/3/2011
C++ (OOP)
Functions
C++ (OOP)
12/3/2011
Functions
The Function Declaration Just as you cant use a variable without first telling the compiler what it is, you also cant use a function without telling the compiler about it. Calling the Function
The Function Definition The definition contains the actual code for the function.
Functions
C++ (OOP)
12/3/2011
Functions
Example: void starline() { for(int j=0; j<45; j++) cout << *; cout << endl; } int main() { starline(); //call to function cout << CECE << endl starline(); //call to function return 0; }
C++ (OOP)
12/3/2011
Functions
1. 2.
Passing Arguments to Functions An argument is a piece of data (an int value, for example) passed from a program to the function. Passing Constants Passing Variables
a) b) c)
C++ (OOP)
Functions
Passing Contstants void repchar(char, int); //function declaration int main() { repchar(-, 43); //call to function return 0; } void repchar(char ch, int n) //function declarator { for(int j=0; j<n; j++) //function body cout << ch; cout << endl; }
C++ (OOP)
12/3/2011
Functions
Passing Variables (by Value) void repchar(char, int); //function declaration int main() { char chin; int nin; cout << Enter a character: ; cin >> chin; cout << Enter number of times to repeat it: ; cin >> nin; repchar(chin, nin); return 0; }
C++ (OOP)
12/3/2011
Functions
Passing Variables (by Value) void repchar(char ch, int n) //function declarator { for(int j=0; j<n; j++) //function body cout << ch; cout << endl; }
C++ (OOP)
12/3/2011
Passing by Value
C++ (OOP)
12/3/2011
Functions
Returning Values from Functions When a function completes its execution, it can return a single value to the calling program.
C++ (OOP)
12/3/2011
Functions
Example float lbstokg(float); //declaration int main() { float lbs, kgs; cout << \nEnter your weight in pounds: ; cin >> lbs; kgs = lbstokg(lbs); cout << Your weight in kilograms is << kgs << endl; return 0; }
Functions
Example float lbstokg(float pounds) { float kilograms = 0.453592 * pounds; return kilograms; }
C++ (OOP)
12/3/2011
Functions
C++ (OOP)
12/3/2011
Functions
Passing by Reference Passing arguments by reference uses a different mechanism. Instead of a value being passed to the function, a reference to the original variable, in the calling program, is passed. (Its actually the memory address of the variable that is passed) An important advantage of passing by reference is that the function can access the actual variables in the calling program. Among other benefits, this provides a mechanism for passing more than one value from the function back to the calling program.
C++ (OOP)
12/3/2011
Functions
Example void order(int&, int&); //prototype int main() { int n1=99, n2=11; int n3=22, n4=88; order(n1, n2); //order each pair of numbers order(n3, n4); cout << n1= << n1 << endl; //print out all numbers cout << n2= << n2 << endl; cout << n3= << n3 << endl; cout << n4= << n4 << endl; return 0; }
C++ (OOP)
12/3/2011
Functions
Example void order(int& numb1, int& numb2) //orders two numbers { if(numb1 > numb2) //if 1st larger than 2nd, { int temp = numb1; //swap them numb1 = numb2; numb2 = temp; } }
Functions
Overloaded Functions An overloaded function appears to perform different activities depending on the kind of data sent to it.
C++ (OOP)
12/3/2011
Functions
Example void repchar(); //declarations void repchar(char); void repchar(char, int); int main() { repchar(); repchar(=); repchar(+, 30); return 0; } void repchar() { for(int j=0; j<45; j++) // always loops 45 times cout << *; // always prints asterisk cout << endl; }
C++ (OOP)
12/3/2011
Functions
Example void repchar(char ch) { for(int j=0; j<45; j++) // always loops 45 times cout << ch; // prints specified character cout << endl; } void repchar(char ch, int n) { for(int j=0; j<n; j++) // loops n times cout << ch; // prints specified character cout << endl; }
C++ (OOP)
12/3/2011
Overloaded Functions
C++ (OOP)
12/3/2011
Functions
Default arguments Surprisingly, a function can be called without specifying all its arguments. This wont work on just any function: The function declaration must provide default values for those arguments that are not specified.
C++ (OOP)
12/3/2011
Functions
Example void repchar(char=*, int=45); //declaration with default arguments int main() { repchar(); //prints 45 asterisks repchar(=); //prints 45 equal signs repchar(+, 30); //prints 30 plus signs return 0; } void repchar(char ch, int n) { for(int j=0; j<n; j++) //loops n times cout << ch; //prints ch cout << endl; }
C++ (OOP)
12/3/2011
Functions
Recursion Functions Recursion involves a function calling itself.
Example (calculates factorials) unsigned long factfunc(unsigned long); //declaration int main() { int n; //number entered by user unsigned long fact; //factorial cout << Enter an integer: ; cin >> n; fact = factfunc(n); cout << Factorial of << n << is << fact << endl; return 0; }
C++ (OOP)
12/3/2011
Functions
unsigned long factfunc(unsigned long n) { if(n > 1) return n * factfunc(n-1); //self call else return 1; }
Assume n =5 Call 5 Call 4 Call 3 Call 2 Call 1 Return 1 Return 2 Return 6 Return 24 Return 120
CECE Academic Team 12/3/2011
C++ (OOP)
Functions
Inline Functions This kind of function is written like a normal function in the source file but compiles into inline code instead of into a function. The source file remains well organized and easy to read, since the function is shown as a separate entity. However, when the program is compiled, the function body is actually inserted into the program wherever a function call occurs.
CECE Academic Team 12/3/2011
C++ (OOP)
Functions
Example inline float lbstokg(float pounds) { return 0.453592 * pounds; } int main() { float lbs; cout << \nEnter your weight in pounds: ; cin >> lbs; cout << Your weight in kilograms is << lbstokg(lbs) << endl; return 0; }
C++ (OOP)
12/3/2011
Functions
Scope & Storage Class There are two types of scope Variables with local scope are visible only within a block. Variables with file scope are visible throughout a file.
There are two storage classes: automatic and static. Variables with storage class automatic exist during the lifetime of the function in which theyre defined. Variables with storage class static exist for the lifetime of the program.
C++ (OOP) CECE Academic Team 12/3/2011
Functions
1.
2. 3.
Scope & Storage Class Local Variable Global Variable Static Local Variable
C++ (OOP)
12/3/2011
Functions
Example For Static Local Variable float getavg(float); //declaration int main() { float data=1, avg; while( data != 0 ) { cout << Enter a number: ; cin >> data; avg = getavg(data); cout << New average is << avg << endl; } return 0; }
C++ (OOP)
12/3/2011
Functions
Example For Static Local Variable float getavg(float newdata) { static float total = 0; //static variables are initialized static int count = 0; // only once per program count++; //increment count total += newdata; //add new data to total return total / count; //return the new average }
Reference
C++ (OOP)
12/3/2011
THANK YOU!
Prepared by: Sameh Attia