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开发环境及其开发
成功安装之后,进行测试是否真的成功安装,点击[开始]----[运行]----输入 CMD,在命令提示符里面输入"Java -version"并按回车键,出现下图,即为安装成功. 选 ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 分享三个USB抓包软件---Bus Hound,USBlyzer 和-USBTrace【转】
转自:http://bbs.armfly.com/read.php?tid=15377 Bus Hound官方下载地址:http://perisoft.net/bushound/ Bus Hound ...
- 【转】mybatis循环map的一些技巧
原文地址:http://blog.csdn.net/linminqin/article/details/39154133 循环key: <foreach collection="con ...
- JMeter 分布式测试部署
对于并发量很大的需求,如上万并发量,受到CPU和内存的限制,单机模拟场景是实现不了的,为了让JMeter提供更大的负载能力,须使用它的分布式机制,即多台机器同时产生负载的功能. 以下参数分析可用于配置 ...
- Spring:与Redis的集成
一个月没写过博客了,一直想记录一下之前学习的Redis的有关知识,但是因为四月太过于慵懒和忙碌,所以一直没有什么机会,今天就来讲讲,如何使用Spring当中的Spring-data-redis去与Re ...
- 走进Spark--云计算大数据新一代技术
什么是Spark? 当然这里说的Spark指的是Apache Spark, Apache Spark™ is a fast and general engine for large-scale dat ...
- Codeforces 626 B. Cards (8VC Venture Cup 2016-Elimination Round)
B. Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces 1059E. Split the Tree
题目:http://codeforces.com/problemset/problem/1059/E 用倍增可以在nlog内求出每个节点占用一个sequence 时最远可以向父节点延伸到的节点,对每个 ...
- ASP.NET Core 2.2 基础知识(十) Web服务器 - Kestrel
ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...