类和对象
1 友元
1.1全局函数做友元
class building
{
friend void goodgay(building *b);
public:
building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
string m_sittingroom;
private:
string m_bedroom;
};
void goodgay(building *b)
{
cout << " 好基友全局函数正在访问:" << b->m_sittingroom << endl;
}
void test()
{
building b;
goodgay(&b);
}
int main()
{
test();
system("pause");
return 0;
}
1.2 类做友元
class building;
class goodgay
{
public:
goodgay();
void visit();
building *b;
};
class building
{
friend class goodgay;
public:
building();
string m_sittingroom;
private:
string m_bedroom;
};
类外写成员函数
building::building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
goodgay::goodgay()
{
b = new building;
}
void goodgay::visit()
{
cout << "好基友在访问:" << b->m_sittingroom << endl;
cout << "好基友在访问:" << b->m_bedroom << endl;
}
void test()
{
goodgay gg;
gg.visit();
}
int main()
{
test();
system("pause");
return 0;
}
1.3成员函数做友元
class building;
class goodgay
{
public:
goodgay();
void visit();
void visit2();
building *b;
};
class building
{
friend void goodgay::visit();
public:
building();
string m_sittingroom;
private:
string m_bedroom;
};
building::building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
goodgay::goodgay()
{
b = new building;
}
void goodgay::visit()
{
cout << "好基友在访问:" << b->m_sittingroom << endl;
cout << "好基友在访问:" << b->m_bedroom << endl;
}
void goodgay::visit2()
{
cout << "好基友在访问:" << b->m_sittingroom << endl;
}
void test()
{
goodgay gg;
gg.visit();
gg.visit2();
}
int main()
{
test();
system("pause");
return 0;
}
2.运算符重载
2.1运算符重载+号
class person
{
public:
int m_a;
int m_b;
};
person operator+(person &p1, int p2)
{
person temp;
temp.m_a = p1.m_a + p2;
temp.m_b = p1.m_b + p2;
return temp;
}
void test01()
{
person p1;
p1.m_a = 10;
p1.m_b = 20;
person p2;
p2.m_a = 10;
p2.m_b = 20;
person p3 = p1 + 10;
cout << "p3.m_a=" << p3.m_a << endl;
cout << "p3.m_b=" << p3.m_b << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
2.2运算符重载左移运算符
class Person {
friend ostream& operator<<(ostream& out, Person& p);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
private:
int m_A;
int m_B;
};
ostream& operator<<(ostream& out, Person& p) {
out << "a:" << p.m_A << " b:" << p.m_B;
return out;
}
void test() {
Person p1(10, 20);
cout << p1 << "hello world" << endl;
}
int main() {
test();
system("pause");
return 0;
}
2.3 运算符重载递增运算符
class MyInteger {
friend ostream& operator<<(ostream& out, MyInteger myint);
public:
MyInteger() {
m_Num = 0;
}
MyInteger& operator++() {
m_Num++;
return *this;
}
MyInteger operator++(int) {
MyInteger temp = *this;
m_Num++;
return temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& out, MyInteger myint) {
out << myint.m_Num;
return out;
}
void test01() {
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt << endl;
}
void test02() {
MyInteger myInt;
cout << myInt++ << endl;
cout << myInt << endl;
}
int main() {
test01();
system("pause");
return 0;
}
2.4 运算符重载赋值运算符
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
Person& operator=(Person &p)
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
2.5 运算符重载关系运算符
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
};
bool operator==(Person & p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
bool operator!=(Person & p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return false;
}
else
{
return true;
}
}
string m_Name;
int m_Age;
};
void test01()
{
Person a("孙悟空", 18);
Person b("孙悟空", 18);
if (a == b)
{
cout << "a和b相等" << endl;
}
else
{
cout << "a和b不相等" << endl;
}
if (a != b)
{
cout << "a和b不相等" << endl;
}
else
{
cout << "a和b相等" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
2.6 函数调用运算符重载
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test01()
{
MyPrint myFunc;
myFunc("hello world");
}
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test02()
{
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;
cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}