C++运算符重载题目练习

5.30

#include <iostream>

using namespace std;

class func{
public:
    int operator() (int x, int y) const;

};

int func::operator() (int x, int y) const{
    int a,b,c;
    cin>>a>>b>>c;
    return a*x*x + b*y + c;
}

int main(){
    func f;
    cout<<f(2,3)<<endl;
    system("pause");
    return 0;
}

5.31

#include <iostream>
#include <cstring>
using namespace std;

class Test{
public:
    Test(char *s){
        str = new char[strlen(s)+1];
        strcpy(str , s);
        len = strlen(s);
    }
    char operator[] (int n){
        if (n > len-1)
        {
            cout<<"数组下标越界"<<endl;
            return ' ';
        }
        else{
            return *(str+n);
        }
        
    }
    void print(){cout<< str <<endl;}
private:
    int len;
    char *str;
};
int main(){
    Test arry("i like study programme!");
    arry.print();
    cout<<arry[200] <<endl;
    cout <<arry[3] <<endl;

    system("pause");
    return 0;
}

5.32 思路:对+进行重载,两条边相加返回边对象,然后再利用对>号的重载,满足两条边相加大于第三边则输出1,否则为0;

#include <iostream>
#include <cstring>
using namespace std;

class Line{
public:
    Line(int n){
        len = n;
    }
    Line operator+(Line l){
        int x= len + l.len;
        return Line(x);
    }
    bool operator>(Line l){
        return (len > l.len)? 1:0;
    }
private:
    int len;
};
int main(){
    Line a(10),b(5),c(14);

    if (a+b>c && a+c>b && b+c>a)
    {
        /* code */
        cout<<"能构成一个三角形"<<endl;
    }else{
        cout<<"不能构成一个三角形"<<endl;
    }
    
    system("pause");
    return 0;
}

5.33 operator double()类型转换运算符:类型转换运算符重载函数没有任何返回类型和任何参数,<类型名>即代表了它的返回参数,而在调用过程必须要带一个对象实参。

#include <iostream>
#include <cstring>
using namespace std;
#define  CHANGE  8.27;

class RMB{
public:
    RMB(){yuan = 0;}
    RMB(double y){yuan =y;}

    void stevalue(double my){
        yuan = my * CHANGE;
    }
    operator double(){
        return yuan/CHANGE;
    }

    void display(){
        cout<<yuan<<"人民币"<<endl;
    }
    
private:
    double yuan;
};
int main(){
    RMB r1(10000),r2;

    double my = double(r1);
    cout<<"1000人民币="<<my<<"美元"<<endl;
    r2.stevalue(10000);
    cout<<"1000美元=";
    r2.display();
    
    
    system("pause");
    return 0;
}

5.34

#include <iostream>
#include <cstring>
using namespace std;

class Three_b{
public:
    Three_b(int i, int j ,int k){
        x=i,y=j,z=k;
    }
    Three_b(){
        x=0,y=0,z=0;
    }
    void Get(int &i,int &j, int &k){i=x, j=y, z=k;}
    void Print(){
        cout<<"("<<x<<","<<y<<","<<z<<")"<<endl;
    }
    Three_b& operator ++(){
        x++,y++,z++;
        return *this;
    }

    Three_b& operator --(){
        x--,y--,z--;
        return *this;
    }

    friend Three_b operator +(Three_b& t1, Three_b& t2);
private:
    int x;
    int y;
    int z;
};

Three_b operator +(Three_b& t1, Three_b& t2){
    return Three_b(t1.x+t2.x, t1.y+t2.y, t1.z+t2.z);
}
int main(){
    
    Three_b obj1(1,2,3),obj2(13,12,11),obj3;
    ++obj1;
    obj1.Print();
    --obj2;
    obj2.Print();
    obj3 = obj1 + obj2;
    obj3.Print();
    system("pause");
    return 0;
}

5.35

#include <iostream>
#include <cstring>
using namespace std;

class Point{
public:
    Point(){x=y=0;}
    int Getx(){return x;}
    int Gety(){return y;}
    Point& operator ++();
    Point& operator ++(int); //后置加加
    Point& operator --();
    Point&operator --(int);  //后置减减

    void Print(){
        cout<<"The Point is ("<<x<<","<<y<<")"<<endl;
    }
private:
    int x,y;
};

 Point& Point::operator ++(){
    this->x++,this->y++;
    return *this;
 }

Point& Point::operator ++(int){
    Point temp = *this;
    this->x++,this->y++;
    return temp;
}
Point& Point::operator --(){
    this->x--,this->y--;
    return *this;

}

Point& Point::operator --(int){
    Point temp = *this;
    //this->x--,this->y--;
    --*this;
    return temp;

 }
int main(){
    Point obj;
    obj.Print();
    obj++;
    obj.Print();
    ++obj;
    obj.Print();
    obj--;
    obj.Print();
    --obj;
    obj.Print();

    system("pause");
    return 0;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值