C++---编程实战(二)

#include <iostream>
using namespace std;

class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0, int i = 0):real(r),image(i){}
    friend Complex operator+(Complex &c1,Complex &c2);
    void display();
};
void Complex::display()
{
    cout<<real<<" "<<image<<endl;
}
Complex operator+(Complex &c1,Complex &c2)
{
    return Complex(c1.real+c2.real,c1.image+c2.image);
}
int main()
{
    Complex c1(2,3),c2(3,4);
    Complex c3;
    c3 = c1+c2;
    c3.display();
    return 0;
}


函数重载

例子:

定义一个复数类 Complex ,重载运算符“+”,“—”,使之能用于复数的加,减运算,运算符重载函数作为 Complex类的成员函数。编程序,分别求出两个复数之和,差。初值自拟。

程序:

#include<iostream>
using namespace std;

class Complex
{
private:
    float real;
    float image;
public:
    Complex(float r=0,float i=0):real(r),image(i){}  //形参必须有初值。
    Complex operator+(Complex &c2);
    Complex operator-(Complex &c2);
    void show();
};
Complex Complex::operator+(Complex &c2)
{
    Complex c;
    c.real = c2.real + real;
    c.image = c2.image + image;
    return c;
}
Complex Complex::operator -(Complex& c2)
{
    Complex c;
    c.real =real-c2.real;
    c.image =image-c2.image;
    return c;
}
void Complex::show()
{
    cout<<"the value : "<<real<<"i"<<"+"<<image<<endl;
}
int main()
{
    Complex c1(6.2,1.0),c2(3.0,7.9),c3;
    c3 = c1+c2;
    c3.show();
    c3 = c1-c2;
    c3.show();

    return 0;
}
注意事项:

(1)这个程序简单,但是我调试了半天,主要的原因就是,在 c3 的 初值上,刚开始的构造函数参数没有初值的,一直报错,调试了好久,才发现。

(2)程序中的 c3 = c1+ c2,在 + 重载为类的成员函数以后,C++编译器将程序中的表达式 c1+c2 解释为: c1.operator+ c2。即是以 c2 为参数调用 c1 的运算符重载函数

operator+(Complex &c2),进行求值,得到两个复数之和。其中operator+是一个函数名字,它是类 Complex的成员函数。

例子:

将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外, 作为 Complex类的友元函数。初值自拟。

#include <iostream>
using namespace std;

class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0, int i = 0):real(r),image(i){}
    friend Complex operator+(Complex &c1,Complex &c2);
    void display();
};
void Complex::display()
{
    cout<<real<<" "<<image<<endl;
}
Complex operator+(Complex &c1,Complex &c2)
{
    return Complex(c1.real+c2.real,c1.image+c2.image);
}
int main()
{
    Complex c1(2,3),c2(3,4);
    Complex c3;
    c3 = c1+c2;
    c3.display();
    return 0;
}

运算符重载为类的友元与重载为类的成员函数的区别就是:


例子:

有两个矩阵 a 和 b,均为 2 行 3 列。求两个矩阵之和。重载运算符“ +”,使之能用 于矩阵相加。如 c=a+b。初值自拟。

#include <iostream>
using namespace std;

#define n 2
#define m 3
class Matrix
{
private:
    int a[n][m];

public:
    Matrix();
    Matrix operator+(Matrix &c2);
    void input();
    void show() const ;
};
Matrix::Matrix()
{
   for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
          a[i][j] = 0;
}
Matrix Matrix::operator+(Matrix &c2)
{
    Matrix c;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
           c.a[i][j]=a[i][j]+c2.a[i][j];
        return c;
}
void Matrix::show()const
{
    cout<<"output: "<<endl;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cout<<a[i][j];
}
void Matrix::input()
{
    cout<<"input: "<<endl;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>a[i][j];
}
int main()
{
    Matrix c1,c2,c3;  
    c1.input();
    c1.show();
    c2.input();
    c2.show();
    c3 = c1+c2;
    c3.show();
    return 0;
}
注意:

成员函数是数组怎么进行类成员的初始化。

例子:

定义一个字符串类 String ,用来存放不定长的字符串,重载运算符“==”,,用于两个字符串的等于比较运算。载运算符"<" ,用于两个
字符串的小于的比较运算。
初值自拟。

#include <iostream>
#include <string.h>
using namespace std;

class String
{
private:
    char*p;
public:
    String(char* pr =NULL):p(pr){}
    friend bool operator>(String &string1,String &string2);
    friend bool operator<(String &string1,String &string2);
    friend bool operator==(String &string1,String &string2);
    void display();
};
 bool operator>(String &string1,String &string2)
{
     if(strcmp(string1.p,string2.p)>0)
        return true;
    else
        return false;
}
 bool operator==(String &string1,String &string2)
{
    if(strcmp(string1.p,string2.p)==0)
        return true;
    else
        return false;
}
 bool operator<(String &string1,String &string2)
{
    if(strcmp(string1.p,string2.p)<0)
        return true;
    else
        return false;
}
void String::display()
{
   cout<<p<<endl;
}
void Compare(String &string1,String &string2)
{
    if(operator>(string1,string2)==1)
        string1.display();
    else if(operator<(string1,string2)==1)
         string2.display();

    else
        string2.display();
}
int main()
{
    String s1("udsfhsdufbsdn"),s2("sfmiw3erjwei");
    Compare(s1,s2);
    return 0;
}

例子:

派生,点派生圆再派生圆柱体

#include <iostream>
using namespace std;

class Point
{
protected:  //如果写成私有成员,则派生类不可以访问,
    int x;  //保护类成员在派生类中是保护成员
    int y;
public:
    Point(int xx , int yy ):x(xx),y(yy){}
};

class Circle:public Point
{
   protected:
      int r;
   public:
      Circle(int xx,int yy,int rr):Point(xx,yy),r(rr){}
};
class Cylinder: public Circle
{
protected:
    int h;
public:
    Cylinder(int xx,int yy,int rr,int hh):Circle(xx,yy,rr),h(hh){}
    int area() const;
    friend
};
int Cylinder::area() const
{
    return 3.14*r*r*h;
}
int main()
{
    Cylinder c(0,0,10,10);
    cout<<"c.area : "<<c.area()<<endl;
}








  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值