1 对象的初始化和清理

1.1 构造函数和析构函数
  
#include<iostream>
using namespace std;
#include<string>
class Person
{
Person()
{
cout << "Person构造函数的调用" << endl;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
};
void test01()
{
Person p;
}
int main()
{
test01();
system("pause");
return 0;
}
1.2 构造函数的分类以及调用

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person()
{
cout << "Person的无参构造函数调用" << endl;
}
Person(int a)
{
age = a;
cout << "Person的有参构造函数调用" << endl;
}
Person(const Person& p)
{
age = p.age;
cout << "Person的拷贝构造函数调用" << endl;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
int age;
};
void test01()
{
Person p1;
Person p2(10);
Person p3(p2);
Person p1;
Person p2 = Person(10);
Person p3 = Person(p2);
Person(10);
Person p4 = 10;
Person p5 = p4;
}
int main()
{
system("pause");
return 0;
}
1.3 拷贝构造函数调用时机

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person()
{
cout << "Person默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person &p)
{
cout << "Person拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "Person析构函数调用" << endl;
}
int m_Age;
};
void test0111()
{
Person p1(20);
Person p2(p1);
}
void dowork(Person p)
{
}
void test02()
{
Person p;
dowork(p);
}
Person doWork2()
{
Person p1;
return p1;
}
void test03()
{
Person p = doWork2();
}
int main()
{
test03();
system("pause");
return 0;
}
1.4 构造函数调用规则

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
void test01()
{
Person p;
p.m_Age = 18;
Person p2(p);
cout << "p2的年龄为:" << p2.m_Age << endl;
}
void test02()
{
}
int main()
{
system("pause");
return 0;
}
1.5 深拷贝与浅拷贝

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age,int height)
{
m_Height = new int(height);
m_Age = age;
cout << "Person的有参构造函数调用" << endl;
}
Person(const Person& p)
{
cout << "Person拷贝构造函数调用" << endl;
m_Age = p.m_Age;
m_Height = new int(*p.m_Height);
}
~Person()
{
if (m_Height != NULL)
{
delete m_Height;
m_Height = NULL;
}
cout << "Person的析构构造函数调用" << endl;
}
int m_Age;
int* m_Height;
};
void test001()
{
Person p1(18,160);
cout << "p1的年龄为:" << p1.m_Age<<"身高为"<<*p1.m_Height << endl;
Person p2(p1);
cout << "p2的年龄为:" << p2.m_Age << "身高为" << *p2.m_Height << endl;
}
int main()
{
test001();
system("pause");
return 0;
}

1.6 初始化列表

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person(int a,int b,int c) :m_A(a), m_B(b), m_C(c)
{
}
void test01()
{
Person p(30,20,10);
}
int m_A;
int m_B;
int m_C;
};
int main()
{
system("pause");
return 0;
}
1.7 类对象作为类成员

#include<iostream>
using namespace std;
#include<string>
class Phone
{
public:
Phone(string pName)
{
m_PName = pName;
cout << "Phone 构造函数" << endl;
}
~Phone()
{
cout << "Phone 析构函数" << endl;
}
string m_PName;
};
class Person
{
public:
Person(string name, string pName) :m_Name(name), m_Phone(pName)
{
cout << "Person构造函数" << endl;
}
~Person()
{
cout << "Person析构函数" << endl;
}
string m_Name;
Phone m_Phone;
};
void test011()
{
Person p ("张三", "华为");
cout << p.m_Name << "拿着" << p.m_Phone.m_PName << endl;
}
int main()
{
test011();
system("pause");
return 0;
}

1.8 静态成员

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
static void func()
{
m_A = 100;
cout << "static void func 调用" << endl;
}
static int m_A;
int m_B;
private:
static void func2()
{
cout << "func2调用" << endl;
}
};
void test011()
{
Person p;
p.func();
Person::func();
}
int main()
{
test011();
system("pause");
return 0;
}
2 C++对象模型和this指针
2.1 成员变量和成员函数分开存储

#include<iostream>
using namespace std;
#include<string>
class Person
{
int m_A;
static int m_B;
void func(){}
static void func2(){}
};
int Person::m_B = 0;
void test011() {
Person p;
cout << "size of p=" << sizeof(p) << endl;
}
void test02() {
Person p;
cout << "size of p=" << sizeof(p) << endl;
}
int main()
{
system("pause");
return 0;
}
2.2 this指针概念

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person(int age)
{
this->age = age;
}
int age;
Person& PersonAddAge(Person& p)
{
this->age += p.age;
return *this;
}
};
void test001()
{
Person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
void test02()
{
Person p1(10);
Person p2(10);
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
2.3 空指针访问成员函数

#include<iostream>
using namespace std;
#include<string>
class Person {
public:
void showClassName() {
cout << "this is Person class" << endl;
}
void showPersonAge()
{
if (this == NULL)
{
return;
}
cout << "agw=" << m_Age << endl;
}
int m_Age;
};
void test011()
{
Person* p = NULL;
p->showClassName();
p->showPersonAge();
}
int main()
{
system("pause");
return 0;
}
2.4 const修饰成员函数

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
void showPerson() const
{
this->m_B = 100;
}
void func()
{
}
int m_A;
mutable int m_B;
};
void test02()
{
const Person p;
p.m_B = 100;
p.showPerson();
}
int main()
{
system("pause");
return 0;
}
2.5 友元

class Building
{
friend void goodGay(Building* building);
public:
Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void goodGay(Building* building)
{
cout << "好基友全局函数 正在访问:"<< building->m_SittingRoom << endl;
cout << "好基友全局函数 正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
Building building;
goodGay(&building);
}
class Building;
class GoodGay {
public:
GoodGay();
void visit();
Building* building;
};
class Building {
friend class GoodGay;
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
building = new Building;
}
void GoodGay::visit()
{
cout << "好基友类正在访问:" << building->m_SittingRoom << endl;
cout << "好基友类正在访问:" << building->m_BedRoom << endl;
}
void test()
{
GoodGay gg;
gg.visit();
}
class Building;
class GoodGay
{
public:
GoodGay();
void visit();
void visit2();
Building* building;
};
class Building
{
friend void GoodGay::visit();
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
building = new Building;
}
void GoodGay::visit()
{
cout << "visit 函数正在访问:" << building->m_SittingRoom << endl;
cout << "visit 函数正在访问:" << building->m_BedRoom << endl;
}
void GoodGay::visit2()
{
cout << "visit 函数正在访问:" << building->m_SittingRoom << endl;
}
int main()
{
}
2.6 运算符重载

2.6.1 加号运算符重载

class Person
{
public:
Person operator+(Person& p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
int m_A;
int m_B;
};
void test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
}
int main()
{
test01();
}
class Person
{
public:
int m_A;
int m_B;
};
Person operator+(Person& p1, Person& p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
void test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
}
int main()
{
test01();
}
2.6.2 左移运算符重载
作用:可以输出自定义数据类型
class Person
{
public:
int m_A;
int m_B;
};
ostream& operator<<(ostream & cout, Person& p)
{
cout << "m_A=" << p.m_A << " m_B=" << p.m_B;
return cout;
}
void test01()
{
Person p;
p.m_A = 10;
p.m_B = 10;
cout << p << endl;;
}
int main()
{
test01();
}
2.6.3 递增运算符重载
作用:通过重载递增运算符,实现自己的整型数据。
class MyInteger
{
friend ostream& operator<<(ostream& cout, 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& cout, MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++myint << endl;
}
void test02()
{
MyInteger myint2;
cout << myint2++ << endl;
cout << myint2 << endl;
}
int main()
{
test01();
}
2.6.4 赋值运算符重载

class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
Person& operator=(Person& p)
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
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;
}
2.6.5 关系运算符

class Person
{
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
bool operator==(Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
bool operator !=(Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return false;
}
return true;
}
string m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1和p2是相等的!" << endl;
}
else
{
cout << "p1和p2是不相等的!" << endl;
}
if (p1 != p2)
{
cout << "p1和p2不是相等的!" << endl;
}
else
{
cout << "p1和p2是相等的" << endl;
}
}
int main()
{
test01();
return 0;
}
2.6.6 函数调用运算符重载

class MyPrint
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
void MyPrint02(string test)
{
cout << test << endl;
}
void test01()
{
MyPrint myprint;
myprint("hello world");
MyPrint02("hello world");
}
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test02()
{
MyAdd myadd;
int ret = myadd(100, 100);
cout << "ret=" << ret << endl;
cout << MyAdd()(100, 100) << endl;
}
int main()
{
test01();
test02();
return 0;
}
|