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)
什么是面向对象编程,对于面向对象编程与面向过程编程的解释随处可见,个人认为对面向对象编程解释最好的一个定义是:依赖倒转原则是面向对象编程的标志,面向对象编程是一种思想,无论使用哪一种编程语言,如果在编 ...
随机推荐
- 手机端的META
一.天猫 <title>天猫触屏版</title> <meta content="text/html; charset=utf-8" http-equ ...
- POJ3466(01背包变形)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- 一道面试题:C++相比C#或者java的优势到底在哪里
被问到了这样一道面试题,当时就懵了,内心一直觉得C++肯定在很多方面要比C#或者java要牛b的. 但是真的不知道怎么回答. 问题是:你以前一直做得是.NET相关项目,现在为什么找C++开发相关工作呢 ...
- 【bzoj4810】由乃的玉米田
lxl丧心病狂-- 首先允许离线的区间询问一看就是莫队.那么我们看下怎么莫队? 不会. "由乃题多半是不可做的."于是我看了下题解--好吧果然是bitset 用bitset维护当前 ...
- 取消SecureCRT的右击粘贴功能
默认为选中时自动复制,右键粘贴 要取消的话在: Options->Global Options ...->Terminal 里面有个Mouse的选项块. Paste on Right/Le ...
- getResourceAsStream用法详解
//使用绝对路径,否则无法读取config.properties //InputStream inStream=new FileInputStream("F:\\android\\test\ ...
- MySQL插入中文数据报错
在操作数据库插入中文会出现如下错误: ERROR 1366 (HY000): Incorrect string value: '\xC4\xE3\xBA\xC3' for column 'userna ...
- The number of steps(概率dp)
Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one r ...
- Oralce聚合多行
拼接的字符串长度满足varchar2(4000)时, 可以用 LISTAGG(NAME, '_') WITHIN GROUP(ORDER BY LEVEL_T DESC) 当拼接大段文本时,采用 10 ...
- (2)SpringBoot 配置
一.SpringBoot全局配置文件 SpringBoot两个全局配置文件,application.properties和application.yml 例如端口号配置 (1)application ...