c++派生类问题
发布网友
发布时间:2023-09-20 15:00
我来回答
共3个回答
热心网友
时间:2024-09-19 14:51
#include <iostream>
#include <string>
using namespace std;
class people{
public:
people(string name1,string sex1,int age1)
{
name=name1;
sex=sex1;
age=age1;
}
void display()
{
cout<<"姓名:"<<name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"年龄:"<<age<<endl;
}
protected:
string name;
string sex;
int age;
};
class teacher:public people{
public:
teacher(string name1,string sex1,int age1,int gnumber1,string cheng1,float money1):people(name1,sex1,age1)
{
gnumber=gnumber1;
cheng=cheng1;
money=money1;
}
void display()
{
people::display();
cout<<"工号:"<<gnumber<<endl;
cout<<"职称:"<<cheng<<endl;
cout<<"工资:"<<money<<endl;
}
private:
int gnumber;
string cheng;
float money;
};
class student:public people{
public:
student(string name1,string sex1,int age1,int s1,int b1,string m1,float s2):people(name1,sex1,age1)
{
snumber=s1;
banji=b1;
major=m1;
score=s2;
}
void display()
{
people::display();
cout<<"学号:"<<snumber<<endl;
cout<<"班级:"<<banji<<endl;
cout<<"专业:"<<major<<endl;
cout<<"入学成绩:"<<score<<endl;
}
private:
int snumber;
int banji;
string major;
float score;
};
int main()
{
teacher t1("崔金宝","男",31,01,"软件工程方向教授",5100.02);
t1.display();
student s1("毛线团团","女",21,02,03,"软件工程",501.11);
s1.display();
return 0;
}
调试通过.
热心网友
时间:2024-09-19 14:51
Class Base
{
char* name;
char sex;
int age;
};
class Teacher:public Base
{
char* No;
char* title;
float salary;
};
class Student:public Base
{
char* id;
int class;
char* major;
float grade;
};
热心网友
时间:2024-09-19 14:52
//People.h file
class People
{
char name[20];
char sex;
int age;
public:
People();
People(char *name,char sex,int age);
void set_name(char s);
void get_name(char s);
void set_sex(char c);
char get_sex();
void set_age(int n);
int get_age();
};
class Teacher:public People
{
char number[20];
char position[20];
float wage;
public:
Teacher();
void set_number(char *number);
void get_number(char *number);
void set_position(char *position);
void get_position(char *position);
void set_wage(float w);
float get_wage();
};
class Student:public People
{
char number[20];
int class_number;
char profession[20];
float score;
public:
Student();
void set_number(char *number);
void get_number(char *number);
void set_class_number(int n);
int get_number();
void set_profession(char *profession);
void get_profession(char *profession);
void set_score(float s);
float get_score();
};
//People.cpp file
#include <stdio.h>
#include "People.h"
People::People()
{
sex = 'm';
age = 0;
}
People::People(char *name,char sex,int age)
{
strcpy(this->name,name);
this->sex = sex;
this->age = age;
}
void People::set_name(char *name)
{
strcpy(this->name,name);
}
void People::get_name(char *name)
{
strcpy(name,this->name);
}
void People::set_sex(char sex)
{
this->sex = sex;
}
char People::get_sex()
{
return sex;
}
void People::set_age(int age)
{
this->age = age;
}
int People::get_age()
{
return age;
}
Teacher::Teacher()
{
wage = 0.0;
}
void Teacher::set_number(int number)
{
this->number = number;
}
int Teacher::get_number()
{
return number;
}
void Teacher::set_position(char *position)
{
strcpy(this->position,position);
}
void Teacher::get_position(char *position)
{
strcpy(posiotion,this->position);
}
void Teacher::set_wage(float wage)
{
this->wage = wage;
}
float Teacher::get_wage()
{
return wage;
}
Student::Student()
{
class_number = 0;
score = 0.0;
}
void Student::set_number(char *number)
{
strcpy(this->number,number);
}
void Student::get_number(char *number)
{
strcpy(number,this->number);
}
void Student::set_class_number(int class_number)
{
this->class_number = class_number;
}
int Student::get_class_number()
{
return class_number;
}
void Student::set_profession(char *profession)
{
strcpy(this->profession,profession);
}
void Student::get_profession(char *profession)
{
strcpy(profession,this->profession);
}
void Student::set_score(float score)
{
this->score = score;
}
float Student::get_score()
{
return score;
}
//main.cpp file
#include <stdio.h>
#include "People.h"
int main()
{
Teacher teacher[10];
Student student[200];
//其他的操作
return 0;
}