2016-05-15 5 views
0

私はC++を勉強しています。多型とopを使用したテンプレートの作成。 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

これは機能します!ありがとうございました。しかし、少し複雑に思えます。 "time :: hour = 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; 
} 

NB:0x36、文字'c'を与えるため、'1'(コード0x31)と'2'(コード0x33)を使用しました。

+0

ありがとうございますが、この方法は動作しません。 エラー: "エラー:クラス 'er 'に 'Time'という名前のフィールドがありません。" これは正しい解決策ではありません。 私はすでにこれをやろうとしましたが、このエラーが発生しました。 このように私はここで正しい解決策を求めています。 – andrea

関連する問題