Unit-5 Part 2
Unit-5 Part 2
Mission 2023
OBJECT ORIENTED SYSTEM
DESIGN(OOSD)
BATCH 2019-2023
INDEX
Inheritance
Types of Inheritance
Polymorphism
Function Overloading
Function Overriding
INHERITANCE
Derived Classes
A Derived class is defined as the class derived from the base class.
The Syntax of Derived class:
// main function
int main() {
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
SINGLE LEVEL INHERITANCE EXAMPLE: INHERITING FIELDS
OUTPUT
C++ SINGLE LEVEL INHERITANCE EXAMPLE: INHERITING
METHODS
Output
:
MULTILEVEL INHERITANCE
Output:
MULTIPLE INHERITANCE
Output:
AMBIGUITY IN MULTIPLE INHERITANCE
Ambiguity can be occurred in
using the multiple inheritance
when a function with the same
name occurs in more than one
base class.
The most obvious problem with
multiple inheritance occurs
during function overriding.
The pointer is a variable, it is also known as locator or indicator that points to an address of a value.
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc.
and used with arrays, structures and functions.
2) We can return multiple values from function using pointer.
3) It allows to access any memory location in the computer's memory.
Declaration of Pointer: ,
,
Example:
class employee {
int code;
char name [20] ;
public:
inline void getdata ( )= 0 ;
inline void display ( )= 0 ;
};
THIS POINTER
unique keyword called "this" to represent an object that invokes a member function.
'this' is a pointer that points to the object for which this function was called.
This unique pointer is called and it passes to the member function automatically.
The pointer this acts as an implicit argument to all the member function, for e.g.
class ABC {
int a ;
-----
-----
};
The private variable ‘a’ can be used directly inside a member function, like a=123;
We can also use the following statement to do the same job.
this → a = 123
THIS POINTER
One important application of pointer this is to return the object it points to.
For example: return *this. Inside member function it
will return an object that
invoked the function
This statement is also important while comparing two or more objects inside a member
function and return invoking object as a result.
Example:
THIS POINTER
class stud {
int a;
public: Output:
void set (int a) { 5
this → a = a; //here this point is used to
assign a class level ‘a’ with the argument
‘a’
}
void show ( ) {
cout << a;
}
};
main ( ) {
stud S1, S2;
S1.bet (5) ;
S2.show ( );
POINTERS TO DERIVED CLASSES
Example:
The term "Polymorphism" is the combination of "poly" + "morphs" which means many
forms.
Types of polymorphism:
DIFFERENCES B/W COMPILE TIME AND RUN TIME
POLYMORPHISM.
OVERLOADING (FUNCTION AND OPERATOR)
If we create two or more members having the same name but different in number or type
of parameter, it is known as overloading. In C++, we can overload:
• methods,
• constructors, and
• indexed properties
Types of overloading:
• Function overloading
• Operator overloading
FUNCTION OVERLOADING
Output:
FUNCTION OVERRIDING
Output:
ACCESS OVERRIDDEN FUNCTION IN C++
Example 2: Accessing
overridden function from
base class.
ACCESS OVERRIDDEN FUNCTION IN C++
Example 3: Calling overridden function using pointer
• A virtual function is not used for performing any task. It only serves as a placeholder.
• When the function has no definition, such function is known as "do-nothing" function.
• The "do-nothing" function is known as a pure virtual function. A pure virtual function is
a function declared in the base class that has no definition relative to the base class.
• A class containing the pure virtual function cannot be used to declare the objects of its
own, such classes are known as abstract base classes.
• The main objective of the base class is to provide the traits to the derived classes and to
create the base pointer used for achieving the runtime polymorphism.
Syntax:
PURE VIRTUAL FUNCTION
Output: