C++编程题,请教高手给个答案,非常感谢
人类person派生出学生类student和教师类teacher。人类有姓名和性别两个属性,student类有学号和成绩属性,teacher类有职称和教龄属性,相应的构造...
人类person派生出学生类student和教师类teacher。人类有姓名和性别两个属性,student类有学号和成绩属性,teacher类有职称和教龄属性,相应的构造函数均带有参数,冰在构造函数中初始化数据成员。student类和teacher类都有输出信息的成员函数Display()。为输出Student类和Teacher类相应的信息,设计类并编写主函数进行测试。
展开
展开全部
新建一个win32控制台空的工程,然后添加一个源文件,也就是.cpp文件。
然后把,下面代码考进去! 运行就可以了!
祝你好运啊!
#include <iostream>
#include <string>
using namespace std;
class Preson
{
public:
Preson(string name,int age);
string GetName(){return strName;}//获取name
int GetAge(){return iAge;}//获取age
private:
string strName;
int iAge;
};
Preson::Preson(string name,int age)
{
strName=name;
iAge=age;
}
class Student:public Preson
{
public:
Student(string name,int age,int number,double score);
int GetNumber(){return iNumber;}//获取学号
double GetScore(){return dScore;}//获取成绩
void display();
private:
int iNumber;
double dScore;
};
Student::Student(string name,int age,int number,double score)
:Preson(name,age)
{
iNumber=number;
dScore=score;
}
void Student::display()//现实学生信息
{
cout<<"I am a student"<<endl;
cout<<"姓名:"<<GetName()<<endl;
cout<<"年龄:"<<GetAge()<<endl;
cout<<"学号:"<<GetNumber()<<endl;
cout<<"成绩:"<<GetScore()<<endl;
}
class Teather:public Preson
{
public:
Teather(string name,int age,string post,int schoolage);
string GetPost(){return strPost;}
int GetSchoolAge(){return iSchoolAge;}
void display();
private:
string strPost;
int iSchoolAge;
};
Teather::Teather(string name,int age,string post,int schoolage)
:Preson(name,age)
{
strPost=post;
iSchoolAge=schoolage;
}
void Teather::display()//现实教师信息
{
cout<<"I am a teacher"<<endl;
cout<<"名字:"<<GetName()<<endl;
cout<<"年龄:"<<GetAge()<<endl;
cout<<"职位:"<<GetPost()<<endl;
cout<<"教龄:"<<GetSchoolAge()<<endl;
}
void main()
{
Student mys("tome",18,2011,95.2);
Teather myt("hell",45,"normal teacher",22);
mys.display();
myt.display();
}
然后把,下面代码考进去! 运行就可以了!
祝你好运啊!
#include <iostream>
#include <string>
using namespace std;
class Preson
{
public:
Preson(string name,int age);
string GetName(){return strName;}//获取name
int GetAge(){return iAge;}//获取age
private:
string strName;
int iAge;
};
Preson::Preson(string name,int age)
{
strName=name;
iAge=age;
}
class Student:public Preson
{
public:
Student(string name,int age,int number,double score);
int GetNumber(){return iNumber;}//获取学号
double GetScore(){return dScore;}//获取成绩
void display();
private:
int iNumber;
double dScore;
};
Student::Student(string name,int age,int number,double score)
:Preson(name,age)
{
iNumber=number;
dScore=score;
}
void Student::display()//现实学生信息
{
cout<<"I am a student"<<endl;
cout<<"姓名:"<<GetName()<<endl;
cout<<"年龄:"<<GetAge()<<endl;
cout<<"学号:"<<GetNumber()<<endl;
cout<<"成绩:"<<GetScore()<<endl;
}
class Teather:public Preson
{
public:
Teather(string name,int age,string post,int schoolage);
string GetPost(){return strPost;}
int GetSchoolAge(){return iSchoolAge;}
void display();
private:
string strPost;
int iSchoolAge;
};
Teather::Teather(string name,int age,string post,int schoolage)
:Preson(name,age)
{
strPost=post;
iSchoolAge=schoolage;
}
void Teather::display()//现实教师信息
{
cout<<"I am a teacher"<<endl;
cout<<"名字:"<<GetName()<<endl;
cout<<"年龄:"<<GetAge()<<endl;
cout<<"职位:"<<GetPost()<<endl;
cout<<"教龄:"<<GetSchoolAge()<<endl;
}
void main()
{
Student mys("tome",18,2011,95.2);
Teather myt("hell",45,"normal teacher",22);
mys.display();
myt.display();
}
展开全部
#ifdef _MSC_VER
#pragma once
#endif
#ifndef _PERSON_H
#define _PERSON_H
#include <string>
#include <tchar.h>
#include <iostream>
#define BASE
#define HIERACHE
using namespace std;
typedef unsigned int uint32;
class BASE CPerson
{
public:
CPerson(string name, int sex):m_szName(name), m_nsex(sex)
{
}
virtual ~CPerson() {}
virtual void Display() = 0;
protected:
std::string m_szName; // 名称
int m_nsex; // 性别
};
class HIERACHE CStudent:public CPerson
{
public:
CStudent(string name, int sex, int id, int score):CPerson(name, sex),m_nid(id), m_nScore(score)
{
}
~CStudent() {}
virtual void Display()
{
cout<<"姓名:"<<m_szName.c_str()<<endl;
cout<<"性别:"<<m_nsex<<endl;
cout<<"学号:"<<m_nid<<endl;
cout<<"成绩:"<<m_nScore<<endl;
}
protected:
uint32 m_nid; // 学号
uint32 m_nScore; // 成绩
};
class HIERACHE CTeacher:public CPerson
{
public:
CTeacher(string name, int sex, int lv, int years):CPerson(name, sex),m_nlev(lv), m_nyears(years)
{
}
~CTeacher() {}
virtual void Display()
{
std::cout<<"姓名:"<<m_szName.c_str()<<std::endl;
std::cout<<"性别:"<<m_nsex<<std::endl;
std::cout<<"职称:"<<m_nlev<<std::endl;
std::cout<<"教龄:"<<m_nyears<<std::endl;
}
protected:
uint32 m_nlev; // 职称
uint32 m_nyears; // 教龄
};
#endif
int main()
{
CTeacher obj1(_T("张老师"), 1, 12, 10);
CStudent obj2(_T("小明"), 1, 10001, 120);
obj1.Display();
obj2.Display();
return 0;
}
#pragma once
#endif
#ifndef _PERSON_H
#define _PERSON_H
#include <string>
#include <tchar.h>
#include <iostream>
#define BASE
#define HIERACHE
using namespace std;
typedef unsigned int uint32;
class BASE CPerson
{
public:
CPerson(string name, int sex):m_szName(name), m_nsex(sex)
{
}
virtual ~CPerson() {}
virtual void Display() = 0;
protected:
std::string m_szName; // 名称
int m_nsex; // 性别
};
class HIERACHE CStudent:public CPerson
{
public:
CStudent(string name, int sex, int id, int score):CPerson(name, sex),m_nid(id), m_nScore(score)
{
}
~CStudent() {}
virtual void Display()
{
cout<<"姓名:"<<m_szName.c_str()<<endl;
cout<<"性别:"<<m_nsex<<endl;
cout<<"学号:"<<m_nid<<endl;
cout<<"成绩:"<<m_nScore<<endl;
}
protected:
uint32 m_nid; // 学号
uint32 m_nScore; // 成绩
};
class HIERACHE CTeacher:public CPerson
{
public:
CTeacher(string name, int sex, int lv, int years):CPerson(name, sex),m_nlev(lv), m_nyears(years)
{
}
~CTeacher() {}
virtual void Display()
{
std::cout<<"姓名:"<<m_szName.c_str()<<std::endl;
std::cout<<"性别:"<<m_nsex<<std::endl;
std::cout<<"职称:"<<m_nlev<<std::endl;
std::cout<<"教龄:"<<m_nyears<<std::endl;
}
protected:
uint32 m_nlev; // 职称
uint32 m_nyears; // 教龄
};
#endif
int main()
{
CTeacher obj1(_T("张老师"), 1, 12, 10);
CStudent obj2(_T("小明"), 1, 10001, 120);
obj1.Display();
obj2.Display();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询