作业:
实现关系运算符重载(仅>,<,==):
#include <iostream>
using namespace std;
class Person
{
int a;
int b;
friend bool operator== (const Person &L,const Person&R);
public:
Person(){}
Person(int a,int b):a(a),b(b){}
bool operator> (const Person&R)const
{
if(a>R.a&&b>R.b)
{
return true;
}
else
{
return false;
}
}
bool operator< (const Person&R)const
{
if(a<R.a&&b<R.b)
{
return true;
}
else
{
return false;
}
}
};
bool operator== (const Person &L,const Person&R)
{
if(L.a==R.a&&L.b==R.b)
{
return true;
}
else
{
return false;
}
}
int main()
{
Person p1(10,10),p2(10,10),p3(20,20);
if((p1>p2)==true)
{
cout << "p1大于p2" << endl;
}
else
cout << "p1不大于p2" << endl;
if((p1==p2)==true)
{
cout << "p1等于p2" << endl;
}
else
cout << "p1不等于p2" << endl;
if((p1<p2)==true)
{
cout << "p1小于p2" << endl;
}
else
cout << "p1不小于p2" << endl;
if((p2>p3)==true)
{
cout << "p2大于p3" << endl;
}
else
cout << "p2不大于p3" << endl;
if((p2==p3)==true)
{
cout << "p2等于p3" << endl;
}
else
cout << "p2不等于p3" << endl;
if((p2<p3)==true)
{
cout << "p2小于p3" << endl;
}
else
cout << "p2不小于p3" << endl;
return 0;
}
拷贝赋值函数实例:
#include <iostream>
using namespace std;
class stu
{
string name;
int age;
public:
void show ()
{
cout << name << age << endl;
}
stu(string name,int age):name(name),age(age)
{
cout << "这是stu的构造函数" << endl;
}
stu(stu &other):name(other.name),age(other.age)//构造函数,需要初始化列表
{
cout << "这是stu的拷贝构造函数" << endl;
}
stu& operator=(const stu& other)//拷贝赋值函数,不需要初始化列表
{
if(this != &other)//避免自己被赋值给自己
{
name=other.name;
age=other.age;
}
cout <<"这是stu的拷贝赋值函数" << endl;
return *this;
}
~stu()
{
cout << "这是stu的析构函数" << endl;
}
};
int main()
{
stu s1("张三",20);
stu s2=s1;
stu s3("",0);
s3=s2;
return 0;
}
匿名对象实例:
#include <iostream>
using namespace std;
class stu
{
string name;
int age;
public:
stu(string name,int age):name(name),age(age)
{
cout << "这是stu的构造函数" << endl;
}
stu(const stu& other):name(other.name),age(other.age)
{
cout << "这是stu的拷贝构造函数" << endl;
}
stu& operator=(const stu& other)
{
name=other.name;
age=other.age;
return *this;
}
~stu()
{
cout <<"这是stu的析构函数" << endl;
}
};
int main()
{
stu s1=stu("张三",20);//用匿名类做赋值操作
stu s[3]={stu("a",1),stu("b",2),stu("c",3)};//用匿名类做类数组的赋值操作
return 0;
}
友元实例:
#include <iostream>
using namespace std;
class Room
{
friend void good(Room &s);//声明good函数是友元函数,就可以调用本类中的所有成员
friend class bad;//声明bad类是友元,bad类就可以调用本类中的所有成员
string badroom;
public:
string livingroom;
Room(string badroom,string livingroom):badroom(badroom),livingroom(livingroom)
{
cout<< "这是Room的构造函数" << endl;
}
Room()//无参构造
{}
};
void good(Room &s)
{
cout << s.livingroom << endl;
cout << s.badroom <<endl;
}
class bad
{
public:
Room s1;
void visit()
{
cout << s1.badroom << endl;//调用s1中的私有成员
cout << s1.livingroom <<endl;
}
};
int main()
{
Room s("卧室","客厅");
good(s);
bad a;
return 0;
}
常成员和常对象实例:
#include <iostream>
using namespace std;
class Stu
{
string name;
mutable int age;//加了mutable,就可以在常成员函数中修改
public:
Stu()//无参构造函数
{}
Stu(string n,int a):name(n),age(a)//有参构造类型
{ }
void show()const//常成员函数,不做修改
{
age=30;//会报错,因为是const类型的成员函数,不能做修改,当age加了mutable,就变成可以在常成员函数中修改
cout << "名字是:" << name << endl;
cout << "年龄是:" << age << endl;
}
void show()//非常成员函数
{
age=25;
cout << "名字是:" << name << endl;
cout << "年龄是:" << age << endl;
}
};
int main()
{
Stu s1("张三",18);//非常对象
s1.show();//有常成员函数和非常成员函数,优先调用非常成员函数,只有常成员函数,调用常成员函数
const Stu s2("里斯",20);//常对象
s2.show();//调用常成员函数,年龄不会发生改变
//当只有非常成员函数时,s2无法调用非常成员函数
return 0;
}
算数运算符重载:
#include <iostream>
using namespace std;
class Person
{
friend const Person operator+(const Person &L,const Person &R);
friend const Person operator>(const Person &L,const Person &R);
int a;
int b;
public:
Person(){};
Person(int a,int b):a(a),b(b)
{}
// const Person operator+(const Person &R)
// {
// Person temp;
// temp.a=a+R.a;
// temp.b=b+R.b;
// return temp;
// }
void show()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};
const Person operator+(const Person &L,const Person &R)//全局
{
Person temp;
temp.a = L.a + R.a;
temp.b = L.b + R.b;
return temp;
}
const Person operator>(const Person &L,const Person &R)
{
Person temp;
temp.a = (L.a > R.a) ? L.a : R.a;
temp.b = (L.b > R.b) ? L.b : R.b;
return temp;
}
int main()
{
Person p1(10,10);
Person p2(10,10);
Person p3=p1+p2;//假设调用类内的运算符重载,本质:p3=p1.operator+(p2)
p3.show();
Person p4=p2+p3;//假设调用类外的运算符重载,本质:p4=operator(p2,p3)
p4.show();
Person p5(5,11);
Person p6;
p6 = p5 > p1;
p6.show();
return 0;
}