*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhello * 完成日期:2016年5月8日 * 版本号:V1.0 * 问题描述:类的派生 * 程序输入:无 * 程序输出:见运行结果 */ #include <iostream> // 点类的实现 using namespace std; class Point { public: Point(double x=0,double y=0); //构造函数 void setPoint(double,double); //设置坐标值 double getX( ) const {return x;} //读x坐标 double getY( ) const {return y;} //读y坐标 void show(); protected: //受保护成员 double x,y; }; //Point的构造函数 Point::Point(double a,double b) { x=a; y=b; } //设置x和y的坐标值 void Point::setPoint(double a,double b) { x=a; y=b; } void Point::show() { cout<<"("<<x<<","<<y<<")"<<endl; } int main( ) { Point p(2.4,5.1); p.show(); p.setPoint(1.8,3.2); p.show(); return 0; }
运行结果:
#include <iostream> //圆类的实现 using namespace std; class Point { public: Point(double x=0,double y=0); //构造函数 void setPoint(double,double); //设置坐标值 double getX( ) const { return x; //读x坐标 } double getY( ) const { return y; //读y坐标 } void show(); protected: //受保护成员 double x,y; }; //Point的构造函数 Point::Point(double a,double b) { x=a; y=b; } //设置x和y的坐标值 void Point::setPoint(double a,double b) { x=a; y=b; } void Point::show() { cout<<"["<<x<<","<<y<<"]"<<endl; } class Circle:public Point //circle是Point类的公用派生类 { public: Circle(double x=0,double y=0,double r=0); //构造函数 void setRadius(double); //设置半径值 double getRadius( ) const; //读取半径值 double area ( ) const; //计算圆面积 void show(); protected: double radius; }; //定义构造函数,对圆心坐标和半径初始化 Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { } //设置半径值 void Circle::setRadius(double r) { radius=r; } //读取半径值 double Circle::getRadius( ) const { return radius; } //计算圆面积 double Circle::area( ) const { return 3.14159*radius*radius; } //输出圆的信息 void Circle::show() { cout<<"Center=("<<x<<", "<<y<<"), r="<<radius<<", area="<<area( )<<endl; } int main( ) { Circle c(3.1,5,6.7); cout<<"original circle:\n"; c.show(); c.setPoint(3,3); cout<<"new circle:\n"; c.show(); return 0; }
运行结果:
#include <iostream> //圆柱类的实现 using namespace std; class Point { public: Point(double x=0,double y=0); //构造函数 void setPoint(double,double); //设置坐标值 double getX( ) const { return x; //读x坐标 } double getY( ) const { return y; //读y坐标 } void show(); protected: //受保护成员 double x,y; }; //Point的构造函数 Point::Point(double a,double b) { x=a; y=b; } //设置x和y的坐标值 void Point::setPoint(double a,double b) { x=a; y=b; } void Point::show() { cout<<"["<<x<<","<<y<<"]"<<endl; } class Circle:public Point //circle是Point类的公用派生类 { public: Circle(double x=0,double y=0,double r=0); //构造函数 void setRadius(double); //设置半径值 double getRadius( ) const; //读取半径值 double area ( ) const; //计算圆面积 void show(); protected: double radius; }; //定义构造函数,对圆心坐标和半径初始化 Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { } //设置半径值 void Circle::setRadius(double r) { radius=r; } //读取半径值 double Circle::getRadius( ) const { return radius; } //计算圆面积 double Circle::area( ) const { return 3.14159*radius*radius; } //输出圆的信息 void Circle::show() { cout<<"Center=["<<x<<", "<<y<<"], r="<<radius<<", area="<<area( )<<endl; } class Cylinder:public Circle { public: Cylinder (double x=0,double y=0,double r=0,double h=0);//构造函数 void setHeight(double); //设置圆柱高 double getHeight( ) const; //读取圆柱高 double area( ) const; //计算圆柱表面积 double volume( ) const; //计算圆柱体积 void show(); protected: double height; //圆柱高 }; //定义构造函数 Cylinder::Cylinder(double a,double b,double r,double h) :Circle(a,b,r),height(h){} //设置圆柱高 void Cylinder::setHeight(double h) { height=h; } //读取圆柱高 double Cylinder::getHeight( ) const { return height; } //计算圆柱表面积 double Cylinder::area( ) const { return 2*Circle::area( )+2*3.14159*radius*height; } //计算圆柱体积 double Cylinder::volume() const { return Circle::area()*height; } void Cylinder::show() { cout<<"Center=["<<x<<","<<y<<"], r="<<radius<<", h="<<height <<"\narea="<<area( )<<", volume="<<volume( )<<endl; } int main( ) { Cylinder cy1(3.5,6.4,5.2,10); cout<<"\noriginal cylinder:\nx="; cy1.show(); cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); cout<<"\nnew cylinder:\n"; cy1.show(); return 0; }
运行结果:
时间: 2024-10-09 13:22:27