码迷,mamicode.com
首页 > 编程语言 > 详细

[RUNOOB]C++继承

时间:2018-05-21 22:51:40      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:int   oid   ima   ace   ref   ted   clu   ring   prot   

REF:

http://www.runoob.com/cplusplus/cpp-inheritance.html

 

一、基类和派生类

程序:

#include "stdafx.h"
#include <iostream>
//#include <string>
//#include <vector>
//#include <cctype>
//#include <cstring>

//using std::string;
//using std::vector;
//using std::isalpha;
//using std::cin;
using std::cout;
using std::endl;
using namespace std;

//基类
class Shape
{
public:
	void setWidth(int w)
	{
		width = w;
	}

	void setHeight(int h)
	{
		height = h;
	}

protected:
	int width;
	int height;
};

//派生类
class Rectangle :public Shape 
{
public:
	int getArea()
	{
		return (width*height);
	}

};

int main(void)
{
	Rectangle Rect;

	Rect.setWidth(5);
	Rect.setHeight(7);

	//输出对象面积
	cout << "Total area:" << Rect.getArea() << endl;

	return 0;
}

执行结果:

技术分享图片

二、访问控制和继承

技术分享图片

三、继承类型

技术分享图片

四、多继承

C++ 类可以从多个类继承成员

技术分享图片

程序:

 

执行结果:

 

[RUNOOB]C++继承

标签:int   oid   ima   ace   ref   ted   clu   ring   prot   

原文地址:https://www.cnblogs.com/qfxlxbbx/p/9069484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!