在C++中创建一个具有多态性和操作重载的模板

Creating a template with polymorphism and op. overloading in C++

本文关键字:多态性 操作 重载 一个 创建 C++      更新时间:2023-10-16

我正在学习C++,我想问:

如何在模板中转换类"时间"?类似于:

template <class genericType>
class time {

我不擅长C++,我想在main中使用其他类型的数据,而不仅仅是下面代码中的"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;
}

这是真的,@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;
}

只需阅读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的系统