1.目的:

/*设计一个计算图形面积的类库。
类库的顶层是一个抽象类,并且提供三个纯虚函数;显示数据成员、返回面积和返回体积。
Class Shape
{
virtual void showData()=0;
virtual double reArea()=0;
virtual double reVolume()=0;
};
第二层由Shape类派生TwoDimShape(二维图形)和ThreeShape(三维图形),
它们增加了有关的数据成员,但没有成员函数的实现。
第三层派生具体的图形类。TwoDimShape类派生Circle(圆)、Elipse(椭圆)、
Rectangle(矩形)和Triangle(三角形)等类。
ThreeShape类派生Ball(球体)、Cylinder(圆柱体)、
RectangularParallelepiped(长方体)等类。
在主函数测试中使用多态方式调用不同对象的求值函数。
*/

2.代码如下:

/*
*This file contains code for C++ 6th experiment
*By LZH
*/
#include<iostream>
#include<string>
using namespace std;
const double PI = acos(-1.0);
// Definition of Shape class and related functions goes here
class Shape
{
virtual void showData() = 0;
virtual double reArea() = 0;
virtual double reVolume() = 0;
};
class TwoDimShape :virtual public Shape {
protected:
double x, y;
public:
virtual void showData() {
return ;
}
virtual double reArea() {
return 0;
}
virtual double reVolume() {
return 0;
}
};
class ThreeShape :virtual public Shape {
protected:
double x, y, z;
public:
virtual void showData() {
return;
}
virtual double reArea() {
return 0;
}
virtual double reVolume() {
return 0;
}
};
class Circle :virtual public TwoDimShape {
public:
Circle(double tp) {
x = tp;
}
Circle(Circle &asp) {
x = asp.x;
}
~Circle()
{ }
void showData() {
cout << "This is a Circle:" << endl
<< "The radiation:" << x << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return PI*x*x;
}
};
class Elipse :virtual public TwoDimShape {
public:
Elipse(double ta, double tb) {
x = ta, y = tb;
}
Elipse(Elipse &asp) {
x = asp.x, y = asp.y;
}
~Elipse() { }
void showData() {
cout << "This is a Elipse:" << endl
<< "The long axis:" << x << endl
<< "The short axis:" << y << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return PI*x*y;
}
};
class Rectangle :virtual public TwoDimShape {
public:
Rectangle(double ta, double tb) {
x = ta, y = tb;
}
Rectangle(Rectangle &asp) {
x = asp.x, y = asp.y;
}
~Rectangle() { }
void showData() {
cout << "This is a Rectangle:" << endl
<< "The long axis:" << x << endl
<< "The short axis:" << y << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return x*y;
}
};
class Triangle :virtual public TwoDimShape {
public:
Triangle(double ta, double tb) {
x = ta, y = tb;
}
Triangle(Triangle &asp) {
x = asp.x, y = asp.y;
}
~Triangle() { }
void showData() {
cout << "This is a Triangle:" << endl
<< "The base length:" << x << endl
<< "The height :" << y << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return x*y / 2.0;
}
};
class Ball :virtual public ThreeShape {
public:
Ball(double ta) {
x = ta;
}
Ball(Ball &asp) {
x = asp.x;
}
~Ball() { }
void showData() {
cout << "This is a Ball:" << endl
<< "The radiation:" << x << endl
<< "The surface area:" << reArea() << endl;
}
double reArea() {
return PI*pow(x, 3)*4.0 / 3.0;
}
double reVolume() {
return PI*x*x;
}
};
class Cylinder :virtual public ThreeShape {
public:
/*
V=PI*r*r*h S=2*PI*r+r*h
*/
Cylinder(double ta, double tb) {
x = ta, y = tb;
}
Cylinder(Cylinder &asp) {
x = asp.x, y = asp.y;
}
~Cylinder() { }
void showData() {
cout << "This is a Cylinder:" << endl
<< "The radiation:" << x << endl
<< "The height:" << y << endl
<< "The surface area:" << reArea() << endl;
}
double reArea() {
return 2 * PI*x + x*y;
}
double reVolume() {
return PI*x*x*y;
}
};
//RectangularParallelepiped
class cuboid :virtual public ThreeShape {
public:
cuboid(double ta, double tb, double tc) {
x = ta, y = tb, z = tc;
}
cuboid(cuboid &asp) {
x = asp.x, y = asp.y, z = asp.z;
}
void showData() {
cout << "This is a cuboid:" << endl
<< "The length:" << x << endl
<< "The width:" << y << endl
<< "The height" << z << endl
<< "The surface area:" << reArea() << endl; }
double reArea() {
return 2 * (x*y + x*z + y*z);
}
double reVolume() {
return x*y*z;
}
};
int main(void) {
TwoDimShape a;
ThreeShape b;
TwoDimShape *p = &a;
ThreeShape *w = &b;
Circle t1(1.0);
Elipse t2(1.0, 2.0);
Rectangle t3(10.0,2.3);
Triangle t4(4.0, 5.0);
Ball t5(2.33333);
Cylinder t6(4.5, 65.0);
cuboid t7(132, 5,156);
p = &t1;
p->showData();
p = &t2;
p->showData();
p = &t3;
p->showData();
p = &t4;
p->showData();
w = &t5;
w->showData();
w = &t6;
w->showData();
w = &t7;
w->showData();
return 0;
}

3 . 测试截图



4.关于多态性

 在这个例子中我用了基类指针指向基类,这个不难理解,在类型兼容规则下,
指向基类的指针可以隐式的转换成派生类的指针。
这是最常见的关于多态的用法,利用该指针指向任意一个子类对象,
就可以调用相应的虚函数,指向的子类的不同,实现的方法也就不同。

C++继承与多态练习--计算图形面积的更多相关文章

  1. C#基础总结之八面向对象知识点总结-继承与多态-接口

    .方法深入讲解(返回值,形参与实参) 方法 public int getName(int i,int j) { int sum = i + j; return sum; } .利用泛型存储对象数据 . ...

  2. C++封装、继承、多态

    C++封装继承多态总结 面向对象的三个基本特征 面向对象的三个基本特征是:封装.继承.多态.其中,封装可以隐藏实现细节,使得代码模块化:继承可以扩展已存在的代码模块(类):它们的目的都是为了--代码重 ...

  3. JavaScript 定义类的最佳写法——完整支持面向对象(封装、继承、多态),兼容所有浏览器,支持用JSDuck生成文档

    作者: zyl910 [TOC] 一.缘由 由于在ES6之前,JavaScript中没有定义类(class)语法.导致大家用各种五花八门的办法来定义类,代码风格不统一.而且对于模拟面向对象的三大支柱& ...

  4. 《JAVA程序设计与实例》记录与归纳--继承与多态

    继承与多态 概念贴士: 1. 继承,即是在已经存在的类的基础上再进行扩展,从而产生新的类.已经存在的类成为父类.超类和基类,而新产生的类成为子类或派生类. 2. Java继承是使用已存在的类的定义作为 ...

  5. Java进阶篇(一)——接口、继承与多态

    前几篇是Java的入门篇,主要是了解一下Java语言的相关知识,从本篇开始是Java的进阶篇,这部分内容可以帮助大家用Java开发一些小型应用程序,或者一些小游戏等等. 本篇的主题是接口.继承与多态, ...

  6. c# 中的封装、继承、多态详解

    面向对象有封装.继承.多态这三个特性,面向对象编程按照现实世界的特点来管理复杂的事物,把它们抽象为对象,具有自己的状态和行为,通过对消息的反应来完成任务.这种编程方法提供了非常强大的多样性,大大增加了 ...

  7. Day7 初识面向对象,面向对象之继承、多态和封装

    一.面向对象引言 一.面向对象的程序设计的由来 详述见:http://www.cnblogs.com/linhaifeng/articles/6428835.html 二.面向对象引子 写一个简单程序 ...

  8. Java编程的逻辑 (15) - 初识继承和多态

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  9. Java 继承和多态

                                                        Java  继承和多态 Java 继承 继承的概念 继承是java面向对象编程技术的一块基石,因 ...

随机推荐

  1. PURGE 的用法说明

    PURGE PurposeUse the PURGE statement to remove a table or index from your recycle bin and release al ...

  2. .NET中怎么有效的使用Cache

    Cache 即高速缓存 ,我想非常多人对他的第一印象一定像我相同,感觉他一定能提高系统得性能和运行速度.的确.Net推出cache的初衷确实是这样的.那么cache是怎么提高系统性能和运行速度呢?是不 ...

  3. 准备你的 In-app Billing 程序

    准备好你的内购应用 在开始使用 In-app Billing 服务之前,你需要先把包含 In-app Billing Version 3 API 的库添加到你的Android工程中.你还需要设置你的应 ...

  4. 沉淀,再出发:VUE的简单理解

    沉淀,再出发:VUE的简单理解 一.前言 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过 ...

  5. August 18th 2017 Week 33rd Friday

    If you shed tears when you miss the sun, you also miss the stars. 如果你因为错过太阳而哭泣,你也将会错过繁星. If you have ...

  6. php中的html元素

    我们先看下面的代码 form2.php <html> <head><title>greetins eartyling</title></head& ...

  7. 关于 Can't connect to MySQL server on 'localhost' (10061) 的一个解决方案

    问题描述: 使用Navicat for mysql 无法远程连接到本地数据库,提示Can't connect to MySQL server on 'localhost' (10038) . 本地服务 ...

  8. 如何实现本机Windows连接虚拟机中的CentOS

    1.确定CentOS的IP地址,命令为 ifconfig,由此可知,LinuxIP地址为 192.168.85.128 2.WIndows的IP地址为192.168.16.1, 3.保证CentOS和 ...

  9. MySQL5.7 不同操作系统下的主从配置

    1. 服务器信息 1.1 Ubuntu 17.0.4  (Master服务器) MySQL版本: 5.7.20 主数据库:dslbcp IP: 192.168.12.130 3306 1.2 Wind ...

  10. 深入理解webpack打包机制

    一.单入口文件如何打包 /src/single/index.js var index2 = require('./index2'); var util = require('./util'); con ...