首页 > 其他分享 >上课铃响以后 //多态性

上课铃响以后 //多态性

时间:2023-04-20 23:14:30浏览次数:51  
标签:上课 铃响 persons 多态性 Teacher Person Student bellRing Principal

如本章开篇所述,当小学里的上课铃响之后,学生(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;
}
 

输入样例:

 

输出样例:

School bell rings...
I am a student learning in classroom.
I am a teacher teaching in classroom.
I am the principal inspecting in campus.
A student object destroyed.
A teacher object destroyed.
A principal object destroyed.
 1 #include <iostream>
 2 using namespace std;
 3 
 4 //定义Person, Student, Teacher, Principal类
 5 
 6 int main() {
 7     cout << "School bell rings..." << endl;
 8     Person* persons[3] = {new Student(),new Teacher(),new Principal()};
 9     persons[0]->bellRing();
10     persons[1]->bellRing();
11     persons[2]->bellRing();
12     for (auto i=0;i<3;i++)
13         delete persons[i];
14     return 0;
15 }
16  

 

 
 1 class Person {
 2     public:
 3     virtual void bellRing()=0;
 4     virtual ~Person(){}
 5 };
 6 class Student:public Person{
 7     public:
 8     virtual void bellRing()
 9     {
10         cout<<"I am a student learning in classroom."<<endl;
11     }
12     virtual ~Student(){cout<<"A student object destroyed."<<endl;}
13 };
14 class Teacher:public Person{
15     public:
16     virtual void bellRing()
17     {
18         cout<<"I am a teacher teaching in classroom."<<endl;
19     }
20     virtual ~Teacher(){cout<<"A teacher object destroyed."<<endl;}
21 };
22 class Principal:public Person{
23     public:
24     virtual void bellRing()
25     {
26         cout<<"I am the principal inspecting in campus."<<endl;
27     }
28     virtual ~Principal(){cout<<"A principal object destroyed."<<endl;}
29 };

 

标签:上课,铃响,persons,多态性,Teacher,Person,Student,bellRing,Principal
From: https://www.cnblogs.com/liubingyu/p/17338682.html

相关文章

  • 多态性8
    #include<iostream>#include<typeinfo>usingnamespacestd;classBase{ public: virtual~Base(){}};classDerived:publicBase{};voidfun(Base*b){ consttype_info&info1=typeid(b); consttype_info&info2=typeid(*b); cout<<"type......
  • 多态性7
    #include<iostream>usingnamespacestd;classBase{ public: virtualvoidfun1(){ cout<<"Base::fun1()"<<endl; } virtual~Base(){}};classDerived1:publicBase{ public: virtualvoidfun1(){ cout<<"Derived1::f......
  • 打卡 上课铃响之后 - C/C++ 多态
    如本章开篇所述,当小学里的上课铃响之后,学生(Student)、教师(Teacher)和校长(Principal)会对同一个消息表现出不同的行为。请设计Person、Student、Teacher以及Principal类,合理安排他们之间的继承关系并将所有类的bellRing()及析构函数设计为虚函数,使得下述代码可以正常执行并产生期望的......
  • 多态性4
    #include<iostream>usingnamespacestd;classBase1{ public: virtualvoiddisplay()const;};voidBase1::display()const{ cout<<"Base1::display()"<<endl;}classBase2:publicBase1{ public: voiddisplay()const;};voidBase2::displ......
  • 多态性3
    #include<iostream>usingnamespacestd;classComplex{ public: Complex(doubler=0.0,doublei=0.0):real(r),imag(i){} friendComplexoperator+(constComplex&c1,constComplex&c2); friendComplexoperator-(constComplex&c1,constComplex......
  • 多态性1
    #include<iostream>usingnamespacestd;classComplex{ public: Complex(doubler=0.0,doublei=0.0):real(r),imag(i){} Complexoperator+(constComplex&c2)const; Complexoperator-(constComplex&c2)const; voiddisplay()const; private: ......
  • 最新版人脸识别小程序 图片识别 生成码签到码 地图上选点进行位置签到 计算签到距离
    技术选型1,前端小程序原生MINA框架cssJavaScriptWxml2,管理后台云开发Cms内容管理系统web网页3,数据后台小程序云开发云函数云开发数据库(基于MongoDB)云存储4,人脸识别算法基于百度智能云实现人脸识别一,用户端效果图预览老规矩我们先来看效果图,如果效果图符合你的需求,就继续往下......
  • iOS7应用开发6:UINavigation, UITabbar控制器的多态性
    1、前期所实现的PlayingCard游戏,其ViewController只能适应PlayingCard这一种游戏规则。而将createDeck函数修改为返回一个nil后,整个ViewController与PlayingCard就没有关......
  • C#多态性学习,虚方法、抽象方法、接口等用法举例
    1.多态性定义  C#中的多态性是OOP(面向对象编程)的一个基本概念,它允许一个对象在不同情况下表现出不同的行为,以增强代码的可重用性和灵活性。  根据网上的教程,我们得......
  • 工程:上课时间调整模块修改
    上课时间调整模块修改1(2023-02-27)序号修改任务完成情况1分页功能(当某页只剩一行数据时,点击删除时应该把总页数减一)√2按照调整前的上课时间排序(默认降序......