c++ ( oop )
TRANSCRIPT
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 1/59
C++ ( OOP )CECE ACADEMIC TEAM
Prepared by: Sameh Attia
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 2/59
Contents
Quick Revision
Structures
Functions
Objects & Classes Arrays & Strings
Operator overloading
Inheritance Pointers
Streams & Files
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 3/59
Quick Revision
Basic Program Construction
#include <iostream>
using namespace std;
int main(){
-----------------
-----------------return 0;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 4/59
Quick Revision
Output Using cout
cout << “CECE Academic Team \n”;
Variables & Data types
Input with cin
int x;
cin >> x;
Arithmetic Operators
+ - * / % ++ -- += -=
Relational Operators
> < == != >= <=
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 5/59
Quick Revision
Logical Operators
&& || !
Bitwise Operators
& | ^(xor) << >> ~
Ternary Operator
Y=x<10 ? a : b ;
Loops1. For Loop
2. While Loop
3. Do Loop
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 6/59
For Loop
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 7/59
While Loop
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 8/59
Do Loop
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 9/59
Quick Revision
Decisions
1. if Statement
2. if...else Statement
3. Switch Statement
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 10/59
If Statement
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 11/59
If .. Else Statement
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 12/59
Switch Statement
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 13/59
Structures
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 14/59
Structures
Definition:
A structure is a collection of simple variables. Thevariables in a structure can be of different types.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 15/59
Defining the Structure
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 16/59
Defining a Structure Variable
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 17/59
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;
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 18/59
Structures
Initializing Structure Members
part part1 = { 6244, 373, 217.55 };
Structure Variables in Assignment Statements
Part part2;part2 = part1;
Arithmatic & Relational Opeartions
part1+part2; //ILLEGAL Statementif( part1 == part2 ) //ILLEGAL Statement
part1.cost+part2.cost; //Legal Statement
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 19/59
Structures
Structures Within Structures
struct Distance
{
int feet;
float inches;};
struct Room
{
Distance length;Distance width;
};
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 20/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 21/59
Structures
Initializing Nested Structures
Room dining = { {13, 6.5}, {10, 0.0} };
Exercisewrite a program that obtains two time values from theuser in 12:59:59 format, stores them in struct timevariables, converts each one to seconds (type int), adds
these quantities, converts the result back to hours-minutes-seconds, stores the result in a time structure, andfinally displays the result in 12:59:59 format.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 22/59
Enumerations
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 23/59
Enumerations
Enumerated types work when you know in advancea finite (usually short) list of values that a data typecan take on.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 24/59
Enumerations’ Declaration
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 25/59
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 comparisonscout << “day1 comes before day2 \n”;
return 0;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 26/59
Functions
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 27/59
Functions
A function groups a number of program statements into aunit and gives it a name. This unit can then be invoked fromother parts of the program.
The most important reason to use functions is to aid in theconceptual organization & clarity of a program.
Another reason is to reduce program size. Any sequence ofinstructions that appears in a program more than once is a
candidate for being made into a function. The function’scode is stored in only one place in memory, even though thefunction is executed many times in the course of theprogram.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 28/59
Functions
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 29/59
Functions
The Function Declaration
Just as you can’t use a variable without first tellingthe compiler what it is, you also can’t use a function
without telling the compiler about it.
Calling the Function
The Function Definition
The definition contains the actual code for the function.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 30/59
Functions
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 31/59
Functions
Example:
void starline()
{
for(int j=0; j<45; j++)
cout << ‘*’;
cout << endl;}
int main()
{
starline(); //call to function
cout << “CECE” << endlstarline(); //call to function
return 0;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 32/59
Functions
Passing Arguments to Functions
An argument is a piece of data (an int value, for
example) passed from a program to the function.
1. Passing Constants
2. Passing Variables
a) Passing by Value
b) Passing Structuresc) Passing by Reference
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 33/59
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 bodycout << ch;
cout << endl;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 34/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 35/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 36/59
Passing by Value
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 37/59
Functions
Returning Values from Functions
When a function completes its execution, it canreturn a single value to the calling program.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 38/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 39/59
Functions
Example
float lbstokg(float pounds)
{
float kilograms = 0.453592 * pounds;
return kilograms;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 40/59
Functions
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 41/59
Functions
Passing by Reference
Passing arguments by reference uses a differentmechanism. Instead of a value being passed to thefunction, a reference to the original variable, in the
calling program, is passed. (It’s actually the memoryaddress of the variable that is passed)
An important advantage of passing by reference is thatthe function can access the actual variables in thecalling program. Among other benefits, this provides amechanism for passing more than one value from thefunction back to the calling program.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 42/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 43/59
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;}
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 44/59
Functions
Overloaded Functions
An overloaded function appears to performdifferent activities depending on the kind of data
sent to it.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 45/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 46/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 47/59
Overloaded Functions
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 48/59
Functions
Default arguments
Surprisingly, a function can be called withoutspecifying all its arguments. This won’t work on just
any function: The function declaration must providedefault values for those arguments that are notspecified.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 49/59
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 timescout << ch; //prints ch
cout << endl;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 50/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 51/59
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 1Return 2
Return 6
Return 24
Return 120
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 52/59
Functions
Inline Functions
This kind of function is written like a normal functionin the source file but compiles into inline code
instead of into a function. The source file remainswell organized and easy to read, since the functionis shown as a separate entity. However, when theprogram is compiled, the function body is actually
inserted into the program wherever a function calloccurs.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 53/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 54/59
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 they’re defined. • Variables with storage class static exist for the lifetime of
the program.
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 55/59
Functions
Scope & Storage Class
1. Local Variable
2. Global Variable
3. Static Local Variable
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 56/59
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;
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 57/59
Functions
Example For Static Local Variable
float getavg(float newdata)
{
static float total = 0; //static variables are initializedstatic int count = 0; // only once per program
count++; //increment count
total += newdata; //add new data to total
return total / count; //return the new average
}
12/3/2011C++ (OOP) CECE Academic Team
8/2/2019 C++ ( Oop )
http://slidepdf.com/reader/full/c-oop- 58/59
Reference
Object-Oriented Programming in C++ by Lafore
12/3/2011C++ (OOP) CECE Academic Team