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