博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++小代码
阅读量:6516 次
发布时间:2019-06-24

本文共 9360 字,大约阅读时间需要 31 分钟。

//复数类,构造函数和重载+运算符#include 
#include
using namespace std;class Complex{public: Complex(double r,double i=0){//缺省参数i real=r; image =i; } void Show(){ cout<
0)cout<<"+"<
<<"i"<
//定义一个抽象类Shape,它有一个纯虚函数GetPerimeter();派生出四边型类Rectangle和圆类Circle,在派生类中重载函数GetPerimeter(),用于求图形的周长,编写测试程序进行测试。#include 
#include
using namespace std;class Shape{public: virtual double GetPPerimeter() const=0;};class Rectangle:public Shape { Rectangle(double w, double h) { width = w; height = h; } double GetPPerimeter()const{ return 2*(width+height); }private: double width, height; };class Circle:public Shape { public: Circle(double r) { radius = r; } double GetPerimeter() const { return 3.1415926 * radius * 2; } private: double radius; };

 如果一个运算符函数是成员函数,则它的第一个(左侧)运算对象绑定到隐式的this指针上,所以成员运算符函数的(显式)参数数量比运算符的运算对象少一个。即一个参数有两个运算对象。

IO运算符一般被声明为友元。

//设计一个日期类Date,,要求:  (1)包含年(year)、月(month)和日(day)私有数据成员。  (2)包含构造函数,重载输出运算符“<<”与重载输入运算符“>>”。#include 
#include
using namespace std;class Date{private: int year; int month; int day; void SetYear(int y) { year = y; } void SetMonth(int m) { month = m; } void SetDay(int d) { day = d; } int GetYear() const { return year; } int GetMonth() const { return month; } int GetDay() const { return day; } public: friend istream &operator>>(istream &in,Date &dt); friend ostream &operator<<(ostream &out, const Date &dt); Date(int y=2010,int m=1,int d=1):year(y),month(m),day(d){}; };istream &operator>>(istream &in,Date &dt){ int y,m,d; cout<<"输入年:"; in>>y; cout<<"输入月:"; in>>m; cout<<"输入日:"; in>>d; dt.SetYear(y); dt.SetMonth(m); dt.SetDay(d); return in; } ostream &operator<<(ostream &out, const Date &dt) { out << dt.GetYear() << "年" << dt.GetMonth() << "月" << dt.GetDay() << "日"; return out; }int main(){ Date d; cin>>d; cout<
<

 

1 //虚继承解决菱形二义性 2 //定义Person(人)类,由Person分别派生出Teacher(教师)类和Cadre(干部)类,再由Teacher(教师)类和Cadre(干部)类采用多重继承方式派生出新类TeacherCadre(教师兼干部)类 3 #include 
4 #include
5 using namespace std; 6 class Person { 7 protected: 8 char name[18]; 9 int age; 10 char sex[3]; 11 public: Person(char nm[], int ag, char sx[]) 12 { 13 strcpy(name, nm); 14 age = ag; 15 strcpy(sex, sx); 16 } 17 void Show() const 18 { cout << "姓名:" << name << endl; 19 cout << "年龄:" << age << endl; 20 cout << "性别:" << sex << endl; } 21 }; 22 class Teacher: virtual public Person 23 { protected: 24 char title[18]; 25 public: 26 Teacher(char nm[], int ag, char sx[], char tl[]): Person(nm, ag, sx) { strcpy(title, tl); } //t1是职称27 void Show() const 28 { Person::Show(); 29 cout << "职称:" << title << endl; 30 cout << endl; } 31 }; 32 class Cadre: virtual public Person 33 { protected: char post[18]; //职务 34 public: Cadre(char nm[], int ag, char sx[], char pt[]): Person(nm, ag, sx) { strcpy(post, pt); }35 void Show() const 36 { Person::Show(); 37 cout << "职务:" << post << endl; 38 cout << endl; } }; 39 class TeacherCadre: public Teacher, public Cadre { 40 protected: double wages; //工资41 public: 42 TeacherCadre(char nm[], int ag, char sx[], char tl[], char pt[], double wg) : Person(nm, ag, sx), Teacher(nm, ag, sx, tl), Cadre(nm, ag, sx, pt) { wages = wg; } 43 void Show() const { 44 Person::Show(); 45 cout << "职称:" << title << endl; 46 cout << "职务:" << post << endl; 47 cout << "工资:" << wages << "元" << endl; 48 cout << endl; } }; 49 int main() { 50 Teacher objTeacher("文冠杰", 48, "男", "教授"); 51 Cadre objCadre("周杰", 56, "男", "院长"); 52 TeacherCadre objTeacherCadre("李靖", 50, "女", "教授", "院长", 6890); 53 objTeacher.Show(); 54 objCadre.Show(); 55 objTeacherCadre.Show(); 56 return 0; 57 }
//虚析构函数//设计一个基类Shape,Shape中包含成员函数Show(),将Show()声明为纯虚函数。Shape类公有派生矩形类Rectangle和圆类Circle,分别定义Show()实现其主要几何元素的显示。使用抽象类Shape类型的指针,当它指向某个派生类的对象时,就可以通过它访问该对象的虚成员函数Show(),要求编写测试程序#include 
#include
using namespace std;const double PI = 3.1415926; class Shape { public: virtual ~Shape() { cout<<"1"<
Show(); delete p; p = new Rectangle(1, 2); p->Show(); delete p; return 0; }//输出结果:3 1 2 1

 

#include 
using namespace std; const double PI = 3.1415926; class Shape { public: Shape() { } ; virtual ~Shape() { }; //抽象类 virtual void ShowArea() const = 0; static double totalArea; static void ShowTotalArea() { cout << "总面积:" << totalArea << endl; }};class Circle: public Shape { private: double radius; public: Circle(double r): radius(r) { totalArea += PI * r * r; } ~Circle() { } virtual void ShowArea() const { cout << "圆面积:" << PI * radius * radius << endl; };};class Rectangle: public Shape { private: double length; double width; public: Rectangle(double l, double w): length(l), width(w){ totalArea += l * w; } ~Rectangle() { } virtual void ShowArea() const { cout << "矩形面积:" << length * width << endl; }; };double Shape::totalArea=0; //静态数据成员的定义int main() { Circle c(1); c.ShowArea(); Rectangle r(1, 2); r.ShowArea(); Shape::ShowTotalArea(); //静态成员函数的调用 return 0;}

 

#include 
using namespace std; class Staff { protected: int num; char name[18]; double rateOfAttend; double basicSal; double prize; static int count; public: Staff(){ } void Input() { num = ++count; cout << "请输入编号为" << num <<"号员工的信息" << endl; cout << "姓名:"; cin >> name; cout << "基本工资:"; cin >> basicSal; cout << "奖金:"; cin >> prize; cout <<"出勤率(0~1):"; cin >> rateOfAttend; } void Output() const { cout << "编号:" << num << endl; cout << "姓名:" << name << endl; cout << "基本工资:" << basicSal << "元" << endl; cout << "奖金:" << prize << "元" << endl; cout << "出勤率:" << rateOfAttend * 100 << "%" << endl; } void OutputWage() const { cout << "实发工资:" << basicSal + prize * rateOfAttend << "元" << endl; }}; int Staff::count = 1000; class Saleman : public Staff { protected: float deductRate; float personAmount; public: Saleman (){ }; void Input() { Staff::Input(); cout << "个人销售额:"; cin >> personAmount; cout << "提成比例:"; cin >> deductRate; } void Output() const { Staff::Output(); cout << "个人销售额:" << personAmount << "元" << endl; cout << "提成比例:" << deductRate * 100 << "%" << endl; } void OutputWage() const { cout << "实发工资:" << basicSal + prize * rateOfAttend + personAmount * deductRate << "元" << endl; }}; class Manager: public Staff { protected: double totalDeductRate; double totalAmount; public: Manager(){ } void Input() { Staff::Input(); cout << "公司总销售额:"; cin >> totalAmount; cout << "提成比例:"; cin >> totalDeductRate; } void Output() const { Staff::Output(); cout << "公司总销售额:" << totalAmount << "元" << endl; cout << "提成比例:" << totalDeductRate * 100 << "%" << endl; } void OutputWage() const{ cout << "实发工资:" << basicSal + prize * rateOfAttend + totalAmount * totalDeductRate << "元" << endl; } }; int main() { char flag = 'Y'; while (toupper(flag) == 'Y') { cout << "请选择录入类别(1.员工 2.销售员 3.经理)"; int n; cin >> n; if (n == 1) { // 员工 Staff objStaff; objStaff.Input(); objStaff.Output(); objStaff.OutputWage(); } else if (n == 2) { Saleman objSaleman; objSaleman.Input(); objSaleman.Output(); objSaleman.OutputWage(); } else if (n == 3) { Manager objManager; objManager.Input(); objManager.Output(); objManager.OutputWage(); } else { cout << "选择有误!"<< endl; } cout << endl << "是否继续录入信息?(Y/N)"; cin >> flag; } return 0; }
View Code

 

转载于:https://www.cnblogs.com/zzy-2017/p/6965335.html

你可能感兴趣的文章
php中的正则函数:正则匹配,正则替换,正则分割 所有的操作都不会影响原来的字符串....
查看>>
【网络协议】TCP协议简单介绍
查看>>
利用SMB jcifs实现对windows中的共享文件夹的操作
查看>>
Spring(十七):Spring AOP(一):简介
查看>>
html5常用属性text-shadow、vertical-align、background如何使用
查看>>
微软正式宣布Azure MongoDB Atlas免费方案
查看>>
Jessica Kerr:高绩效团队简史
查看>>
开发者需要知道的有关软件架构的五件事
查看>>
GitLab 9提供了子群组、部署面板和集成监控
查看>>
继爆款超级账本后,IBM再次推出新产品
查看>>
贝壳金控赵文乐:基于 Spring Cloud 的服务治理实践
查看>>
Pyspider框架 —— Python爬虫实战之爬取 V2EX 网站帖子
查看>>
区域生长算法 C++实现
查看>>
数据分析-从入门到崩溃
查看>>
web.xml 中的listener、 filter、servlet 加载顺序
查看>>
MyBatis原理简介和小试牛刀
查看>>
js部分基础
查看>>
脏读,幻读,不可重复读解释和例子
查看>>
Tomcat指定(JDK路径)JAVA_HOME而不用环境变量
查看>>
Bluemix专属版本落地中国 开放物联网和认知计算能力
查看>>