DATA
STRUCTURE
6.53
Lec.3
Inheritance
→Creating new classes from existing classes called
base classes.
→The new class that we create from the existing
classes is called the derived class.
→The derived class inherits the properties of the base
classes to reduce software complexity.
→Single Inheritance: the derived class is derived from a
single base class.
→Multiple Inheritance: the derived class is derived
from more than one base class.
Inheritance
• Rules:
-Private members of the base class cannot be inherited.
→To perform inheritance:
-Public members of the base class can be inherited either
as public or private members by the derived class according
Derived Class to inheritance type.
Inheritance Type
-Derived class can have more/additional member and functions
Base Class than the base class.
-Derived class can redefine a function with the same name,
number, and parameters of the base class as it (derived class)
wishes, but that doesn’t affect the function in the base class.
-All members (variables, functions) of the base class also become
members of the derived class.
Inheritance -Derived a class “Square” with public
inheritance from class “Rectangle”,
now NOTE:
1. ”length” and “width” are callable
which means they’re inherited from
“Rectangle’s” public members.
2. I redefine “set_length_width()” to
take a single parameter “l” and assign
it to be my length and width, this is
called Overriding.
-Defining my base class “Rectangle” with
3. In my main, I made an object of
public variables “length”, “width”, also
“Square” called “sq” and I called the
defining function “set_length_width()” to
“Area()” function in my cout and used
take the length and width and assign them
it even though I didn’t define it in my
to their variables, then defining “Area()”
derived class, that’s because it
to calculate the area.
inherited it from the base class.
Inheritance
-Scope resolution operator “::” used to access a function in a class then you can redefine it.
-The constructors of a derived class can (directly) initialize only the public data members inherited
from the base class of the derived class.
Inheritance
Protected: members labelled as “protected” means that they cannot be accessed by anything in the program
except the class itself or its derived class, meaning that even an object of the class can’t access it, a friend
function can access them.
-If the inheritance type of class B is “Protected” inheritance from class A that means:
→Public members of A will be inherited as Protected in B.
→Protected members of A will be inherited as Protected in B.
→Private members of A cannot be accessed from B, they are not inherited.
Composition
Composition is also another kind of relationship between Classes.
→It means that one or more members of a class are objects from another.
Composition Inheritance
Composition is a “has-a” relationship.
Overloading
Overloading means that a thing can have more than one meaning depending on where or how it’s used.
Operator Overloading:
→ “<<“ : Is used as both a stream insertion operator and a left shift operator .
Ex: cout<<“Hi there !”<<endl; ,it’s used as an insertion operator to take the statement written to the program
→ “>>”: Is used as both a stream extraction operator and a right shift operator .
Ex: cin>>x; ,it’s used as in extractor operator to extract value from the user to x.
→ “+,-”: The results of + and – are different for integer arithmetic, floating-point arithmetic, and pointer
arithmetic.
Pointer “this”
Each class has a pointer inside its member function has a pointer called “this”.
→ this= (address), *this=(value).
Friend Functions
→A friend function of a class is a nonmember function of the class but has access to all the members (public
or non-public) of the class.
→A friend function is not a member of a class, its declaration can be placed within the private, protected, or
public part of the class.
Friend function
Function Templates
Function templates means that I don’t have to keep defining the function multiple times for each type of data
(int, float, string..etc), instead I can create a template for the function so it works according to the type of
data it takes.
Class Template
-Same thing goes for a whole class:
-Now if you want to make an object with
a certain type of the class template use
“<(type)>” after the class name
Example:
listType<int> intList; //int class
aslistType<string> stringList; //string class
Note: the “Type” and “elemType” in both examples are used before any definition to declare that this variable,
function..etc could get a value of any type, we do this by starting: template <class (type)>
Note: The function members of a class template are considered function templates.