cpp面向对象编程
如下图,先建好文件, 这里用的是Visual studio 2010

当然也可以用eclipse for cpp,如下图:

AbstractShape.h
#ifndef ABSTRACTSHAPE_H_
#define ABSTRACTSHAPE_H_
/**
* 抽象形状类
*/
class AbstractShape
{
private:
//私有字段
int edge; public:
//构造函数
AbstractShape(int edge); //实例方法,子类继承后可以重用
int getEdge(); //纯虚函数,父类没有实现,调用时只会调用子类的实现
virtual int calcArea()=;
}; #endif /* ABSTRACTSHAPE_H_ */
AbstractShape.cpp
#include "AbstractShape.h" AbstractShape::AbstractShape(int edge)
{
this->edge = edge;
} int AbstractShape::getEdge()
{
return this->edge;
}
Triangle.h
#include "AbstractShape.h"
#ifndef TRIANGLE_H_
#define TRIANGLE_H_ /**
* 三角形类,继承自抽象形状类
*/
class Triangle: public AbstractShape
{
private:
//私有字段
int bottom;
int height; public:
//构造函数
Triangle(int bottom, int height); //重写父类同名方法,用于实现多态性
int calcArea();
}; #endif /* TRIANGLE_H_ */
Triangle.cpp
#include "AbstractShape.h"
#include "Triangle.h" Triangle::Triangle(int bottom, int height) :
AbstractShape()
{
this->bottom = bottom;
this->height = height;
} int Triangle::calcArea()
{
return this->bottom * this->height / ;
}
Rectangle.h
#include "AbstractShape.h"
#ifndef RECTANGLE_H_
#define RECTANGLE_H_ /**
* 矩形类,继承自形状类
*/
class Rectangle: public AbstractShape
{
private:
//私有字段
int bottom;
int height; public:
//构造函数
Rectangle(int bottom, int height); //重写父类同名方法,用于实现多态性
int calcArea();
}; #endif /* RECTANGLE_H_ */
Rectangle.cpp
#include "AbstractShape.h"
#include "Rectangle.h" Rectangle::Rectangle(int bottom, int height) :
AbstractShape()
{
this->bottom = bottom;
this->height = height;
} int Rectangle::calcArea()
{
return this->bottom * this->height;
}
Main.cpp
#include "AbstractShape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include <iostream>
using namespace std; int main()
{
Triangle triangle = Triangle(, );
cout << triangle.getEdge() << endl;
cout << triangle.calcArea() << endl; Rectangle rectangle = Rectangle(, );
cout << rectangle.getEdge() << endl;
cout << rectangle.calcArea() << endl; return ;
}
编译运行,运行结果:
cpp面向对象编程的更多相关文章
- C++ Primer 学习笔记_72_面向对象编程 --句柄类与继承[续]
面向对象编程 --句柄类与继承[续] 三.句柄的使用 使用Sales_item对象能够更easy地编写书店应用程序.代码将不必管理Item_base对象的指针,但仍然能够获得通过Sales_item对 ...
- ndk学习之c++语言基础复习----面向对象编程
关于面向对象编程对于一个java程序员那是再熟悉不过了,不过对于C++而言相对java还是有很多不同点的,所以全面复习一下. 类 C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程 ...
- uda 4.C++面向对象编程
Python vs C++ 对比课 在本课中,你将学习如何用 C++ 编写类.像以前的课程一样,你需要比较 Python 的编程方式和 C++ 中编程方式的不同. 我们直接看例子.下面是一个名为 ...
- angular2系列教程(六)两种pipe:函数式编程与面向对象编程
今天,我们要讲的是angualr2的pipe这个知识点. 例子
- 带你一分钟理解闭包--js面向对象编程
上一篇<简单粗暴地理解js原型链--js面向对象编程>没想到能攒到这么多赞,实属意外.分享是个好事情,尤其是分享自己的学习感悟.所以网上关于原型链.闭包.作用域等文章多如牛毛,很多文章写得 ...
- PHP 面向对象编程和设计模式 (1/5) - 抽象类、对象接口、instanceof 和契约式编程
PHP高级程序设计 学习笔记 2014.06.09 什么是面向对象编程 面向对象编程(Object Oriented Programming,OOP)是一种计算机编程架构.OOP 的一条基本原则是计算 ...
- Delphi_09_Delphi_Object_Pascal_面向对象编程
今天这里讨论一下Delphi中的面向对象编程,这里不做过多过细的讨论,主要做提纲挈领的描述,帮助自己抓做重点. 本随笔分为两部分: 一.面向对象编程 二.面向对象编程详细描述 ------------ ...
- python基础-面向对象编程
一.三大编程范式 编程范式即编程的方法论,标识一种编程风格 三大编程范式: 1.面向过程编程 2.函数式编程 3.面向对象编程 二.编程进化论 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 ...
- 面向对象编程(OOP)
什么是面向对象编程,对于面向对象编程与面向过程编程的解释随处可见,个人认为对面向对象编程解释最好的一个定义是:依赖倒转原则是面向对象编程的标志,面向对象编程是一种思想,无论使用哪一种编程语言,如果在编 ...
随机推荐
- java List排序 顺序 倒序 随机
List list = new LinkedList(); for ( int i = 0 ; i < 9 ; i ++ ) { list.add( " a " + i); ...
- 【Mysql优化】索引碎片与维护
在长期的数据更改过程中, 索引文件和数据文件,都将产生空洞,形成碎片.(不停的删除修改导致) 解决办法: (1)我们可以通过一个nop操作(不产生对数据实质影响的操作), 来修改表. 比如: 表的引擎 ...
- Appium===Appium+Python API(转)
Appium+python自动化8-Appium Python API 前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts contex ...
- try_module_get和module_put【转】
转自:http://blog.csdn.net/adaptiver/article/details/7000617 转自:http://apps.hi.baidu.com/share/detail/4 ...
- linux环境下的GUN make学习笔记(一)
第一章:概述 1.1:make概述 在linux环境下使用make工具能够比较容易的构建一个属于自己的工程,整个工程的编译只需要一个命令就可以完成编译.连接以至于最后的执行.不过我们需要投入一些时间去 ...
- nodejs的包管理器npm和cnpm
http://www.ydcss.com/archives/18 3.npm介绍 3.1.说明:npm(node package manager)nodejs的包管理器,用于node插件管理(包括安装 ...
- 【LeedCode】String to integer(atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- matlab保存图片成eps格式不全,导致latex中图片显示不全的问题
我们经常会遇到这样的问题.用将matlab生成的图保存EPS格式后,用GSVIEW打开后,可以看到图片显示不全.遇到这种情况是,我们可以使用dvipdf——dvips的方法来生成PDF,这样生成的pd ...
- 杀掉TOMCAT并重启的脚本
/usr/local/tomcat7/bin/shutdown.sh sleep #具体时间就看你得webapp在调用shutdown.sh后多久后处于僵死状态 ps -ef | grep sleep ...
- (五)cobbler自定义系统安装
注意:需要提前获取到物理机对应的网卡的MAC地址,例如我这里使用虚拟机进行演示 cobbler system add --name=linux-node2.com --mac=00:50:56:22: ...