Oop Unit III Unit 3 Notes
Oop Unit III Unit 3 Notes
C++ Inheritance
In C++, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such way, you can reuse, extend or modify
the attributes and behaviors which are defined in other class.
In C++, the class which inherits the members of another class is called derived class and
the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.
Advantage of C++Inheritance
Code reusability: Now you can reuse the members of your parent class. So, there is no
need to define the member again. So less code is required in the class.
Types of Inheritance
o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance
Single inheritance is defined as the inheritance wchich a derived class is inherited from
the only one base class.
Multiple inheritance is the process of deriving an class that inherits the attributes from
two or more classes.
Hierarchicalinheritanceis defined as the process of deriving more than one class from a
base class.
Single inheritance ->
#include <iostream>
using namespace std;
// main function
int main()
{
// creating a child class object
Child obj1;
return 0;
}
#include <iostream>
using namespace std;
// Base class
class Base {
public:
// data member
int publicVar;
// member method
void display()
{
cout << "Value of publicVar: " << publicVar;
}
};
// Derived class
class Derived : public Base {
public:
// Function to display inherited member
void displayMember()
{
// accessing public base class member method
display();
}
int main()
{
// Create an object of Derived class
Derived obj;
return 0;
}
C++Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which means many
forms. It is a greek word. In object-oriented programming, we use 3 main concepts:
inheritance, encapsulation, and polymorphism.
If we create two or more members having the same name but different in number or typeof
parameter, it is known as C++ overloading. In C++, we can overload:
o methods,
o constructors,and
o indexed properties
o Functionoverloading
o Operator overloading
C++FunctionOverloading
Function Overloading is defined as the process of having two or more function with the
same name, but different in parameters is known as function overloading in C++. In
function overloading, the function is redefined by using either different types of arguments
or a different number of arguments. It is only through these differences compiler can
differentiate between the functions. The advantage of Function overloading is that it
increases the readability of the program because you don't need to use different names for
the same action.
Differences b/w compile time and run time polymorphhism.
Compiletimepolymorphism Runtimepolymorphism
It is less flexible as mainly all the things It is more flexible as all the thing
s
Execute at the compile time. Execute at the
runtime.