0% found this document useful (0 votes)
17 views47 pages

Inheritance

Uploaded by

Anchal Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views47 pages

Inheritance

Uploaded by

Anchal Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

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.
Types Of Inheritance
C++ supports five types of inheritance:
• Single inheritance
• Multiple inheritance
• Hierarchical inheritance
• Multilevel inheritance
• Hybrid inheritance
Derived Classes
A Derived class is defined as the class derived from the
base class.
The Syntax of Derived class:
class derived_class_name :: visibility-
mode base_class_name
{
// body of the derived class.
}
derived_class_name: It is the name of the
derived class.
visibility mode: The visibility mode specifies
whether the features of the base class are
publicly inherited or privately inherited. It can be
public or private.
base_class_name: It is the name of the base class.
• When the base class is privately inherited by the
derived class, public members of the base class
becomes the private members of the derived class.
Therefore, the public members of the base class are not
accessible by the objects of the derived class only by
the member functions of the derived class.
• When the base class is publicly inherited by the derived
class, public members of the base class also become
the public members of the derived class. Therefore, the
public members of the base class are accessible by the
objects of the derived class as well as by the member
functions of the base class.
Note:
• In C++, the default mode of visibility is
private.
• The private members of the base class are
never inherited.
C++ Single Inheritance
• Single inheritance is defined as the inheritance in
which a derived class is inherited from the only one
base class.
C++ Single Level Inheritance Example: Inheriting Fields
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
How to make a Private Member Inheritable

• The private member is not inheritable.


• If we modify the visibility mode by making it
public, but this takes away the advantage of
data hiding.
• C++ introduces a third visibility modifier,
i.e., protected.
• The member which is declared as protected will
be accessible to all the member functions within
the class as well as the class immediately
derived from it.
Visibility modes can be classified into
three categories
• Public: When the member is declared as public,
it is accessible to all the functions of the
program.
• Private: When the member is declared as
private, it is accessible within the class only.
• Protected: When the member is declared as
protected, it is accessible within its own class as
well as the class immediately derived from it.
Multilevel Inheritance
Multilevel inheritance is a process of deriving a class
from another derived class.
When one class inherits another class which is further inherited by
another class, it is known as multi level inheritance in C++. Inheritance
is transitive so the last derived class acquires all the members of all its
base classes.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking..."<<endl;
}
};
class BabyDog: public Dog
{
public:
void weep() {
cout<<"Weeping...";
}
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
}
Multiple Inheritance
• Multiple inheritance is the process of deriving a new
class that inherits the attributes from two or more
classes.
C++ Hybrid Inheritance
• Hybrid inheritance is a combination of more than one
type of inheritance.
C++ Hierarchical Inheritance
• Hierarchical inheritance is defined as the process of
deriving more than one class from a base class.
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
There are two types of polymorphism in C++
Compile time polymorphism
• The overloaded functions are invoked by matching the type
and number of arguments.
• This information is available at the compile time and,
therefore, compiler selects the appropriate function at the
compile time.
• It is achieved by function overloading and operator
overloading which is also known as static binding or early
binding.
• Now, let's consider the case where function name and
prototype is same.
Run time polymorphism
• Run time polymorphism is achieved when the object's
method is invoked at the run time instead of compile
time.
• It is achieved by method overriding which is also
known as dynamic binding or late binding.
Differences b/w compile time and run time
polymorphism
C++ Runtime Polymorphism Example
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
Runtime Polymorphism with Data Members
#include <iostream>
using namespace std;
class Animal { // base class declaration.
public:
string color = "Black";
};
class Dog: public Animal // inheriting Animal class.
{
public:
string color = "Grey";
};
int main(void) {
Animal d= Dog();
cout<<d.color;
}
C++ 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 C++ overloading. In C+
+, we can overload:
• methods,
• constructors, and
• indexed properties
Types of overloading in C++ are
• Function overloading
• Operator overloading
C++ Function Overloading Example
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C;
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}
#include<iostream>
using namespace std;
int mul(int,int);
float mul(float,int);

int mul(int a,int b)


{
return a*b;
}
float mul(double x, int y)
{
return x*y;
}
int main()
{
int r1 = mul(6,7);
float r2 = mul(0.2,3);
std::cout << "r1 is : " <<r1<< std::endl;
std::cout <<"r2 is : " <<r2<< std::endl;
return 0;
}
C++ Operators Overloading
• Operator overloading is a compile-time polymorphism in
which the operator is overloaded to provide the special
meaning to the user-defined data type.
• Operator overloading is used to overload or redefines
most of the operators available in C++.
• It is used to perform the operation on the user-defined
data type.
• For example, C++ provides the ability to add the
variables of the user-defined data type that is applied to
the built-in data types.
Operator that cannot be overloaded are as follows:

• Scope operator (::)


• Sizeof
• member selector(.)
• member pointer selector(*)
• ternary operator(?:)
Syntax of Operator Overloading
return_type class_name : : operator op(argument_list)
{
// body of the function.
}
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(int c)
{
num=c;
}

void operator ++()


{
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt(10);
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(int i)
{
x=i;
}
void operator+(A a)
{
int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;
}

void display();
};
int main()
{
A a1(5);
A a2(4);
//a1+a2;
a1.operator+(a2);
return 0;
}
C++ Function Overriding
• If derived class defines same function as defined
in its base class, it is known as function
overriding in C++.
• It is used to achieve runtime polymorphism.
• It enables you to provide specific implementation
of the function which is already provided by its
base class.
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{
cout<<"Eating bread...";
}
};
• int main()
• {
• Dog d = Dog();
• d.eat();
• return 0;
•}
C++ virtual function
• A C++ virtual function is a member function in the base class that
you redefine in a derived class. It is declared using the virtual
keyword.
• It is used to tell the compiler to perform dynamic linkage or late
binding on the function.
• There is a necessity to use the single pointer to refer to all the
objects of the different classes. So, we create the pointer to the
base class that refers to all the derived objects.
• But, when base class pointer contains the address of the derived
class object, always executes the base class function. This issue can
only be resolved by using the 'virtual' function.
• A 'virtual' is a keyword preceding the normal declaration of a
function.
• When the function is made virtual, C++ determines which function
is to be invoked at the runtime based on the type of the object
pointed by the base class pointer.
Late binding or Dynamic linkage
In late binding function call is resolved during runtime.
Therefore compiler determines the type of object at
runtime, and then binds the function call.
Rules of Virtual Function
• Virtual functions must be members of some class.
• Virtual functions cannot be static members.
• They are accessed through object pointers.
• A virtual function must be defined in the base class,
even though it is not used.
• The prototypes of a virtual function of the base class
and all the derived classes must be identical.
• If the two functions with the same name but different
prototypes, C++ will consider them as the overloaded
functions.
Pure Virtual Function
• 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.
virtual void display() = 0;
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class.";
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
In the above example, the base class contains the pure virtual function.
Therefore, the base class is an abstract base class. We cannot create
the object of the base class.

You might also like