2016-05-15 79 views
0

我正在學習C++,我想問:使用多態性和操作創建模板。在C++重載

我怎樣才能在模板中轉換類「時間」? 喜歡的東西:

template <class genericType> 
class time { 

我沒有C++好,我想要做的是在主要使用其它類型的數據,而不僅僅是「INT」,如下面的代碼。

我想作這樣的:

time <char>t('a','a','a'); 
t.show(); 

er <char>t2('b','b','b'); 
t2.show(); 

time <char>t3=t+t2; 
t3.show(); 

謝謝大家。 這是代碼我想在模板變換:

#include <iostream> 
using namespace std; 

class time{ 
protected: 

    int hour, minuts , seconds; 

public: 

    time(int x=0, int y=0, int z=0){ 
     hour=x; 
     minuts=y; 
     seconds=z; 
    } 
    virtual void show(){ 
     cout<<"it's "<<hour<<":"<<minuts<<":"<<seconds<<endl; 
    } 


    time operator+(time &te){ 

     cout<<"sum everything: "; 
     time bho; 
     bho.hour=hour+te.hour; 
     bho.minuts+=minuts+te.minuts; 
     bho.seconds+=seconds+te.seconds; 
     return bho; 
    } 

}; 

class er: public time { 

public: 

    er(int x=0,int y=0,int z=0):time(x,y,z){}; 

    void show() { 
    cout<< "Inside er: it's "<<hour<<":"<<minuts<<":"<<seconds<<endl; 
    }; 
}; 

int main() 
{ 

    time t(10,10,10); 
    t.show(); 

    er t2(20,20,20); 
    t2.show(); 

    time *pt= new er(60,60,60); 
    pt->show(); 

    time t3=t+t2; 
    t3.show(); 

    return 0; 
} 
+1

有人能告訴我爲什麼我收到我的問題低評分? 我正在學習C++,而且我對一些我不知道該怎麼做的問題提出了一個簡單的問題。 如果一個人對於他所要求的問題收到較低的評價,那麼這個網站的用途是什麼?謝謝。 – andrea

回答

1

確實如此, @serge Ballesta的解決方案是錯誤的。 也許你可以試試這個:

#include <iostream> 
using namespace std; 

template <class genericType> 
class time{ 
protected: 

    genericType hour, minutes , seconds; 

public: 

    time(genericType x=0, genericType y=0, genericType z=0){ 
     hour=x; 
     minutes=y; 
     seconds=z; 
    } 
    virtual void show(){ 
     cout<<"it's: "<<hour<<":"<<minutes<<":"<<seconds<<endl; 
    } 

    time operator+(time &te){ 

     cout<<"sum everything: "<<endl; 
     time bho; 
     bho.hour=hour+te.hour; 
     bho.minutes+=minutes+te.minutes; 
     bho.seconds+=seconds+te.seconds; 
     return bho; 
    } 

}; 

template <class genericType> 
class er: public time <genericType>{ 

    public: 

    er(genericType x=0,genericType y=0, genericType z=0){ 
     time<genericType>::hour=x; 
     time<genericType>::minutes=y; 
     time<genericType>::seconds=z; 
    } 

    void show() { 
    cout<< "Inside er: it's "<<time<genericType>::hour<<":"<<time<genericType>::minutes<<":"<<time<genericType>::seconds<<endl; 
    }; 
}; 

int main() 
{ 
    time <char>t('a','a','a'); 
    t.show(); 

    er <char>t2('b','b','b'); 
    t2.show(); 

    time <char>t3=t+t2; 
    t3.show(); 

    return 0; 
} 
+0

這個工程!謝謝。但似乎有點複雜。還有另一種比以下更簡單的解決方案:「時間 ::小時= x;」 ??? – andrea

0

剛剛看了一個C++教程,並加魔法template <class T> ...

這裏是你的代碼的模板版本:

#include <iostream> 
using namespace std; 

template<class T> 
class time{ 
protected: 

    T hour, minuts , seconds; 

public: 

    time(T x=0, T y=0, T z=0){ 
     hour=x; 
     minuts=y; 
     seconds=z; 
    } 
    virtual void show(){ 
     cout<<"it's "<<hour<<":"<<minuts<<":"<<seconds<<endl; 
    } 


    time operator+(time &te){ 

     cout<<"sum everything: "; 
     time bho; 
     bho.hour=hour+te.hour; 
     bho.minuts+=minuts+te.minuts; 
     bho.seconds+=seconds+te.seconds; 
     return bho; 
    } 

}; 

template <class T> 
class er: public time<T> { 

public: 

    er(T x=0,T y=0,T z=0):time(x,y,z){}; 

    void show() { 
    cout<< "Inside er: it's "<<hour<<":"<<minuts<<":"<<seconds<<endl; 
    }; 
}; 

int main() 
{ 

    time<char> t('2', '2', '2'); 
    t.show(); 

    er<char> t2('1', '1', '1'); 
    t2.show(); 

    time<int> *pt= new er<int>(60,60,60); 
    pt->show(); 

    time<char> t3=t+t2; 
    t3.show(); 

    return 0; 
} 

注意:我使用'1'(代碼0x31)和'2'(代碼0x33)字符,因爲總和給出了0x63,字符'c' - 提供了使用基於ASCII的系統

+0

謝謝,但這種方法不起作用。 我得到這個錯誤: 「錯誤:類'er '沒有任何字段名爲'時間'」 這不是正確的解決方案。 我已經試圖做到這一點,但我收到了這個錯誤。 這樣我在這裏問的是正確的解決方案。 – andrea