当前位置:
文档之家› 第10章C语言程序设计习题答案
第10章C语言程序设计习题答案
circle c(3);
cout<<c.GetArea()<<endl;
cout<<c.GetPerimeter()<<endl;
}
9.定义一个坐标类,有横坐标和纵坐标两个属性及输出坐标位置成员函数,重载“+”使其可以对两个坐标类对象进行求和。
#include<iostream.h>
class coordinate
{
public:
cylinder(float r,float h)
{
radius=r;
height=h;
}
float Volume();
private:
float radius;
float height;
};
float cylinder::Volume()
{
return 3.14*radius*radius*height;
⑶、当类的继承方式为私有(private)继承时,基类的公有(public)成员和保护(protected)成员将成为派生类的私有成员,而基类的私有成员不能被派生类访问。
5.定义一个圆柱体类,其属性为圆柱体的底面半径和高,能计算出圆柱体的体积。
#include<iostream.h>
class cylinder
{
private:
int x_axis;
int y_axis;
public:
coordinate(int x=0,int y=0)
{
x_axis=x;
y_axis=y;
}
void PrintCoordinate()
{
cout<<x_axis<<","<<y_axis<<endl;
}
friend coordinate operator +(coordinate a,coordinate b)
{
return width*height;
}
float GetPerimeter()
{
return 2*(width+height);
}
};
class circle:public shape
{
private:
float radius;
public:
circle(float r)
{
radius=r;
}
cout<<"p1与p2两点之间的距离为:"<<endl;
p1.show(); //输出p1坐标
cout<<"->";
p2.show(); //输出p2坐标
cout<<"="<<p1-p2<<endl;//输出两点之间的距离
Circle c1(0,0,1); //定义Circle类对象c1
Circle c2(3,0,2); //定义Circle类对象c2
}
class cone:public cylinder
{
public:
float Volume();
cone(float r,float h):cylinder(r,h){};
};
float cone::Volume()
{
return cylinder::Volume()/3.0;
}
void main()
}
void main()
{
float r,h;
cout<<"请输入圆柱体的底面半径和高:";
cin>>r>>h;
cylinder x(r,h);
cout<<x.Volume()<<endl;
}
6.从第5题中定义的圆柱体类中派生出圆锥类,覆盖计算体积的成员函数。
#include<iostream.h>
cout<<"两圆相切"<<endl;
if((cr1.GetCenter()-cr2.GetCenter())<(cr1+cr2))
cout<<"两圆相交"<<endl;
}
void main()
{
Point p1(1,1); //定义Point类对象p1
Point p2(4,5); //定义Point类对象p2
x1.PrintCoordinate();
x2.PrintCoordinate();
w=x1+x2;
w.PrintCoordinate();
}
10.创建一个Point类,用来描述坐标系中的一个点。点具有横坐标和纵坐标两个属性,同时应具有成员函数用来显示点的坐标。再创建一个Circle类,用来描述坐标系中的一个圆。圆应具有圆心坐标和半径两个属性,还应具有得到圆心坐标和显示圆的属性等相关的成员函数。由于圆心是一个点(Point类),所以在创建Circle类时可以从Point类继承。
float GetPerimeter();
};
class rectangle:public shape
{
private:
float width;
float height;
public:
rectangle(float w,float h)
{
width=w;
height=h;
}
float GetArea()
}
};
void Crelation(Circle cr1,Circle cr2)
{ //定义函数说明两圆的关系
if((cr1.GetCenter()-cr2.GetCenter())>(cr1+cr2))
cout<<"两圆相离"<<endl;
if((etCenter()-cr2.GetCenter())==(cr1+cr2))
4.简述公有继承、私有继承和保护继承三种继承方式的区别。
⑴、当类的继承方式为公有(public继承)时,基类的公有(public)成员和保护(protected)成员仍然成为派生类的公有成员和保护成员,而基类的私有成员不能被派生类访问。
⑵、当类的继承方式为保护(protected)继承时,基类的公有(public)成员和保护(protected)成员将成为派生类的保护成员,而基类的私有成员不能被派生类访问。
构造函数的作用就是在对象在被创建时利用特定的值构造对象,将对象初始化。析构函数的作用与构造函数正好相反,它是用来在对象被删除前进行一些清理工作。析构函数调用之后,对象被撤消了,相应的内存空间也将被释放。
3.简述什么是友元函数。
友元函数是在类定义中由关键字friend修饰的非成员函数。友元函数可以是一个普通函数,也可以其它类中的一个成员函数,它不是本类的成员函数,但它可以访问本类的私有成员和保护成员。
class cylinder
{
public:
cylinder(float r,float h)
{
radius=r;
height=h;
}
float Volume();
private:
float radius;
float height;
};
float cylinder::Volume()
{
return 3.14*radius*radius*height;
{
float r,h;
cout<<"请输入圆锥体的底面半径和高:";
cin>>r>>h;
cone x(r,h);
cout<<x.Volume()<<endl;
}
7.定义一个生日(birthday)类,有年、月、日三个数据成员和输出生日成员函数。
#include<iostream.h>
class birthday
{
private:
double x,y; //私有成员(点的坐标)
public:
Point(){} //构造函数
Point(double px,double py) //构造函数
{x=px;y=py;}
void show() //成员函数(显示坐标)
{
cout<<"("<<x<<","<<y<<")";
}
friend double operator-(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
//"-"运算符重载
//返回两点之间的距离
};
class Circle:public Point //从Point类派生Circle类
{
private:
Point p;
double r; //私有成员
public:
Circle(double px,double py,double cr):Point(px,py)
{ //派生类的构造函数
p=Point(px,py);
r=cr;
}
void show() //覆盖基类的成员函数
{
cout<<"[p";