Oop 4
Oop 4
INHERITANCE
CONTENTS
Inheritance
Single inheritance
Base class and derived classes
Protect members of class
Types of inheritance
Constructors
Functions under inheritance
Multiple inheritance
Hierarchical Inheritance
Multilevel Inheritance
INHERITANCE
Inheritance is a fundamental concept in
several fields, including biology, law, and
computer science.
Simple program for two classes
#include<iostream>
using namespace std;
class Person
{
private:
string name;
int age;
string email;
public:
};
class student
{
private:
int grade;
public:
void getvalue(int g)
{
grade = g;
}
void print()
{
cout << "===========" << endl;
cout << grade << " \n" << endl;
}
};
int main()
{
Person ob;
ob.setValues("ali", 123,"g.");
ob.print();
student ob1;
ob1.getvalue(78);
ob1.print();
return 0;
}
O/P
ali
123
g.
===========
78
Single inheritance
Single inheritance is a concept in object-oriented
programming (OOP) where a class can inherit
properties and behaviors (methods) from one and
only one parent class.
Single Inheritance
#include<iostream>
class Person
private:
string name;
int age;
string email;
public:
void setValues(string n, int a, string em)
{
name = n;
age = a;
email = em;
}
void print()
{
cout << name << " \n" << age << " \n" << email << endl;
}
};
class student : public Person // derived class(inheritance function)
{
private:
int grade;
public:
void getvalue(int g) //calling function
{
grade = g;
}
void dispaly()
{
{
cout << "===========" << endl;
cout << grade << " \n" << endl;
}
};
int main()
{
student ob1;
ob1.getvalue(78);
ob1.dispaly();
ob1.setValues("ala",1, "h");
ob1.print();
return 0;
}
O/P
78
ala
1
h
Inheritance Using Private
#include <iostream>
class f
private:
int x = 10;
public:
void print()
};
class s : private f // derived class(inheritance function)
{
};
int main()
{
S ob1;
//F ob1;
ob1.print();// error(print it’s private)
return 0;
}
Default Constructor In Inheritance
In C++, a default constructor is a constructor that can be called with no
arguments.
O/P
hi i am first constructor
i am second constructor
Parametrized Constructor In Inheritance
a parameterized constructor in the derived class can
be used to initialize both the base class and the
derived class objects.
#include <iostream>
using namespace std;
class first
{
private:
int x;
public:
first(int a) // parametiized constructor
{
x = a;//10
cout << " This is first constructor" << endl;
}
};
class second :public first // parametrized constructor for Derived class
{
private:
int y;
public:
second(int a, int c):first(a)//const receiving two values for(f,s)
{
y = c;//11
cout << " It's second constructor" << endl;
}
};
int main()
{
o/p
This is first constructor
It's second constructor
Protected
In object-oriented programming (OOP), the term
protected refers to a specific access modifier that
controls the visibility of class members (attributes
and methods).
#include <iostream>
using namespace std;
class first
{
private:
int x=2;
public:
int y=5;
protected:
int z=6;
public:
void print()
{
cout << z << endl;
}
};
class second : public first// Derived class
{
public:
void show()
{
cout << "Z is the protected " << z << endl;
}
};
int main()
{
second ob;
ob.show();
}
O/P
Z is the protected 6
Protected With Inheritance
#include <iostream>
using namespace std;
class first
{
private:
int X = 7;
public:
int y = 4;
protected:
int z = 9;
public:
void display()
{
cout << "This is frist class " << endl;
}
};
class second :protected first
{
public:
/* void showX()//private
{
cout << "This IS x " << X << endl;//ERROR BCZ I CAN'T CALLING FUCTION OF
PRIVATE OUTSIDE CLASS
}*/
void showy()//public
{
cout << "This IS y " << y << endl;
}
void showz()//protected
{
cout << "This IS z " << z << endl;
}
};
int main()
{
second ob;
//ob.showX();//it’s private
ob.showy();// public
ob.showz();// protected
return 0;
O/P
This IS y 4
This IS z 9
Functions Under Inheritance
A virtual function is a member function in the
base class that we expect to redefine in derived
classes.
To avoid this, we declare the print() function of
the Base class as virtual by using the virtual
keyword.
#include <iostream>
#include <string>
using namespace std;
// Superclass
class Animal {
public:
virtual string sound()// virtual function
{
return "sound";
}
};
//Subclass
class Dog : public Animal // inheritance
{
public:
string sound() override // Method overriding
{
return "Bark";
}
};
int main() {
Dog dog;// object class
return 0;
}
O/P
Bark
Multiple-inheritance
Multiple inheritance
Multiple inheritance is a feature in object-oriented
programming (OOP) where a class can inherit from
more than one parent class.
Simple program of multiple-inheritance
#include <iostream>
using namespace std;
// Base class 1
class Animal {
public:
void eat() {
cout << "Animal is Eat." << endl;
}
};
// Base class 2
class Bird {
public:
void fly() {
cout << "Bird is flying." << endl;
}
};
// Derived class that inherits from both Animal and Bird
class Sparrow : public Animal, public Bird {
public:
void sound() {
cout << "Sparrow is chirping." << endl;
}
};
int main() {
// Create an object of the derived class
Sparrow sparrow;
// Call functions from both base classes and the derived class
sparrow.eat(); // Inherited from Animal
sparrow.fly(); // Inherited from Bird
sparrow.sound(); // Defined in Sparrow
return 0;
}
O/P
Animal is Eat.//CLASS 1
Bird is flying. //CLASS 2
Sparrow is chirping. //CLASS 3
Simple program of multiple-inheritance(with override)
#include <iostream>
using namespace std;
// Base class 1
class Animal {
public:
void p() {
cout << "Animal is Eat." << endl;
}
};
// Base class 2
class Bird {
public:
void p() {
cout << "Bird is flying." << endl;
}
};
// Derived class that inherits from both Animal and Bird
class Sparrow : public Animal, public Bird {
public:
void p() {
// override function
Animal::p()
Bird::p();
cout << "Sparrow is chirping." << endl;
}
};
int main() {
// Create an object of the derived class
Sparrow ob;
// Call functions from both base classes and the derived class
ob.p(); // Defined in Sparrow
return 0;
}
O/P
Animal is Eat.
Bird is flying.
Sparrow is chirping.
Simple program of multiple-inheritance with parameterized const
#include <iostream>
#include <string>
// Base class 1
class Person {
protected:
string name;
int age;
public:
// Parameterized constructor for Person
Person(string n, int a) : name(n), age(a) {}
void displayPersonInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Base class 2
class Employee {
protected:
string position;
public:
// Parameterized constructor for Employee
Employee(string p) : position(p) //is a member initialization list. (special for Employee
objec)
{} void displayEmployeeInfo() {
cout << "Position: " << position << endl;
}
};
// Derived class
class Manager : public Person, public Employee {
private:
string department;
public:
// Constructor for Manager which calls constructors of both base classes
Manager(string n, int a, string p, string d)
: Person(n, a), Employee(p), department(d) {}
void displayManagerInfo() {
displayPersonInfo(); // Calls Person's function
displayEmployeeInfo(); // Calls Employee's function
cout << "Department: " << department << endl;
}
};
int main() {
// Create a Manager object
Manager manager("Ahmed", 35, "Software Engineer", "IT");
return 0;
}
O/ p
Name: Ahmed, Age: 35
Position: Software Engineer
Department: IT
Hierarchical Inheritance
hierarchical inheritance occurs
when multiple classes inherit from
a single base class.
Simple Program Of hierarchical Inheritance
#include <iostream>
// Base class
class Animal {
public:
void eat() {
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void print() {
cat.eat();
cat.print();
return 0;
O/P
This animal eats food.
The dog barks.
This animal eats food.
The cat meows.
Multilevel Inheritance
multilevel inheritance is a type of inheritance in
which a class is derived from another class, and
then that derived class becomes the base class
for another derived class.
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
// Derived class 1
class Mammal : public Animal {
public:
void sleep() {
cout << "Sleeping..." << endl;
}
};
// Derived class 2 (derived from Mammal)
class Dog : public Mammal {
public:
void bark() {
}
cout << "Barking..." << endl;
}
};
int main() {
Dog dog;// create object
dog.eat(); // From Animal class
dog.sleep(); // From Mammal class
dog.bark(); // From Dog class
return 0;
}
O/P
Eating...
Sleeping...
Barking...