1。編寫一個學生和教師資料的輸入和顯示程式,學生資料有編號、姓名、班號和成績,教師資料有編號、姓名、職稱和部門。要求將編號、姓名的輸入和顯示設計成一個類person,並作為學生資料操作類student和教師資料操作類teacher的基類。
2.建立一個基類building,含有保護成員floors、rooms和square,分別用來表示一座樓房的層數、房間數以及它的總面積。建立類building的派生類house,含有私有成員bedrooms,balcony,分別用來表示臥室與陽臺的數量。另外,建立類building的派生類office,含有私有成員phones和meeting_rooms,分別用來表示電話與會議室的數目。這兩個派生類都含有建構函式和show()函式,用於對資料成員進行初始化和顯示出這些資料。
1。程式程式碼:
#include
using namespace std;
class person
{
private:
string num;
string name;
public:
person(string n,string m);
void show();
};
person::person(string n,string m)
{
num=n;name=m;
}
void person::show()
{
cout< } class student { private: string class0; string grades; public: student(string n,string m,string class1,string g); void show(); person p; }; student::student(string n,string m,string class1,string g):p(n,m){ class0=class1;grades=g; } void student::show() { p。show(); cout< } class teacher { private: string title; string department; public: teacher(string n,string m,string t,string d ); void show(); person p; }; teacher::teacher(string n,string m,string t,string d):p(n,m) { title=t;department=d; } void teacher::show() { p。show(); cout< } int main() { cout<<“學生”< student stu1(“0524”,“shushu”,“2002”,“100”); stu1。show(); cout<<“教師”< teacher tea1(“0112”,“zs”,“wwwww”,“information”); tea1。show(); return 0; } 2。程式程式碼: #include using namespace std; class buliding { protected: int floor; int rooms; double square; public: buliding(int f,int r,double s){floor=f;rooms=r;square=s;} void show(){cout<<“層數”< }; class house:private buliding{ private: int bedrooms; int balcony; public: house(int f,int r,double s ,int be,int ba):buliding( f, r, s){ bedrooms=be;balcony=ba; } void show1(){show();cout<<“臥室”< }; class office:private buliding{ private: string phone; int metting_rooms; public: office(int f,int r,double s,string p,int m):buliding(f, r, s){ phone=p;metting_rooms=m; } void show2(){show();cout<<“電話”< }; int main() { house h1(18,60,2000。88,240,60); h1。show1(); office of1(10,34,3333。44,“18888”,33); of1。show2(); return 0; }