Object-Oriented Programming: Inheritance
Outline • Introduction • Protected members in inheritance • Relationship between Base Classes and Derived Classes • Benefits and Drawbacks of Inheritance • Constructors and Destructors in Derived Classes • Public, Protected and Private Inheritance • Software Engineering with Inheritance
Introduction • Inheritance is a core concept in object-oriented programming (OOP). It allows a class to inherit properties and behaviors from another class. This is a powerful way to reuse code and promote code abstraction. • Inheritance works by creating a subclass that inherits from a superclass. The subclass inherits all of the attributes and methods of the superclass, and it can also add its own attributes and methods. • Parent – Child Relationship.
Types of inheritance Here are some of the types of inheritance: • Single inheritance: A subclass inherits from a single superclass. • Multiple inheritance: A subclass inherits from multiple superclasses. • Hierarchical inheritance: A class hierarchy is formed when a subclass inherits from another subclass. • Hybrid inheritance: A mix of two or more of the above types of inheritance occurs.
Syntax class Subclass’Name : access_specifier BaseClass’Name { // members of subclass }; The access_specifier can be one of the following: • public: The subclass inherits all of the public members of the base class. • protected: The subclass inherits all of the protected members of the base class. • private: The subclass inherits none of the members of the base class.
Example Animal class is Base Class/parent class and Dog class is Derived class/sub class.
Protected members in inheritance,oop,c++ In C++, protected members are members of a class that can only be accessed by the class itself, its subclasses, and friends. • Protected members are similar to private members, but they can be inherited by subclasses. Private members are not accessible to subclasses or friends. • The syntax for declaring a protected member in C++ is: class MyClass { protected: int x; };
Relationship between Base Classes and Derived Classes In C++, a base class is a class from which other classes are derived. The derived classes are called subclasses. The base class is the "parent" of the subclasses, and the subclasses are the "children" of the base class. The relationship between a base class and its subclasses is one of inheritance. This means that the subclasses inherit all of the properties and behaviors of the base class. The subclasses can then add their own properties and behaviors, or they can override the properties and behaviors of the base class.
Benefits and Drawbacks of Inheritance Here are some of the benefits of using inheritance in C++: I. It allows us to reuse code. II. It helps us to organize our code. III. It can help us to make our code more robust and easier to maintain. Here are some of the drawbacks of using inheritance in C++: I. It can make the code more complex. II. It can make it difficult to track down the source of errors.
Constructors and Destructors in Derived Classes When a subclass inherits from a base class, the subclass inherits the base class's constructors and destructors. However, the subclass can also define its own constructors and destructors. If the subclass defines its own constructors, the base class's constructors will be called automatically when the subclass object is created. The order in which the constructors are called is as follows: • The base class's default constructor is called. • The subclass's default constructor is called. • If the subclass defines any parameterized constructors, they are called in the order that they are defined. If the subclass defines its own destructor, the base class's destructor will be called automatically when the subclass object is destroyed. The order in which the destructors are called is as follows: • The subclass's destructor is called. • The base class's destructor is called.
Example When a DerivedClass object is created, the following happens: • The BaseClass class's default constructor is called. • The DerivedClass class's default constructor is called. When a DerivedClass object is destroyed, the following happens: • The DerivedClass class's destructor is called. • The BaseClass class's destructor is called.
Public, Protected and Private Inheritance • Public inheritance is the default type of inheritance in C++. When a class inherits from another class in public mode, the subclass inherits all of the public members of the base class. This means that the subclass can access the public members of the base class as if they were its own members. • Protected inheritance is a type of inheritance where the subclass inherits all of the protected members of the base class. This means that the subclass can access the protected members of the base class, but it cannot access the public members of the base class. • Private inheritance is a type of inheritance where the subclass inherits all of the private members of the base class. This means that the subclass can only access the private members of the base class through the base class's methods.
Public, Protected and Private Inheritance….. Type of inheritance Access to base class members Public inheritance Public and protected members Protected inheritance Protected members Private inheritance Private members
Software Engineering with Inheritance • Inheritance is a powerful tool in software engineering that can be used to improve the design and implementation of our code. It allows us to reuse code that has already been written, and it can also help us to abstract away the details of how something works. Here are some of the ways that inheritance can be used in software engineering: • To create a hierarchy of classes: Inheritance can be used to create a hierarchy of classes, where each class inherits from a more general class. This can help to organize our code and make it easier to understand. • To implement polymorphism: Inheritance can be used to implement polymorphism, which allows us to treat different objects in the same way. This can make our code more flexible and easier to use. • To reuse code: Inheritance can be used to reuse code that has already been written. This can save us time and effort, and it can also help to keep our code consistent.
End

Object oriented programming new syllabus presentation

  • 1.
  • 2.
    Outline • Introduction • Protectedmembers in inheritance • Relationship between Base Classes and Derived Classes • Benefits and Drawbacks of Inheritance • Constructors and Destructors in Derived Classes • Public, Protected and Private Inheritance • Software Engineering with Inheritance
  • 3.
    Introduction • Inheritance isa core concept in object-oriented programming (OOP). It allows a class to inherit properties and behaviors from another class. This is a powerful way to reuse code and promote code abstraction. • Inheritance works by creating a subclass that inherits from a superclass. The subclass inherits all of the attributes and methods of the superclass, and it can also add its own attributes and methods. • Parent – Child Relationship.
  • 4.
    Types of inheritance Hereare some of the types of inheritance: • Single inheritance: A subclass inherits from a single superclass. • Multiple inheritance: A subclass inherits from multiple superclasses. • Hierarchical inheritance: A class hierarchy is formed when a subclass inherits from another subclass. • Hybrid inheritance: A mix of two or more of the above types of inheritance occurs.
  • 5.
    Syntax class Subclass’Name :access_specifier BaseClass’Name { // members of subclass }; The access_specifier can be one of the following: • public: The subclass inherits all of the public members of the base class. • protected: The subclass inherits all of the protected members of the base class. • private: The subclass inherits none of the members of the base class.
  • 6.
    Example Animal class isBase Class/parent class and Dog class is Derived class/sub class.
  • 7.
    Protected members ininheritance,oop,c++ In C++, protected members are members of a class that can only be accessed by the class itself, its subclasses, and friends. • Protected members are similar to private members, but they can be inherited by subclasses. Private members are not accessible to subclasses or friends. • The syntax for declaring a protected member in C++ is: class MyClass { protected: int x; };
  • 8.
    Relationship between BaseClasses and Derived Classes In C++, a base class is a class from which other classes are derived. The derived classes are called subclasses. The base class is the "parent" of the subclasses, and the subclasses are the "children" of the base class. The relationship between a base class and its subclasses is one of inheritance. This means that the subclasses inherit all of the properties and behaviors of the base class. The subclasses can then add their own properties and behaviors, or they can override the properties and behaviors of the base class.
  • 9.
    Benefits and Drawbacksof Inheritance Here are some of the benefits of using inheritance in C++: I. It allows us to reuse code. II. It helps us to organize our code. III. It can help us to make our code more robust and easier to maintain. Here are some of the drawbacks of using inheritance in C++: I. It can make the code more complex. II. It can make it difficult to track down the source of errors.
  • 10.
    Constructors and Destructorsin Derived Classes When a subclass inherits from a base class, the subclass inherits the base class's constructors and destructors. However, the subclass can also define its own constructors and destructors. If the subclass defines its own constructors, the base class's constructors will be called automatically when the subclass object is created. The order in which the constructors are called is as follows: • The base class's default constructor is called. • The subclass's default constructor is called. • If the subclass defines any parameterized constructors, they are called in the order that they are defined. If the subclass defines its own destructor, the base class's destructor will be called automatically when the subclass object is destroyed. The order in which the destructors are called is as follows: • The subclass's destructor is called. • The base class's destructor is called.
  • 11.
    Example When a DerivedClassobject is created, the following happens: • The BaseClass class's default constructor is called. • The DerivedClass class's default constructor is called. When a DerivedClass object is destroyed, the following happens: • The DerivedClass class's destructor is called. • The BaseClass class's destructor is called.
  • 12.
    Public, Protected andPrivate Inheritance • Public inheritance is the default type of inheritance in C++. When a class inherits from another class in public mode, the subclass inherits all of the public members of the base class. This means that the subclass can access the public members of the base class as if they were its own members. • Protected inheritance is a type of inheritance where the subclass inherits all of the protected members of the base class. This means that the subclass can access the protected members of the base class, but it cannot access the public members of the base class. • Private inheritance is a type of inheritance where the subclass inherits all of the private members of the base class. This means that the subclass can only access the private members of the base class through the base class's methods.
  • 13.
    Public, Protected andPrivate Inheritance….. Type of inheritance Access to base class members Public inheritance Public and protected members Protected inheritance Protected members Private inheritance Private members
  • 14.
    Software Engineering withInheritance • Inheritance is a powerful tool in software engineering that can be used to improve the design and implementation of our code. It allows us to reuse code that has already been written, and it can also help us to abstract away the details of how something works. Here are some of the ways that inheritance can be used in software engineering: • To create a hierarchy of classes: Inheritance can be used to create a hierarchy of classes, where each class inherits from a more general class. This can help to organize our code and make it easier to understand. • To implement polymorphism: Inheritance can be used to implement polymorphism, which allows us to treat different objects in the same way. This can make our code more flexible and easier to use. • To reuse code: Inheritance can be used to reuse code that has already been written. This can save us time and effort, and it can also help to keep our code consistent.
  • 15.