Inheritance of c + + foundation

Posted by watsmyname on Sun, 28 Jul 2019 05:19:01 +0200

Catalog

Inheritance Realization

Access Qualifier protected

Base Class Initialization

Covering base classes in derived classes

Structural and destructive sequence

Private Inheritance

Protection of succession

Excision problem

Multiple Inheritance

Examples of has-a and is-a relations

Inheritance Realization

Inherited classes are called base classes or superclasses; classes derived from base classes are called derived classes or subclasses.

#include <iostream>
 using namespace std;

 class Fish
 {
 public:
	 bool FreshWaterFish;

	 void Swim()
	 {
		 if (FreshWaterFish)
			 cout << "Swims in lake" << endl;
		 else
			 cout << "Swims in sea" << endl;
	 }
 };

//Tuna inherited Fish
 class Tuna :public Fish
 {
 public:
	 Tuna()
	 {
		 FreshWaterFish = false;
	 }
 };

 //Carp inherited Fish
 class Carp :public Fish
 {
 public:
	 Carp()
	 {
		 FreshWaterFish = true;
	 }
 };


 int main()
 {
	 Carp myLunch;
	 Tuna myDinner;

	 cout << "Lunch: ";
	 myLunch.Swim();

     cout << "Dinner: ";
	 myDinner.Swim();
     system("pause");
	 return 0;
 }



 

PS: It is not difficult to inherit. The popular understanding is to use existing classes to construct new classes.

Access Qualifier protected

If I want some attributes of the base class to be accessible in derived classes, but not outside the inheritance hierarchy. You need to use protect ed. As an example of the above code, I hope that the Boolean tag FreshWaterFish of the Fish class can be accessed in the derived classes Tuna and Carp, but not in the main() of the instantiated Tuna and Carp. Modify it slightly as follows:

#include <iostream>
 using namespace std;

 class Fish
 {
 protected: //Changes
	 bool FreshWaterFish;
 public:
	 void Swim()
	 {
		 if (FreshWaterFish)
			 cout << "Swims in lake" << endl;
		 else
			 cout << "Swims in sea" << endl;
	 }
 };

//Tuna inherited Fish
 class Tuna :public Fish
 {
 public:
	 Tuna()
	 {
		 FreshWaterFish = false;
	 }
 };

 //Carp inherited Fish
 class Carp :public Fish
 {
 public:
	 Carp()
	 {
		 FreshWaterFish = true;
	 }
 };


 int main()
 {
	 Carp myLunch;
	 Tuna myDinner;

	 cout << "Lunch: ";
	 myLunch.Swim();

     cout << "Dinner: ";
	 myDinner.Swim();
     system("pause");

	 //myLunch.FreshWaterFish = false;
	 return 0;
 }



If you comment out which line in the Main function, you will get an error because FreshWaterFish is protected and cannot be accessed from main.

Base Class Initialization

If the base class contains overloaded constructors, it needs to be provided with arguments when it is instantiated. At this time, when creating a derived object, the appropriate base class constructor is invoked through the constructor of the derived class by initializing the list.

Covering base classes in derived classes

If the derived class implements functions inherited from the base class, and the return value is the same as the eigenvalue, it is equivalent to covering the method of the base class. Of course, if you still want to call the base class, you can call myDinner.Fish::Swim().

#include <iostream>
 using namespace std;

 class Fish
 {
 protected: //Changes
	 bool FreshWaterFish;
 public:
	 void Swim()
	 {
		 if (FreshWaterFish)
			 cout << "Swims in lake" << endl;
		 else
			 cout << "Swims in sea" << endl;
	 }
 };

//Tuna inherited Fish
 class Tuna :public Fish
 {
 public:
	 Tuna()
	 {
		 FreshWaterFish = false;
	 }
 };

 //Carp inherited Fish
 class Carp :public Fish
 {
 public:

	 void Swim()
	 {
		 cout << "Carp swims real slow" << endl;//This line has the same name as the function in the original base class, and it has been rewritten to use this as the output when it is called.
	 }

	 Carp()
	 {
		 FreshWaterFish = true;
	 }
 };


 int main()
 {
	 Carp myLunch;
	 Tuna myDinner;

	 cout << "Lunch: ";
	 myLunch.Swim();
	 
	 //Calling Swim in a Derived Class
     cout << "Dinner: ";
	 myDinner.Swim();

	 //Or do you want to call Swim in the base class?
	 cout << "Dinner: ";
	 myDinner.Fish::Swim();

     system("pause");

	 //myLunch.FreshWaterFish = false;
	 return 0;
 }



It is also possible to call functions of base classes repeatedly in derived classes:

Structural and destructive sequence

Base class objects are instantiated before derived class objects. First instantiate the member attributes, then call the constructor to ensure that the member attributes are ready for use by the constructor.

However, the order of destructions is opposite to that of constructions. First, the derived class object is destructed and then the base class object is destructed.

Private Inheritance

             

To open a function (SS) in another derived class, the base class function you want to call is:

#include <iostream>
 using namespace std;

 class Fish
 {
 protected: //Changes
	 bool FreshWaterFish;
 public:
	 void Swim()
	 {
		 if (FreshWaterFish)
			 cout << "Swims in lake" << endl;
		 else
			 cout << "Swims in sea" << endl;
	 }
 };

//Tuna inherited Fish
 class Tuna :private Fish
 {
 public:
	 void SS()  //Here we have to construct another function, put the function of the base class in it, and then call the function, but can not show the function of the base class.
	 {
	    Swim();
	 }

 };

 int main()
 {
	 Tuna myDinner;

	 cout << "Lunch: ";
	 myDinner.SS();
	 


     system("pause");

	 //myLunch.FreshWaterFish = false;
	 return 0;
 }



The normal inheritance relationship is: base class - > derived class - > derived class instantiation

When public calls: Derived class instantiation can call base class member function, also can call member function of derived class;

When pravate calls: Derived class instantiation can call functions of derived class, but can not call member functions of base class; and derived class functions can call functions of base class.

Protection of succession

Even the derived class generated by the base class can have the same permissions as the first generation of the derived class, but can not directly call the member function of the base class in the instantiated function of the derived class, but private is not possible, only the first generation has the right to access the member function of the base class.

PS: Use private or protect inheritance only when necessary.

Excision problem

Do not pass parameters by value, yo, by pointer or reference, or by pointer to base class and reference to const.

Multiple Inheritance

Grammar:

 

Examples of has-a and is-a relations

There are many kinds of sheep in our common animals. They are just a general term. Sheep is one of them. So the relationship between sheep and sheep is-a, that is, they are inheritance relations.

Moreover, sheep have wool on their bodies, and different kinds of sheep have different biological forms of wool, such as sheep and antelope. The former is rich, fluffy, soft and white, while the latter is rare, rigid and grayish brown. For sheep, the relationship between wool and sheep is has-a. The same is true for antelopes. We can distinguish the breeds of sheep according to the difference of wool.

Express them in natural language:

is-a: This kind of thing (sheep) is a kind of thing (sheep).

has-a: This kind of thing (wool) belongs to that kind of thing (sheep) and is a part of it.