继承与派生、运算符的重载、虚函数的应用
来源:互联网 发布:deepin linux 安装软件 编辑:程序博客网 时间:2024/06/07 04:29
定义了一个person类,派生出一个student类,通过person的一个指针来调用student类中定义成虚函数的show函数,重载了运算符'>',比较两个学生的成绩,先比较总分,总分相同比较数学,重载流插入运算符,用来输出成绩较高的同学的各科成绩;
代码:
#include <iostream>#include <string>using namespace std;class person{private: string name; int age; char sex;public: void set_info(){ cin>>name>>age>>sex; } virtual void show(){ cout<<"name:"<<name<<" "<<"age:"<<age<<endl; }};class student:public person{private: string major; int id; int math, english, computer; int sum;public: void set_info(){ person::set_info(); cin>>major>>id>>math>>english>>computer; } int getsum(){ sum = math + english + computer; return sum; } void show(){ person::show(); cout<<"major:"<<major<<" "<<"id:"<<id<<endl; } bool operator>(student &stu){ if(this->getsum() > stu.getsum()){ return true; } else if(this->sum == stu.getsum()){ if(this->math > stu.math){ return true; } } else return false; } friend ostream &operator<<(ostream &out, const student &stu){ out<<"math:"<<stu.math<<endl; out<<"english:"<<stu.english<<endl; out<<"computer:"<<stu.computer<<endl; return out; }};int main(){ person *st; student s, t; s.set_info(); t.set_info(); st = &s; st->show(); st = &t; st->show(); cout<<"成绩高的同学的成绩是:"<<endl; if(s > t) cout<<s; else cout<<t; return 0;}
阅读全文
0 0
- 继承与派生、运算符的重载、虚函数的应用
- c++派生,运算符重载,虚函数的使用
- Time Date DateTime的继承与派生及运算符重载
- Time Date DateTime的继承与派生及运算符重载
- Time Date DateTime的继承与派生及运算符重载
- 058day(自增,自减运算符重载和继承与派生的基本概念)
- 派生与继承的应用1
- 继承与派生的应用3
- 重载运算符的实现与应用
- 继承与派生:派生类的构造函数
- 继承与派生:派生类的析构函数
- 继承与派生:虚基类及其派生类的构造函数
- 三十九、继承与派生:派生类的构造函数****
- 四十、继承与派生:派生类的析构函数
- 派生类的复制构造函数与赋值运算符
- 继承,重载函数,派生函数
- C++之派生类的拷贝构造与赋值运算符重载
- C++语法细微(四)(运算符重载/继承派生)
- Java中的static关键字解析
- CXF结合Spring 开发SOAP WebService服务
- 迅速买入涨停板,强势不要放手
- Microsoft Word for Mac激活版(Word 2016无需输入激活秘钥版) V15.4已破解版
- Python基础-正则表达式
- 继承与派生、运算符的重载、虚函数的应用
- zookeeper安装和使用 windows环境
- java调用api方法
- 回调函数和钩子函数
- Hibernate -- xxx.hbm.xml ORM元数据 表对象关系映射文件
- 「多线程/线程池专题」这是一份java高级工程师都收藏的专题合集
- EJB详解
- 列举IE与其他浏览器不一样的特性?
- ionic3 prod mode error @angular-devkit\build-optimizer\webpack-loader not found