Inheritance
Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions, except for the constructors and destructor.
"Is a" Relationship
Constructor and Destructor
- Although the private members are inherited, they are invisible to child. They can be accessed by the member functions of the parent class
- Inheritance does not work in reverse. It is impossible for a parent class to call a member funciton of a child class
- Pass arguments to the parent class by calling the constructor of the parent
- Car(std::string p, std::string m):Vehicle(std::string p), in the definition of a constructor, not in a prototype
- Creating a child object, the constructor of parent will be called first, then the constructor of child will be called; when the object is deleted, the destructor of child will be called first, then the destructor of parent will be called
Class Access Specification
The default access specification is private, class Parent : Grand equals to class Parent : private Grand
Redefine Functions

- Protected members of a base class can be accessed by functions in a derived class
- Call Grand::getX() from Parent
- Call Grand::getX() and Parent::getX() from Child
Multiple Inheritance
Constructor, Copy Constructor, Move Constructor, Copy Assignment, Move Assignment, Destructor
- //V.h
- #ifndef V_H
- #define V_H
- #include <iostream>
- #include <string>
-
- class V
- {
- private:
- int size;
- int *array;
- public:
- //constructor
- V(int s):size(s)
- {
- std::cout<<"Constructor ..."<<std::endl;
- array = new int[size];
- for(int i = 0; i < size; i++)
- array[i] = 10*i;
- std::cout<<"----End Constructor ..."<<std::endl;
- }
-
- //copy constructor
- V(const V &right):size(right.size), array(size?new int [size]:nullptr)
- {
- std::cout<<"Copy Constructor ..."<<std::endl;
- std::copy(right.array, right.array+size, array);
- std::cout<<"----End Copy Constructor ..."<<std::endl;
- }
-
- //move constructor
- V(V && right):size(0), array(nullptr)
- {
- std::cout<<"Move Constructor ..."<<std::endl;
- size = right.size;
- array = right.array;
- right.size = 0;
- right.array = nullptr;
- std::cout<<"----End Move Constructor ..."<<std::endl;
- }
-
- int *getAddress() const {return array;}
- int getSize() const {return size;}
-
- void display() const
- {
- for(int i = 0; i < size; i++)
- std::cout<<array[i]<<" ";
- std::cout<<std::endl;
- }
-
- std::string toString() const
- {
- std::string str = "V: ";
- for(int i = 0; i < size; i++)
- str += std::to_string(array[i])+" ";
- return str;
- }
-
- V & time(int n)
- {
- for(int i = 0; i < size; i++)
- array[i] *= n;
- return *this;
- }
-
- //copy assignment
- const V& operator=(const V &right)
- {
- std::cout<<"Copy Assignment ..."<<size<<std::endl;
- if(this != &right)
- {
- V temp(right);
- //std::swap(size, temp.size);
- //std::swap(array, temp.array);
- std::swap(*this, temp);
- }
- std::cout<<"----End Assignment ..."<<std::endl;
-
- return *this;
- }
-
- //move assignment
- V & operator=(V&& right)
- {
- std::cout<<"Move Assignment ..."<<std::endl;
- if(this != &right)
- {
- delete [] array;
- array = 0;
- size = 0;
-
- size = right.size;
- array = right.array;
-
- right.size = 0;
- right.array = nullptr;
- }
- std::cout<<"----End Move Assignment ..."<<std::endl;
-
- return *this;
- }
-
- ~V()
- {
- std::cout<<"Destructor ..."<<size<<std::endl;
- delete array;
- array = 0;
- size = 0;
- }
- };
- #endif
-
- //VV.h
- #ifndef VV_H
- #define VV_H
- #include <iostream>
- #include <string>
- #include "V.h"
-
- class VV : public V
- {
- private:
- int type;
- public:
- //constructor inheritance
- VV(int t, int s):type(t), V(s)
- {
- std::cout<<"WW Constructor ..."<<std::endl;
- std::cout<<"----End WW Constructor ..."<<std::endl;
- }
-
- //copy constructor inheritance
- VV(const VV &right):type(right.type), V(right)
- {
- std::cout<<"VV Copy Constructor ..."<<std::endl;
- std::cout<<"----End VV Copy Constructor ..."<<std::endl;
- }
-
- //move constructor
- VV(VV && right):type(right.type), V(std::move(right))
- {
- std::cout<<"VV Move Constructor ..."<<std::endl;
- std::cout<<"----End VV Move Constructor ..."<<std::endl;
- }
-
- void display() const
- {
- std::cout<<"Type: "<<type<<" -- ";
- V::display();
- }
-
- //copy assignment
- const VV& operator=(const VV &right)
- {
- std::cout<<"VV Copy Assignment ..."<<std::endl;
- type = right.type;
- V::operator=(right);
- std::cout<<"----End VV Assignment ..."<<std::endl;
-
- return *this;
- }
-
- //move assignment
- VV & operator=(VV&& right)
- {
- std::cout<<"VV Move Assignment ..."<<std::endl;
- type = right.type;
- V::operator=(std::move(right));
- std::cout<<"----End VV Move Assignment ..."<<std::endl;
-
- return *this;
- }
-
- ~VV()
- {
- std::cout<<"VV Destructor ..."<<std::endl;
- }
- };
- #endif
-
- //main.cpp
- #include <iostream>
- #include "VV.h"
-
- int main(int argc, char *argv[])
- {
- //V constructor
- //VV constructor
- VV vv(1, 2);
- vv.display();//0 10
-
- //V copy constructor
- //VV copy constructor
- VV vv2(vv);
- vv.display();//0 10
- vv2.display();//0 10
-
- //V move constructor
- //VV move constructor
- VV vv3(std::move(vv));
- vv.display();//empty
- vv3.display();//0 10
-
- //VV copy assignment
- //V copy assignment
- //V copy constructor, temp(right)
- //V move assignment, swap
- //V move assignment
- //V move assignment
- VV vv4(2, 4);
- vv4.display();//0 10 20 30
- vv4 = vv3;
- vv3.display();//0 10
- vv4.display();//0 10
-
- //VV move assignment
- //V move assignment
- VV vv5(3, 8);
- vv5.display();//0 10 20 30 40 50 60 70
- vv4 = std::move(vv5);
- vv5.display();//empty
- vv4.display();//0 10 20 30 40 50 60 70
-
- return 0;
- }
-