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

C++:派生示例,Person类派生Student类

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

C++:派生示例,Person类派生Student类

#pragma once

#include 
using namespace std;
class Person
{
public:
	Person();
	~Person();
public:
	void SetName(char *name);		//设置姓名
	char* Getname();				//获取姓名
	void SetSex(bool sex);			//设置性别
	bool GetSex();					//获取性别
private:
	char m_name[20];				//姓名
	bool m_sex;						//性别,1—男,0—女

};

#include "stdafx.h"
#include "Person.h"


#include 
using namespace std;
Person::Person()
{
}


Person::~Person()
{
}
void Person::SetName(char *name)		//设置姓名
{
	strcpy_s(m_name, name);
}
char* Person::Getname()				//获取姓名
{
	return m_name;
}
void Person::SetSex(bool sex)			//设置性别
{
	m_sex = sex;
}
bool Person::GetSex()
{
	return m_sex;
}
#pragma once
#include "Person.h"
#include 
using namespace std;
class Student :	public Person		//表示Student派生于Person类
{
public:
	Student();
	~Student();
public:
	void SetSNO(char *sno);			//设置学生学号
	char* GetSNO();					//获取学生学号
	void SetMajor(char *major);		//设置专业信息
	char* GetMajor();				//获取专业信息
	void DisplayInfo();
private:
	char m_sno[8];					//学号
	char m_major[20];				//专业


};

#include "stdafx.h"
#include "Student.h"


#include 
using namespace std;
Student::Student()
{
}


Student::~Student()
{
}
void Student::SetSNO(char *sno)			//设置学生学号
{
	strcpy_s(m_sno, sno);
}
char* Student::GetSNO()					//获取学生学号
{
	return m_sno;
}
void Student::SetMajor(char *major)		//设置专业信息
{
	strcpy_s(m_major, major);
}
char* Student::GetMajor()				//获取专业信息
{
	return m_major;
}
void Student::DisplayInfo()
{
	cout << "学生信息:" << endl
		<< "学号:" << m_sno << endl
		<< "姓名:" << Getname() << endl
		<< "性别:";
	if (GetSex() == true)
		cout << "男" << endl;
	else
		cout << "女" << endl;
	cout << "专业:" << m_major << endl;
}
// PaishengExample.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"Student.h"
#include"Person.h"
#include 
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	Student stu;
	stu.SetSNO("1111100");
	stu.SetName("小燕子");
	stu.SetSex(0);	//女
	stu.SetMajor("人工智能");
	stu.DisplayInfo();
	
	getchar();
	return 0;
}

运行结果:

 

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

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

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