a c++ program of derived class
#include"iostream"
#include"string"
using namespace std;
class Student
{
public:
Student(string no,string name);
Student();
void show_infor();
protected:
string stu_no;
string stu_name;
};
Student::Student(string no,string name):stu_no(no),stu_name(name)
{
//Bode intentionally empty
}
Student::Student():stu_no("No number yet"),stu_name("No name yet")
{
//Body intentionally empty
}
void Student::show_infor()
{
cout<<"student number "<<stu_no<<endl;
cout<<"student name "<<stu_name<<endl;
}
class English_stu:public Student
{
private:
string depart;
double score;
public:
English_stu(string dep,double sco);
English_stu();
void show_infor(Student& stu);
};
English_stu::English_stu(string dep,double sco):depart(dep),score(sco)
{
//Body intentionally empty
}
English_stu::English_stu():depart("No department yet"),score(0)
{
//Body intentionally empty
}
void English_stu::show_infor(Student& stu)
{
cout<<"student department "<<depart<<endl;
cout<<"student score "<<score<<endl;
stu.show_infor();
}
int main()
{
string x,y;
cout<<"Please enter the student's name"<<endl;
cin>>x;
cout<<"Please enter the student's number"<<endl;
cin>>y;
Student stu_1(x,y),stu_2;
stu_1.show_infor();
stu_2.show_infor();
string a;
double b;
cout<<"Please enter the English student's depart"<<endl;
cin>>a;
cout<<"Please enter the English student's score"<<endl;
cin>>b;
English_stu estu_1(a,b),estu_2;
estu_1.show_infor(stu_1);
estu_2.show_infor(stu_2);
return 0;
}