110 likes | 232 Views
A SUV is a vehicle, but a vehicle may not be a SUV. Inheritance: From membership point of view. Vehicle SUV. Base concept. Honda Pilot. Derived concept. Java. Object. Number. Base class. Integer. Double. Derived class. All vehicle can do, a SUV can too.
E N D
A SUV is a vehicle, but a vehicle may not be a SUV Inheritance:From membership point of view Vehicle SUV Base concept Honda Pilot Derived concept Java Object Number Base class Integer Double Derived class
All vehicle can do, a SUV can too Inheritance:From functionality point of view SUV Vehicle Derived class Base 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; }; Base class & Derived class B inherits A’s functions and variables. int main() { A a(2); B b(3); cout << a.get_a(); cout << b.get_a() << b.get_b(); }
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; }; Base classand Derived class You can decide how to call A’s constructor. 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.
class A { ...... get_a() }; class B: public A { ...... get_b() }; Parameters andDerived class void fun_for_A(A a) { ....a.get_a()....; } void fun_for_B(B b) { ....b.get_a()....; ....b.get_b()....; } 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. int main() { A a(2); B b(3); fun_for_A(b); // This is OK! fun_for_B(a); // This is not allowed }
// 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; }; // Derived class class C: public B { public: C(int a); int get_c(); private: int my_c; }; 3 Generations 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(); }
a: public: A(int a); int get_a(); private: int my_a = 1; int main() { A a(1); B b(2); C c(3); ..... } Richer and Richer Inherited from A b: public: B(int a); int get_a(); int get_b(); private: int my_a = 3; int my_b = 2; 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) {} c: 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; Inherited from B 6 The constructors of the base classes will not be inherited.
// 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; }; Inheritance and Big-Three // 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
// 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; }; Inheritance and copy Constructor // Derived class B::B(const B & b) :A(b) // call the copy constructor of A; { size_B = b.size_B; array_B = newint[size_B]; for (int i=0; i<size_B; i++) array_B[i] = b. array_B[i]; }
// 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; }; Inheritance and assignment = // Derived class B & B::operator = (const B & b) { A::operator=(b); if (size_B != b.size_B()) { delete [] array_B; array_B = newint[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
// 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; }; Inheritance and destructor // Derived class B::~B() { delete [] array_B; }