c++mid104_2

 avatar
user_3763047219
c_cpp
a year ago
4.2 kB
0
Indexable
Never
#include <iostream>
#include <string>

class Employee{
public:
    Employee(int i):id(i){  show();}
    int getID() const{  return id;}
    void show() const{  std::cout<<"[employee] ";    std::cout<<"Hiii,Employee No."<<getID()<<std::endl;}
protected:
    const int id;
};

class Teacher:public Employee{
private:
    std::string expert;
public:
    //在Employee(i)的地方會先到上面的class Employee,先show一次
    //接著往後執行show出[teacher],再由Employee::show()回到上面class Employee的show函式
    Teacher(int i,std::string e):Employee(i){   setExpert(e);   std::cout<<"[teacher 1] ";  Employee::show(); }
    Teacher(const Teacher &t):Employee(t.getID()){  setExpert(t.getExpert());   std::cout<<"[teacher 2]";  show(); }
    //這裡的show是Teacher裡面的函式show(可以發現會印出專業)
    void setExpert(std::string e){  expert = e;}
    std::string getExpert() const{  return expert;}
    void show() const{  std::cout<<"Hi Teacher No."<<getID()<<" with "<<expert<<std::endl;}
};

void ChangeExpert(Teacher t, int i){    std::cout<<"--call-by-value--"<<std::endl;}
//在Teacher t的時候會以teacher建立一個變數t,t沒有指定參數所以是teacher建構函式2
//teacher的建構函式2中,會先碰到employee的建構函式
//因此cout順序:employee的show>>teacher的show
void ChangeExpert(Teacher &t, double d){    std::cout<<"--call-by-reference--"<<std::endl;}
//因為是call by reference 沒有建立新的teacher變數,不會跑到建構子那行
//所以也不會進入employee的建構子,因此兩個show都沒有
void ChangeExpert(Teacher *t, char c){    std::cout<<"--call-by-pointer--"<<std::endl;}

void ChangeExpert(Teacher *t1, Teacher &t2, Teacher t3){
    //只有t3是建立一個新變數,因此:teacher建構函式2->employee建構函式

    Teacher tmp=t3;

    std::cout<<"hi"<<std::endl;
    Teacher((*t1).getID(), t2.getExpert());
    //teacher建構函式1->employee建構函式
    std::cout<<"hi"<<std::endl;
    (*t1).setExpert(t2.getExpert());
    t2.setExpert(t3.getExpert());
    //t1、t2都是取位址,所以在這邊設定會改到原本的值

    //std::cout<<t1->getExpert()<<std::endl;
    t3.setExpert(t1->getExpert());
    std::cout<<"t3: " <<t3.getExpert() << std::endl;
    //t3是一個新建的變數,複製了Hooman的東西
    //所以setExpert是設定到這個t3而不是Hooman
}

int main(){

    std::cout<<std::endl<<"ANS-PART 1:"<<std::endl;
    Teacher BSLin(1, "Embedded System");
    Teacher Hana(2, "Data Mining");
    Teacher Hooman(3, "Artificial Intelligent");
    //以上三行各有兩行輸出
    Teacher *t1 = &BSLin;
    Teacher &t2 = Hana;
    //Teacher *t3 = &Hooman;
    std::cout<<std::endl<<"ANS-Part2:"<<std::endl;
    ChangeExpert(BSLin, 1);

    std::cout<<std::endl<<"ANS-Part3:"<<std::endl;
    ChangeExpert(Hana,2.0);

    std::cout<<std::endl<<"ANS-Part4:"<<std::endl;
    ChangeExpert(&Hooman,'a');

    std::cout<<std::endl<<"ANS-Part5:"<<std::endl;
    /*
    std::cout<<"BSLin: " <<BSLin.getExpert() << std::endl;
    std::cout<<"Hana: " <<Hana.getExpert() << std::endl;
    std::cout<<"Hooman: " <<Hooman.getExpert() << std::endl;
    std::cout<<std::endl<<"ANS-Part5.2:"<<std::endl;
    */
    ChangeExpert(&BSLin, Hana, Hooman);

    std::cout<<std::endl<<"ANS-Part6:"<<std::endl;
    std::cout<<"BSLin: " <<BSLin.getExpert() << std::endl;
    std::cout<<"Hana: " <<Hana.getExpert() << std::endl;
    std::cout<<"Hooman: " <<Hooman.getExpert() << std::endl;
    //std::cout<<t2.<<std::endl;
    //std::cout<<&Hana<<std::endl;
    std::cout<<std::endl<<"ANS-Part7:"<<std::endl;
    std::cout<<&t1<<std::endl;
    //&t1: t1的位址
    std::cout<<t1<<std::endl;
    //t1: t1這個變數的值(BSLin的位址)
    std::cout<<&BSLin<<std::endl;
    ChangeExpert(t1, t2, *t1);
    //*t1是甚麼?

    std::cout<<std::endl<<"ANS-Part8:"<<std::endl;
    std::cout<<"BSLin: " <<BSLin.getExpert() << std::endl;
    std::cout<<"Hana: " <<Hana.getExpert() << std::endl;
    std::cout<<"Hooman: " <<Hooman.getExpert() << std::endl;




}