1. 已知一个名为Complex的复数类,这个类包含:
(1)私有成员:实部、虚部,且均为int 型
(2)公有的带默认形参值的构造函数、复制构造函数
(3)公有成员函数Display,其作用为显示复数
要求:
(1)实现满足上述属性和行为的Complex类定义;
(2)设计函数AddComplex,函数AddComplex功能为实现两个复数相加,要求该函数的形参为复数类的常引用;
(3)保证如下主函数能正确运行,
int main(){
Complex c0(2,-3);
Complex c1(c0);
cout<<"c1 is: ";
c1.Display();
Complex c2(3,4);
cout<<"c2 is: ";
c2.Display();
Complex c3;
c3 = AddComplex(c1,c2);
cout<<"c3 is: ";
c3.Display();
return 0;
}
且输出结果如下:
c1 is: 2-3i
c2 is: 3+4i
c3 is: 5+1i
2.已知表示点的类CPoint和表示线段的CLine类,
类CPoint包含:(1)表达点位置的私有数据成员x,y
(2)构造函数及复制构造函数
类CLine包含:
(1)两个CPoint的点对象(该两点分别为线段的两个端点)
(2)构造函数(提示:构造函数中用初始化列表对内嵌对象进行初始化)
(3)公有成员函数GetLen,其功能为返回线段的长度
(4)类属性成员count用于记录创建的CLine类对象的个数,及用于显示count值的ShowCount函数;
要求:
(1)实现满足上述属性和行为的CPoint类及CLine类定义;
(2)保证如下主函数能正确运行,
#include <iostream>
using namespace std;
int main(){
CPoint p1(1,1);
CPoint p2(4,5);
CLine line1(p1,p2);
cout<<"the length of line1 is: "<<line1.GetLen()<<endl;
CLine line2(line1);
cout<<"The count of line is: "<<CLine::ShowCount()<<endl;
return 0;
}
且输出结果如下:
The length of line1 is: 5
The count of Line is: 2
3. 创建一个表示点的类Point,运用组合的方式创建圆形类。 圆形有个成员函数isPointIn用于判断一个点是否在该圆形的内部。 完成对应类代码,使得主函数可以正确运行。
int main(int argc, char const *argv[])
{
int x,y;
double r;
cin>>x>>y>>r;
Point p(x,y);
Circle c(p,r);
cin>>x>>y;
Point p1(x,y);
bool b = c.isPointIn(p1);
cout<<b<<endl;
}
输入
1 1 1.5 2 2
输出
1