Week8.[Inheritance]Virtual class

Virtual class

Description

設計一個基類base,包含姓名和年齡私有數據成員以及相關的成員函數,由它派生出領導類leader,包含職務和部門私有數據

成員以及相關的成員函數,再由base派生出工程師類engineer,包含職稱和專業私有成員以及相關的成員函數。然後由leader

和engineer類派生出主任工程類chairman,採用一些數據測試。

基類:

class base

{

char *name;

int age;

public:

base(){}

void setname(char arr[]);

void setage(int age);

char* getname();

int getage();

};

提交時,需要提交類base、leader、engineer、chairman的定義與實現;

主函數如下:

int main()

{

//freopen("in.txt", "r", stdin);

//freopen("out.txt", "w", stdout);

int n;

cin>>n;

for (int i=0;i<n;i++)

{

char name[20],job[20],dep[20],major[20],prof[20];

int age;

cin>>name>>age>>job>>dep>>major>>prof;

chairman man;

man.setname(name);

man.setage(age);

man.setjob(job);

man.setdep(dep);

man.setmajor(major);

man.setprof(prof);

cout<<"name: "<<man.getname()<<" age: "<<man.getage()<<" dep: "<<man.getdep()

<<" job: "<<man.getjob()<<endl;

cout<<"prof: "<<man.getprof()<<" major: "<<man.getmajor()<<endl;

}

return 0;

}

#include<iostream>#include<string.h>#include<cstring>#include <fstream>using namespace std;#include "source.h"int main(){ //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int n; cin>>n; for (int i=0;i<n;i++) { char name[20],job[20],dep[20],major[20],prof[20]; int age; cin>>name>>age>>job>>dep>>major>>prof; chairman man; man.setname(name); man.setage(age); man.setjob(job); man.setdep(dep); man.setmajor(major); man.setprof(prof); cout<<"name: "<<man.getname()<<" age: "<<man.getage()<<" dep: "<<man.getdep() <<" job: "<<man.getjob()<<endl; cout<<"prof: "<<man.getprof()<<" major: "<<man.getmajor()<<endl; } return 0;}#include <iostream>#include <string>using namespace std;/********define base********/class base{ char* name; int age;public: base(){ name = new char[20]; } ~base(){ delete [] name; } void setname(char arr[]) { //strcpy完成後,name指針指向內存中一處字元串 strcpy(name, arr); //所屬類析構後如果不清除name指向的內容,rte } //故而需要在deconstructor中delete,相應的 void setage(int age) { //constructor中配上new. 20180428SUN.五一快樂! this->age = age; } char* getname() { return name; } int getage() { return age; }};/********define of leader, virtually derived from base*******/class leader: virtual public base{ char* job; char* dep;public: leader(){ job = new char[20]; dep = new char[20]; } ~leader(){ delete [] job; delete [] dep; } void setjob(char arr[]) { strcpy(job, arr); } void setdep(char arr[]) { strcpy(dep, arr); } char* getjob() { return job; } char* getdep() { return dep; }};/********define engineer, virtually derived from base********/class engineer: virtual public base{ char* major; char* prof;public: engineer(){ major = new char[20]; prof = new char[20]; } ~engineer(){ delete [] major; delete [] prof; } void setmajor(char arr[]) { strcpy(major, arr); } void setprof(char arr[]) { strcpy(prof, arr); } char* getmajor() { return major; } char* getprof() { return prof; }};/********define chairman, derived from leader & engineer*******/class chairman: public leader, public engineer{public: chairman(){} ~chairman(){}};

推薦閱讀:

TAG:虛擬現實VR | 科技 | 編程 |