INHERITANCE
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and
behavior from another class. The class that is being inherited from is called the base class or parent class, while the class that
is doing the inheriting is called the derived class or child class.
• In C++, inheritance is denoted by a colon followed by the name of the base class. For example:
• Class Animal {
• public:
• void sound() { std::cout << “Animal makes a sound” << std::endl; }
• };
•
• class Dog : public Animal {
• public:
• void sound() { std::cout << “Dog barks” << std::endl; }
};
SINGLE INHERITANCE
• Single inheritance in C++ is when a class inherits from a single base class. The syntax for single inheritance is:
• Class DerivedClass : public BaseClass {
• // members of DerivedClass
• };
• Example
• Class Animal {
• public:
• void sound() { std::cout << “Animal makes a sound” << std::endl; }
• };
•
• class Dog : public Animal {
• public:
• void bark() { std::cout << “Dog barks” << std::endl; }
• };
MULTILEVEL INHERITANCE
Multilevel inheritance is a type of inheritance in C++ where a class inherits from a base class, and the base class itself inherits from another base class. This creates a hierarchy of classes, where a class can
inherit properties and behavior from multiple levels of base classes.
Syntax:class Animal {
public:
void sound() { std::cout << “Animal makes a sound” << std::endl; }
};
class Mammal : public Animal {
public:
void eat() { std::cout << “Mammal eats” << std::endl; }
};
class Dog : public Mammal {
public:
void bark() { std::cout << “Dog barks” << std::endl; }
• };
MULTIPLE INHERITANCE
Multiple inheritance is a type of inheritance in C++ where a class can inherit properties and behavior from multiple base classes. This means that a class can have
more than one direct parent class.
Syntax:class BaseClass1 {
// members of BaseClass1
};
class BaseClass2 {
// members of BaseClass2
};
class DerivedClass : public BaseClass1, public BaseClass2 {
// members of DerivedClass
• };
HIERIECAL INHERITANCE
• Hierarchical inheritance is a type of inheritance in C++ where a class inherits from a base class, and another class inherits from the first class, creating a hierarchy of classes.
This allows for a more organized and structured way of defining classes, where a class can inherit properties and behavior from multiple levels of base classes.
• Ex:-class Animal {
• public:
• void sound() { std::cout << “Animal makes a sound” << std::endl; }
• };
•
• class Mammal : public Animal {
• public:
• void eat() { std::cout << “Mammal eats” << std::endl; }
• };
•
• class Carnivore : public Mammal {
• public:
• void hunt() { std::cout << “Carnivore hunts” << std::endl; }
• };
•
• class Lion : public Carnivore {
• public:
• void roar() { std::cout << “Lion roars” << std::endl; }
• };
HYBRID INHERITANCE
Hybrid inheritance is a type of inheritance in C++ that combines multiple inheritance and multilevel
inheritance. It allows a class to inherit properties and behavior from multiple base classes, and also allows
for multilevel inheritance.
• In hybrid inheritance, a class can inherit from multiple base classes, and each base class can also inherit
from another base class. This creates a complex hierarchy of classes, where a class can inherit
properties and behavior from multiple levels of base classes.
Class Animal {
public:
void sound() { std::cout << “Animal makes a sound” << std::endl; }
};
class Mammal : public Animal {
public:
void eat() { std::cout << “Mammal eats” << std::endl; }
};
class Carnivore : public Mammal {
public:
void hunt() { std::cout << “Carnivore hunts” << std::endl; }
};
class Dog : public Carnivore, public Animal {
public:
void bark() { std::cout << “Dog barks” << std::endl; }
• };
CONSTRUCTORS IN DERIVED CLASSES
Constructors in derived classes are used to initialize objects of the derived class. When an object of a derived class is created, the
constructor of the derived class is called, which in turn calls the constructor of the base class.
Here are some key points to note about constructors in derived classes:
1. The constructor of the derived class must call the constructor of the base class, either explicitly or implicitly.
2. If the base class has a default constructor (a constructor with no parameters), it is called automatically by the derived class constructor.
3. If the base class has a parameterized constructor (a constructor with parameters), the derived class constructor must explicitly call it
using the `base_class_name(parameters)` syntax.
4. The derived class constructor can also initialize its own members and perform additional initialization tasks.
• 5. If the derived class has multiple base classes, the constructors of all base classes must be called in the order they are listed in the
derived class’s inheritance list.
Example:-class BaseClass {
public:
BaseClass(int x) { std::cout << “BaseClass constructor called” << std::endl; }
};
class DerivedClass : public BaseClass {
public:
DerivedClass(int x, int y) : BaseClass(x) {
std::cout << “DerivedClass constructor called” << std::endl;
}
};
Initialisation list in constructers:-
Class Person {
public:
Person(std::string name, int age) : name_(name), age_(age) {}
private:
std::string name_;
int age_;
};
MEMBER CLASSES: NESTING IN CLASS
Member classes, also known as nested classes, are classes that are defined inside another class. The
nested class is a member of the enclosing class and has access to its private and protected members.
Syntax:class EnclosingClass {
public:
class NestedClass {
// members of NestedClass
};
• };

More Related Content

PPTX
C++ Presen. tation.pptx
PPT
Inheritance
PPTX
Inheritance
PPTX
Introduction to inheritance and different types of inheritance
PPTX
inheritance
PDF
lecture 6.pdf
PPTX
Multiple Inheritance
PPT
session 24_Inheritance.ppt
C++ Presen. tation.pptx
Inheritance
Inheritance
Introduction to inheritance and different types of inheritance
inheritance
lecture 6.pdf
Multiple Inheritance
session 24_Inheritance.ppt

Similar to Aryan's pres. entation.pptx (20)

PPT
10.Inheritance.ppt for oops programinggg
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PDF
Object-oriented Programming-with C#
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPTX
Inheritance
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PPTX
Inheritance
PPTX
Inheritance
PPT
Inheritance in C++
PPTX
PPTX
29csharp
PPTX
OOPS IN C++
PPT
Inheritance
PPT
Inheritance in C++
PDF
Inheritance In Java
PPTX
[OOP - Lec 20,21] Inheritance
PDF
lecture-2021inheritance-160705095417.pdf
PPT
11 Inheritance.ppt
DOCX
oop database doc for studevsgdy fdsyn hdf
PPT
Inheritance OOP Concept in C++.
10.Inheritance.ppt for oops programinggg
Chapter 6 and inheritance OOP C++ tu ioe
Object-oriented Programming-with C#
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Inheritance
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Inheritance
Inheritance
Inheritance in C++
29csharp
OOPS IN C++
Inheritance
Inheritance in C++
Inheritance In Java
[OOP - Lec 20,21] Inheritance
lecture-2021inheritance-160705095417.pdf
11 Inheritance.ppt
oop database doc for studevsgdy fdsyn hdf
Inheritance OOP Concept in C++.
Ad

Recently uploaded (20)

PDF
Introduction to Database Systems Lec # 1
PPTX
Stats annual compiled ipd opd ot br 2024
PPTX
cyber row.pptx for cyber proffesionals and hackers
PPTX
Bussiness Plan S Group of college 2020-23 Final
PPTX
Reinforcement learning in artificial intelligence and deep learning
PPTX
DAA UNIT 1 for unit 1 time compixity PPT.pptx
PDF
American Journal of Multidisciplinary Research and Review
PPTX
indiraparyavaranbhavan-240418134200-31d840b3.pptx
PPTX
langchainpptforbeginners_easy_explanation.pptx
PPTX
inbound6529290805104538764.pptxmmmmmmmmm
PPTX
PPT for Diseases (1)-2, types of diseases.pptx
PDF
Concepts of Database Management, 10th Edition by Lisa Friedrichsen Test Bank.pdf
PPTX
transformers as a tool for understanding advance algorithms in deep learning
PPTX
DATA ANALYTICS COURSE IN PITAMPURA.pptx
PDF
Book Trusted Companions in Delhi – 24/7 Available Delhi Personal Meeting Ser...
PPTX
Capstone Presentation a.pptx on data sci
PDF
Buddhism presentation about world religion
PPT
2011 HCRP presentation-final.pptjrirrififfi
PPTX
ifsm.pptx, institutional food service management
PPT
Classification methods in data analytics.ppt
Introduction to Database Systems Lec # 1
Stats annual compiled ipd opd ot br 2024
cyber row.pptx for cyber proffesionals and hackers
Bussiness Plan S Group of college 2020-23 Final
Reinforcement learning in artificial intelligence and deep learning
DAA UNIT 1 for unit 1 time compixity PPT.pptx
American Journal of Multidisciplinary Research and Review
indiraparyavaranbhavan-240418134200-31d840b3.pptx
langchainpptforbeginners_easy_explanation.pptx
inbound6529290805104538764.pptxmmmmmmmmm
PPT for Diseases (1)-2, types of diseases.pptx
Concepts of Database Management, 10th Edition by Lisa Friedrichsen Test Bank.pdf
transformers as a tool for understanding advance algorithms in deep learning
DATA ANALYTICS COURSE IN PITAMPURA.pptx
Book Trusted Companions in Delhi – 24/7 Available Delhi Personal Meeting Ser...
Capstone Presentation a.pptx on data sci
Buddhism presentation about world religion
2011 HCRP presentation-final.pptjrirrififfi
ifsm.pptx, institutional food service management
Classification methods in data analytics.ppt
Ad

Aryan's pres. entation.pptx

  • 1. INHERITANCE Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and behavior from another class. The class that is being inherited from is called the base class or parent class, while the class that is doing the inheriting is called the derived class or child class. • In C++, inheritance is denoted by a colon followed by the name of the base class. For example: • Class Animal { • public: • void sound() { std::cout << “Animal makes a sound” << std::endl; } • }; • • class Dog : public Animal { • public: • void sound() { std::cout << “Dog barks” << std::endl; } };
  • 2. SINGLE INHERITANCE • Single inheritance in C++ is when a class inherits from a single base class. The syntax for single inheritance is: • Class DerivedClass : public BaseClass { • // members of DerivedClass • }; • Example • Class Animal { • public: • void sound() { std::cout << “Animal makes a sound” << std::endl; } • }; • • class Dog : public Animal { • public: • void bark() { std::cout << “Dog barks” << std::endl; } • };
  • 3. MULTILEVEL INHERITANCE Multilevel inheritance is a type of inheritance in C++ where a class inherits from a base class, and the base class itself inherits from another base class. This creates a hierarchy of classes, where a class can inherit properties and behavior from multiple levels of base classes. Syntax:class Animal { public: void sound() { std::cout << “Animal makes a sound” << std::endl; } }; class Mammal : public Animal { public: void eat() { std::cout << “Mammal eats” << std::endl; } }; class Dog : public Mammal { public: void bark() { std::cout << “Dog barks” << std::endl; } • };
  • 4. MULTIPLE INHERITANCE Multiple inheritance is a type of inheritance in C++ where a class can inherit properties and behavior from multiple base classes. This means that a class can have more than one direct parent class. Syntax:class BaseClass1 { // members of BaseClass1 }; class BaseClass2 { // members of BaseClass2 }; class DerivedClass : public BaseClass1, public BaseClass2 { // members of DerivedClass • };
  • 5. HIERIECAL INHERITANCE • Hierarchical inheritance is a type of inheritance in C++ where a class inherits from a base class, and another class inherits from the first class, creating a hierarchy of classes. This allows for a more organized and structured way of defining classes, where a class can inherit properties and behavior from multiple levels of base classes. • Ex:-class Animal { • public: • void sound() { std::cout << “Animal makes a sound” << std::endl; } • }; • • class Mammal : public Animal { • public: • void eat() { std::cout << “Mammal eats” << std::endl; } • }; • • class Carnivore : public Mammal { • public: • void hunt() { std::cout << “Carnivore hunts” << std::endl; } • }; • • class Lion : public Carnivore { • public: • void roar() { std::cout << “Lion roars” << std::endl; } • };
  • 6. HYBRID INHERITANCE Hybrid inheritance is a type of inheritance in C++ that combines multiple inheritance and multilevel inheritance. It allows a class to inherit properties and behavior from multiple base classes, and also allows for multilevel inheritance. • In hybrid inheritance, a class can inherit from multiple base classes, and each base class can also inherit from another base class. This creates a complex hierarchy of classes, where a class can inherit properties and behavior from multiple levels of base classes.
  • 7. Class Animal { public: void sound() { std::cout << “Animal makes a sound” << std::endl; } }; class Mammal : public Animal { public: void eat() { std::cout << “Mammal eats” << std::endl; } }; class Carnivore : public Mammal { public: void hunt() { std::cout << “Carnivore hunts” << std::endl; } }; class Dog : public Carnivore, public Animal { public: void bark() { std::cout << “Dog barks” << std::endl; } • };
  • 8. CONSTRUCTORS IN DERIVED CLASSES Constructors in derived classes are used to initialize objects of the derived class. When an object of a derived class is created, the constructor of the derived class is called, which in turn calls the constructor of the base class. Here are some key points to note about constructors in derived classes: 1. The constructor of the derived class must call the constructor of the base class, either explicitly or implicitly. 2. If the base class has a default constructor (a constructor with no parameters), it is called automatically by the derived class constructor. 3. If the base class has a parameterized constructor (a constructor with parameters), the derived class constructor must explicitly call it using the `base_class_name(parameters)` syntax. 4. The derived class constructor can also initialize its own members and perform additional initialization tasks. • 5. If the derived class has multiple base classes, the constructors of all base classes must be called in the order they are listed in the derived class’s inheritance list.
  • 9. Example:-class BaseClass { public: BaseClass(int x) { std::cout << “BaseClass constructor called” << std::endl; } }; class DerivedClass : public BaseClass { public: DerivedClass(int x, int y) : BaseClass(x) { std::cout << “DerivedClass constructor called” << std::endl; } }; Initialisation list in constructers:- Class Person { public: Person(std::string name, int age) : name_(name), age_(age) {} private: std::string name_; int age_; };
  • 10. MEMBER CLASSES: NESTING IN CLASS Member classes, also known as nested classes, are classes that are defined inside another class. The nested class is a member of the enclosing class and has access to its private and protected members. Syntax:class EnclosingClass { public: class NestedClass { // members of NestedClass }; • };