定义复数Complex类模板,其中包括构造函数以及显示复数的函数,最后在主函数中创建该类的2个对象用C++
定义复数Complex类模板,其中包括构造函数以及显示复数的函数,最后在主函数中创建该类的2个对象,向类型参数分别传递int型和double型数据.......请用C++...
定义复数Complex类模板,其中包括构造函数以及显示复数的函数,最后在主函数中创建该类的2个对象,向类型参数分别传递int型和double型数据.......请用C++
展开
2个回答
展开全部
#include "stdafx.h"
#include "iostream"
using namespace std;
class complex
{
public:
double a,b; //a+bi
complex(double,double);
void print();
};
complex::complex(double x,double y)
{
a=x;
b=y;
}
void complex::print()
{
cout<<a<<"+"<<b<<"i";
}
void main()
{
complex z1(3,5);
complex z2(1.9,8.7);
//输出
cout<<"z1=";z1.print();
cout<<endl;
cout<<"z2=";z2.print();
cout<<endl;
}
#include "iostream"
using namespace std;
class complex
{
public:
double a,b; //a+bi
complex(double,double);
void print();
};
complex::complex(double x,double y)
{
a=x;
b=y;
}
void complex::print()
{
cout<<a<<"+"<<b<<"i";
}
void main()
{
complex z1(3,5);
complex z2(1.9,8.7);
//输出
cout<<"z1=";z1.print();
cout<<endl;
cout<<"z2=";z2.print();
cout<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
class complex
{
private:
double r,i;
public:
complex(double ,double);
void display();
};
complex::complex(double R=0,double I=0)
{
r=R;
i=I;
}
void complex::display()
{
cout<<r;
if(i>=0)
cout<<"+"<<i<<"i";
if(i<0)
cout<<i<<"i";
}
void main()
{
complex c1(2,3),c2(3,-3),c3(4.2,5);
c1.display();
cout<<endl;
c2.display();
cout<<endl;
c3.display();
cout<<endl;
}
using namespace std;
class complex
{
private:
double r,i;
public:
complex(double ,double);
void display();
};
complex::complex(double R=0,double I=0)
{
r=R;
i=I;
}
void complex::display()
{
cout<<r;
if(i>=0)
cout<<"+"<<i<<"i";
if(i<0)
cout<<i<<"i";
}
void main()
{
complex c1(2,3),c2(3,-3),c3(4.2,5);
c1.display();
cout<<endl;
c2.display();
cout<<endl;
c3.display();
cout<<endl;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询