第五次C++上实验
2016-05-06 17:48
495 查看
/* *文件名称:Ex1-5.cpp *作者:邓利超 *完成日期:2016年5月6日 *对任务及求解方法的描述部分: *输入描述:无 *问题描述:1.程序填空 2.类的编写 *程序输出:按要求写出程序 *问题分析:略 *算法分析:略 */ //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:public Person //继承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:public Leader // 继承Leader的公有成员 { 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 Engineer { } ;// 继承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; } #include<iostream> #include<string> using namespace std; class Teacher { string title; public: void get(string name,int age,string sex,string title);//输入职称、名字、年龄、性别 void show_it(); string name; int age; string sex; }; void Teacher::get(string name,int age,string sex,string title) { this->name=name; this->age=age; this->sex=sex; this->title=title; } void Teacher::show_it() { cout<<"姓名:"<<name<<endl; cout<<"年龄:"<<age<<endl; cout<<"性别:"<<sex<<endl; cout<<"职称:"<<title<<endl; } class Cadre:public Teacher { string post; public: void get1(string post);//输入职务 void showpost(); }; void Cadre::get1(string post) { this->post=post; } void Cadre::showpost() { Teacher::show_it(); cout<<"职务:"<<post<<endl; } class Teacher_Cadre:public Cadre { double wages; public: void get2(double wages);//输入工资 void show();//最终输出 }; void Teacher_Cadre::get2(double wages) { this->wages=wages; } void Teacher_Cadre::show() { Cadre::showpost(); cout<<"工资:"<<wages<<"元"<<endl; } int main() { Teacher_Cadre c; c.get("曾辉",42,"男","副教授"); c.get1("主任"); c.get2(1534.5); c.show(); return 0; }
相关文章推荐
- C++标准库中next_permutation和pre_permutation实现原理
- leetcode_13_Roman to Integer(C++)(easy)
- C++三种容器:list、vector和deque的区别
- More Effective C++----(19)理解临时对象的来源
- 【C语言】自定义连接两个字符串函数
- C++字符串相关操作函数
- c++ 如何把this指针传入成员函数 像全局函数一样调用成员函数
- vector删除指定元素
- C语言中extern的用法
- leetcode_169_Majority Element(C++)(easy)
- c++
- More Effective C++----(18)分期摊还期望的计算
- C++实现线索化二叉树
- C++模板
- C++ 子类继承带参的父类构造函数应该怎么写
- 【C语言】过滤字符串非字母的字符并统计字母数
- C++基础知识汇总
- C语言的运行环境有哪些?哪些是值得推荐的?
- C++11中的线程同步(简单测试)
- C++中继承,虚函数的调用问题