求助:重载运算符+继承类

Lewencn 2007-05-06 03:34:48
这个是一个作业,前两题很简单,但是第三题我不知道要怎么做了,我觉得第三题的话直接建一个double的类不就可以了,继承反而还不会做了 :(

麻烦大家告诉一下思路好吗?谢谢了

题目如下:

-------------------------------------------------------------------
1. 定义一个整数计算类Integer,实现短整数 +,-,*,/ 基本算术运算。要求可以进行数据范围检查(-32768~32767,或自行设定),数据溢出时显示错误信息并中断程序运行。

2. 定义一个实数计算类Real,实现单精度浮点数 +,-,*,/ 基本算术运算。要求可以进行数据范围(-3.4×1038~3.4×1038,或自行设定)检查,数据溢出时显示错误信息并中断程序运行。

3.为1题2题的Integer和Real类定义一个派生类IntReal:
class IneReal : public Integer, public Real ;
使其可以进行 +,-,*,/,= 左右操作数类型不同的相容运算,并符合原有运算类型转换的语义规则。

-----------------------------------------------------------------------
...全文
516 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
eggqq007 2007-05-07
  • 打赏
  • 举报
回复
看你搞得这么麻烦,给你一篇文章参考。
http://blog.csdn.net/wingfiring/archive/2006/08/04/1019551.aspx
Lewencn 2007-05-07
  • 打赏
  • 举报
回复
汗。。。你以为我想这么麻烦啊,,题目是这样要求的嘛。。。不然一个double就解决啦。。
Lewencn 2007-05-06
  • 打赏
  • 举报
回复
Real类中几个地方打错了,应该是这样的:
to:dai_weitao(疯狂Linux C++)
直接继承的话不行啊,会出现二义性

to:eggqq007(后起之秀,前途未卜。)
虚基类是用来存放那个元素吧?但是这样的话就只调用整型那个类了,float那个没用了


看看我的程序问题在哪里,要怎么改才行呢?还是要怎么写才行?谢谢:

---------------------------------------------------------

#include<iostream.h>
#include<stdlib.h>
class base
{
public:
double n;
};
//******************************Integer 类*****************************************
class Integer:virtual public base
{
public:
Integer(){n=0;};
int operator +(Integer t);
int operator -(Integer t);
int operator *(Integer t);
int operator /(Integer t);
};
int Integer::operator +(Integer t)
{
int temp;
temp=n+t.n;
return temp;
}
int Integer::operator -(Integer t)
{
int temp;
temp=n-t.n;
return temp;
}

int Integer::operator *(Integer t)
{
int temp;
temp=n*t.n;
return temp;
}

int Integer::operator /(Integer t)
{
int temp;
temp=n/t.n;
return temp;

}
//******************************Real类********************************************
class Real:virtual public base
{
public:
Real(){n=0;};
float operator +(Real t);
float operator -(Real t);
float operator *(Real t);
float operator /(Real t);
};
float Real::operator +(Real t)
{
float temp;
temp=n+t.n;
return temp;
}
float Real::operator -(Real t)
{
float temp;
temp=n-t.n;
return temp;
}

float Real::operator *(Real t)
{
float temp;
temp=n*t.n;
return temp;
}

float Real::operator /(Real t)
{
float temp;
temp=n/t.n;
return temp;
}

//**********************************IneReal类*************************************

class IneReal : public Integer, public Real
{
public:

};

void main()
{
int m;
cout << "请输入你要进行的操作(输入数字序号):" << "1.a+b" << '\t' << "2.a-b" << '\t' << "3.a*b" << '\t' << "4.a/b" << '\t' ;
cin >> m ;
IneReal a,b;
cout << "请输入数值:" << endl;
cout << "a" << '\t' ;
cin >> a.n ;
cout << "b" << '\t' ;
cin >> b.n ;
switch (m)
{
case 1:cout << "a+b=" << a+b << endl;break;
case 2:cout << "a-b=" << a-b << endl;break;
case 3:cout << "a*b=" << a*b << endl;break;
case 4:cout << "a/b=" << a/b << endl;break;
default:cout << "选择输入错误";
}

}
----------------------------------------------------------


望大家帮忙解决一下,谢谢
Lewencn 2007-05-06
  • 打赏
  • 举报
回复
to:dai_weitao(疯狂Linux C++)
直接继承的话不行啊,会出现二义性

to:eggqq007(后起之秀,前途未卜。)
虚基类是用来存放那个元素吧?但是这样的话就只调用整型那个类了,float那个没用了


看看我的程序问题在哪里,要怎么改才行呢?还是要怎么写才行?谢谢:

---------------------------------------------------------

#include<iostream.h>
#include<stdlib.h>
class base
{
public:
double n;
};
//******************************Integer 类*****************************************
class Integer:virtual public base
{
public:
Integer(){n=0;};
int operator +(Integer t);
int operator -(Integer t);
int operator *(Integer t);
int operator /(Integer t);
};
int Integer::operator +(Integer t)
{
int temp;
temp=n+t.n;
return temp;
}
int Integer::operator -(Integer t)
{
int temp;
temp=n-t.n;
return temp;
}

int Integer::operator *(Integer t)
{
int temp;
temp=n*t.n;
return temp;
}

int Integer::operator /(Integer t)
{
int temp;
temp=n/t.n;
return temp;

}
//******************************Real类********************************************
class Real:virtual public base
{
public:
Real(){n=0;};
float operator +(Real t);
float operator -(Real t);
float operator *(Real t);
float operator /(Real t);
};
float Real::operator +(Real t)
{
float temp;
temp=n+t.n;
return temp;
}
float Real::operator -(Real t)
{
int temp;
temp=n-t.n;
return temp;
}

float Real::operator *(Real t)
{
int temp;
temp=n*t.n;
return temp;
}

float Real::operator /(Real t)
{
int temp;
temp=n/t.n;
return temp;
}

//**********************************IneReal类*************************************

class IneReal : public Integer, public Real
{
public:

};

void main()
{
int m;
cout << "请输入你要进行的操作(输入数字序号):" << "1.a+b" << '\t' << "2.a-b" << '\t' << "3.a*b" << '\t' << "4.a/b" << '\t' ;
cin >> m ;
IneReal a,b;
cout << "请输入数值:" << endl;
cout << "a" << '\t' ;
cin >> a.n ;
cout << "b" << '\t' ;
cin >> b.n ;
switch (m)
{
case 1:cout << "a+b=" << a+b << endl;break;
case 2:cout << "a-b=" << a-b << endl;break;
case 3:cout << "a*b=" << a*b << endl;break;
case 4:cout << "a/b=" << a/b << endl;break;
default:cout << "选择输入错误";
}

}
----------------------------------------------------------
eggqq007 2007-05-06
  • 打赏
  • 举报
回复
先做一个虚基类,然后继承。在子类里实现就可以了啊。
dai_weitao 2007-05-06
  • 打赏
  • 举报
回复
因为int到float是可以自动转换的.
dai_weitao 2007-05-06
  • 打赏
  • 举报
回复
直接继承之后应该可以实现的啊.
你自己再重载一下operator = 就行了.

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧