![](https://i-blog.csdnimg.cn/blog_migrate/51b648e06420d03ce0bbec340690e4c6.png)
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;
}