0% found this document useful (0 votes)
26 views20 pages

Inheritance and Its Types (Our Presentation)

INHERITANCE presentation

Uploaded by

Muhammad Abuzar
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)
26 views20 pages

Inheritance and Its Types (Our Presentation)

INHERITANCE presentation

Uploaded by

Muhammad Abuzar
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

Inheritance and its

types
Inheritance and its types

Group members
 Muhammad Afaq (BCS23028)
 Danish Naseer (BCS23006)
 Muhammad Ahmad (BCS23014)
 Muhammad Abuzar (BCS23037)
Inheritance and its types

 Inheritance in C++ is a concept where one class (called the

What is derived class) can use the properties and functions of another
class (called the base class).

inheritance  This means the derived class inherits all the data and
behaviors of the base class.

 It helps in reusing code and avoids duplication.


Inheritance and its types

 Base class:
The base class is the existing class from which properties (like

What is variables and functions) are inherited. It provides the fundamental


structure or behavior that other classes can reuse or build upon.
It helps in reusing code and avoids duplication.
It's also called the parent class or superclass.
inheritance  Derived class:
The derived class is the new class that inherits properties and
behaviors from the base class. It can use the functionality of the
base class and can also add new features or override existing
ones. It's also called the child class or subclass.
Inheritance and its types

Syntax

Class A {
Structure private:
It helps in reusing code and avoids duplication.
int a;
of class protected:
int b;
public:
int c;
void AB(){
cout<<“member function”
}
};
Inheritance and its types

Syntax
class DerivedClass : accessSpecifier BaseClass {
// Body of derived class
};
What is It helpsDerivedClass:
in reusing code and avoids duplication.

inheritance This is the new class that will inherit from the base class.

BaseClass:
This is the class from which properties and behaviors
are inherited.

accessSpecifier:
It defines how the members of the base class will be
inherited by the derived class. It can be Private, Public or
Protected.
Inheritance and its types

Types of  Public inheritance


It helps in reusing code and avoids duplication.
 Private inheritance
inheritance  Protected inheritance
Inheritance and its types

Public inheritance in C++ is a type of inheritance where the public and


protected members of the base class remain public and protected,
respectively, in the derived class.

Public ItPublic
helps in reusing code
Members: and
Public avoids duplication.
members of the base class stay public in the
derived class, meaning they can be accessed by any code that has access to
inheritance the derived class.

Protected Members: Protected members of the base class remain protected


in the derived class, meaning they are accessible within the derived class and
any classes that derive from it but not by outside code.
Inheritance and its types
Example of Public inheritance
#include <iostream>
using namespace std; class Derived : public Base {
class Base { private:
private: void privateMethodDerived() {
void privateMethod() { cout << "Private method in Derived" << endl;
cout << "Private method in Base" << endl; }
} protected:
protected: void and
It helps in reusing code protectedMethodDerived()
avoids duplication. {
void protectedMethod() { cout << "Protected method in Derived" << endl;
cout << "Protected method in Base" << endl; }
} public:
public: void publicMethodDerived() {
void publicMethod() { cout << "Public method in Derived" << endl;
cout << "Public method in Base" << endl; }
} void accessBaseMethods() {
void accessPrivate() { publicMethod();
protectedMethodDerived();
privateMethod(); protectedMethod();
}
} accessPrivate();
};
}; privateMethodDerived();
Inheritance and its types
Example of Public inheritance

int main() {
Derived obj;
obj.publicMethodDerived();
obj.publicMethod(); It helps in reusing code and avoids duplication.
// obj.protectedMethod(); // Error: protectedMethod is protected in Base
// obj.privateMethod(); // Error: privateMethod is private in Base
obj.accessBaseMethods();
return 0;
}
Inheritance and its types

Private inheritance in C++ is a type of inheritance where all public and


protected members of the base class become private members in the derived
class.

Private ItPublic
helps in reusing code
Members: and
Public avoids duplication.
members of the base class become private in the
derived class, so they are only accessible within the derived class itself and
inheritance not from outside or by further derived classes.

Protected Members: Protected members of the base class also become


private in the derived class, meaning they are not accessible by further
derived classes or external code.
Inheritance and its types
Example of Private inheritance
#include <iostream> class Derived : private Base {
using namespace std; private:
class Base { void privateMethodDerived() {
private: cout << "Private method in Derived" << endl;
void privateMethod() { }
cout << "Private method in Base" << endl; protected:
} It helps in reusing codevoid
and protectedMethodDerived()
avoids duplication. {
protected: cout << "Protected method in Derived" << endl;
void protectedMethod() { }
cout << "Protected method in Base" << endl; public:
} void publicMethodDerived() {
public: cout << "Public method in Derived" << endl;
void publicMethod() { }
cout << "Public method in Base" << endl; void accessBaseMethods() {
} publicMethod();
void accessPrivate() { protectedMethod(); protectedMethodDerived();
privateMethod(); privateMethodDerived(); }
} };
};
Inheritance and its types
Example of Private inheritance

int main() {
Derived obj;
obj.publicMethodDerived();
// obj.publicMethod(); // Error:ItpublicMethod
helps in reusing code and
is private avoids duplication.
in derived
// obj.protectedMethod(); // Error: protectedMethod is private in derived
obj.accessBaseMethods();
return 0;
}
Inheritance and its types

Protected inheritance in C++ is a type of inheritance where all public and


protected members of the base class become protected members in the
derived class.

Protected ItPublic
helps in reusing code
Members: and
Public avoids duplication.
members of the base class become protected in the
derived class, so they are accessible within the derived class and any classes
inheritance that further derive from it, but not by external code.

Protected Members: Protected members of the base class remain protected


in the derived class, meaning they are accessible within the derived class and
its further derived classes, but not by external code.
Inheritance and its types
Example of Protected inheritance
#include <iostream> class Derived : protected Base {
using namespace std; private:
class Base { void privateMethodDerived() {
private: cout << "Private method in Derived" << endl;
void privateMethod() { }
cout << "Private method in Base" << endl; protected:
} It helps in reusing codevoid
and protectedMethodDerived()
avoids duplication. {
protected: cout << "Protected method in Derived" << endl;
void protectedMethod() { }
cout << "Protected method in Base" << endl; public:
} void publicMethodDerived() {
public: cout << "Public method in Derived" << endl;
void publicMethod() { }
cout << "Public method in Base" << endl; void accessBaseMethods() {
} publicMethod(); protectedMethodDerived();
void accessPrivate() { protectedMethod(); }
privateMethod(); privateMethodDerived(); };
}
};
Inheritance and its types
Example of Protected inheritance

int main() {
Derived obj;
obj.publicMethodDerived();
// obj.publicMethod(); // Error:ItpublicMethod
helps in reusing code and
is private avoids duplication.
in derived
// obj.protectedMethod(); // Error: protectedMethod is private in derived
obj.accessBaseMethods();
return 0;
}
Inheritance and its types

 Code reusability
Advantages It Ease
helps of maintenance
in reusing code and avoids duplication.
 Extensibility
of  Improved code organization
inheritance
Inheritance and its types

 Fragile base class problem


DisadvantagesIt helps in reusing code and avoids duplication.
Increased complexity
of inheritance  Propagation of Bugs
Inheritance and its types

Why Encapsulation:
Private members are only accessible within their own class to
private is Itprotect
helps inthe
reusing code andstate
internal avoids duplication.
and implementation details.

always Class Integrity:


Keeping private members hidden ensures that derived classes
cannot directly alter or misuse the base class's internal data.
hidden?
Inheritance and its types

Difference Visibility of Base Class Members

between Private Inheritance: Base class members are not accessible by classes
derived from the derived class. The base class’s interface is hidden from

Private further derived classes and outside code.

Protected Inheritance: Base class members are accessible to classes


and derived from the derived class. This allows further derived classes to
access the base class’s interface

Protected

You might also like