習題 11.1 將例11.1的程式片斷補充和改寫成一個完整、正確的程式,用公用繼承方式。在程式中應包括輸入資料的函式,在程式執行時輸入num,name,sex,age,addr的值,程式應輸出以上。。
阿新 • • 發佈:2018-12-10
C++程式設計(第三版) 譚浩強 習題11.1 個人設計
習題 11.1 將例11.1的程式片斷補充和改寫成一個完整、正確的程式,用公用繼承方式。在程式中應包括輸入資料的函式,在程式執行時輸入num,name,sex,age,addr的值,程式應輸出以上5個數據的值。
程式碼塊:
#include <iostream> #include <string> using namespace std; class Student { public: void get_value(){ cout<<"Please enter num, name, sex: "; cin>>num>>name>>sex; } void display(){ cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; } private: int num; string name; char sex; }; class Student1: public Student { public: void get_value1(){ get_value(); cout<<"Please enter age, address: "; cin>>age>>addr; } void display_1(){ display(); cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl; } private: int age; string addr; }; int main() { Student1 stud; stud.get_value1(); stud.display_1(); system("pause"); return 0; }