私は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;
}
私の質問に低い評価を受けた理由を教えてもらえますか? 私はC++を学んでいます。私は簡単な質問をしました。 人が質問した質問に対して低い評価を受けた場合、このサイトの使用は何ですか?ありがとうございました。 – andrea