[RUNOOB]C++继承

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++ 类可以从多个类继承成员

程序:

执行结果:

猜你喜欢

转载自www.cnblogs.com/qfxlxbbx/p/9069484.html