程序6.4

按照下图的类层次结构编写程序,定义属于score的对象c1以及类teacher的对象t1,分别输入每个数据成员的值后再显示出这些数据。



7411425-038e8cee4fe200fa.png


#include "stdafx.h"

#include

#include

using namespace std;


class person{

private:

   string name;

   int ID;

public:

   string getName() {return name;}

   int getID() {return ID;}

   person(string na, int I) : name(na), ID(I) {}

   virtual ~person(){}

};


class stud{

private:

   string addr;

   string tel;

public:

   string getAddr() {return addr;}

   string getTel() {return tel;}

   stud(string _addr, string _tel) :addr(_addr), tel(_tel) {}

   virtual ~stud() {}

};


class score{

private:

   float eng;

   float math;

public:

   score(float _eng, float _math) :eng(_eng), math(_math){}

   virtual ~score() {}

   float getEng() { return eng; }

   float getMath() { return math; }

   void print(){cout << "English:" << eng<<"    Math:"<

};


class teacher : public person{

private:

    int degree;

   string dep;

public:

   int getDegree() { return degree; }

   string getDep() { return dep; }

   void print(){cout << "Name:" << person::getName()<< "\tID:" << person::getID() <<"\tDegree:" << degree << "\tDepatrment:"<< dep << endl;}

   teacher(string na, int I, int _degree, string _dep) :person(na, I),degree(_degree), dep(_dep) {}

   virtual ~teacher() {}

};


class student : public person{

private:

   int old;

   int sno;

   stud info;

   score scores;

public:

   int getOld() { return old; }

   int getSno() { return sno; }

   student(string na, int I, int _old, int _sno, string _addr, string _tel,int _eng, int _math)

       :person(na, I), info(_addr, _tel), scores(_eng, _math), old(_old),sno(_sno) {}

   virtual ~student() {}

};


int main()

{

   score s1(120, 130);

   teacher tea("Wang wei", 10086, 2, "House");

   s1.print();

   tea.print();


   return 0;

}

转载于:https://www.jianshu.com/p/d69fbf41f6ce

猜你喜欢

转载自blog.csdn.net/weixin_33853794/article/details/91252769
6.4