Download - 4%29Operator Overloading %26 Inheritance
-
8/6/2019 4%29Operator Overloading %26 Inheritance
1/33
-
8/6/2019 4%29Operator Overloading %26 Inheritance
2/33
C++ ( OOP )OVERLOADING & INHERITACE
Prepared by: Mohamed Farag
-
8/6/2019 4%29Operator Overloading %26 Inheritance
3/33
Contents
Operator Overloading Definition
Syntax
Overloading Unary Operators
Overloading Binary Operators
Data Conversion Inheritance
Definition
Base And Derived Classes
Access Specifier(Private ,Protected,public)
Constructor ,Destructor in derived class Private,Protected,Public inheritance
Example
Visual Example
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
4/33
Definition
Operator overloading is one of the most excitingfeatures of object-oriented programming.
In C++ the overloading principle applies not only
to functions, but to operators too.
For example, statements like
d3.addobjects(d1, d2);
d3 = d1.addobjects(d2); can be changed to the much more readable
d3 = d1 + d2;
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
5/33
Syntax
Operator Overloading is often similar toOverloading a Function Using operator Keywordfollowed by the operator we want to overload.
Return Type Operator Operator_sign (Args)
{
}
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
6/33
Overloading Unary Operators
Unary operators is that operators which act on only one operand like(++,--,Minus -)
EX:-
Class Counter
{
private:
unsigned int count; //count
public:
Counter() : count(0) //constructor
{ }
unsigned int get_count() //return count
{ return count; }
void operator ++ () //increment (prefix) {
++count;
}
};
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
7/33
Overloading Unary Operators
int main()
{
Counter c1; //define and initialize
cout
-
8/6/2019 4%29Operator Overloading %26 Inheritance
8/33
Overloading Unary Operators
Overloading Return Value:-
EX: -
Counter operator ++ () //increment count
{
++count; //increment count
Counter temp; //make a temporary Counter
temp.count = count; //give it same value as this obj return temp; //return the copy
}
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
9/33
Overloading Unary Operators
Nameless Temporary Objects:-
Ex:-
Counter operator ++ () //increment count
{
++count; // increment count, then return
return Counter(count); // an unnamed temporary object
} // initialized to this count
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
10/33
Overloading Binary Operators
Binary operators can be overloaded just as easily asunary operators .Arithmatic operators, comparisonoperators, and arithmetic assignment operators.
Ex:- Distance Addition
Distance operator +(Distance C)
{ Meters+=C.Meters;
CMs+=C.CMs;
If(CMs>=100)
{Meters++;CMs-=100;}
Return *this;//this is a pointer to the created object itself
}
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
11/33
Overloading Binary Operators
Ex:- Distance Comparison
bool operator
-
8/6/2019 4%29Operator Overloading %26 Inheritance
12/33
Overloading Binary Operators
Ex:- Arithmetic Assignment Operator
void operator += (Distance C)
{
Meters+= C.Meters; //add the Meters
CMs += C.CMs; //add the CMs
if(CMs >= 100) //if total exceeds 100,
{ //then decrease CMs
CMs -= 100; //by 100 and
Meters++; //increase Meters by 1
}
}
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
13/33
Data Conversion
Basic Type Data Conversion:- Ex:-
Int x=5;
float y=x; ///or float y = float(x); or y=static_cast=x;
Conversion Between Basic Types and Objects:- Ex:-
Operator float()
{ float x=Meters+CMs/100; return x;
}
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
14/33
Exercise
Write a program that uses += operator for the overloadedfor concatenation and should allow statements like
s1 += s2;
where s2 is added (concatenated) to s1 and the result is left ins1.
The operator should also permit the results of the operation tobe used in other calculations, as in
s3 = s1 += s2;
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
15/33
Solution
15/3/2011C++ (OOP) CECE Academic Team
class String //user-defined string type
{
private:
enum { SZ = 80 }; //size of String objects
char str[SZ]; //holds a C-string
public:
String() //no-arg constructor
{ strcpy(str, ); }
String( char s[] ) //1-arg constructor
{ strcpy(str, s); }
void display() //display the String { cout
-
8/6/2019 4%29Operator Overloading %26 Inheritance
16/33
-
8/6/2019 4%29Operator Overloading %26 Inheritance
17/33
Break :D
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
18/33
Inheritance
Definition
Base And Derived Classes
Access Specifier(Private ,Protected,public)
Constructor ,Destructor in derived class
Private,Protected,Public inheritance
Example
Visual Example
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
19/33
Definition
Inheritance is probably the most powerful feature of
object-oriented programming, after classes
themselves.
Inheritance is a mechanism of reusing and extendingexisting classes without modifying them, thus
producing hierarchical relationships between them.
Its big payoff is that it permits code reusability
Inheritance can also help in the original
conceptualization of a programming problem, and in
the overall design of the program.
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
20/33
Base And Derived Classes
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
21/33
Example
Rem:Counter Example in Operator Overloading
class Counter //base class(Parent)
{
protected: //NOTE: not private
unsigned int count; //count
public: Counter() : count(0) //no-arg constructor
{ }
Counter(int c) : count(c) //1-arg constructor
{ }
unsigned int get_count() const //return count
{ return count; } Counter operator ++ () //incr count (prefix)
{ return Counter(++count); }
};
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
22/33
Example
15/3/2011C++ (OOP) CECE Academic Team
class CountDn : public Counter //derived class(Child)
//setup the relationship between Counter and CountDn
{
public: Counter operator -- () //decr count (prefix)
{ return Counter(--count); } //unnamed Temp Class
};
Note : Members of Derived class can't access members of baseclass Unless they are public or protected
-
8/6/2019 4%29Operator Overloading %26 Inheritance
23/33
Example(Access specifiers)
15/3/2011C++ (OOP) CECE Academic Team
-
8/6/2019 4%29Operator Overloading %26 Inheritance
24/33
Constructor and Destructor in Derived Class
15/3/2011C++ (OOP) CECE Academic Team
We knew that the constructor is called by defaultwhen you Declare an Object
If this Object declared from a derived class,then the
constructor of the base class must be called too orthe Data member must be initilized ,so
What is the solution????
-
8/6/2019 4%29Operator Overloading %26 Inheritance
25/33
Constructor and Destructor in Derived Class
15/3/2011C++ (OOP) CECE Academic Team
The solution is to invoke its direct base class'sconstructor either
explicitly (via a base-class member initializer) or
implicitly (calling the base class's defaultconstructor)
-
8/6/2019 4%29Operator Overloading %26 Inheritance
26/33
Private , Protected, Public inheritance
15/3/2011C++ (OOP) CECE Academic Team
Base-class member
access specifier
Type of inheritance
( derived class access )
Derived class member
Public Private Private
Protected Private Private
Private Private Inaccessible
Public Protected Protected
Protected Protected Protected
Private Protected InaccessiblePublic Public Public
Protected Public Protected
Private Public Inaccessible
-
8/6/2019 4%29Operator Overloading %26 Inheritance
27/33
Visual Example
15/3/2011C++ (OOP) CECE Academic Team
Counter Example
Accessing Base Class Members
Substituting Base Class Constructors
Substituting Base Class Member Functions Derived Class Constructors
Overriding Member Functions
-
8/6/2019 4%29Operator Overloading %26 Inheritance
28/33
Steps for writing a Program
15/3/2011C++ (OOP) CECE Academic Team
1. File->new->Project->win32->win32 console
application
2. Write project name->OK->next
3. Choose console application-uncheck Precompiled
4. Check empty project->ok
5. You will find the solution explorer like
That in the picture on the left hand side.
-
8/6/2019 4%29Operator Overloading %26 Inheritance
29/33
Steps for Writing a program
15/3/2011C++ (OOP) CECE Academic Team
To add header files and Source files Right click on header files->add->new
Write class name then choose header file (.h)
Note:the structure of header file
#ifndef CLASSNAME_H
#define CLASSNAME_H
Class Classname
{ };
#endif
-
8/6/2019 4%29Operator Overloading %26 Inheritance
30/33
Steps for Writing a program
15/3/2011C++ (OOP) CECE Academic Team
To add Source Files(.cpp) Right click on source files->add->new item
Write the name of cpp file
Choose C++ FILE(.cpp)
Note:- Each header file has a .CPP file for writing the
implementation of the Function in a separate file. Dont forget to include the class in the main cpp file.
Dont forget to include the Base Class in Derived Classes .
-
8/6/2019 4%29Operator Overloading %26 Inheritance
31/33
Exercise
15/3/2011C++ (OOP) CECE Academic Team
In counter Example, It can increment or decrement a
counter, but only using prefix notation.
Using inheritance, add the ability to use postfix
notation for both incrementing and decrementing.
-
8/6/2019 4%29Operator Overloading %26 Inheritance
32/33
References
WWW.CProgramming.com
Object-Oriented Programming in C++ by Lafore
Email:[email protected]
15/3/2011C++ (OOP) CECE Academic Team
http://www.cprogramming.com/http://www.cprogramming.com/ -
8/6/2019 4%29Operator Overloading %26 Inheritance
33/33
THANK YOU!
P d b M h d F13/3/2011
C++ (OOP) CECE Academic Team