Object oriented programming C + +: inheritance, derivation and polymorphism

Experiment content: using inheritance and derivation to realize function

(1) Three derived classes, classroom, office and laboratory, are derived from the "room" base class. The requirements are as follows:
"Classroom" category includes: building number, room number, number of seats and person in charge (one executive);
"Office" category includes: building number, room number, number of personnel, administrative personnel 1, administrative personnel 2;
"Laboratory" category includes: building number, room number, number of teachers, number of students, teacher 1, teacher 2,..., student 1, student 2;

(2) The three derived classes of "student", "teacher" and "administration" are derived from the base class of "personnel", and the requirements are as follows:
"Student" category includes: name, gender, college, age, year of enrollment and major;
"Teacher" category includes: name, gender, college, age, professional title and research direction;
"Administration" category includes: name, gender, college, age and responsible work;

Define the corresponding base class and derived class according to the above requirements description, define the member functions such as construction, deconstruction and display information according to the requirements, write the main program verification function, define the following objects and output relevant information on the console:
1 classroom;
1 Office (including at least 2 administrative staff);
1 Laboratory (including at least 2 teachers and 3 students);

1, Experimental code

#include<iostream>
#include<string>
#include<iomanip> 	// Contains setw() output field width function 
using namespace std;
/***************************************************************/
//Define "people" base class 
class Person
{
public:
	string name;	   //Define name 
	string sex;		   //Define gender 
	string college;	   //Definition College 
	int age;		   //Define age 
	Person(string,string,string,int);//Constructor 
	Person();		  //default constructor  
	~Person();		  //Destructor 
	virtual void OutputPerson();	//Virtual function declaration output personnel information
};

Person::Person(string nametemp,string sextemp,string collegetemp,int agetemp)
{
	name=nametemp;sex=sextemp;college=collegetemp;age=agetemp;
}
Person::Person(){}

Person::~Person(){}
//Output personnel information: attributes include name, gender, college and age 
void Person::OutputPerson()
{
	//Adjust the output layout through the field width setw() 
	cout<<setw(10)<<"full name"<<setw(18)<<"Gender"<<setw(18)<<"college"<<setw(18)<<"Age"<<endl;
	cout<<setw(10)<<name<<setw(18)<<sex<<setw(18)<<college<<setw(18)<<age<<endl;
}
/***************************************************************/
//Defines the derived student class
class Student : public Person
{
public:
	string year;	//Define year of enrollment 
	string major; 	//Define specialty 
	Student(string name,string sex,string college,int age,string year,string major);//Constructor
	Student();		//default constructor 
	~Student();		//Destructor 
	void OutputPerson();//Output student information 
};
//Constructor: inherit the "personnel" class for name, gender, college and age attributes 
Student::Student(string name,string sex,string college,int age,string yeartemp,string majortemp) :Person(name,sex,college,age)
{
	year=yeartemp;
	major=majortemp;
}
Student::Student(){}
Student::~ Student(){}
//Print student status function
void Student::OutputPerson()
{
	cout<<setw(10)<<"full name"<<setw(18)<<"Gender"<<setw(18)<<"college"<<setw(18)<<"Age"<<setw(18)<<"Enrollment Year "<< setw(18) << "major"<<endl; 
	cout<<setw(10)<<name<<setw(18)<<sex<<setw(18)<<college<<setw(18)<<age<<setw(18)<<year << setw(18) << major << endl;
} 
/***************************************************************/
//Defines the derived teacher class
class Teacher : public Person
{
	string title;		//Define title 
	string direction; 	//Define research direction 
public:
	Teacher(string name,string sex,string college,int age,string title,string direction);//Constructor
	Teacher();			//default constructor  
	~Teacher();			//Destructor  
	void OutputPerson();//Output teacher information 
};
//Constructor: inherit "personnel" class for name, gender, college and age attributes
Teacher::Teacher(string name,string sex,string college,int age,string titletemp,string directiontemp) :Person(name,sex,college,age)
{
	title=titletemp;direction=directiontemp;
}
Teacher::Teacher(){}
Teacher::~Teacher(){}
//Print teacher status function
void Teacher::OutputPerson()
{

	cout<<setw(10)<<"full name"<<setw(18)<<"Gender"<<setw(18)<<"college"<<setw(18)<<"Age"<<setw(18)<<"title"<< setw(18) << "Research direction"<<endl; 
	cout<<setw(10)<<name<<setw(18)<<sex<<setw(18)<<college<<setw(18)<<age<<setw(18)<<title << setw(18) << direction << endl;
}
/*************************************************************/
//Define derived administrative classes
class Admstaff : public Person
{
	string work; 	//Define responsible work 
public:
	Admstaff(string name,string sex,string college,int age,string work);//Constructor 
	Admstaff();		//default constructor  
	~Admstaff();	//Destructor 

	void OutputPerson();
};
//Constructor: inherit the "personnel" class for name, gender, college and age attributes
Admstaff::Admstaff(string name,string sex,string college,int age,string worktemp) :Person(name,sex,college,age)
{
	work=worktemp;
}
Admstaff::Admstaff(){}
Admstaff::~Admstaff(){}
//Print executive status function
void Admstaff::OutputPerson()
{
	cout<<setw(10)<<"full name"<<setw(18)<<"Gender"<<setw(18)<<"college"<<setw(18)<<"Age"<<setw(18)<<"Responsible for work"<<endl; 
	cout<<setw(10)<<name<<setw(18)<<sex<<setw(18)<<college<<setw(18)<<age<<setw(18)<<work<<endl;
}


/****************************************************************/
//Define the room class 
class Room 
{
public:
	string build_number;	//Define building number 
	string room_number;		//Define room number 
	Room(string,string);	//Constructor 
	Room();		//default constructor  
	~Room();	//Destructor 
	virtual void OutputRoom();	//Declare virtual function output room information
};
//Constructor: the property contains building number and room number 
Room::Room(string build_numbertemp,string room_numbertemp)
{
	build_number=build_numbertemp;
	room_number=room_numbertemp;
}
Room::Room(){}
Room::~Room(){}
//Output room information 
void Room::OutputRoom()
{
	cout<<setw(10)<<"Building number"<<setw(18)<<"room number"<<endl;
	cout<<setw(10)<<build_number<<setw(18)<<room_number<<endl;
}
/***************************************************************/
//Defines the classroom class derived from the room class
class Classroom : public Room
{
public:
	int seat_num;		//Define the number of seats 
	Admstaff adm1;		//Define administrative personnel adm1 
	Classroom(string,string,int,Admstaff);//Constructor 
	Classroom();		//default constructor  
	~Classroom();		//Destructor 
	void OutputRoom();
};
//Constructor: the building number and room number attributes inherit the "room" class 
Classroom::Classroom(string build_number,string room_number,int seat_numtemp,Admstaff adm1temp) :Room(build_number,room_number)
{
	seat_num=seat_numtemp;
	adm1=adm1temp;
}
Classroom::~ Classroom(){}
//Print classroom situation function
void Classroom::OutputRoom()
{
	cout<<"Classroom information:"<<endl; 
	cout<<setw(10)<<"Building number"<<setw(18)<<"room number"<<setw(18)<<"Number of seats"<<endl;
	cout<<setw(10)<<build_number<<setw(18)<<room_number<<setw(18)<<seat_num<<endl;
	cout<<setw(10)<<"person in charge:"<<endl;
	adm1.OutputPerson();
	cout<<endl;
} 
/**************************************************************/
//Defines the office class derived from the room class
class Office : public Room
{
public:
	int adm_num;		//Define the number of administrative personnel 
	Admstaff adm1,adm2;	//Define administrative personnel adm1 and adm2 
	Office(string,string,int,Admstaff,Admstaff);//Constructor 
	Office();			//default constructor  
	~Office();			//Destructor 
	void OutputRoom();	//Output teacher information 
};
//Constructor: the building number and room number attributes inherit the "room" class
Office::Office(string build_number,string room_number,int adm_numtemp,Admstaff adm1temp,Admstaff adm2temp) :Room(build_number,room_number)
{
	adm_num=adm_numtemp;
	adm1=adm1temp;
	adm2=adm2temp;
}
Office::~ Office(){}
//Print office situation function
void Office::OutputRoom()
{
	cout<<"Office information:"<<endl; 
	cout<<setw(10)<<"Building number"<<setw(18)<<"room number"<<setw(18)<<"Number of personnel"<<endl;
	cout<<setw(10)<<build_number<<setw(18)<<room_number<<setw(18)<<adm_num<<endl;
	cout<<setw(10)<<"Administrative staff arrangement:"<<endl;
	adm1.OutputPerson();
	adm2.OutputPerson();
	cout<<endl;
} 
/****************************************************************/
//Defines the lab class derived from the room class
class Laboratory : public Room
{
public:
	int stu_num;			//Define number of students 
	int tea_num; 			//Define the number of Teachers 
	Student stu1,stu2,stu3;	//Define staff stu1,stu2,stu3 of student class
	Teacher tea1,tea2;		//Define the staff Tea1 and Tea2 of the teacher class
	Laboratory(string,string,int,int,Teacher,Teacher,Student,Student,Student);//Constructor 
	Laboratory();			//default constructor  
	~Laboratory();			//Destructor 
	void OutputRoom();		//Output laboratory information 
};
//Constructor: the building number and room number attributes inherit the "room" class
Laboratory::Laboratory(string build_number,string room_number,int stu_numtemp,int tea_numtemp,Teacher tea1temp,Teacher tea2temp,Student stu1temp,Student stu2temp,Student stu3temp) :Room(build_number,room_number)
{
	stu_num=stu_numtemp;
	tea_num=tea_numtemp;
	tea1=tea1temp;
	tea2=tea2temp;
	stu1=stu1temp;
	stu2=stu2temp;
	stu3=stu3temp;
}
Laboratory::~ Laboratory(){}
//Print lab situation function
void Laboratory::OutputRoom()
{
	cout<<"Laboratory information:"<<endl; 
	cout<<setw(10)<<"Building number"<<setw(18)<<"room number"<<setw(18)<<"Number of Teachers"<<setw(18)<<"Number of students"<<endl;
	cout<<setw(10)<<build_number<<setw(18)<<room_number<<setw(18)<<tea_num<<setw(18)<<stu_num<<endl;
	cout<<setw(10)<<"Teacher arrangement:"<<endl;
	tea1.OutputPerson();
	tea2.OutputPerson();
	cout<<setw(10)<<"Student arrangement:"<<endl;
	stu1.OutputPerson();
	stu2.OutputPerson();
	stu3.OutputPerson();
} 

//Main function 
int main()
{
//	Person per("Liu Yifei 1",woman, "Communication College 1", 18);
//	p.OutputPerson();
//For the convenience of debugging, keyboard input is not adopted here, and relevant personnel information and room information are directly defined 
	Student stu1("Liu Xiaofei","female","School of communication",18,"2018","Performance department");
	Student stu2("Chen Xiaohuang","male","School of Electronics",20,"2018","department of electronic studies");
	Student stu3("Zhong Xiaohong","female","College of mechanical and electrical engineering",20,"2018","automation");
	Teacher tea1("Luo Daguang","male","School of communication",30,"Teaching director","Performance class");
	Teacher tea2("Cai Daqing","female","School of Physics",28,"instructor","Law");
	Admstaff adm1("Zhang Zhongzheng","male","law school",28,"Department coordination");
	Admstaff adm2("Yu Zhongying","female","law school",26,"file management");
//	Room r("Zhili building", "505");
//	r.OutputRoom();
	Classroom cla1("Academic Building","101",50,adm1);
	Office off1("Zhiwenlou","303",2,adm1,adm2);
	Laboratory lab1("Zhili building","505",3,2,tea1,tea2,stu1,stu2,stu3);

	Room* r1=&cla1;		//The pointer type is determined by the reference type after the equal sign 
	r1 -> OutputRoom();	   //The output subclass object is determined only when the runtime points to the base class 
	cout<<"--------------------------------------------------------------------------"<<endl;
	Room* r2=&off1;
	r2 -> OutputRoom();
	cout<<"--------------------------------------------------------------------------"<<endl;
	Room* r3=&lab1;
	r3 -> OutputRoom();
	return 0;
}

2, Experimental results

1. Test case:

Student stu1("Liu Xiaofei","female","School of communication",18,"2018","Performance department");
Student stu2("Chen Xiaohuang","male","School of Electronics",20,"2018","department of electronic studies");
Student stu3("Zhong Xiaohong","female","College of mechanical and electrical engineering",20,"2018","automation");
Teacher tea1("Luo Daguang","male","School of communication",30,"Teaching director","Performance class");
Teacher tea2("Cai Daqing","female","School of Physics",28,"instructor","Law");
Admstaff adm1("Zhang Zhongzheng","male","law school",28,"Departmental coordination");
Admstaff adm2("Yu Zhongying","female","law school",26,"file management");

Classroom cla1("Academic Building","101",50,adm1);
Office off1("Zhiwenlou","303",2,adm1,adm2);
Laboratory lab1("Zhili building","505",3,2,tea1,tea2,stu1,stu2,stu3);

2. Output results

3. Experimental analysis

① The data member in the room base class contains the building number build_number and room_number
public:
string build_number;
string room_number;
② The data members in the personnel base class include name, sex sex, college, and age
public:
string name;
string sex;
string college;
int age;

summary

  1. Analyze the inheritance and aggregation relationship between classes in combination with requirements:
    ① Inheritance relationship refers to the ability of a class (called subclass and sub interface) to inherit the functions of another class (called parent class and parent interface) and add its own new functions. Therefore, inheritance and multiple inheritance are required when one class needs another class's methods or attributes. In this example, the three derived classes of classroom, office and laboratory need to use the building number and room number attribute information of room, and the three derived classes of student, teacher and administration need to use the name, gender, college Attribute information for age.
    ② Aggregation relationship is also a delegation relationship. A class contains a pointer to another class. You also have all the functions of the included class, which reflects the relationship between the whole and part. At this time, the whole and part can be separated, they can have their own life cycle, and the part can belong to or be shared by multiple whole objects. For example, "student" and "classroom" can exist independently, but "student" can also exist in "classroom", and the same is true for other derivatives.
  2. Assignment compatibility principle:
    ① Subclass objects can be used as parent objects
    ② Subclass objects can be assigned directly to parent objects
    ③ Subclass objects can directly initialize parent objects
    ④ A parent class pointer can point directly to a child class object
    ⑤ Parent class references can directly reference child class objects
  3. How to achieve runtime polymorphism:
    Runtime polymorphism is realized through virtual functions. The virtual function is used to tell the compiler not to determine the object pointed to by the pointer immediately and leave it to the runtime for decision. The virtual function declaration can only appear in the function prototype declaration in the class definition, not when the member function is implemented. Virtual functions are generally not declared as inline functions, because the call to virtual functions requires dynamic binding, while the processing of inline functions is static.
    //Define the room base class
    class Room
    {
    public:
    ...... ......
    virtual void OutputRoom(); // Virtual function declaration output room information
    };
    //Define the "people" base class
    class Person
    {
    public:
    ...... ......
    virtual void OutputPerson(); // Virtual function declaration output personnel information
    };
    //Classroom, office and laboratory during operation:
    Room* r1=&cla1; // The pointer type is determined by the reference type after the equal sign
    r1 -> OutputRoom(); // The output subclass object is determined only when the runtime points to the base class
    Room* r2=&off1;
    r2 -> OutputRoom();
    Room* r3=&lab1;
    r3 -> OutputRoom();

Keywords: C++

Added by chawezul on Fri, 21 Jan 2022 05:31:51 +0200