物件104-2

 avatar
user_6817964
c_cpp
a year ago
2.2 kB
3
Indexable
Never
#include <iostream>
#include <string>
using namespace std;

class Employee{
public:
    Employee(int i):id(i){ show();}
    int getID() const {return id;}
    void show() const {cout << "Employee No." << getID() << endl;}
private:
    const int id;
};

class Teacher:public Employee{
private:
    string expert;
public:
    Teacher(int i, string e):Employee(i){setExpert(e); Employee::show();}
    Teacher(const Teacher &t):Employee(t.getID()){ setExpert(t.getExpert()); show();}
    void setExpert(string e){expert = e;}
    string getExpert() const {return expert;}
    void show() const {cout << "Teacher No." << getID() << "with" << expert << endl;}
};

void ChangeExpert(Teacher t, int i){cout << "call by value" << endl;}
void ChangeExpert(Teacher &t, double d){cout << "call by reference" << endl;}
void ChangeExpert(Teacher *t, char c){cout << "call by pointer" << endl;}
void ChangeExpert(Teacher *t1, Teacher &t2, Teacher t3)
{
    Teacher t((*t1).getID(), t2.getExpert());

    (*t1).setExpert(t2.getExpert());
    t2.setExpert(t3.getExpert());
    t3.setExpert(t.getExpert());
}

int main()
{
    cout << "part1" << endl << endl;
    Teacher BSLin(1, "E S");
    Teacher Hana(2, "D M");
    Teacher Hooman(3, "A I");
    Teacher *t1 = &BSLin;
    Teacher &t2 = Hana;

    cout << endl << "part2" << endl << endl;
    ChangeExpert(BSLin, 1);

    cout << endl << "part3" << endl << endl;
    ChangeExpert(Hana, 2.0);

    cout << endl << "part4" << endl << endl;
    ChangeExpert(&Hooman, 'a');

    cout << endl << "part5" << endl << endl;
    ChangeExpert(&BSLin, Hana, Hooman);

    cout << endl << "part6" << endl << endl;
    cout << "BSLin:" << BSLin.getExpert() << endl;
    cout << "Hana:" << Hana.getExpert() << endl;
    cout << "Hooman:" << Hooman.getExpert() << endl;

    cout << endl << "part7" << endl << endl;
    ChangeExpert(t1, t2, *t1);

    cout << endl << "part8" << endl << endl;
    cout << "BSLin:" << BSLin.getExpert() << endl;
    cout << "Hana:" << Hana.getExpert() << endl;
    cout << "Hooman:" << Hooman.getExpert() << endl;

    return 0;
}