Title | Inheritance - Lecture notes 8 |
---|---|
Course | Programming Technique II |
Institution | Universiti Teknologi Malaysia |
Pages | 3 |
File Size | 94.3 KB |
File Type | |
Total Downloads | 75 |
Total Views | 774 |
Programming Technique 2: Inheritance Base Class & Derived Class • Base class= parent • Derived class =child • child inherit the members in base class without rewriting them • class FinalExam : public GradedActivity //format for child class ❖ [FinalExam=child, Gradedactivity=base] • 3...
Programming Technique 2: Inheritance
Base Class & Derived Class •
Base class= parent
•
Derived class =child
•
child inherit the members in base class without rewriting them
•
class FinalExam : public GradedActivity //format for child class ❖ [FinalExam=child, Gradedactivity=base]
•
3 diff type of inheritance : private, protected and public
•
All private member variable of each inherited type is inaccessible by child class
•
base class’s constructor is called before the derived class’s constructor
•
destructors are called in reverse order, with the derived class’s destructor being called first
•
If base class( parent ) has more than 1 constructor ❖ specify it like this >>> Cube( ) : Rectangle( )
▪
Rectangle constructor will be call first than cube constructor
❖ If the base class has no default constructor, then the derived class must have a constructor that calls one of the base class constructors.
•
Base class function can be redefined in derived class
•
Derived class can become base class of another class at the same time
•
If derived class’s member function has same name as base class member function, it means derived class function redefines base class function
•
Redefined class function is DIFFERENT as overloaded function ❖ overloaded has same name but diff parameter ❖ redefined has same name and same parameter
•
object of base class will still execute base class function even though it is being redefined by derived class
•
If we redefine a function derived from base class in derived class, when we want to call that redefined function, we need to put "class name & object " as parameter. The function we want to call should have the word" virtual" in front of the function for example: virtual char getLetterGrade() const; ❖ this will tell the compiler that this function should be called and not the previous function ❖ put virtual only if the function is defined in the class ❖ The result will based on the function in the class of the object
•
Aggregation: ❖ GradedActivity *exam = new PassFailExam(100, 25, 70.0); ❖ cout getScore()...