如何使函数返回一个类呀?
各位高手好,我才刚开始进入C++的世界
现在遇到一个难题:
我需要使类中的一个公有函数返回这个类,如何实现
请指教!
这个程序是C++程序,不涉及VC
用类实现复数的四则运算
以下是我的程序源码:
#include"complex.h"
void main()
{
ComplexOper comp1,comp2,comp3;
comp1.SetCompOper(3.5,2.7);
comp2.SetCompOper(5.6,34.4);
comp3=comp1.AddComp(comp2);
comp3.display();
}
#ifndef complex_h_
#define complex_h_
#include <iostream.h>
//复数基本四则运算通过类的实现
class ComplexOper
{
public:
ComplexOper();
void SetCompOper(const double dReal,const double dImag);
ComplexOper AddComp(ComplexOper);
//void SubComp();
//void MulComp();
//void DivComp();
void display();
private:
double RealPart;
double ImagPart;
};
ComplexOper::ComplexOper()
{
RealPart=ImagPart=0.0;
}
void ComplexOper::SetCompOper(const double dReal,const double dImag)
{
RealPart=dReal;
ImagPart=dImag;
}
ComplexOper ComplexOper::AddComp(ComplexOper Comp)
{
double x,y;
x=RealPart+Comp.RealPart;
y=ImagPart+Comp.ImagPart;
return ComplexOper(x,y);
}
void ComplexOper::display()
{
cout<<RealPart<<(ImagPart>0 ? "+" :"" )<<ImagPart<<endl;
}
#endif
恳请指教!