文章目录
一、内存分区模型
1.1 程序运行前(代码区、全局区)
在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域
代码区:
- 存放函数体的二进制代码(CPU执行的机器指令,由操作系统进行管理)
- 代码区是共享的,目的是对于频繁被执行的程序,内存中只要有一份即可
- 代码区是只读的
全局区:
- 全局变量和静态变量存放在此
- 全局区还包含了常量区,字符串常量和其他常量也存放在此
- 该区域的数据在程序结束后由操作系统释放
1.2 程序运行时(栈区、堆区)
栈区:
- 由编译器自动分配释放,存放函数的形参、局部变量等
- 不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
int* func()
{
int a = 10;
return &a; //返回局部变量的地址,存放在栈区
}
int main()
{
int* p = func();
cout << *p << endl;//10 第一次输出正确是因为编译器做了保留
cout << *p << endl;//-858993460
system("pause");
return 0;
}
堆区:
- 由程序员分配和释放,若不释放,程序结束时由操作系统回收
- 主要利用new在堆区开辟内存
int* func()
{
int* p = new int(10); //指针本质也是局部变量,放在栈上,指针保存的数据放在堆区
return p;
}
void test()
{
int* arr = new int[10];//代表数组有10个元素
for (int i = 0; i < 10; i++)
{
arr[i] = i + 100;
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;
}
delete[] arr;
arr = NULL;
}
int main()
{
int* p = func();
cout << *p << endl;//10
cout << *p << endl;//10
delete p;
p = NULL;
test();
system("pause");
return 0;
}
二、函数重载
满足条件:
1.同一作用域下
2.函数参数名相同
3.函数形参列表不同(参数类型不同、个数不同、顺序不同)
注意事项:
//1.引用作为重载条件
void fun(int& a)
{
cout << "调用fun(int &a)" << endl;
}
void fun(const int& a)
{
cout << "调用fun(const int& a)" << endl;
}
//2.函数重载遇到默认参数
void fun2(int a,int b = 10)
{
cout << "调用fun(int a,int b = 10)" << endl;
}
void fun2(int a)
{
cout << "调用fun(int a)" << endl;
}
int main()
{
//调用fun(int& a)
int a = 10;
fun(a);
//调用fun(const int& a)
fun(10); //int &a = 10不合法
fun2(10); //有二义性,即二义性调用
fun2(10, 20);
system("pause");
return 0;
}
三、类和对象(封装、继承、多态)
1.封装
意义:
- 将属性和行为作为一个整体,表现事物
- 将属性和行为加以权限控制:
//三种权限
//公共权限 public 成员 类内可以访问,类外可以访问
//保护权限 protected 成员 类内可以访问,类外不可以访问
//私有权限 private 成员 类内可以访问,类外不可以访问
//继承中子类可以访问父类保护权限下的属性,但不能访问私有权限中的
class Person
{
string m_Name;//默认是私有权限
};
struct Person1 //C中结构体内不能写函数
{
string m_Name;//默认是公共权限
};
语法:class 类名{访问权限: 属性 / 行为}
2.对象初始化和清理
- 构造函数:主要作用在于创建对象时为成员属性初始化赋值,构造函数由编译器自动调用。(语法: 类名() {}; 可以有参数,因此可重载; 只会调用一次)
- 析构函数:主要作用在于对象销毁前自动调用,执行清理工作(语法: ~类名() {}; 不可以有参数; 只会调用一次 )
(如果不提供构造和析构,编译器会提供,并且是空实现)
//构造函数的分类及调用
//分类方式: 按照参数分类: 无参构造 ;有参构造
// 按照类型分类: 普通构造 ;拷贝构造
class Person
{
public:
int m_age;
//普通构造
Person()
{
m_age = 0;
cout << "Person无参构造函数调用" << endl;
}
Person(int a)
{
m_age = a;
cout << "Person有参构造函数调用" << endl;
}
//拷贝构造函数
Person(const Person &p)
{
//将传入的对象的所有属性拷贝的自己身上
m_age = p.m_age;
cout << "Person拷贝构造函数调用" << endl;
}
~Person()
{
cout << "Person析构函数调用" << endl;
}
};
void test1()
{
//调用
//1.括号法
Person p; //默认构造函数调用
Person p1(10); //有参构造函数调用
Person p2(p1); //拷贝构造函数调用
//注意事项: 调用默认构造函数不要用(),Person p()会被编译器误认为是函数声明
cout << "p1年龄:" << p1.m_age << endl;
cout << "p2年龄:" << p2.m_age << endl;
//2.显示法
Person p; //默认构造函数调用
Person p1 = Person(10); //有参构造函数调用。
Person(10);//直接Person(10)称为匿名对象,执行结束后系统立即回收
Person p2 = Person(p1); //拷贝构造函数调用。
Person(p2);//直接Person(p3)这种方式错误,编译器会认为Person(p2) == Person p2,重定义
//3.隐式转化法
Person p; //默认构造函数调用
Person p1 = 10; //Person p1 = Person(10);
Person p2 = p1; //Person p2 = Person(p1);
//拷贝构造函数调用时机:
//- 使用一个已经创建完毕的对象来初始化一个新对象
//- 值传递方式给函数参数传值
//- 以值方式返回局部对象
}
//如果用户定义有参构造函数,C++不再提供默认无参构造,但会提供默认拷贝函数
//构造如果用户定义拷贝构造函数,C++不会提供其他构造函数
int main()
{
test1();
system("pause");
return 0;
}
- 浅拷贝:简单的复制拷贝操作
- 深拷贝:在堆区重新申请空间,进行拷贝操作。new操作
- 初始化列表初始化属性
Person(int a,int b,int c):m_A(a), m_B(b), m_C(c)
{
}
3.静态成员
静态成员就是在成员变量和成员函数前加上关键字static
- 静态成员变量
所有对象共享同一份数据
在编译阶段分配内存
类内声明,类外初始化
- 静态成员函数
所有对象共享同一份函数
静态成员函数只能访问静态成员变量
class Person
{
public:
static void func() //静态成员函数有访问权限
{
m_A = 200;
m_B = 10;//会报错。静态成员函数只能访问静态成员变量
cout << "static void func调用" << endl;
}
static int m_A;
int m_B;//先创建对象才能访问
};
int Person::m_A = 100;//类外初始化
void test1()
{
Person p;
cout << p.m_A << endl;
Person p1;
p1.m_A = 200;
cout << p.m_A << endl;
cout << p1.m_A << endl; //数据共享
//因此可直接通过类名访问
cout << Person::m_A << endl;
}
void test2()
{
Person p;
p.func();
Person::func();
}
int main()
{
//test1();
test2();
system("pause");
return 0;
}
4.C++对象模型和this指针
成员变量与成员函数是分开存储的,只有非静态成员变量属于类的对象。
4.1 this指针
class Person
{
public:
Person(int age)
{
//age = age;//会输出乱码。解决方案:好好命名qaq
this->age = age; //或通过this指针,指向被调用的成员函数所属的对象,不需要定义直接使用
}
/*void PersonAddAge(Person &p)
{
this->age += p.age;
}*/
Person& PersonAddAge(Person& p) //返回 Person&。不返回引用 会通过拷贝构造函数创建新对象
{
this->age += p.age;
return *this;
}
int age;
};
//this指针用途
//1 解决名称冲突
void test1()
{
Person p1(18);
cout << "p1年龄:" << p1.age << endl;
}
//2 返回对象本身用*this
void test2()
{
Person p1(10);
Person p2(18);
//p2.PersonAddAge(p1);
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);//链式编程思想
cout << "p2年龄:" << p2.age << endl;
}
4.2 空指针访问成员函数
class Person
{
public:
//空指针访问成员函数
void showClassNname()
{
cout << "this is Person Class" << endl;
}
void showPersonAge()
{
if (this == NULL) return;//增强代码健壮性
//会报错,因为传入的指针为空,无法访问属性
cout << "age= " << m_age << endl; //或this->m_age
}
int m_age;
};
void test1()
{
Person* p = NULL;
p->showClassNname();
p->showPersonAge();
}
4.3 const修饰成员函数``
class Person
{
public:
//常函数:
//成员函数后加const后称之为常函数
//常函数内不可以修改成员属性
//成员属性声明时加上关键字mutable后,在常函数中依然可以修改
void showPerson() const //相当于 const Person * const this,所以this指向的值也不可改
{
//this->m_A = 100; //this指针本质是指针常量(指向不可改)
this->m_B = 100;
}
void func()
{
}
int m_A;
mutable int m_B;
};
void test1()
{
Person p;
p.showPerson();
//常对象:
//声明对象前加const称该对象为常对象
//常对象只能调用常函数
const Person p1;
//p1.m_A = 100;
p1.m_B = 10;
p1.showPerson();
//p1.func(); //常对象 不可以调用普通成员函数,因为普通函数可修改属性
}
5.友元
5.1 友元:目的是让一函数或者类访问另一个类中私有成员属性
5.2 友元的三种实现: 关键字 friend
- 全局函数做友元
class Building
{
friend void goodGay(Building* building);//友元
public:
Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
string m_SittingRoom;
private:
string m_BedRoom;
};
//全局函数
void goodGay(Building *building)
{
cout << "好基友全局函数 正在访问:" << building->m_SittingRoom << endl;
cout << "好基友全局函数 正在访问:" << building->m_BedRoom << endl;
}
void test1()
{
Building building;
goodGay(&building);
}
- 类做友元
- 成员函数做友元
class Building;
class GoodGay
{
public:
GoodGay();
void visit(); //函数可以访问Building中私有成员
Building* building;
};
class Building
{
friend class GoodGay; //类做友元
friend void GoodGay::visit();//成员函数做友元
public:
Building();
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 test1()
{
GoodGay gg;
gg.visit();
}
6.运算符重载
运算符重载:对已有运算符重新定义,赋予其另一种功能,适应不同的数据类型
6.1 加号运算符重载 : 实现两个自定义类型数据类型相加运算
//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;
};
//2.全局函数重载+号
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;
}
Person operator+(Person& p1, int num)
{
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test1()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
//成员函数重载本质调用:p3 = p1.operator+(p2)
//全局函数重载本质调用:p3 = operator+(p1,p2)
cout << p3.m_A << " " << p3.m_B << endl;
Person p4 = p1 + 10;
cout << p4.m_A << " " << p4.m_B << endl;
}
6.2 左移运算符重载: 可以输出自定义数据类型
class Person
{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a,int b)
{
m_A = a;
m_B = b;
}
private:
int m_A;
int m_B;
};
//无法利用成员函数重载左移运算符
ostream& operator<<(ostream& out,Person& p)
{
out << "m_A = " << p.m_A << " m_B = " << p.m_B;
return out; //链式编程思想
}
void test1()
{
Person p(10,10);
cout << p << endl;
}
6.3 递增运算符重载: 实现自己的整型数据
class MyInteger //自定义整型变量
{
friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++()
{
m_Num++;
return *this;
}
//重载后置++运算符
//int 代表占位参数,区分前置后置递增
MyInteger& operator++(int)
{
MyInteger* temp = new MyInteger(*this);
m_Num++;
return *temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& cout, MyInteger& myint)
{
cout << "myint= " << myint.m_Num;
return cout;
}
void test1()
{
MyInteger myint;
cout << ++(++myint) << endl;//返回引用的缘故。2
cout << myint << endl;//2
//cout << myint++ << endl;//0
//cout << myint << endl;//1
}
6.4 赋值运算符重载:
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
Person& operator=(Person& p)
{
//编译器提供浅拷贝
//m_Age = p.m_Age;
//应先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
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 test1()
{
Person p1(18);
Person p2(20);
Person p3(22);
//p2 = p1;
p3 = p2 = p1;
cout << "p1年龄:" << *p1.m_Age << endl;
cout << "p2年龄:" << *p2.m_Age << endl;
cout << "p3年龄:" << *p3.m_Age << endl;
}
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;
}
string m_Name;
int m_Age;
};
void test1()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1和p2相等" << endl;
}
else
{
cout << "p1和p2不相等" << endl;
}
}
6.6 函数调用运算符 () 重载:
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test1()
{
MyPrint myPrint;
myPrint("hello world!");
//由于重载运算符()后使用的方式非常像函数的调用,因此称为仿函数
//匿名函数对象
MyPrint()("hello world!");
}
//仿函数非常灵活,没有固定写法
7 继承
7.1 继承方式
- 公共继承
- 保护继承
- 私有继承
class A
{
public:
int a;
protected:
int b;
private:
int c;
};
class B1 : public A
{
public:
int a;
protected:
int b;
//private: //不可访问
//int c;
};
class B2 : protected A
{
protected:
int a;
int b;
//private: //不可访问
//int c;
};
class B3 : private A
{
private:
int a;
int b;
//private://不可访问
//int c;
};
7.2 继承中的对象模型
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son : public Base
{
public:
int m_D;
};
void test1()
{
cout << "size of son = " << sizeof(Son) << endl; //16
//父类中所有的非静态成员属性都会被子类继承下去
//父类中私有成员属性是被编译器隐藏,无法访问
}
7.3 继承中构造和析构顺序
class Base
{
public:
Base()
{
cout << "Base的构造函数" << endl;
}
~Base()
{
cout << "Base的析构函数" << endl;
}
};
class Son : public Base
{
public:
Son()
{
cout << "Son的构造函数" << endl;
}
~Son()
{
cout << "Son的析构函数" << endl;
}
};
void test1()
{
Son son;
//Base的构造函数
//Son的构造函数
//Son的析构函数
//Base的析构函数
}
7.4 继承中同名成员处理方式
class Base
{
public:
Base()
{
m_A = 100;
}
void func()
{
cout << "Base下的成员函数" << endl;
}
void func(int a)
{
cout << "Base下的有参成员函数" << endl;
}
int m_A;
};
class Son : public Base
{
public:
Son()
{
m_A = 200;
}
void func()
{
cout << "Son下的成员函数" << endl;
}
int m_A;
};
void test1()
{
Son s;
cout << "Son下的 m_A = " << s.m_A << endl; //200
cout << "Base下的 m_A = " << s.Base::m_A << endl; //100
s.func(); //"Son下的成员函数"
s.Base::func(); //"Base下的成员函数"
//s.func(100);//子类中的同名成员会隐藏掉父类中所有同名成员函数(包括重载)
s.Base::func(100);
}
7.6 多继承
实际开发不建议使用
class Base1
{
public:
Base1()
{
m_A = 100;
}
int m_A;
};
class Base2
{
public:
Base2()
{
m_A = 200;
}
int m_A;
};
class Son : public Base1, public Base2
{
public:
Son()
{
m_C = 300;
m_D = 400;
}
int m_C;
int m_D;
};
void test1()
{
Son s;
cout << "sizeof son = " << sizeof(s) << endl;//16
cout << "Base1:: m_A = " << s.Base1::m_A << endl;
cout << "Base2:: m_A = " << s.Base2::m_A << endl;
}
7.7 菱形继承
class Animal
{
public:
int m_Age;
};
//菱形继承会造成数据重复,资源浪费
//利用虚继承,解决菱形继承导致的数据重复问题
//继承之前 加上关键字 virtual 变为虚继承,Animal类称为虚基类
//羊类
class Sheep :virtual public Animal{};
//驼类
class Tuo :virtual public Animal{};
//羊驼类Alpaca
class SheepTuo : public Sheep, public Tuo{};
void test1()
{
SheepTuo st;
st.Sheep::m_Age = 18;
st.Tuo::m_Age = 28;
cout << "st.Sheep::m_Age = " << st.Sheep::m_Age << endl; //28
cout << "st.Tuo::m_Age = " << st.Tuo::m_Age << endl; //28
cout << "st.m_Age = " << st.m_Age << endl; //28
}
底层实现
8 多态
多态的优点
- 代码组织结构清晰
- 可读性强
- 利于前期和后期的扩展以及维护
8.1 动态多态
//动态多态满足条件
//1.有继承关系
//2.子类要重写父类虚函数
class Animal
{
public:
/*void speak() //地址早绑定 在编译阶段确定函数地址
{
cout << "动物在说话" << endl;
}*/
virtual void speak() //虚函数,使地址晚绑定
{
cout << "动物在说话" << endl;
}
};
class Cat : public Animal
{
//重写父类虚函数,virtual可写可不写
void speak()
{
cout << "小猫在说话" << endl;
}
};
//动态多态使用
//父类的指针或引用 执行子类对象
void doSpeak(Animal& animal)
{
animal.speak();
}
void test1()
{
Cat cat;
doSpeak(cat);// "动物在说话"
}
多态原理
8.2 纯虚函数和抽象类
在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容,因此可以将虚函数改为纯虚函数。
纯虚函数语法:virtual 返回值类型 函数名(参数列表)= 0;
当类中有了纯虚函数,这个类也成为抽象类。
抽象类特点
- 无法实例化对象
- 子类必须重写抽象类中的纯虚函数,否则也属于抽象类
8.3 虚析构和纯虚析构
多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用子类的析构代码,所以需要将父类中的析构函数改为虚析构或纯虚析构
虚析构和纯虚析构共性
- 可以解决父类指针释放子类对象
- 都需要具体的函数实现
虚析构和纯虚析构区别:如果是纯虚析构,该类属于抽象类,无法实例化对象
虚析构语法:virtual ~类名() {}
纯虚析构语法:virtual ~类名() = 0; 类外实现:类名:: ~类名(){}
四、文件操作
- ofstream:写操作
- ifstream:读操作
- fstream:读写操作
4.1 文本文件 - 以文本的ASCII码形式存储
① 写文件步骤:
ofstream ofs; //创建流对象
ofs.open(“文件路径”,打开方式);
ofs << “写入的数据”<< endl;
ofs.close();
文件的打开方式
ios :: in | 为读文件而打开文件 |
---|---|
ios :: out | 为写文件而打开文件 |
ios :: ate | 初始位置:文件尾 |
ios :: app | 追加方式写文件 |
ios :: trunc | 如果文件存在先删除在创建 |
ios :: binary | 二进制方式 |
文件打开方式可以配合使用,利用 | 操作符
② 读文件步骤:
ifstream ifs; //创建流对象
ifs.open(“文件路径”,打开方式);
读数据,四种方式读取
ifs.close();
void test()
{
ofstream ofs;
ofs.open("test.txt", ios::out);
ofs << "姓名:张三" << endl;
ofs << "性别:男" << endl;
ofs << "年龄:18" << endl;
ofs.close();
//创建流对象
ifstream ifs;
//打开文件并判断是否打开成功
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//读数据
//方式1
char buf1[1024] = { 0 };
while (ifs >> buf1)
{
cout << buf1 << endl;
}
//方式2
char buf2[1024] = { 0 };
while(ifs.getline(buf2,sizeof(buf2)))
{
cout << buf2 << endl;
}
//方式3
string buf3;
while (getline(ifs, buf3))
{
cout << buf3 << endl;
}
//方式4
char c;
while ((c = ifs.get()) != EOF) //每次读一个,判断是否读到文件尾
{
cout << c;
}
//关闭文件
ifs.close();
}
4.2 二进制文件 - 以文本的二进制形式存储
class Person
{
public:
char m_Name[64];
int m_Age;
};
void test()
{
ofstream ofs;
//ofstream ofs("person.txt",ios::out | ios::binary);
ofs.open("person.txt",ios::out | ios::binary);
Person p = { "张三",18 };
ofs.write((const char *)&p,sizeof(Person));
//write函数原型
//ofstream& write(const char* buffer,int len)
//字符指针buffer指向内存中的一段存储空间,len是读写的字节数
ofs.close();
ifstream ifs("person.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
Person p;
ifs.read((char*)&p,sizeof(p));
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
ifs.close();
}