导航
  • 全部
...

C++的一个问题,牵扯到继承和重载运算符,正文是为了表达清楚我的意思写的一个试验性的小程序

baobaopangpang8989 2011-06-27 10:33:44
头文件,其中包括的其他头文件和命名空间就不写了
class Complex//定义个叫Complex的类
{
private:
int nR;
int nI;
public:
Complex(int tmpR=-100,int tmpI=-100);//构造函数
void display();//定义展示nR和nI
friend ostream& operator << (ostream&, const Complex&);//重载了<<
};
class NameComplex:public Complex//继承与Complex
{
private:
string strName;
public:
NameComplex(const string&,int,int);//构造函数
void display();//定义展示strName,nR和nI
friend ostream& operator << (ostream&, const NameComplex&);//重载了<<
};

cpp文件
Complex::Complex(int tmpR, int tmpI)//Complex构造函数的定义
{
this->nR = tmpR;
this->nI = tmpI;
}
void Complex::display() //Complex的display的定义
{
cout<<"实数部分是:"<<this->nR<<endl;
cout<<"虚数部分是:"<<this->nI<<endl;
}
ostream& operator << (ostream& os,const Complex &tmpCOM)//Complex的重载了<<的定义
{
os<<"实数部分为:"<<tmpCOM.nR<<"虚数部分为:"<<tmpCOM.nI<<endl;
return os;
}
NameComplex::NameComplex(const string &str, int nR, int nI):Complex(nR,nI)//NameComplex的构造函数定义
{
strName = str;
}
void NameComplex::display()//NameComplex的display定义
{
cout<<"这个是高级的"<<this->strName<<endl;
Complex::display();
}
ostream& operator << (ostream& os,const NameComplex &tmpName)
{
//这里就是问题,我希望在使用这个重载函数的时候调用:基类的这个<<的重载函数
}


问题就写在代码里面了,希望各位大大提醒一下小弟,小弟会感激不尽的,如果有描述不清楚的地方,希望可以指出
虽然我的分数不是很多,我也尽可能的给出我的分数了,先谢谢各位了
...全文
给本帖投票
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
the_bblythe 2011-06-28
  • 打赏
  • 举报
回复
把基类的那个<<重载声明为友元呢
我也是才学,你试试呢。
就想叫yoko 2011-06-28
  • 打赏
  • 举报
回复
ostream& operator << (ostream& os,const NameComplex &tmpName)
{
//这里就是问题,我希望在使用这个重载函数的时候调用:基类的这个<<的重载函数
return os << (Complex)tmpName;
}

给你个思路
不知道你具体是怎么需要调用基类的这个<<的重载函数
baobaopangpang8989 2011-06-28
  • 打赏
  • 举报
回复
多谢楼上的各位大大了,非常感谢,又拓宽了我的思路,谢谢大家,在此结贴~
GoonYangXiaofang 2011-06-27
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

class Complex
{
protected:
// private:
int nR;
int nI;
public:
Complex(int tmpR = -100, int tmpI = -100);
virtual void display();
virtual ostream& print(ostream& os) const;
friend ostream& operator << (ostream&, const Complex&);
};

class NameComplex : public Complex
{
private:
string strName;
public:
NameComplex(const string& = "abc", int = -100, int = -100);
virtual void display();
virtual ostream& print(ostream& os) const;
// friend ostream& operator << (ostream&, const NameComplex&);
};

Complex::Complex(int tmpR, int tmpI) : nR(tmpR), nI(tmpI) {}

void Complex::display()
{
cout << nR << endl;
cout << nI << endl;
}

ostream& Complex::print(ostream& os) const
{
os << nR << ' ' << nI;
return os;
}

/*
ostream& operator << (ostream& os, const Complex& rhs)
{
os << rhs.nR << ' ' << rhs.nI;
return os;
}
*/

ostream& operator << (ostream& os, const Complex& rhs)
{
return rhs.print(os);
}

NameComplex::NameComplex(const string& str, int nR, int nI) : Complex(nR, nI), strName(str) {}

void NameComplex::display()
{
cout << strName << endl;
Complex::display();
}

/*
ostream& operator << (ostream& os, const NameComplex& rhs)
{
os << rhs.strName << ' ';
operator << (os, static_cast<Complex>(rhs));
return os;
}
*/

ostream& NameComplex::print(ostream& os) const
{
os << strName << ' ';
return Complex::print(os);
}

int main()
{
Complex a;
cout << a << endl;

NameComplex b;
cout << b << endl;
}
baobaopangpang8989 2011-06-27
  • 打赏
  • 举报
回复
没人回答么,我觉得应该不是很难的吧,就是应该是有什么东西或者什么方法我没有想到
希望大家帮帮忙~

65,166

社区成员

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

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

手机看
关注公众号

关注公众号

客服 返回
顶部