在C++中创建一个具有多态性和操作重载的模板
Creating a template with polymorphism and op. overloading in C++
我正在学习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的系统
相关文章:
- 多态性和功能结合
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- C++boost序列化多态性问题
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 如何在多线程中正确使用unique_ptr进行多态性?
- 具有智能指针的多态性
- 在 C++ 中在堆栈上创建实例时如何保持多态性?
- 继承/多态性 - 我是否被迫使用"protected"变量?
- C++ 多态性在代码::块 17.12 中不起作用
- C++ 泛型和多态性:这种模式可行吗?
- 为什么我们实际上需要运行时多态性?
- 如何在这个简单的最小示例中实现多态性?
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 无法初始化已知大小的矢量指针,该大小不会因多态性而更改
- 运行时多态性和dynamic_cast需要澄清
- 防止在不同尺寸的,模板,多态性的数学向量上进行组件操作
- 如何使用多态性将相同的操作应用于具有相同基类的不同对象的unordered_sets
- 在C++中创建一个具有多态性和操作重载的模板
- 根据"foreign"子类型采取特定操作,不带开关、强制转换等。我们可以以某种方式使用多态性吗?