剛學C++時實踐課上的代碼,要就的比較簡單,主要是熟悉一下類。介面也很不友好(估計除了我沒人能看懂這是幹啥的)。
/*設計一個用於人事管理的People(人員)類。考慮到通用性,這裡只抽象出所有類型人員都具有的屬性:number(編號),sex(性別),birthday(出生日期),id(社會安全號碼)等等。其中"出生日期"定義為一個"日期"類內嵌子物件。用成員函數實現對人員資訊的錄人和顯示。要求包括:建構函式和解構函式、拷貝建構函式、內聯成員函數、聚集。 定義一個Date類,包括year、month、day。在定義People類,包括number(編號),sex(性別),birthday(出生日期),id(社會安全號碼)。在main()函數中構造若干個People類對象,提供若干成員函數實現對人員資訊的錄入和顯示,另外每個類都要求有建構函式和解構函式。程式名:lab2_3.cpp。*/#include<iostream>using namespace std;class Date{private:int year;int month;int day;public:Date(){cout<<"Date is created"<<endl;}void input(){cin>>year>>month>>day;}void print(){ cout<<year<<"."<<month<<"."<<day<<"."<<endl;}~Date(){cout<<"Date is ruined"<<endl;}};class People{private:int number;int sex;Date birthday;int id;public:People(){ cout<<"Create a people"<<endl<<"Please input his number,sex(0 is man.1 is woman),id and birthday"<<endl;}People(People & a){cout<<"Copy a people"<<"\n";number=a.number;sex=a.sex;birthday=a.birthday;id=a.id; }void input(){ cin>>number>>sex>>id;birthday.input();}void print(){cout<<number<<endl<<sex<<endl<<id<<endl;birthday.print(); }~People(){cout<<"People is delete"<<endl;}};void main(){People people1;people1.input();people1.print();People people2(people1); people2.print(); }
程式運行如下,估計看著泛懵,好在代碼簡單