Skip to content

Instantly share code, notes, and snippets.

@popmentos
Created December 5, 2010 14:18
Show Gist options
  • Save popmentos/729116 to your computer and use it in GitHub Desktop.
Save popmentos/729116 to your computer and use it in GitHub Desktop.

Revisions

  1. popmentos revised this gist Dec 5, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hm4.cpp
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ class car :public carcar{ //使用虛擬函數完成此類別
    class Benz: public car { //完成此類別
    public:
    Benz(){this->name = "Benz";}
    void move(){cout << this->name << " in Greman" << endl;}
    void move(){cout << this->name << " in German" << endl;}
    } ;

    class Volvo: public car { //完成此類別
  2. popmentos created this gist Dec 5, 2010.
    55 changes: 55 additions & 0 deletions hm4.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #include <iostream>
    using namespace std;


    class carcar{//使用純粹虛擬函數完成此類別
    protected:
    string name;
    public:
    virtual void move()=0;
    };

    class car :public carcar{ //使用虛擬函數完成此類別
    public:
    car(){this->name = "car";}
    virtual void move(){cout << this->name << endl;}
    } ;

    class Benz: public car { //完成此類別
    public:
    Benz(){this->name = "Benz";}
    void move(){cout << this->name << " in Greman" << endl;}
    } ;

    class Volvo: public car { //完成此類別
    public:
    Volvo(){this->name = "Volvo";}
    void move(){cout << this->name << " in Sweden" << endl;}
    } ;

    void demo(car c){
    c.move();
    }

    void demo(car *c){ //完成此函數
    c->move();
    }

    //以下請勿更動
    int main(){
    car c;
    Benz b ;
    Volvo v;
    cout << "傳入變數" << endl;
    demo(c);
    demo(b);
    demo(v);

    cout << "傳入指標" << endl;
    demo(&c);
    demo(&b);
    demo(&v);

    system("pause");
    return 0;
    }