// 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