c++之继承二

下面的实例是使用继承完成点、圆、圆柱体的层次结构

  1 #include <iostream>
  2 using namespace std;
  3 #define PI 3.1415926
  4
  5 class Point
  6 {
  7     friend ostream& operator << (ostream& output,Point&);
  8 public:
  9     Point(int x =0,int y =0);    //带默认参数的构造函数
 10     void setPoint(int x,int y);    //设置点坐标
 11     int getX();    //得到点的x轴坐标
 12     int getY();    //得到点的y轴坐标
 13 protected:    //成员函数,x,y轴的坐标
 14     int x,y;
 15 };
 16
 17 class Circle: public Point        //类Circle公有继承Point
 18 {
 19     friend ostream& operator << (ostream& output,Circle&);
 20 public:
 21     Circle(double r = 0.0,int x =0,int y =0);    //带默认参数的构造函数
 22     void setRadius(double r);    //设置圆的半径
 23     double getRadius();        //得到圆的半径
 24     double getArea();        //得到圆的面积
 25 protected:
 26     double radius;        //成员函数,半径
 27 };
 28
 29 class Cylinder:public Circle    //类Cylinder继承Circle
 30 {
 31     friend ostream& operator <<(ostream& output,Cylinder&);
 32 public:
 33     Cylinder(double height=0.0,double radius =0.0,int x=0,int y=0);        //带默认参数的构造函数
 34     void setHeight(double height);    //设置圆柱体高度值
 35     double getHeight();    //返回圆柱体的高度值
 36     double getArea();    //返回圆柱体的面积
 37     double getVolume();    //返回圆柱体的体积
 38 protected:
 39     double height;    //成员函数,圆柱体的高度
 40 };
 41
 42 //Point类的实现
 43 Point::Point(int x,int y)
 44 {
 45     this->x = x;
 46     this->y = y;
 47 }
 48 void Point::setPoint(int x,int y)
 49 {
 50     this->x = x;
 51     this->y = y;
 52 }
 53 int Point::getX()
 54 {
 55     return this->x;
 56 }
 57 int Point::getY()
 58 {
 59     return this->y;
 60 }
 61 ostream& operator <<(ostream& output,Point& point)
 62 {
 63     cout << "[point: x="<<point.getX()<<" ,"<<"y="<<point.getY() << "]" <<endl;
 64     return output;
 65 }
 66
 67 //Circle类的实现
 68 Circle::Circle(double radius,int x,int y):Point(x,y)
 69 {
 70     this->radius = radius;
 71 }
 72 void Circle::setRadius(double radius)
 73 {
 74     this->radius = radius;
 75 }
 76 double Circle::getRadius()
 77 {
 78     return this->radius;
 79 }
 80 double Circle::getArea()
 81 {
 82     return PI * radius * radius;
 83 }
 84 ostream& operator <<(ostream& output,Circle& circle)
 85 {
 86     cout << "[circle: x=" << circle.getX() << " ,y=" <<circle.getY() <<" ,radius=" << circle.getRadius() << " ,area=" <<circle.getArea() << "]"<<endl;
 87     return output;
 88 }
 89
 90 //类Cylinder的实现
 91 Cylinder::Cylinder(double height,double radius,int x,int y):Circle(radius,x,y)
 92 {
 93     this->height = height;
 94 }
 95
 96 void Cylinder::setHeight(double height)
 97 {
 98     this->height = height;
 99 }
100 double Cylinder::getHeight()
101 {
102     return this->height;
103 }
104 double Cylinder::getArea()
105 {
106     return 2*Circle::getArea()+2 * PI * radius * height;
107 }
108 double Cylinder::getVolume()
109 {
110     return Circle::getArea()*height;
111 }
112 ostream& operator <<(ostream& output,Cylinder& cylinder)
113 {
114     cout << "[cylinder: x=" << cylinder.getX() << " ,y=" <<cylinder.getY() << " ,height=" << cylinder.getHeight() <<" ,radius=" << cylinder.getRadius() << " ,area=" <<cylinder.getArea() <<" ,volume=" << cylinder.getVolume()<< "]"<<endl;
115     return output;
116 }
117
118 void main()
119 {
120     Point p ( 72, 115 ) ;        //定义点对象并初始化
121    cout << "The initial location of p is " << p << endl ;
122    p.setPoint ( 10, 10 ) ;        //置点的新数据值
123    cout << "\nThe new location of p is " << p << endl ;    //输出数据
124    Circle c ( 2.5, 37, 43 ) ;    //定义圆对象并初始化
125    cout<<"\nThe initial location and radius of c are\n"<<c<<"\nArea = "<<c.getArea()<<"\n" ;
126    //置圆的新数据值
127    c.setRadius ( 4.25 ) ;
128    c.setPoint ( 2, 2 ) ;
129    //输出圆心坐标和圆面积
130    cout<<"\nThe new location and radius of c are\n"<<c<<"\nArea = "<<c.getArea()<< "\n" ;
131    Cylinder cyl ( 5.7, 2.5, 12, 23 ) ;    //定义圆柱体对象并初始化
132    //输出圆柱体各数据和表面积,体积
133    cout << "\nThe initial location, radius ang height of cyl are\n" << cyl
134        << "Area = " << cyl.getArea() << "\nVolume = " << cyl.getVolume() << ‘\n‘;
135    //置圆柱体的新数据值
136    cyl.setHeight ( 10 ) ;
137    cyl.setRadius ( 4.25 ) ;
138    cyl.setPoint ( 2, 2 ) ;
139    cout << "\nThe new location, radius ang height of cyl are\n" << cyl
140        << "Area = " << cyl.getArea() << "\nVolume = "<<cyl.getVolume()<< "\n" ;
141 } 
时间: 2024-08-10 17:09:47

c++之继承二的相关文章

C++之继承(二)

目录 C++之继承(二) 一.多继承 二.重复继承 三.多重继承 C++之继承(二) 一.多继承 多继承是指一个子类继承多个父类.多继承对父类的个数没有限制,继承方式可以是公共继承.保护继承和私有继承, 不写继承方式,默认是private继承. #include <iostream> using namespace std; class B1{ public: B1(){cout<<"B1\n";} }; class B2{ public: B2(){cout&

【C++】浅谈三大特性之一继承(二)

三,继承方式&访问限定符 派生类可以继承基类中除了构造函数和析构函数之外的所有成员,但是这些成员的访问属性是由继承方式决定的. 不同的继承方式下基类成员在派生类中的访问属性: 举例说明: (1)public继承 eg1: #include <iostream> using namespace std; class Person { public://公有数据成员 int length;//身高 int weight;//体重 }; class Student:public Person

原型,原型对象,原型链,构造函数,继承(二)

1.prototype(原型对象)有一个属性叫做constructor,constructor默认指向prototype(原型对象)所在的构造函数 2.constructor属性是定义在prototype上的,那就意味着可以被实例对象所继承 3.可以使用hasOwnProperty 方法来验证一个属性是自己的还是继承过来的 4.constructor的作用: 作用一:分辨原型对象到底属于哪个构造函数 instanceof() 作用二:可以从一个实例对象创建另一个实例对象 var Fun = fu

java回顾之继承 二

调用父类构造器 子类不会获得父类构造器,但是子类构造器里面可以调用父类构造器初始化代码类似于一个构造器调用另外一个构造器: public class Box { String name; String color; String weight; public Box(String name,String color) { this.name=name; this.color=color; } public Box(String name,String color,String weight) {

C++ 类的继承二(赋值兼容性原则)

//赋值兼容性原则 #include<iostream> using namespace std; class PointA{ public: PointA(){ x = 0; y = 0; } void Set(){ } private: int x; int y; }; class PointB :public PointA{ public: private: int c; }; /* 赋值兼容性原则内部原理分析 在子类继承父类的时候,在子类的内存区域上,父类的元素会放在前面,子类的元素会

Js继承二

<html> <head> <title></title> <style> #div1{ width:200px; height:200px; background:yellow; position:absolute; } #div2{ width:200px; height:200px; background:green; position:absolute; } </style> <script> window.onl

Java三大特征之继承(二)

在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句话中最引人注目的是“复用代码”,尽可能的复用代码使我们程序员一直在追求的,现在我来介绍一种复用代码的方式,也是java三大特性之一---继承. 在讲解之前先看一个例子:这个例子还是上一篇博文[Java三大特征之封装(一)]中的. 从这里我们可以看出,Wife.Husband两个类除了各自的husband.wi

VC++之 继承(二)

C++多重继承的概念 由多个基类共同派生出派生类的继承结构称为多重继承或多继承(multiple-inheritance). 多重继承是从实际的需要产生的.例如:从大学在册人员产生学生和教职工.再从学生派生研究生.如果考虑到研究生可以当助教,那么他们又有了教职工的特性.教职工可分为教师和行政人员,但行政人员也可以去授课,兼有教师的特点等.这就是多继承,其继承关系如下图所示. 图 大学在册人员继承关系 多重继承下的歧义性问题及解决 问题1:教职工兼研究生,在其基类“教职工”中有一个“身份证号”,另

关于js的对象原型继承(二)

本章讨论使用new一个构造函数来创建一个对象. 前期知识点说明: 1.prototype是函数的一个属性,每个函数都有一个prototype属性.这个属性是一个指针,指向一个对象.它是显示修改对象的原型的属性. 2.__proto__是一个对象拥有的内置属性(请注意:prototype是函数的内置属性,__proto__是对象的内置属性),是JS内部使用寻找原型链的属性. 首先上代码: //构造函数 function Cat(name) { this.name=name||'某只猫'; this