5. Inheritance
•is the process by which a new class—known as a derived class—is created from another class, called the base class
6. Inheritance
•is the process by which a new class—known as a derived class—is created from another class, called the base class
•A derived class automatically has all the member variables and all the ordinary member functions that the base class has, and can have additional member functions and additional member variables.
7. Inheritance
•We have Parent class “ Vehicle” and child class “ Wheeled vehicle” and child of child “Car” .
•So each “Car” is a “ Wheeled vehicle” and “Vehicle” .
8. Inheritance
•To derive class from base class , we write : class DerivedClass : public BaseClass{ class_members }
9. Inheritance
•To derive class from base class , we write : class DerivedClass : public BaseClass{ class_members }
• A class that is a parent of a parent of a parent of another class, is often called an ancestor class
• If class A is an ancestor of class B, then class B is often called a descendant of class A
10. Inheritance
•The members of the Child classes are members from the Parent classes , so any operation or any function is defined in Parent class , will be defined in Child classes .
11. Inheritance
•The members of the Child classes are members from the Parent classes , so any operation or any function is defined in Parent class , will be defined in Child classes .
•The other way isn’t correct !!
22. Inheritance
•If a function is defined in Parentclass and in Childclass , Then an Object from Child can reach to the function In Parentclass By this method :
•ChildClass Object. ParentClass :: function(parameters)
23. Inheritance
•CONSTRUCTORS IN DERIVED CLASSES :
•A constructor in a base class is not inherited in the derived class, but you can invoke a constructor of the base class within the definition of a derived class constructor .
24. Inheritance
•CONSTRUCTORS IN DERIVED CLASSES :
•A constructor in a base class is not inherited in the derived class, but you can invoke a constructor of the base class within the definition of a derived class constructor .
• constructors are called in hierarchial , from BaseClass constructors CalledClass constructors .
25. Inheritance
•CONSTRUCTORS IN DERIVED CLASSES :
Derived::Derived(const Derived& Object)
: Base(Object), <probably more initializations>
{
...
}
26. Inheritance
•DESTRUCTOR IN DERIVED CLASSES :
•The Destructor for the ParentClass is called automatically from the ChildClass Destructor , so just define the ChildClasses Destructors .
27. Inheritance
•DESTRUCTOR IN DERIVED CLASSES :
•The Destructor for the ParentClass are called automatically from the ChildClass Destructor , so just define the ChildClasses Destructors .
•The Destruction is in an opposite way of the Construction!!
28. Inheritance
•you cannot access a private member variable or private member function in the definition or implementation of a derived class.
•THE protected QUALIFIER :
29. Inheritance
•you cannot access a private member variable or private member function in the definition or implementation of a derived class.
•THE protected QUALIFIER :
•if a member is marked as protected in a base class, then it can be accessed by name in the definitions of all descendant classes, not just in those classes directly from the base class.
31. Inheritance
•NOTES :
•Overloaded assignment operators and constructors are not inherited.
•You have to Overload it using the operator in ParentClass like that:
Derived& Derived::operator =(const Derived& rightSide)
{
Base::operator =(rightSide);
...
}
32. Inheritance
•NOTES :
•It’s not necessary for ChildClass and ParentClass being from the same Namespace !
33. Inheritance
•NOTES :
•It’s not necessary for ChildClass and ParentClass being from the same Namespace !
•You can inherit from more one class !
childClass: ParentClass1, ParentClass2 {… …. }
34. That’s for today
Go and Play !!
That’s for today guys !!
Bye Bye !
35. 2 bytes team
Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account
2 bytes team