首页 > 代码库 > C++小代码
C++小代码
//复数类,构造函数和重载+运算符 #include <iostream> #include <string> using namespace std; class Complex{ public: Complex(double r,double i=0){//缺省参数i real=r; image =i; } void Show(){ cout<<real; if(image>0)cout<<"+"<<image<<"i"<<endl; else if(image<0)cout<<image<<"i"<<endl; else cout<<endl; } Complex operator+(const Complex &obj){ //形参为对象常引用 Complex temp(real+obj.real,image+obj.image); return temp; } private: double real,image; }; int main(){ Complex z1(2,-6),z2(3,8),z3(0); z1.Show(); z2.Show(); z3.Show(); z3=z1+z2; z3.Show(); return 0; };
//定义一个抽象类Shape,它有一个纯虚函数GetPerimeter();派生出四边型类Rectangle和圆类Circle,在派生类中重载函数GetPerimeter(),用于求图形的周长,编写测试程序进行测试。 #include <iostream> #include <string> 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 <iostream> #include <string> 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<<d<<endl; return 0; };
1 //虚继承解决菱形二义性 2 //定义Person(人)类,由Person分别派生出Teacher(教师)类和Cadre(干部)类,再由Teacher(教师)类和Cadre(干部)类采用多重继承方式派生出新类TeacherCadre(教师兼干部)类 3 #include <iostream> 4 #include <string> 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 <iostream> #include <string> using namespace std; const double PI = 3.1415926; class Shape { public: virtual ~Shape() { cout<<"1"<<endl;}; virtual void Show() const = 0; }; class Rectangle: public Shape { private: double height; double width; public: Rectangle(double h, double w): height(h), width(w) { } ; ~ Rectangle(){cout<<"2"<<endl;}; void Show() const { cout << "矩形:" << endl; cout << "高:" << height << endl; cout << "宽:" << width << endl; cout << "周长:" << 2 * (height + width) << endl; cout << "面积:" << height * width << endl << endl; } }; class Circle: public Shape { private: double radius; public: Circle(double r): radius(r) { } ~Circle(){cout<<"3"<<endl;}; void Show() const { cout << "圆形:" << endl; cout << "半径:" << radius << endl; cout << "周长:" << 2 * PI * radius << endl; cout << "面积:" << PI * radius * radius << endl << endl; } }; int main() { Shape *p; p = new Circle(1); p->Show(); delete p; p = new Rectangle(1, 2); p->Show(); delete p; return 0; }//输出结果:3 1 2 1
#include <iostream> 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 <iostream> 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; }
C++小代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。