c++编程题?
发布网友
发布时间:2022-05-06 21:24
我来回答
共2个回答
热心网友
时间:2023-09-19 19:21
#include <iostream.h>
#include<string.h>
class student
{
public:
void get(char m[20],char *p)
{
strcpy(name,m);
id=p;
}
protected:
char name[20];
char *id;
};
class MajorStudent:public student // 类MajorStudent是类Student的公有派生类
{
public:
void fun(char ip[25],int c)
{
Malor=ip;
cost=c;
}
void print()
{
cout<<"该学生的ID为:"<<id<<endl;
cout<<"该学生的名字为:"<<name<<endl;
cout<<"该学生的专业为:"<<Malor<<endl;
cout<<"该学生的费用为:"<<cost<<endl;}
protected:
char *Malor;
int cost;
};
void main()
{
char p2[20],a[25],m[20];
int c1;
MajorStudent stu1;
cout<<"请输入学生名字:";
cin>>m;
cout<<"请输入学生的ID:";
cin>>p2;
cout<<"请输入学生专业:";
cin>>a;
cout<<"请输入学生费用:";
cin>>c1;
stu1.get(m,p2);
stu1.fun(a,c1);
stu1.print();
}
这个程序我测试过了,绝对正确!!!
热心网友
时间:2023-09-19 19:21
class Student
{
public:
Student(char aName[], char* anId)
{
strcpy(name, aName);
id = anId;
}
char* getName() const
{
return (char*)name;
}
char* getId() const
{
return id;
}
private:
char name[10];
char* id;
};
class MajorStudent: public Student
{
public:
MajorStudent(char* name, char* id, char* theMajor)
: Student(name, id)
{
major = theMajor;
}
int getCost() const
{
return cost;
}
char* getMajor() {
return major;
}
private:
char* major;
int cost;
};