0% found this document useful (0 votes)
7 views18 pages

C++ Inheritance and Polymorphism Guide

Uploaded by

kantie18jooh
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)
7 views18 pages

C++ Inheritance and Polymorphism Guide

Uploaded by

kantie18jooh
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

Programming C++

z
Inheritance
and Polymorphism
z
Inheritance

 Inheritance allows one class to reuse attributes and methods


from another class. It helps you write cleaner, more efficient
code by avoiding duplication.

 We group the "inheritance concept" into two categories:

 derived class (child class, or subclass.) - the class that


inherits from another class

 base class (parent class, or superclass) - the class being


inherited from
z

 A child class inherits both behaviors (member functions) and


properties (member variables) from the parent (subject to some
access restrictions that we’ll cover in a future lesson).

 These variables and functions become members of the derived


class.

 Because child classes are full-fledged classes, they can (of


course) have their own members that are specific to that class.

 It’s possible to inherit from a class that is itself derived from


another class. There is nothing noteworthy or special when
doing so
 In the example below, the Car class (child) inherits the attributes and methods from the Vehicle class
z (parent):

 // Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n" ;
}
};

// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};

int main() {
Car myCar;
[Link]();
cout << [Link] + " " + [Link];
return 0;
}
z
Types of Inheritance in C++

1. Single Inheritance
 When a child/derived class inherits only one base/parent class, it
is called a single inheritance.
z
2. Multilevel Inheritance

 A class can also be derived from one class, which is already derived
from another class.
 In the following example, MyGrandChild is derived from class MyChild (which is derived
from MyClass)
z
 // Base class (parent)
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};

// Derived class (child)


class MyChild: public MyClass {
};

// Derived class (grandchild)


class MyGrandChild: public MyChild {
};

int main() {
MyGrandChild myObj;
[Link]();
return 0;
}
3. Multiple Inheritance
z

 Multiple inheritance enables a derived class to inherit members


from more than one parent.
Example

z  // Base class
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};

// Another base class


class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};

// Derived class
class MyChildClass: public MyClass, public MyOtherClass {
};

int main() {
MyChildClass myObj;
[Link]();
[Link]();
return 0;
}
z
4. Hierarchical Inheritance

 If more than one derived class inherits a single base class, a


hierarchy of base classes is formed and so it is known as
hierarchical inheritance.
z
5. Hybrid Inheritance

 It is the combination of more than one type of inheritance i.e


hybrid variety of inheritance.
z
Why And When To Use "Inheritance"?

 Inheriting from a base class means we don’t have to redefine the


information from the base class in our derived classes.

 We automatically receive the member functions and member


variables of the base class through inheritance, and then simply
add the additional functions or member variables we want.

 This not only saves work, but also means that if we ever update
or modify the base class (e.g. add new functions, or fix a bug), all
of our derived classes will automatically inherit the changes!
Polymorphism
z

 In programming, polymorphism refers to the ability of an entity to have


multiple forms (the term “polymorphism” literally means “many forms”).

 It occurs when we have many classes that are related to each other by
inheritance.

 Inheritance lets us inherit attributes and methods from another class.

 Polymorphism uses those methods to perform different tasks. This


allows us to perform a single action in different ways.
z

 For example, imagine a base class Animal with a method


called makeSound(). Derived classes of Animals could be Pigs,
Cats, Dogs, Birds, etc. Every animal can "make a sound", but
each one sounds different:

 Pig: wee wee

 Dog: bow wow

 Bird: tweet tweet

 This is polymorphism - the same action (making a sound)


behaves differently for each animal:
Example

z  // Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
}
};

// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n";
}
};

// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n";
}
};
Now we can create Pig and Dog objects and override
z the animalSound() method:

// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
}
};

// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n";
}
};

// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n";
}
};

int main() {
Animal myAnimal;
Pig myPig;
Dog myDog;

[Link]();
[Link]();
[Link]();
return 0;
}
z
Conclusion

 Inheritance allows us to reuse classes by having other classes


inherit their members

 it is useful for code reusability: reuse attributes and methods of an


existing class when you create a new class.
z
References

 [Link]

 [Link]

You might also like