Unit 3 - OOP
Unit 3 - OOP
Unit 3 - OOP
Inheritance is the capability of one class to acquire properties and characteristics from another
class. The class whose properties are inherited by other class is called
the Parent or Base or Super class. And, the class which inherits properties of other class is
called Child or Derived or Sub class.
Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields
become available in the new class, hence code is reused.
1. Code Reusability
2. Method Overriding (Hence, Runtime Polymorphism.)
3. Use of Virtual Keyword
Example of Inheritance
public:
int legs = 4;
};
public:
int tail = 1;
};
int main()
Dog d;
}
Access Modifiers and Inheritance: Visibility of Class Members
Depending on Access modifier used while inheritance, the
availability of class members of Super class in the sub class changes.
It can either be private, protected or public.
1) Public Inheritance
This is the most used inheritance mode. In this the protected
member of super class becomes protected members of sub class
and public becomes public.
2) Private Inheritance
In private mode, the protected and public members of super class
become private members of derived class.
3) Protected Inheritance
In protected mode, the public and protected members of Super
class becomes protected members of Sub class.
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
Output
This is a Vehicle
This is a 4 wheeler Vehicle
Hierarchical Inheritance in C++
In this type of inheritance, multiple derived classes inherits from a single base class.
Output
This is a Vehicle
This is a Vehicle
#include <iostream>
using namespace std;
class A {
public:
void display{
cout<<"Base class content.";
}
};
int main() {
C obj;
obj.display();
return 0;
}
Output
Base class content.
Hybrid (Virtual) Inheritance in C++
Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void info() {
cout << "I am an animal." << endl;
}
};
// derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "I am a Dog. Woof woof." << endl;
}
};
// derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "I am a Cat. Meow." << endl;
}
};
int main() {
// Create object of Dog class
Dog dog1;
cout << "Dog Class:" << endl;
dog1.info(); // Parent Class function
dog1.bark();
return 0;
}
Output
Dog Class:
I am an animal.
I am a Dog. Woof woof.
Cat Class:
I am an animal.
I am a Cat. Meow.
Ever wondered what virtual base classes are? You can use them in virtual
inheritance in a technique of checking multiple “instances” of a specified class
looking in an inheritance hierarchy through multiple inheritances. When you use
multiple inheritances, a virtual base class in C++ is used to avoid multiple "instances"
of a given class from occurring in an inheritance hierarchy.
Now, in this article, you will explore the virtual base class in C++ as a whole.
Consider the case where there is just one class A. Two other grades, B and C,
descend from this class A.
The data members/functions of class A are inherited twice to class D, as seen in the
diagram. The first will take you through class B, and the second will take you through
class C. When an object of class D accesses any data or function member of class
A, it's unclear which data or function member would be named. One was inherited
via B, and it inherited the other via C. This throws the compiler into a loop and
causes an error to appear.
Example:
#include <iostream>
using namespace std;
class A {
public:
int a;
A(){
a = 10;
}
};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};
int main(){
//creating class D object
D object;
cout << "a = " << object.a << endl;
return 0;
}
Output
#include<iostream>
using namespace std;
class B {
public: int b;
};
int main() {
D3 obj;
obj.d1 = 60;
obj.d2 = 70;
obj.d3 = 80;
Abstract Class
By definition, an abstract class in C++ is a class that has at
least one pure virtual function (i.e., a function that has no definition). The
classes inheriting the abstract class must provide a definition for the pure
virtual function; otherwise, the subclass would become an abstract class
itself.
class Shape {
public:
virtual int Area() = 0; // Pure virtual function is declared as follows.
// Function to set width.
void setWidth(int w) {
width = w;
}
// Function to set height.
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
int main() {
Rectangle R;
Triangle T;
R.setWidth(5);
R.setHeight(10);
T.setWidth(20);
T.setHeight(8);
cout << "The area of the rectangle is: " << R.Area() << endl;
cout << "The area of the triangle is: " << T.Area() << endl;
}