栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 面向对象 - 类的继承与派生

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 面向对象 - 类的继承与派生

第一题:公有继承 —— 学生信息类

#include "people.h"     // People 类定义在这里面
#include 
#include 

using namespace std;


//公有继承 People
class Student:public People
{
	public:
		int SID;
        void  PrintSID();
};


void Student::PrintSID()
{
    
    //输出 SID
    
    cout<<"学号:"<SID=sid;
    ptr->Name=name;
    
    
    
}
第2题:保护继承 —— 学生信息类
#include "people.h"     // People 类定义在这里面
#include 
#include 
using namespace std;


//保护继承 People
class Student:protected People
{
	public:
		int SID;
		void PrintSID();
    	//添加一个 Set 函数来设置父类的 Name 成员
    friend	void Set(int id,string name,Student *ptr);

};


void Student::PrintSID()
{
    
    //输出学号 SID
    cout<<"学号:"<SID=id;
    ptr->Name=name;
    
    
    
}

第三题:研究生信息类

#include "people.h" //People类定义在这里面
#include 
#include 
using namespace std;


//私有继承 People 类
class Student :private People
{
	public:
		int SID;
		void PrintSID();
		//添加一个 Set 函数来设置父类的 Name 成员
		 void SetName(string name);
};


void Student::SetName(string name)
{
    Name=name;
}

void Student::PrintSID()
{
    
    //输出学号 SID
    cout<<"学号:"<SID=sid;
    ptr->ResearchID=rid;
    ptr->Set(name);
    
    
    
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976875.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号