C++纯虚函数与抽象类
纯虚函数
抽象类
#include <iostream>
using namespace std;
class Shape
{
public:
virtual float area() const{return 0;}
virtual float volume() const{return 0;}
virtual void shapeName() const=0;
};
class Point:public Shape
{
public:
Point(float a=0,float b=0);
void setPoint(float a,float b);
float getX() const {return x;}
float getY() const {return y;}
virtual void shapeName() const {cout << "Points" << endl;}
friend ostream& operator << (ostream& output,Point& p);
protected:
float x,y;
};
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,Point& p)
{
output << "[" << p.getX() << "," << p.getY() << "]" << endl;
return output;
}
class Circle:public Point
{
public:
Circle(float a,float b,float r):Point(a,b),radius(r){}
void setRadius(float r);
float getRadius() const {return radius;}
virtual float area() const ;
virtual void ShapeName() const {cout << "Circle" << endl;}
friend ostream& operator << (ostream& output,Circle &c);
protected:
float radius;
};
void Circle::setRadius(float r)
{
radius=r;
}
float Circle::area() const
{
return 3.14159*radius*radius;
}
ostream& operator << (ostream& output,Circle &c)
{
output << "Center [" << c.x << "," << c.y << "] radius=" << c.radius
<< " area=" << c.area() << endl;
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}
void setHeight(float h){height=h;}
float getHeight() const{return height;}
virtual float area() const;
virtual float volume() const;
virtual void ShapeName() const {cout << "Cylinder" << endl;}
friend ostream& operator << (ostream& output,Cylinder& cy);
protected:
float 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,Cylinder& cy)
{
output << "Center [" << cy.getX() << "," << cy.getY() << "] r="
<< cy.radius << "\narea=" << cy.area() << " volume=" << cy.volume()
<< endl;
return output;
}
int main()
{
Point p(1,2);
Circle cir(1,2,3);
Cylinder cy(1,2,3,4);
cout <<"Cylinder\n" << "Center [" << cy.getX() << "," << cy.getY() << "] r="
<< cy.getRadius() << "\narea=" << cy.area() << " volume=" << cy.volume()
<< endl;
cy.setHeight(1);
cy.setPoint(2,3);
cy.setRadius(4);
cout << "Cylinder\n" << cy << endl;
Point &px=cy;
cout << "Point\n" << px;
Circle &c=cy;
cout << "Circle\n" << c;
Shape *pt=&p;
pt->shapeName();
cout << "area=" << pt->area() << " volume=" << pt->volume() << endl;
pt=○
pt->shapeName();
cout << "area=" << pt->area() << " volume=" << pt->volume() << endl;
pt=&cy;
pt->shapeName();
cout << "area=" << pt->area() << " volume=" << pt->volume() << endl;
return 0;
}
1.6一个基类如果包括一个或一个以上的纯虚函数,就是抽象基类。抽象基类不能也不必要定义对象。
C++纯虚函数与抽象类相关推荐
- c++ 纯虚函数和抽象类那些事(一)
1.纯虚函数与抽象类 C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数! 纯虚函数:没有函数体的虚函数 抽象类:包含纯虚函数的类 /*** @br ...
- C++纯虚函数和抽象类
基本概念 纯虚函数和抽象类 纯虚函数是一个在基类中说明的虚函数,但是在基类中没有定义,要求任何派生类都定义自己的版本 纯虚函数为个派生类提供一个公共界面(接口的封装和设计.软件模块功能的划分) 纯虚函 ...
- C++基本概念复习之二:多重继承、虚继承、纯虚函数(抽象类)
一.多重继承: #include <iostream> using namespace std; class Horse { public: Horse(){cout<<&qu ...
- C++之纯虚函数和抽象类
纯虚函数和抽象类 1.基本概念 2.案例 #include <iostream> using namespace std;////面向抽象类编程(面向一套预先定义好的接口编程)//解耦合 ...
- 9-2:C++多态之纯虚函数和抽象类以及接口继承和实现继承
文章目录 (1)纯虚函数和抽象类的概念 (2)抽象类的意义 (3)接口继承与实现继承 (1)纯虚函数和抽象类的概念 如果一个类的虚函数后面写上=0,同时不写它的实现,那么这样的虚函数称之为纯虚函数,包 ...
- C++ 虚函数,纯虚函数,抽象类整理
抽象类,类中包含纯虚函数的为抽象类,其中抽象类的子类必须实现抽象类的纯虚函数方法. 抽象类无法实例化 虚函数,子类可以实现或者不实现该方法都可以 如果父类调用子类的基类指针时,有虚函数的则使用子类的实 ...
- c/c++入门教程 - 2.4.7 多态、函数地址晚绑定(重写,虚函数,纯虚函数,抽象类,虚析构,纯虚析构)
目录 4.7 多态 4.7.1 多态的基本概念(超级重要) 4.7.2 多态的原理刨析(超级重要) 4.7.2 多态案例一:计算器类 4.7.3 纯虚函数和抽象类 4.7.4 多态案例二 - 制作饮品 ...
- C++ 纯虚函数与抽象类
1.虚函数 1.1 虚函数简介 可以毫不夸张地说虚函数是C++最重要的特性之一,我们先来看一看虚函数的概念. 在基类的定义中,定义虚函数的一般形式为: virtual 函数返回值类型 虚函数名(形参表 ...
- cc32a_demo-32dk2j_cpp_纯虚函数与抽象类-txwtech
//32dk2j_cpp_纯虚函数与抽象类cc32a_demo-txwtech //纯虚函数是用来继承用的 //纯虚函数 //抽象类-抽象数据类型 //*任何包含一个或者多个纯虚函数的类都是抽象类 / ...
最新文章
- 基于Annotation的输入校验
- jittor和pytorch生成网络对比之wgan_gp
- node工程默认url_node.js处理url常用方法
- centos6.5 搭建NFS 服务
- AUTOSAR从入门到精通100讲(三十九)-AUTOSAR 通信服务-Com模块两部曲-概念详解
- [css] 请使用css画一个圆,方法可以多种
- 链路层到网络层的数据传递
- 【R】函数-数学函数
- Cython基础--Cython入门
- lua数据结构php,Lua数据结构
- 会不会导致内存泄漏_使用ThreadLocal不当可能会导致内存泄露
- python-socket模块基本用法收集
- 电脑、手机装机必备软件的抉择[土豆-侃天下]
- Centos linux 修改ip地址
- 边缘计算的发展和应用前景
- SPSS软件中常见的统计分析方法有哪些
- Mysql,姓名按笔画排序
- 别忘了看,今年中秋月亮“瘦”了!网友扎心了:我还没瘦…
- pytorch张量相乘matmul函数
- 企业级大数据项目【1】需求分析及概要设计篇
热门文章
- 彩虹浏览器使用技巧:页签自动切换
- jeecg 查看 页面 父子表 横向滚动条效果 官方被禁用
- vue-cli打包后怎么修改服务器地址实践有效
- springboot 关于 Class path contains multiple SLF4J bindings.警告的解决
- keras训练完以后怎么预测_农村小孩只有户口,没有承包地,以后怎么养老?看完我安心了...
- 计算机仿真在电力领域的应用,仿真技术在电力系统中的应用实例
- json tcl_确定TCL中变量的类型
- python绘制饼图程序_python使用Matplotlib绘制饼图
- Java常用设计模式————桥接模式
- php瀑布墙,phpwind Next增加“瀑布流”图墙展示模式