码迷,mamicode.com
首页 > 编程语言 > 详细

C++小代码

时间:2017-06-10 22:35:39      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:引用   prot   成员函数   public   通过   方式   使用   class   input   

//复数类,构造函数和重载+运算符
#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;  
}
View Code

 

C++小代码

标签:引用   prot   成员函数   public   通过   方式   使用   class   input   

原文地址:http://www.cnblogs.com/zzy-2017/p/6965335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!