C++习题 对象转换

C++习题 对象转换

时间: 1ms        内存:128M

描述:

定义一个Teacher(教师)类(教师号,姓名,性别,薪金)和一个Student(学生)类(学号,姓名,性别,成绩),二者有一部分数据成员是相同的,num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师数据的一部分。

输入:

一个教师的信息和一个学生的信息

输出:

学生的信息和学生转换为教师后的信息

示例输入:

10001 Li f 1234.5
20010 Wang m 89.5

示例输出:

student1:
num:20010
name:Wang
sex:m
score:89.50
teacher2:
num:20010
name:Wang
sex:m
pay:1500.00

提示:

参考答案(内存最优[0]):

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
class Student
{
public:
    Student() {}
    Student(int n,string na,char s,float sc):num(n),name(na),sex(s),score(sc) {}
    void display();
    int get_num()
    {
        return num;
    }
    string get_name()
    {
        return name;
    }
    char get_sex()
    {
        return sex;
    }
private :
    int num;
    string name;
    char sex;
    float score;
};
class Teacher
{
public:
    Teacher() {}
    Teacher(int n,string na,char s,float p):num(n),name(na),sex(s),pay(p) {}
    Teacher(Student &s)
    {
        num=s.get_num();
        name=s.get_name();
        sex=s.get_sex();
    }
    void display();
    void setpay(double p);
private :
    int num;
    string name;
    char sex;
    float pay;
};

void Student::display()
{
    cout<<"num:"<<num<<'\n'<<"name:"<<name<<'\n'<<"sex:"<<sex<<'\n'<<"score:"<<score<<'\n';
}
void Teacher::display()
{
    cout<<"num:"<<num<<'\n'<<"name:"<<name<<'\n'<<"sex:"<<sex<<'\n'<<"pay:"<<pay<<'\n';
}
void Teacher::setpay(double p)
{
    pay=p;
}
int main()
{
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    int num;
    char name[20];
    char sex;
    float score;
    float pay;
    cin>>num>>name>>sex>>pay;
    Teacher teacher1(num,name,sex,pay);
    cin>>num>>name>>sex>>score;
    Student student1(num,name,sex,score);
    cout<<"student1:"<<endl;
    student1.display();
    Teacher teacher2=Teacher(student1);
    teacher2.setpay(1500);
    cout<<"teacher2:"<<endl;
    teacher2.display();
    return 0;
}

参考答案(时间最优[0]):

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
class Student
{
public:
    Student(int,char[],char,float);
    int get_num()
    {
        return num;
    }
    char * get_name()
    {
        return name;
    }
    char get_sex()
    {
        return sex;
    }
    void display()
    {
        cout<<"num:"<<num<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"score:"<<score<<endl;
    }
private:
    int num;
    char name[20];
    char sex;
    float score;
};

Student::Student(int n,char nam[],char s,float so)
{
    num=n;
    strcpy(name,nam);
    sex=s;
    score=so;
}

class Teacher
{
public:
    Teacher() {}
    Teacher(Student&);
    Teacher(int n,char nam[],char sex,float pay);
    void display();
    void setpay(int pay){this->pay = pay;}
private:
    int num;
    char name[20];
    char sex;
    float pay;
};

Teacher::Teacher(int n,char nam[],char s,float p)
{
    num=n;
    strcpy(name,nam);
    sex=s;
    pay=p;
}

Teacher::Teacher(Student& stud)
{
    num=stud.get_num();
    strcpy(name,stud.get_name());
    sex=stud.get_sex();
    //pay=1500;
}

void Teacher::display()
{
    cout<<"num:"<<num<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"pay:"<<pay<<endl;
}

int main()
{
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    int num;
    char name[20];
    char sex;
    float score;
    float pay;
    cin>>num>>name>>sex>>pay;
    Teacher teacher1(num,name,sex,pay);
    cin>>num>>name>>sex>>score;
    Student student1(num,name,sex,score);
    cout<<"student1:"<<endl;
    student1.display();
    Teacher teacher2=Teacher(student1);
    teacher2.setpay(1500);
    cout<<"teacher2:"<<endl;
    teacher2.display();
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注