标签:
项目1:程序填空
#include <iostream> #include <string> using namespace std; class Person { string name; int age; public: Person() {} void setname(string na) { name = na; } void setage(int a) { age = a; } string getname() { return name; } int getage() { return age; } }; class Leader: virtual public Person { string job; string dep; public: Leader() {} void setjob(string jb) { job = jb; } void setdep(char dp[]) { dep = dp; } string getjob() { return job; } string getdep() { return dep; } }; class Engineer: virtual public Person { string major; string prof; public: Engineer () {} void setmajor(string maj) { major = maj; } void setprof(string pf) { prof = pf; } string getmajor() { return major; } string getprof() { return prof; } }; class chairman: public Leader, public Engineer{ }; int main() { chairman c; c.setname("张三"); c.setage(42); c.setjob("处长"); c.setdep("技术处"); c.setmajor("轮机设计"); c.setprof("高级工程师"); cout << c.getname() << "," << c.getage() << "岁,担任" << c.getdep() << c.getjob() << endl; cout << c.getprof() << ",从事" << c.getmajor() << "专业" << ". " << endl; return 0; }
项目2: 教师干部类
#include <iostream> #include <string> using namespace std; class Teacher { string name; int age; bool sex; string title; public: Teacher(string, int, bool, string); void display(); }; Teacher::Teacher(string _name, int _age, bool _sex, string _title) { name = _name; age = _age; sex = _sex; title = _title; } void Teacher::display() { cout << "姓名:" << name << endl << "年龄:" << age << endl << "性别:" << (sex?"男":"女") << endl << "职称:" << title << endl; } class Cadre { string name; int age; bool sex; string post; public: Cadre(string, int, bool, string); void setpost(string); string getpost(); }; Cadre::Cadre(string _name, int _age, bool _sex, string _post) { name = _name; age = _age; sex = _sex; post = _post; } void Cadre::setpost(string _post) { post = _post; } string Cadre::getpost() { return post; } class Teacher_Cadre: public Teacher, public Cadre { float wages; public: Teacher_Cadre(string, int, bool, string, string, float); void show(); }; Teacher_Cadre::Teacher_Cadre(string _name, int _age, bool _sex, string _title, string _post, float _wages): Teacher(_name, _age, _sex, _title), Cadre(_name, _age, _sex, _post) { wages = _wages; } void Teacher_Cadre::show() { Teacher::display(); cout << "职务:" << Cadre::getpost() << endl << "工职:" << wages << endl; } int main() { Teacher_Cadre zh("曾辉", 42, 1, "副教授", "主任", 1534.5); zh.show(); return 0; }
标签:
原文地址:http://blog.csdn.net/index42/article/details/51329383