#pragma once
#include <iostream>
using namespace std;
class Person
{
public:
Person(char *name, bool sex);
~Person();
public:
void SetName(char *name); //设置姓名
char* Getname(); //获取姓名
void SetSex(bool sex); //设置性别
bool GetSex(); //获取性别
void DisplayInfo(); //信息显示
private:
char m_name[20]; //姓名
bool m_sex; //性别,1—男,0—女
};
#include "stdafx.h"
#include "Person.h"
#include <iostream>
using namespace std;
Person::Person(char *name, bool sex)
{
strcpy_s(m_name, name);
m_sex = sex;
cout << "Person类构造函数被调用!" << endl;
}
Person::~Person()
{
cout << "Person类析构函数被调用!" << endl;
}
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;
}
void Person::DisplayInfo() //信息显示
{
cout << "个人信息:" << endl
<< "姓名:" << m_name << endl
<< "性别:";
if (m_sex == true)
cout << "男" << endl;
else
cout << "女" << endl;
}
#pragma once
#include "Person.h"
class Teacher : public Person //派生于Person
{
public:
Teacher(char *tno, char *name,bool sex, char *depart );
~Teacher();
public:
void SetTNO(char *tno); //设置学生学号
char* GetTNO(); //获取学生学号
void SetDepart(char *depart); //设置系
char* GetDepart(); //获取系
private:
char m_tno[6]; //教师号
char m_depart[20]; //系
};
#include "stdafx.h"
#include "Teacher.h"
Teacher::Teacher(char *tno, char *name, bool sex, char *depart) :Person(name,sex)
{
strcpy_s(m_tno, tno);
strcpy_s(m_depart, depart);
cout << "Teacher类构造函数被调用!" << endl;
}
Teacher::~Teacher()
{
cout << "Teacher类析构函数被调用!" << endl;
}
void Teacher::SetTNO(char *tno) //设置学生学号
{
strcpy_s(m_tno, tno);
}
char* Teacher::GetTNO() //获取学生学号
{
return m_tno;
}
void Teacher::SetDepart(char *depart) //设置系
{
strcpy_s(m_depart, depart);
}
char* Teacher::GetDepart() //获取系
{
return m_depart;
}
#pragma once
#include "Person.h"
#include <iostream>
using namespace std;
class Student : public Person //表示Student派生于Person类
{
public:
Student(char *sno, char *name, bool sex, char *major);
~Student();
public:
void SetSNO(char *sno); //设置学生学号
char* GetSNO(); //获取学生学号
void SetMajor(char *major); //设置专业信息
char* GetMajor(); //获取专业信息
void DisplayInfo();
private:
char m_sno[20]; //学号
char m_major[20]; //专业
};
#include "stdafx.h"
#include "Student.h"
#include <iostream>
using namespace std;
Student::Student(char *sno, char *name, bool sex, char *major) :Person(name, sex)
{
strcpy_s(m_sno, sno);
strcpy_s(m_major, major);
cout << "Student类构造函数被调用!" << endl;
}
Student::~Student()
{
cout << "Student类析构函数被调用!" << endl;
}
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() //重定义基类中的DisplayInfo() 函数
{
cout << "学生信息:" << endl
<< "学号:" << m_sno << endl
<< "姓名:" << Getname() << endl
<< "性别:";
if (GetSex() == true)
cout << "男" << endl;
else
cout << "女" << endl;
cout << "专业:" << m_major << endl;
}
#pragma once
#include "Student.h"
#include"Teacher.h"
class TA : public Student, public Teacher
{
public:
TA(char *sno, char *name, bool sex, char *majoe, char *tno, char *depart);
~TA();
};
#include "stdafx.h"
#include "TA.h"
TA::TA(char *sno, char *name, bool sex, char *major, char *tno, char *depart)
:Teacher(tno, name, sex, depart), Student(sno,name,sex,major)
{
cout << "TA 类构造函数被调用" << endl;
}
TA::~TA()
{
cout << "TA 类析构函数被调用" << endl;
}
// PaishengExample.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include"Student.h"
#include"Person.h"
#include "TA.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Student stu("20220501", "大苹果", true, "计算机工程");
//stu.DisplayInfo();
//Person person("123456", true); //Person(char *name, bool sex);
//person.DisplayInfo();
//stu.SetSNO("1111100");
//stu.SetName("小燕子");
//stu.SetSex(0); //女
//stu.SetMajor("人工智能");
//stu.DisplayInfo();
//测试多继承类TA
TA ta("20220501", "变形金刚", true, "计算机应用", "09110", "计算机科学与技术系");
getchar();
return 0;
}
运行结果: