两次实验总结

上个周末无与伦比的忙,忘记了写实验总结,补上补上。
主要是a题://这是一个多态的题,但老师提前布置了,不过动动脑子不用多态也可以
定义Person类,
1.有一个int类型属性age和1个char类型属性sex,分别为年龄和姓名。
2.构造函数和析构函数,输出如样例所示的信息。
定义Student类,是Person类的子类:
1.有一个int类型属性,是学生所在的班级号。
2.构造函数与析构函数,输出如样例所示的信息。
定义Teacher类,是Person类的子类:
1.有一个int类型属性,表示老师的工资。
2.构造函数与析构函数,输出如样例所示的信息。

Input
输入有多行,每行是一个测试用例。每行的第1个是一个正整数,是年龄;第2个是一个字符,表示性别;第3个是一个字符t或s,表示老师或学生;第4个是一个正整数,表示班号(对于学生)或工资(对于老师)。

Output

见样例。

Sample Input
18 f s 1
35 m t 8001
Sample Output
Person age = 18, sex = f is created.
Student of class 1 is created.
Student of class 1 is erased.
Person age = 18, sex = f is erased.
Person age = 35, sex = m is created.
Teacher with salary 8001 is created.
Teacher with salary 8001 is erased.
Person age = 35, sex = m is erased.
提供的主函数

int main()
{
    Person *p;
    int age, par;
    char sex, t;
    while (cin>>age>>sex>>t>>par)
    {
        if (t == 's')
        {
            p = new Student(age, sex, par);
        }
        else
        {
            p = new Teacher(age, sex, par);
        }
        delete p;
    }
 
}

本次总结:
别忘了类的组合里的传引用,
每一个类都要给其虚基类传参数,如果不传参,设置无参构造函数应该也可以。

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
class Citrus
{
public:
    string name;
    double wei;
    Citrus(string a,double b):name(a),wei(b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is citrus fruit."<<endl;
    }
};
class Mandarin:virtual public Citrus
{
public:
    Mandarin(string a,double b):Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is mandarin."<<endl;
    }
};
class Pomelo:virtual public Citrus
{
public:
    Pomelo(string a,double b):Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is pomelo."<<endl;
    }
};
class Citron:virtual public Citrus
{
public:
    Citron(string a,double b):Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is citron."<<endl;
    }
};
class Orange:virtual public Mandarin,virtual public Pomelo
{
public:
    Orange(string a,double b):Mandarin(a,b),Pomelo(a,b),Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is orange."<<endl;
    }
};
class Lime:virtual public Citron,virtual public Pomelo
{
public:
    Lime(string a,double b):Citron(a,b),Citrus(a,b),Pomelo(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is lime."<<endl;
    }
};
class Tangerine:virtual public Orange ,virtual public Mandarin
{
public:
    Tangerine(string a,double b):Citrus(a,b),Orange(a,b),Mandarin(a,b),Pomelo(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is tangerine."<<endl;
    }
};
class Grapefruit:virtual public Orange,virtual public Pomelo
{
public:
    Grapefruit(string a,double b):Citrus(a,b),Orange(a,b),Pomelo(a,b),Mandarin(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is grapefruit."<<endl;
    }
};
class Lemon:virtual public Orange,virtual public Lime
{
public:
    Lemon(string a,double b):Mandarin(a,b),Citrus(a,b),Orange(a,b),Lime(a,b),Pomelo(a,b),Citron(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is lemon."<<endl;
    }
};
 
int main()
{
    Citrus     *c;
    Mandarin   *ma;
    Pomelo     *po;
    Citron     *ci;
    Orange     *og;
    Lime       *li;
    Tangerine  *ta;
    Grapefruit *gr;
    Lemon      *le;
 
    string name;
    double weight;
    while(cin >> name >> weight)
    {
        if(name == "Tangerine")
        {
            Tangerine tangerine(name, weight);
            c = ma = og = ta = &tangerine;
            c->show();
            ma->show();
            og->show();
            ta->show();
        }
        if(name == "Grapefruit")
        {
            Grapefruit grapefruit(name, weight);
            c = po = gr = &grapefruit;
            c->show();
            po->show();
            gr->show();
        }
        if(name == "Lemon")
        {
            Lemon lemon(name, weight);
            ma = og = &lemon;
            ci = li = &lemon;
            po = le = &lemon;
            ma->show();
            po->show();
            ci->show();
            og->show();
            li->show();
            le->show();
        }
    }
}

or

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
class Citrus
{
public:
    string name;
    double wei;
    Citrus(string a=" ",double b=0):name(a),wei(b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is citrus fruit."<<endl;
    }
};
class Mandarin:virtual public Citrus
{
public:
    //Mandarin(string a,double b):Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is mandarin."<<endl;
    }
};
class Pomelo:virtual public Citrus
{
public:
    //Pomelo(string a,double b):Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is pomelo."<<endl;
    }
};
class Citron:virtual public Citrus
{
public:
    //Citron(string a,double b):Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is citron."<<endl;
    }
};
class Orange:virtual public Mandarin,virtual public Pomelo
{
public:
    //Orange(string a,double b):Mandarin(a,b),Pomelo(a,b),Citrus(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is orange."<<endl;
    }
};
class Lime:virtual public Citron,virtual public Pomelo
{
public:
    //Lime(string a,double b):Citron(a,b),Citrus(a,b),Pomelo(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is lime."<<endl;
    }
};
class Tangerine:virtual public Orange ,virtual public Mandarin
{
public:
    Tangerine(string a,double b):Citrus(a,b){}//,Orange(a,b),Mandarin(a,b),Pomelo(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is tangerine."<<endl;
    }
};
class Grapefruit:virtual public Orange,virtual public Pomelo
{
public:
    Grapefruit(string a,double b):Citrus(a,b){}//,Orange(a,b),Pomelo(a,b),Mandarin(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is grapefruit."<<endl;
    }
};
class Lemon:virtual public Orange,virtual public Lime
{
public:
    Lemon(string a,double b):Citrus(a,b){}//,Mandarin(a,b),Orange(a,b),Lime(a,b),Pomelo(a,b),Citron(a,b){}
    void show(){
        cout<<name<<" "<<wei<<"kg, is lemon."<<endl;
    }
};
int main()
{
    Citrus     *c;
    Mandarin   *ma;
    Pomelo     *po;
    Citron     *ci;
    Orange     *og;
    Lime       *li;
    Tangerine  *ta;
    Grapefruit *gr;
    Lemon      *le;

    string name;
    double weight;
    while(cin >> name >> weight)
    {
        if(name == "Tangerine")
        {
            Tangerine tangerine(name, weight);
            c = ma = og = ta = &tangerine;
            c->show();
            ma->show();
            og->show();
            ta->show();
        }
        if(name == "Grapefruit")
        {
            Grapefruit grapefruit(name, weight);
            c = po = gr = &grapefruit;
            c->show();
            po->show();
            gr->show();
        }
        if(name == "Lemon")
        {
            Lemon lemon(name, weight);
            ma = og = &lemon;
            ci = li = &lemon;
            po = le = &lemon;
            ma->show();
            po->show();
            ci->show();
            og->show();
            li->show();
            le->show();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值