Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Inheritance: From membership point of view Vehicle SUV Honda Pilot Object Number Integer Double Base concept Derived concept Java Base class Derived.

Similar presentations


Presentation on theme: "1 Inheritance: From membership point of view Vehicle SUV Honda Pilot Object Number Integer Double Base concept Derived concept Java Base class Derived."— Presentation transcript:

1 1 Inheritance: From membership point of view Vehicle SUV Honda Pilot Object Number Integer Double Base concept Derived concept Java Base class Derived class A SUV is a vehicle, but a vehicle may not be a SUV

2 2 Inheritance: From functionality point of view SUV Vehicle Derived class Base class All vehicle can do, a SUV can too

3 3 Base class & Derived class // A is a base class class A { public: A(int a); int get_a(); private: int my_a; }; // B is a derived class class B: public A { public: B(int b); int get_b(); private: int my_b; }; int main() { A a(2); B b(3); cout << a.get_a(); cout << b.get_a() << b.get_b(); } B inherits A’s functions and variables.

4 4 Base class and Derived class class A { public: A(int a); int get_a(); private: int my_a; }; class B: public A { public: B(int b); int get_b(); private: int my_b; }; B::B(int b) :my_b(b), A(2*b) { } Need to call the constructor of A to initialize A’s private variable; the constructors will not be inherited. You can decide how to call A’s constructor.

5 5 Parameters and Derived class class A {...... get_a() }; class B: public A {...... get_b() }; void fun_for_A(A a) {....a.get_a()....; } void fun_for_B(B b) {....b.get_a()....;....b.get_b()....; } int main() { A a(2); B b(3); fun_for_A(b); // This is OK! fun_for_B(a); // This is not allowed } because fun_for_B may use a member function of class B that is newly defined when derive B but not available in class A.

6 6 3 Generations // Base class class A { public: A(int a); int get_a(); private: int my_a; }; // Derived class class B: public A { public: B(int a); int get_b(); private: int my_b; }; int main() { A a(1); B b(2); C c(3); cout << a.get_a(); cout << b.get_a() << b.get_b(); cout << c.get_a() << c.get_b() << c.get_c(); } // Derived class class C: public B { public: C(int a); int get_c(); private: int my_c; };

7 7 Richer and Richer public: A(int a); int get_a(); private: int my_a = 1; int main() { A a(1); B b(2); C c(3);..... } public: B(int a); int get_a(); int get_b(); private: int my_a = 3; int my_b = 2; public: C(int a); int get_a(); int get_b(); int get_c(); private: int my_a = 4; int my_b = 5; int my_c = 3; a: b: c: Suppose constructors are defined as: A::A(int a) :my_a(a) {} B::B(int b) :my_b(b), A(b+1) {} C:C(int c) :my_c(c), B(c+2) {} //C:C(int c) //:my_c(c), A(c+1), B(c+2) {} The constructors of the base classes will not be inherited. Inherited from A Inherited from B 6

8 8 Inheritance and Big-Three // Base class class A { public: A(int a); A(const & A); //Copy constructor; A & operator = (const A & a); // overload = ~A(); // Destructor;..... private: int size_A; int *array_A; }; // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; In private section!! Use A’s constructor

9 9 Inheritance and copy Constructor // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; // Derived class B::B(const B & b) :A(b) // call the copy constructor of A; { size_B = b.size_B; array_B = new int[size_B]; for (int i=0; i<size_B; i++) array_B[i] = b. array_B[i]; }

10 10 Inheritance and assignment = // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; // Derived class B & B::operator = (const B & b) { A::operator=(b); if (size_B != b.size_B()) { delete [] array_B; array_B = new int[size_B]; }; for (int i =0; i<size_B; i++) array_B[i] = b.array_B[i]; return *this; } This will take care of the variables in the base class

11 11 Inheritance and destructor // Derived class class B : public A { public: B(int b); B(const & B); //Copy constructor; B & operator = (const B & b); // ~B(); // Destructor;..... private: int size_B; int *array_B; }; // Derived class B::~B() { delete [] array_B; }


Download ppt "1 Inheritance: From membership point of view Vehicle SUV Honda Pilot Object Number Integer Double Base concept Derived concept Java Base class Derived."

Similar presentations


Ads by Google