C++继承与多态练习--计算图形面积
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++继承与多态练习--计算图形面积的更多相关文章
- C#基础总结之八面向对象知识点总结-继承与多态-接口
.方法深入讲解(返回值,形参与实参) 方法 public int getName(int i,int j) { int sum = i + j; return sum; } .利用泛型存储对象数据 . ...
- C++封装、继承、多态
C++封装继承多态总结 面向对象的三个基本特征 面向对象的三个基本特征是:封装.继承.多态.其中,封装可以隐藏实现细节,使得代码模块化:继承可以扩展已存在的代码模块(类):它们的目的都是为了--代码重 ...
- JavaScript 定义类的最佳写法——完整支持面向对象(封装、继承、多态),兼容所有浏览器,支持用JSDuck生成文档
作者: zyl910 [TOC] 一.缘由 由于在ES6之前,JavaScript中没有定义类(class)语法.导致大家用各种五花八门的办法来定义类,代码风格不统一.而且对于模拟面向对象的三大支柱& ...
- 《JAVA程序设计与实例》记录与归纳--继承与多态
继承与多态 概念贴士: 1. 继承,即是在已经存在的类的基础上再进行扩展,从而产生新的类.已经存在的类成为父类.超类和基类,而新产生的类成为子类或派生类. 2. Java继承是使用已存在的类的定义作为 ...
- Java进阶篇(一)——接口、继承与多态
前几篇是Java的入门篇,主要是了解一下Java语言的相关知识,从本篇开始是Java的进阶篇,这部分内容可以帮助大家用Java开发一些小型应用程序,或者一些小游戏等等. 本篇的主题是接口.继承与多态, ...
- c# 中的封装、继承、多态详解
面向对象有封装.继承.多态这三个特性,面向对象编程按照现实世界的特点来管理复杂的事物,把它们抽象为对象,具有自己的状态和行为,通过对消息的反应来完成任务.这种编程方法提供了非常强大的多样性,大大增加了 ...
- Day7 初识面向对象,面向对象之继承、多态和封装
一.面向对象引言 一.面向对象的程序设计的由来 详述见:http://www.cnblogs.com/linhaifeng/articles/6428835.html 二.面向对象引子 写一个简单程序 ...
- Java编程的逻辑 (15) - 初识继承和多态
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- Java 继承和多态
Java 继承和多态 Java 继承 继承的概念 继承是java面向对象编程技术的一块基石,因 ...
随机推荐
- ssm 注解@ResponseBody 返回json 乱码问题
方法一:在@RequestMapping上加入 produces方法 @RequestMapping(value = "/upload.do",method = RequestMe ...
- numpy数组的创建
创建数组 创建ndarray 创建数组最简单的方法就是使用array函数.它接收一切序列型的对象(包括其他数组),然后产生一个新的含有传入数据的Numpy数组. array函数创建数组 import ...
- mac下 IDEA 的pom下 出现 Cannot access in offline mode 问题
在mac下 配置完maven后发现总是不能引入最新的jar包,google了好久总算找到解决办法: 默认带有work offline ,不清楚这个是干嘛用的.勾选掉 了就行了.
- [翻译] VICMAImageView
VICMAImageView https://github.com/vitoziv/VICMAImageView Change image view's content mode with your ...
- Git小抄
生存必备 分支 忽略已提交的文件 版本回退 撤销修改 删除文件 修改最后一次提交的说明信息 tag 生存必备 git init git add <file1> <file2> ...
- Linux命令--文件处理
touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. ls -l 可以显示档案的时间记录. 语法 touch [-a ...
- SQL Server系统常用存储过程
SQL Server系统存储过程也是好几百个,算了,还是写几个常用的. 1.sp_help 查询表的信息 执行存储过程: sp_help Person 显示结果如下: 妈了个B,有了这张图,你还不懂怎 ...
- Github文件夹下载到本地
1.如图:需要将以下文件夹下载到本地. https://github.com/aspnet/Docs/tree/master/aspnet/mvc/overview/getting-started/i ...
- 内网渗透中的mimikatz
0x00 前言 上篇测试了中间人攻击利用框架bettercap,这次挑选一款更具代表性的工具--mimikatz 0x01 简介 mimikatz,很多人称之为密码抓取神器,但在内网渗透中,远不止这么 ...
- facebook开源的代码审核工具phabricator
主页地址:http://phabricator.org/