Objective: 1. Learn to define and use the inheritance relationship of a class and define a derived class.
2. Familiar with access control for base class members in different inheritance modes.
3. Learn to use the virtual base class to solve the problem of ambiguity.
Content (algorithms, programs, steps, and methods ):
1. define a base class animal with a private integer member variable Age to construct its derived class dog. assign a value to age directly in its member function setage (int n) to see what problems may occur, change to a public member variable to observe the changes.
Algorithm: Define the base class and derived class as required. To reflect changes, define the function printage () in the derived class to output values.
Program:
# Include <iostream. h>
Class animal
{
PRIVATE:
Int age;
};
Class dog: Public animal
{
Public:
Void setage (int n) {age = N ;}
Void printage () {cout <"age =" <age <Endl ;}
};
Void main ()
{
Dog dog1;
Dog1.setage (5 );
Dog1.printage ();
}
Define a base class baseclass with an integer member variable number, construct its derived class derivedclass, and observe the execution of constructor and destructor.
Algorithm: first define the base class and derived class as described above. In order to display the execution of constructor and destructor, the prompt statements can be output in these two functions.
Program:
# Include <iostream. h>
Class baseclass
{
PRIVATE:
Int number;
Public:
Baseclass () {cout <"calls the base class constructor! "<Endl ;}
~ Baseclass () {cout <"calls the basic class destructor! "<Endl ;}
};
Class derivedclass: Public baseclass
{
Public:
Derivedclass () {cout <"calls the subclass constructor! "<Endl ;}
~ Derivedclass () {cout <"calls the subclass destructor! "<Endl ;}
};
Void main ()
{
Derivedclass son;
}
3. Define a base class, including member variables, and other member functions to derive the member variables such as the height of the bicycle class and automobile class, and the member variables such as the number of seats of the automobile class, A motorcycle class is derived from a bicycle class and an automobile class. During the inheritance process, we observe the difference between setting a car class as a virtual base class and setting a motorcycle class.
Algorithm: Define the corresponding class according to the topic description. To display changes, a function can be defined in the motorcycle class for output.
Program:
# Include <iostream. h>
Class Vehicle
{
Protected:
Int maxspeed;
Int weight;
Public:
Vehicle (int s, int W) {maxspeed = s; Weight = W ;}
Void setmaxspeed (INT s) {maxspeed = s ;}
Void setweight (int w) {Weight = W ;}
Void run () {cout <"class vehicle is running! "<Endl ;}
Void stop () {cout <"class vehicle has stopped! "<Endl ;}
};
Class bicycle: virtual public vehicle
{
Protected:
Int height;
Public:
Bicycle (int h, int S, int W): Vehicle (S, W)
{Height = H ;}
};
Class motorcar: virtual public vehicle
{
Protected:
Int seatnum;
Public:
Motorcar (int s, int S, int W): Vehicle (S, W)
{Seatnum = s ;}
};
Class motorcycle: Public bicycle, public motorcar
{
Public:
Motorcycle (ints, int W, int H, int S): Vehicle (S, W), bicycle (H, S, W), motorcar (S, S, W ){}
Void show ()
{
Cout <"maxspeed:" <maxspeed <Endl;
Cout <"Weight:" <weight <Endl;
Cout <"height:" Cout <"seatnum:" <seatnum <Endl;
}
};
Void main ()
{
Motorcycle MC (150,200 );
MC. Show ();
}
Results and Analysis:
1. The problem is as follows:
Error c2248: 'age': cannot access private member declared in class 'animal'
Analysis: The subclass cannot directly access the Private Members in the parent class. If you change the age in the base class to a public member, the execution result is as follows:
2. Execution result:
Analysis: The program first executes the constructor of the base class and calls the constructor of the subclass. The opposite is called when the constructor of the subclass is called, then, call the destructor of the base class.
3. Execution result:
If the car base class is not set as a virtual base class, the program will not separately find out whether the members inherited from the motorcycle class inherit from the bicycle class or the automobile class, so there will be ambiguity.