首页 > 编程语言 >打卡 上课铃响之后 - C/C++ 多态

打卡 上课铃响之后 - C/C++ 多态

时间:2023-04-19 23:12:32浏览次数:127  
标签:cout Teacher 多态 C++ public Person Student 打卡 bellRing

如本章开篇所述,当小学里的上课铃响之后,学生(Student)、教师(Teacher)和校长(Principal)会对同一个消息表现出不同的行为。请设计Person、Student、Teacher以及Principal类,合理安排他们之间的继承关系并将所有类的bellRing()及析构函数设计为虚函数,使得下述代码可以正常执行并产生期望的执行结果。

裁判测试程序样例:

#include <iostream>
using namespace std;

//定义Person, Student, Teacher, Principal类

int main() {
    cout << "School bell rings..." << endl;
    Person* persons[3] = {new Student(),new Teacher(),new Principal()};
    persons[0]->bellRing();
    persons[1]->bellRing();
    persons[2]->bellRing();
    for (auto i=0;i<3;i++)
        delete persons[i];
    return 0;
}

解题思路:虚函数重载,虚析构函数重载

class Person{
public:
virtual void bellRing(){}
virtual ~Person(){}
};
class Student:public Person{
public:
void bellRing(){
cout<<"I am a student learning in classroom."<<endl;
}
~Student(){
cout<<"A student object destroyed."<<endl;
}
};
class Teacher:public Person{
public:
void bellRing(){
cout<<"I am a teacher teaching in classroom."<<endl;
}
~Teacher(){
cout<<"A teacher object destroyed."<<endl;
}
};
class Principal:public Person{
public:
void bellRing(){
cout<<"I am the principal inspecting in campus."<<endl;
}
~Principal(){
cout<<"A principal object destroyed."<<endl;
}
};

标签:cout,Teacher,多态,C++,public,Person,Student,打卡,bellRing
From: https://www.cnblogs.com/qmz-znv2/p/17335012.html

相关文章

  • leetcode_打卡08
    leetcode_打卡08题目:334.递增的三元子序列思路:分成左边L和右边R,只要找到该数左边比它小的,右边比他大的即可代码:classSolution{publicbooleanincreasingTriplet(int[]nums){intn=nums.length;int[]L=newint[n];int[]R=newint[n];......
  • 打卡3
    4.#include<iostream>usingnamespacestd;classCRectangle{ private: doubleh,w; public:  CRectangle(doubleh=1,doublew=1):h(h),w(w)  {     }  voidinput()  {   cin>>h>>w;   if(h<0||h>50)   h=1.0;   if(w&......
  • c打卡补2
    1.classCircle{private:   doubler;public:   Circle(doubler)   {cout<<"Constructorcalled"<<endl;       this->r=r;   }   Circle(Circle&c)   {       this->r=c.r;       cout<<"Copy......
  • 4/19打卡
    classPeople{protected:intage;stringname;public:People(){}People(inta,stringn):age(a),name(n){}virtual~People(){}voidsetValue(intm,stringstr){age=m;name=str;}virtualvoiddi......
  • 第四天打卡
    #include<iostream>usingnamespacestd;intmain(){inta,c,x,i;for(a=0;a<=9;a++)for(c=0;c<=9;c++){if(a!=c){//cout<<a<<c<<endl;x=1000*a+100*a+10*c+c;//......
  • 每日打卡-8.2
    一.问题描述输入一个长度为n的整数序列。接下来再输入m个询问,每个询问输入一对l,r。对于每个询问,输出原序列中从第l个数到第r个数的和。二.设计思路 直接套用模板即可三.流程图四.伪代码 1五.代码实现 1#include<iostream>usingnamespacestd;constintN=1000......
  • 每日打卡-8.1
    一.问题描述  输入一个n行m列的整数矩阵,再输入q个询问,每个询问包含四个整数x1,y2,x1,y2,表示一个子矩阵的左上角坐标和右下角坐标。  对于每个询问输出子矩阵中所有数的和。二.设计思路很典型的二维前缀模板题 先求前缀和,再利用公式ans=s[x2][y2]-s[x2][y1-1]-s[x......
  • 2022.4.19编程一小时打卡
    一、问题描述:设计一个类people,有保护数据成员:age(年龄,整型),name(姓名,string),行为成员:两个构造函数(一个默认,另一个有参数);默认析构函数;voidsetValue(intm,stringstr)给age和name赋值;有一个void类型的纯虚函数display()。设计一个学生类student,公有继承类people,有私有成员......
  • 打卡1
    问题描述:一只公鸡值五钱,一只母鸡值三钱,三只小鸡值一钱,现在要用百钱买百鸡,请问公鸡、母鸡、小鸡各多少只?流程图: 伪代码:cock<-0forcock<-0To20hen=0forhen<-0To33chicken=0forchicken<-0To100ifcock+hen+chicken=100&&cock*5+hen*3+chicken/3=100outputcock......
  • 打卡2
    问题描述:小明有5本新书,要借给A、B、C三位小朋友,若每人每次只能接1本,则可以有多少种不同的借法?流程图: 伪代码:fora<-1to5forb<-1to5forc<-1to5ifa=b=cthencontinueelseoutputa,b,c代码:#include<iostream>usingnamespacestd;intmain(){inta,b,c;f......