[C++ Experiment] Polymorphism and Virtual Functions (Selection of Outstanding Candidates)

Polymorphism and virtual functions

  • The concept of polymorphism refers to what happens when
    the same object is received by different objects . 消息不同行为
    • 消息means right 类的成员函数的调用;
    • 不同的行为refers to different implementations, ie 调用不同的函数.
  • Assignment compatibility rules
    • Objects of the base class and its public derived classes can be processed uniformly using the same function, without the need to design separate functional modules for each class.
    • When the formal parameter of a function is a base class object, the actual parameter can be an object of a derived class.
    • When the formal parameter of a function is a pointer to a base class object, the actual parameter can be the address of the derived class object.
    • When the formal parameter of a function is a reference to a base class object, the actual parameter can be an object of the derived class.
  • How to use virtual functions
    (1) Use virtual to declare member functions as virtual functions in the base class. Redefine the function of the same name in the derived class to give it new functionality.
    (2) When redefining this function in a derived class, the function name, function type, number and type of parameters are required to be the same as the virtual function of the base class, and the function body is redefined as needed. C++ stipulates that when a member function is declared as virtual function, the function of the same name in its derived class automatically becomes a virtual function.
    (3) Define a pointer variable pointing to the base class object and let it obtain the address of an object in the same class family.
    (4) Use the pointer variable to call a virtual function, and what is called is the virtual function of the class to which the object belongs.

Experiment content

Design procedures for selecting outstanding teacher and outstanding student candidates. If a student's score is greater than 90, he can be rated as an outstanding student; if
the number of papers published by a teacher is greater than 3, he can be rated as an outstanding teacher. The specific requirements are as follows:
(1) Define the base class Base: ① Protected data member
char name[8]; //Storage name
int num; //Storage score or number of papers
②Public member function
Base(): Constructor, enter name;
void print): functional function, output data members;
virtual int Isgood()=0: pure virtual function, used to determine whether conditions are met;
(2) Derive the student class Student from the base class and define public member functions:
Student(): Constructor, input scores;
int Isgood(): According to the standards of excellent students, return 1 if the conditions are met, otherwise return 0;
(3) Derive the teacher class Teacher from the base class, and define public member functions:
Teacher(): Constructor, input Number of papers;
int Isgood(): According to the standards of excellent teachers, if the conditions are met, 1 is returned, otherwise 0 is returned;
(4) Define the student array and teacher array in the main function, and after inputting a series of teacher or student records, the outstanding teacher and student candidates
are listed and reflect the polymorphism of operations

Code display

#include <iostream>
using namespace std;
class Base
{
    
    
	protected:
		char name[8];  //存放姓名 
		int num;  //存放分数或论文数
	public:
		Base();  //构造函数,输入姓名
		void print();  //功能函数,输出数据成员 
		virtual int Isgood()=0;  //纯虚函数,满足条件返回1,否则返回0 
};
Base::Base()
{
    
    
	cout<<"姓名:";
	cin>>name; 
}
void Base::print()
{
    
    
	cout<<"姓名:"<<name<<'\t'<<num<<'\n';	
}

class Student:public Base
{
    
    
	public:
		Student();  //构造函数,输入分数 
		int Isgood();  //根据优秀学生的标准,满足条件返回1,否则返回0 
		
};
Student::Student()
{
    
    
	cout<<"考试成绩:";
	cin>>num;
}
int Student::Isgood()
{
    
    
	return (num>90)?1:0;	
}

class Teacher:public Base
{
    
    
	public:
		Teacher();  //构造函数,输入论文数 
		int Isgood();	//根据优秀教师的标准,满足条件返回1,否则发怒hi0 
};
Teacher::Teacher()
{
    
    
	cout<<"论文数:";
	cin>>num;
}
int Teacher::Isgood()
{
    
    
	return (num>3)?1:0;
}


int main()
{
    
    
	cout<<"请输入学生信息:\n";
	Student stu[5];
	cout<<"\n请输入教师信息:\n"; 
	Teacher	tea[5];
	cout<<"\n优秀学生候选人:\n";
	int i;
	Base *p;  
	for(i=0,p=stu;i<5;i++,p++)
		if(p->Isgood()) 
		p->print();
	cout<<"\n优秀教师候选人:\n";
	for(i=0,p=tea;i<5;i++,p++)
		if(p->Isgood())
		p->print();
} 

Output results

Insert image description here

Guess you like

Origin blog.csdn.net/Taylor_Kurt/article/details/128270923