第六章第一题(其余类似上次派生继承题)
来源:互联网 发布:开淘宝用什么软件好 编辑:程序博客网 时间:2024/06/05 04:20
Zhixin.cpp:#include<iostream>#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);cout << "\n new cylinder : \n " << cy1;Point & pRef = cy1;cout << "\n nRef as a Point: " << pRef;Circle & cRef = cy1;cout << "\n cRef as a Circle: " << cRef;return 0;}Point.cpp:#include<iostream>#include"Point.h"using namespace std;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;}Circle.cpp:#include<iostream>#include"Circle.h"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;}Cylinder.cpp:#include<iostream>#include"Cylinder.h"using namespace std;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 << "\n area = " << cy.area()<< ", volume = " << cy.volume() << endl;return output;}Point.h:#include<iostream>using namespace std;class Point{public:Point(float x = 0, float y = 0);void setPoint(float , float);float getX() const {return x;}float getY() const {return y;}friend ostream & operator << (ostream & , const Point &);float x ,y ;};Circle.h:#include<iostream>#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 &);float radius;};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&);float height;};
0 0
- 第六章第一题(其余类似上次派生继承题)
- 第六章第一题
- 自考-C++—第六章继承和派生
- 第六章数组 第一题
- c++第六章第一题
- C++第六章作业其余部分
- C++ Primer Plus(第六版) 第八章 第一题
- C++Primer Plus(第六版) 第十二章 第一题
- C++Primer Plus(第六版) 第十三章 第一题
- C++Primer Plus(第六版) 第十四章 第一题
- C++Primer Plus(第六版) 第十六章 第一题
- C++第六章习题第一题
- 第十一章 继承和派生(一)
- 第十一章 继承和派生(二)
- python 第六周第一题
- 6w4:第六周程序填空题1(虚函数与派生)
- 第六章第六题
- 第三章 继承和派生
- 【华为OJ】求int型数据在内存中存储时1的个数
- iOS-项目开发,细节,报错,处理(3)
- ubuntu 修改环境变量导致的错误 登录失败
- 贪心算法初练
- Jquery 知识点总结
- 第六章第一题(其余类似上次派生继承题)
- 用位运算实现四则运算之加减乘除(用位运算求一个数的1/3)
- C++第六章第五题
- android 电子词典
- Android系统Intent,系统Action调用大全
- 7-2
- 常见邮件服务器(接收服务器和发送邮件服务器)地址
- Direct-X学习笔记--光照&材质&DX自带几何体绘制
- 7-3