定义Point(点)类,由Point类派生出Circle(圆)类,再由Circle类派生出Cylinder(圆柱体)类。将类的定义部分分别作为3个头文件

定义Point(点)类,由Point类派生出Circle(圆)类,再由Circle类派生出Cylinder(圆柱体)类。将类的定义部分分别作为3个头文件,对它们的成员函数的声明部分分别作为3个源文件(.cpp文件),在主函数中用#include命令把它们包含进来,形成一个完整的程序,并上机运行。

头文件1:

#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;
class Point
{
	public:
	Point(float =0,float =0);
	void setPoint(float,float);
	float getX() const{return x;}
	float getY() const{return y;}
	friend ostream &operator<<(ostream &,const Point &);
	protected:
		float x,y;	
};
#endif
头文件2:

#ifndef CIRCLE_H
#define CIRCLE_H
#include"point.h"
using namespace std;
class Circle:public Point
{
	public:
		Circle(float x=0,float y=0,float r=0);
	void setRadius(float);
	float getRadius() const;
	float area() const;
	friend ostream &operator<<(ostream &,const Circle &);
		protected:
		float radius;
		
};
#endif
头文件3:

#ifndef CYLINDER_H
#define CYLINDER_H
#include<iostream>
#include"circle.h"
using namespace std;
class Cylinder:public Circle
{
	public:
		Cylinder(float x=0,float y=0,float r=0,float h=0);
	void setHeight(float);
	float getHeight() const;
	float area() const;
	float volume() const;
	friend ostream&operator<<(ostream&,const Cylinder&);
	protected:
		float height;
};
#endif

源文件1:

#include"point.h"
Point::Point(float a,float b)
{
	x=a;
	y=b;
}
void Point::setPoint(float a,float b)
{
	x=a;
	y=b;
}
ostream &operator<<(ostream &output,const Point &p)
{
	output<<"["<<p.x<<","<<p.y<<"]"<<endl;
	return output;
}
源文件2

#include"circle.h"
#include<iostream>
using namespace std;
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){
}
void Circle::setRadius(float r)
{
	radius=r;
}
float Circle::getRadius() const{return radius;}
float Circle::area() const
{
	return 3.14159*radius*radius;
}
ostream &operator<<(ostream &output,const Circle &c)
{
	output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area()<<endl;
	return output;
}
源文件3:

#include"cylinder.h"
#include<iostream>
Cylinder::Cylinder(float a,float b,float r,float h)
:Circle(a,b,r),height(h){
}
void Cylinder::setHeight(float h)
{
	height=h;
}
float Cylinder:: getHeight() const{return height;}
float Cylinder::area() const{
return 2*Circle::area()+2*3.14159*radius*height;};
	float Cylinder::volume() const
	{
		return Circle::area()*height;
	};
ostream&operator<<(ostream&output,const Cylinder&cy)
{
	output<<"Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height
	<<"\narea="<<cy.area()<<",volume="<<cy.volume()<<endl;
	return output;
}
主函数:

#include <iostream>
#include"point.h"
#include"circle.h"
#include"cylinder.h"
using namespace std;
int main()
{
Cylinder cy1(3.5,6.4,5.2,10);
cout<<"original cylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="
<<cy1.getHeight()<<"\narea="<<cy1.area()<<",volume="<<cy1.volume()<<endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5.5,6.4);
cout<<"\nnew cylinder:\n"<<cy1;
Point &pRef=cy1;
cout<<"\npRef as a point:"<<pRef;
Circle &cRef=cy1;
cout<<"\ncRef as a Circle:"<<cRef;
return 0;
}

original cylinder:
x=3.5,y=6.4,r=5.2,h=10
area=496.623,volume=849.486

new cylinder:
Center=[5.5,6.4],r=7.5,h=15
area=1060.29,volume=2650.72

pRef as a point:[5.5,6.4]

cRef as a Circle:Center=[5.5,6.4],r=7.5,area=176.714

--------------------------------
Process exited after 0.1994 seconds with return value 0
请按任意键继续. . .






猜你喜欢

转载自blog.csdn.net/w3071206219/article/details/52743000