家教课程

Description

老师都是穷人,所以需要经常去外面搞点兼职啥的。可是除了上课,啥也不会啊。所以就只好做家教了。现在请利用面向对象的思想设计这样一个系统。具有如下类:

  1. Person类:有一个string类型的属性,表明对象的名字。是Student和Teacher的父类。

  2. Student类:是Person类的子类,拥有一个int类型的属性,表明对象的序号。

  3. Teacher类:是Person类的子类,拥有一个string类型的属性,表明对象的职称。

  4. Course类:是一个组合类,有1个Teacher类的对象、1个Student类型的对象,以及一个string类型的属性(表明对象的名称)组成。

请定义上述类的构造函数和析构函数,并在函数中输出相应的字符串。具体格式请参照样例输出。
Input

输入5行,前4个是4个字符串,分别是老师的名字、学生的名字、老师的职称、课程的名字。最后一行是一个整数,表示学生的序号。
Output

见样例~
Sample Input
Tom
Jack
Prof
C++
10
Sample Output
Person Tom is created.
Person Jack is created.
Person Tom is created.
Teacher Tom with position Prof is created.
Person Jack is created.
Student Jack with id 10 is created.
Person Jack is created.
Teacher Jack with position Prof is created.
Person Jack is created.
Student Jack with id 10 is created.
Course C++ is created.
Course C++ is erased.
Student Jack with id 10 is erased.
Person Jack is erased.
Teacher Jack with position Prof is erased.
Person Jack is erased.
Student Jack with id 10 is erased.
Person Jack is erased.
Teacher Tom with position Prof is erased.
Person Tom is erased.
Person Jack is erased.
Person Tom is erased.
HINT

注意根据main函数分析各个构造函数的每个参数的含义。

Append Code
append.cc,

int main()
{
    string s1, s2, s3, s4;
    int i;
    cin>>s1>>s2>>s3>>s4>>i;
    Person person1(s1), person2(s2);
    Teacher teacher(s1, s3);
    Student student(s2, i);
    Course course(s1, s2, s3, s4, i);
    return 0;
}

AC代码一

#include <iostream>

using namespace std;
class Person
{
private:
    string name;
public:
    Person(string na=""):name(na){cout<<"Person "<<name<<" is created."<<endl;}
    string getname(){return name;}
    ~Person(){cout<<"Person "<<name<<" is erased."<<endl;}
};
class Teacher:public Person
{
private:
    string position;
public:
    Teacher(string s1="",string s3=""):Person(s1),position(s3){cout<<"Teacher "<<getname()<<" with position "<<position<<" is created."<<endl;}
    ~Teacher(){cout<<"Teacher "<<getname()<<" with position "<<position<<" is erased."<<endl;}
};
class Student:public Person
{
private:
    int id;
public:
    Student(string s2="",int i=0):Person(s2),id(i){cout<<"Student "<<getname()<<" with id "<<id<<" is created."<<endl;}
    ~Student(){cout<<"Student "<<getname()<<" with id "<<id<<" is erased."<<endl;}
};
class  Course
{
private:
    Teacher te;
    Student st;
    string cours;
public:
    Course(string s1,string s2,string s3,string s4,int i):te(s2,s3),st(s2,i),cours(s4)//这里非Teacher(s2,s3),Student(s2,i)因为没有用到继承;
    {cout<<"Course "<<cours<<" is created."<<endl;}
    ~Course(){cout<<"Course "<<cours<<" is erased."<<endl;}
};
int main()
{
    string s1, s2, s3, s4;
    int i;
    cin>>s1>>s2>>s3>>s4>>i;
    Person person1(s1), person2(s2);
    Teacher teacher(s1, s3);
    Student student(s2, i);
    Course course(s1, s2, s3, s4, i);
    return 0;
}

上面的这个代码其实不需要用到virtual,一般什么时候用virtual呢?一般在main函数中有指向父类指针的时候,删除父类的时候为了同时调用子类的析构函数或者为了避免内存泄漏的时候最好用virtual;

1、对于虚拟析构函数的用法
(一)虚析构函数的作用
总的来说虚析构函数是为了避免内存泄露,而且是当子类中会有指针成员变量时才会使用得到的。也就说虚析构函数使得在删除指向子类对象的基类指针时可以调用子类的析构函数达到释放子类中堆内存的目的,而防止内存泄露的.
(二)总结虚析构函数的作用:
(1)如果父类的析构函数不加virtual关键字
当父类的析构函数不声明成虚析构函数的时候,当子类继承父类,父类的指针指向子类时,delete掉父类的指针,只调动父类的析构函数,而不调动子类的析构函数。
(2)如果父类的析构函数加virtual关键字
当父类的析构函数声明成虚析构函数的时候,当子类继承父类,父类的指针指向子类时,delete掉父类的指针,先调动子类的析构函数,再调动父类的析构函数。


AC代码二

#include <iostream>

using namespace std;
class Person
{
private:
    string name;
public:
    Person(string na=""):name(na){cout<<"Person "<<name<<" is created."<<endl;}
    string getname(){return name;}
    ~Person(){cout<<"Person "<<name<<" is erased."<<endl;}
};
class Teacher:public Person
{
private:
    string position;
public:
    Teacher(string s1="",string s3=""):Person(s1),position(s3){cout<<"Teacher "<<getname()<<" with position "<<position<<" is created."<<endl;}
    ~Teacher(){cout<<"Teacher "<<getname()<<" with position "<<position<<" is erased."<<endl;}
};
class Student:public Person
{
private:
    int id;
public:
    Student(string s2="",int i=0):Person(s2),id(i){cout<<"Student "<<getname()<<" with id "<<id<<" is created."<<endl;}
    ~Student(){cout<<"Student "<<getname()<<" with id "<<id<<" is erased."<<endl;}
};
class  Course:public Teacher,public Student
{
private:
    string cours;
public:
    Course(string s1,string s2,string s3,string s4,int i):Teacher(s2,s3),Student(s2,i),cours(s4){cout<<"Course "<<cours<<" is created."<<endl;}
    ~Course(){cout<<"Course "<<cours<<" is erased."<<endl;}
};
int main()
{
    string s1, s2, s3, s4;
    int i;
    cin>>s1>>s2>>s3>>s4>>i;
    Person person1(s1), person2(s2);
    Teacher teacher(s1, s3);
    Student student(s2, i);
    Course course(s1, s2, s3, s4, i);
    return 0;
}

不建议采用这种方法,因为题目中说是Course类:是一个组合类,组合类而不是是......的子类,因此最好不用继承的方式,尽管得出来的结果是一样的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值