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
};
• };

Aryan's pres. entation.pptx

  • 1.
    INHERITANCE Inheritance is afundamental 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 • Singleinheritance 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 inheritanceis 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 inheritanceis 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 • Hierarchicalinheritance 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 inheritanceis 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: voidsound() { 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 DERIVEDCLASSES 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(intx) { 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: NESTINGIN 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 }; • };