C++小练习--继承



main.h 

#include<string>
#include<iostream>
using namespace std;
#include "Person.h"
#include "Student.h"
#include "Teacher.h"
int main(){
	//Person类
	Person p1=Person("包子",21);
	p1.print();
	//学生类
	Student s1=Student("包子",21,"计算机",60);
	s1.print();
	//教师类
	Teacher t1=Teacher("包子",21,"计算机",6000);
	t1.print();


	return 0;
}


Person.h

#ifndef PERSON_H
#define PERSON_H 0
#include<string>
#include<iostream>
using namespace std;
//基类 Person
class Person{
protected:
	string name;
	int num;
public:
	Person(string name,int num);
	string getName();
	int getNum();
	void print();
	~Person(){};
};
#endif


Student.h

#ifndef STUDENT_H
#define STUDENT_H 0
#include "Person.h"
//学生类 Student
class Student:private Person{
protected:
	string zhuanye;
	int ave;
public:
	Student(string name,int num,string zhuanye,int ave):Person(name,num){
		this->zhuanye=zhuanye;
		this->ave=ave;
	}
	void print();
	~Student(){};
};
#endif


Teacher.h

#ifndef TEACHER_H
#define TEACHER_H
#include "Person.h"
//教师类 Teacher
class Teacher:private Person{
protected:
	string zhicheng;
	int salary;
public:
	Teacher(string name,int num,string zhicheng,int salary):Person(name,num){
		this->zhicheng=zhicheng;
		this->salary=salary;
	}
	void print();
	~Teacher(){};
};
#endif


P_Function.cpp

#include "Person.h"
#include "main.h"
//构造函数
Person::Person(string name,int num){
	this->name=name;
	this->num=num;
};
//返回name
string Person::getName(){
	return this->name;
}
//返回num
int Person::getNum(){
	return this->num;
}
//输出
void Person::print(){
	cout<<"姓名为:"<<name<<"     "<<"编号为:"<<num<<endl;
}


S_Function.cpp

#include "Student.h"
#include "Person.h"
#include <iostream>
using namespace std;

void Student::print(){
	cout<<"姓名为:"<<this->getName()<<"     "<<"编号为:"<<this->getNum()<<"     "<<"专业为:"<<zhuanye<<"     "<<"平均成绩为:"<<ave<<endl;
}


T_Function.cpp

#include "Person.h"
#include "Teacher.h"

void Teacher::print(){
	cout<<"姓名为:"<<this->getName()<<"     "<<"编号为:"<<this->getNum()<<"     "<<"职称为:"<<zhicheng<<"     "<<"工资为:"<<salary<<endl;
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值