Looking for a Tutor Near You?

Post Learning Requirement » x
Ask a Question
x
x

Direction

x

Ask a Question

x

Hire a Tutor

Notes On Inheritance, C++

Published in: Coding & Programming
956 Views

Learn this in-depth concept of inheritance with the following document which will clear all the concepts of inheritance including the types of inheritance and examples related to it.

Prerna S / Sharjah

4 years of teaching experience

Qualification: B.E in computer Science

Teaches: GRE, SAT, ACT, Maths, MS Office, Web Development, HTML, JAVA, English, Hindi, Science, Chemistry, Computer Science, Mathematics, English Language

Contact this Tutor
  1. Introduction to inheritance
  2. Consider apples and bananas. Although apples and bananas are different fruits, both have in common that they are fruits. And because apples and bananas are fruits, simple logic tells us that anything that is true of fruits is also true of apples and bananas. For example, all fruits have a name, a color, and a size. Therefore, apples and bananas also have a name, a color, and a size. We can say that apples and bananas inherit (acquire) these all of the properties of fruit because they are fruit. We also know that fruit undergoes a ripening process, by which it becomes edible. Because apples and bananas are fruit, we also know that apples and bananas will inherit the behavior of ripening. Apple Fruit Banana
  3. In an inheritance (is-a) relationship, the class being inherited from is called the parent class, base class, or superclass, and the class doing the inheriting is called the child class, derived class, or subclass. Shape Triangle Right Triangle Quadrilateral Rectangle Square
  4. Person class class Person public: string m _ name; int m _ age; Person(string name = m name=name; m_age=age; , int age string getName() const { return m name; } int getAge() const { return m _ age; }
  5. class BaseballPlayer public: double m_battingAverage; int m homeRuns; BaseballPlayer(double battingAverage = 0.0, int homeRuns = O)
  6. Person BaseballPlayeT int main() // Create a new BaseballPlayer object BaseballPlayer joe; // Assign it a name (we can do this directly because m _ name is public) joe.m_name = "Joe"; // Print out the name std::cout
  7. Practise and discuss order of inheritance using 4 classes 1 e bk-c
  8. Types of inheritance :public: Access specifier in base class Access specifier when inherited publicly Public Private Protected Public Inaccessible Protected Public inheritance is what you should be using unless you have a specific reason not to. Rule: Use public inheritance unless you have a specific reason to do otherwise.
  9. class Base public: int m _ public; private: int m _ private; protected: int m _ protected;
  10. class Pub: public Base // note: public inheritance // Public inheritance means: // Public inherited members stay public (so m _ public is treated as public) // Protected inherited members stay protected (so m _ protected is treated as protected) // Private inherited members stay inaccessible (so m_private is inaccessible) public: Pub() m _ public = 1; // okay: m _ public was inherited as public m_private = 2; // not okay: m_private is inaccessible from derived class m _ protected = 3; // okay: m _ protected was inherited as protected
  11. Private inheritance With private inheritance, all members from the base class are inherited as private. This means private members stay private, and protected and public members become private. ' // Private inheritance means: // Public inherited members become private (so m _ public is treated as pr ivate) // Protected inherited members become private (so m_protected is treat ed as private) // Private inherited members stay inaccessible (so m _ private is inaccessi ble)
  12. Access specifier in base class Public Private Protected Access specifier when inherited privately Private Inaccessible Private
  13. Protected inheritance Protected inheritance is the last method of inheritance. It is almost never used, except in very particular cases. With protected inheritance, the public and protected members become protected, and private members stay inaccessible. Access specifier in base class Public Private Protected Access specifier when inherited protectedly Protected Inaccessible Protected
  14. Access specifier in base class Public Private Protected Access specifier when inherited publicly Public Inaccessible Protected Access specifier when inherited privately Private Inaccessible Private Access specifier when inherited protectedly Protected Inaccessible Protected