您的位置:首页 > 编程语言 > C语言/C++

c++ 父类和子类的继承

2018-01-11 11:37 169 查看
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
//下面定义一个父类
class guozhu{
protected:
int age;
public :
void setAge(int va);
int getAge();
guozhu(int va){
age = va;
}
};
void guozhu::setAge(int va){
age = va;
}
int guozhu::getAge(){
return age;
}

//下面定义一个子类
class haitao :public guozhu{
public:
haitao(int mm) :guozhu(mm){}
int getNianLing(){
return age; //由于age是protected的,因此对子类可见
}
};

int _tmain(int argc, _TCHAR* argv[])
{
haitao tt(52);
cout <<tt.getAge() <<"    "<<tt.getNianLing()<< endl;

system("pause");
return 0;
}


上面代码,程序运行结果如下:



FR:海涛高软(hunk Xu ) QQ技术交流群:386476712
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: