您的位置:首页 > 编程语言 > C语言/C++

C++课程设计类作业2

2017-06-10 23:30 513 查看
不要问我一个晚上在干啥,就写写这种烦到极点的类,啰嗦!

#include <bits/stdc++.h>
using namespace std;
class complexed
{
public:
complexed();
complexed(double real);
complexed(double real,double imag);
void display();
void set(double r,double i);
private:
double real,imag;
};
complexed::complexed()
{
set(0.0,0.0);
cout<<"default constructor.\n";
}
complexed::complexed(double real)
{
set(real,0.0);
cout<<"construct called.\n";
}
complexed::complexed(double real,double imag)
{
set(real,imag);
cout<<"constructor :real="<<real<<",imag="<<imag<<endl;
}
void complexed::display()
{
if(imag<0)
cout<<real<<imag<<'i'<<endl;
else
cout<<real<<'+'<<imag<<'i'<<endl;
}
void complexed::set(double r,double i)
{
real=r;
imag=i;
}
int main()
{
complexed c1;
complexed c2(6.8);
complexed c3(5.6,7.9);
c1.display();
c2.display();
c3.display();
c1=complexed(1.2,3.4);
c2=5;
c3=complexed();
c1.display();
c2.display();
c3.display();
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: