c++实验答案1
- 格式:pdf
- 大小:120.96 KB
- 文档页数:9
t1 = x - b.x; t2 = y - b.y; t3 = z - b.z;
if( t1 == 0 && t2 == 0 && t3 == 0 )
return true; else
return false; }
void Point3D::print() {
cout << "( " << x << ", " << y << ", " << z << " )" << endl; }
left,double bottom,double
right,double top)
{
double z;
z=2*(abs(left-right)+abs(bot tom-top));
cout<<" 该 长 方 形 的 周 长 为 :
1
"<<z<<endl;
}
void
Getp1(double
left,double bottom)
{
cout<<" 长 方 形 的 左 下 角 坐 标
是
:
"<<left<<","<<bottom<<endl;
}
void
Getp2(double
right,double top)
{
cout<<" 长 方 形 的 右 上 角 坐 标
是:"<<right<<","<<top<<endl;
}
};
void main() {
class Rectangle
{
private:
double left;
double bottom;
double right;
double top;
public:
void
SetPoint(double
l,double b,double r,double t)
{
left=l;
bottom=b;
right=r;
return *this;
}
void
Scomplex::print()
{
cout << real;
2
if( image >= 0 ) {
cout << '+'; } cout << image << 'i' << endl; } int main() { Scomplex t1(1, 2), t2(3, 4), t3; t3 = 1 + t2; t3.print(); t3 = 2 - t2; t3.print(); t3 = t1; t3.print(); return 0; } 4+4i -1-4i 1+2i 2. 空 间 一 点 p 的 坐 标 为 (x,y,z),其中 x,y,z 为整数。编写点类 Point3D,定义空间两点 之 间 的 加 ”+”, 减 ”-” 运 算为相应三个坐标值分 别进行加、减运算,要 求实现空间两点之间的 加 ”+” 减 ”-” 赋 值 ”=” 运 算,空间两点间的比 较”= =”运算。要求编写 Point3D 类的声明定义 和测试程序。 #include <iostream>
请输入年份:5
该树年龄为:10
3. 声明一个名为 Rectangle 的矩 形类,其属性为矩形的左下角和
右上角两点的坐标,并有成员函
数计算矩形的周长及面积。编程
实现求左下角与右上角坐标分别
为(2.1,3.2),(5.2,6.3)的
矩形周长及面积。
#include<iostream.h>
#include<math.h>
} else {
cout << "p1 != p2" << endl;
}
return 0; } (3,3,3) (1,1,1) (1,1,1) p1!=p2 3. 设计一个时间类 Time,
包括时、分、秒等私有 数据 成员。重 载“+”和 “-”运算 符以实现 时间 的加法和减法运算,并 进行测试。 #include <iostream>
{
private:
double real;
double image;
public:
Scomplex( double
x = 0, double y = 0 );
friend Scomplex
operator+( int a,
Scomplex b );
friend Scomplex
operator-( int a,
class circle
{
private:
int radius;
public:
void getvalue()
半径:";
{ cout<<" 输 入
cin>>radius;
}
double getarea()
{
return
3.14*radius*radius;
}
void output()
{
cout<<"半径
Point3D temp;
temp.x = x + b.x; temp.y = y + b.y; temp.z = z + b.z;
return temp; }
Point3D Point3D::operator-( Poin t3D b ) {
Point3D temp;
temp.x = x - b.x; temp.y = y - b.y; temp.z = z - b.z;
Scomplex b );
friend Scomplex
operator+( double a,
Scomplex b );
friend Scomplex
operator-( double a,
Scomplex b );
Scomplex
&operator=( const
Scomplex &b );
void print();
Rectangle r1; double l,b,r,t; cout<<"请输入长方形的左下角 的坐标:"; cin>>l>>b; cout<<"请输入长方形的右上角 的坐标:"; cin>>r>>t; r1.Getp1(l,b); r1.Getp2(r,t); r1.cal(l,b,r,t); r1.zhou(l,b,r,t); } 请输入长方形的左下角的坐标: 2.1 3.2 请输入长方形的右上角的坐标: 5.2 6.3 长 方 形 的 左 下 角 的 坐 标 : 2.1 3.2 长 方 形 的 右 上 角 的 坐 标 : 5.2 6.3
2: #include<iostream> using namespace std; class tree { public:
tree(int a) {
ages=a; } void grow(int years); void age(); private: int ages; }; void tree::grow(int years) { cout<<"请输入年份:"; cin>>years; ages+=years; } void tree::age() { cout<<" 该 树 年 龄 为 : "<<ages<<endl; } int main() { tree t1(5); t1.age(); t1.grow(4); t1.age(); system("pause"); return 0; } 该树年龄为:5
Scomplex b )
{
Scomplex temp;
temp.real = a -
b.real;
temp.image
=
-b.image;
return temp;
}
Scomplex
operator-( int a, const
Scomplex b )
{
Scomplex temp;
temp.real = a -
b.real;
temp.image
=
-b.image;
return temp;
}
Scomplex
&Scomplex::operator=( con
st Scomplex &b )
{
if( this == &b )
{
return *this;
}
real = b.real;
image = b.image;
是 : "<<radius<<","<<" 面 积 是 :
"<<getarea();
}
};
int main() {
circle circle1; double radius; circle1.getvalue(); circle1.getarea(); circle1.output(); } 输入半径:2 半径是:2,面积是:12.56