//程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放
//通过文件可以将数据持久化
//C++中对文件操作需要包含头文件//文件流
//文件类型分为两种:
//1.文本文件:文本以文本的ASCII码形式存储在计算机中
//2.二进制文件:文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们
//操作文件的三大类:
//1.ofstream:写操作 o-output f-file
//2.ifstream:读操作 i-input
//3.fstream:读写操作
1.文本文件
1.1写文件
//1.包含头文件#include
//2.创建流对象 ofstream ofs;
//3.打开文件 ofs.open(“文件路径”,打开方式);
//4.写数据 ofs<<“写入的数据”;
//5.关闭文件 ofs.close();
//文件打开方式:
//ios::in 为读文件而打开文件
//ios::out 为写文件而打开文件
//ios::ate 初始位置:文件尾
//ios::app 追加方式写文件
//ios::trunc 如果文件存在先删除,再创建
//ios::binary 二进制方式
//注意:文件打开方式可以配合使用,利用 | 操作符
//例如:用二进制方式写文件 ios::binary | ios::out
//总结:
//文件操作必须包含头文件 fstream
//读文件可以利用 ofstream ,或者fstream类
//打开文件时候需要指定操作文件的路径,以及打开方式
//利用 << 可以向文件中写数据
//操作完毕,要关闭文件
代码:
/*文本文件*/
//写文件
//1.包含头文件#include<fstream>
//2.创建流对象 ofstream ofs;
//3.打开文件 ofs.open("文件路径",打开方式);
//4.写数据 ofs<<"写入的数据";
//5.关闭文件 ofs.close();
//文件打开方式:
//ios::in 为读文件而打开文件
//ios::out 为写文件而打开文件
//ios::ate 初始位置:文件尾
//ios::app 追加方式写文件
//ios::trunc 如果文件存在先删除,再创建
//ios::binary 二进制方式
//注意:文件打开方式可以配合使用,利用 | 操作符
//例如:用二进制方式写文件 ios::binary | ios::out
void test_xwj01()
{
ofstream ofs;
ofs.open("test.txt", ios::out);
ofs << "姓名:张三" << endl;
ofs << "性别:男" << endl;
ofs << "年龄:18" << endl;
ofs.close();
}
//总结:
//文件操作必须包含头文件 fstream
//读文件可以利用 ofstream ,或者fstream类
//打开文件时候需要指定操作文件的路径,以及打开方式
//利用 << 可以向文件中写数据
//操作完毕,要关闭文件
main函数:
/*文本文件*/
//写文件
test_xwj01();
1.2读文件
//1.包含头文件 #include
//2.创建流对象 ifstream ifs;
//3.打开文件并判断文件是否打开成功 ifs.open(“文件路径”,打开方式); 通过ifs.is_open()来判断
//4.读数据 四种方式读取
//5.关闭文件 ifs.close();
//总结:
//读文件可以利用 ifstream ,或者fstream类
//利用is_open函数可以判断文件是否打开成功
//close 关闭文件
代码:
//读文件
//1.包含头文件 #include<fstream>
//2.创建流对象 ifstream ifs;
//3.打开文件并判断文件是否打开成功 ifs.open(“文件路径”,打开方式); 通过ifs.is_open()来判断
//4.读数据 四种方式读取
//5.关闭文件 ifs.close();
void test_dwj01()
{
ifstream ifs;
ifs.open("test.txt",ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//读数据
第一种
//char buf[1024] = { 0 };
//while (ifs >> buf)//一行一行的读,读到头以后返回一个false
//{
// cout << buf << endl;
//}
第二种
//char buf[1024] = { 0 };
//while (ifs.getline(buf, sizeof(buf)))
//{
// cout << buf << endl;
//}
第三种
//string buf;
//while (getline(ifs, buf))
//{
// cout << buf << endl;
//}
//第四种,不推荐用
char c;
while ((c = ifs.get()) != EOF)//EOF是end of file文件尾,看是否读到文件尾
{
cout << c;
}
ifs.close();
}
//总结:
//读文件可以利用 ifstream ,或者fstream类
//利用is_open函数可以判断文件是否打开成功
//close 关闭文件
main函数:
/*文本文件*/
//写文件
test_dwj01();
2.二进制文件
//以二进制的方式对文件进行读写操作
//打开方式要指定为:ios::binary
2.1写文件
//二进制方式写文件主要利用流对象调用成员函数write
//函数原型ostream& write(const char * buffer,int len);
//参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
//总结:
//文件输出流对象 可以通过write函数,以二进制方式写数据
代码:
//写文件
//二进制方式写文件主要利用流对象调用成员函数write
//函数原型ostream& write(const char * buffer,int len);
//参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
class Person_xwj
{
public:
public:
char m_Name[64];//姓名 不要用string,可能会出现问题,要用char
int m_Age;//年龄
};
void test_xwj02()
{
ofstream ofs;
ofs.open("person.txt", ios::out | ios::binary);
Person_xwj p = { "张三",18 };
ofs.write((const char *)&p, sizeof(Person_xwj));
ofs.close();
}
//总结:
//文件输出流对象 可以通过write函数,以二进制方式写数据
main函数:
/*二进制文件*/
//写文件
test_xwj02();
2.2读文件
//二进制方式读文件主要利用流对象调用成员函数read
//函数原型:istream& read(char *buffer, int len);
//参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
代码:
//读文件
//二进制方式读文件主要利用流对象调用成员函数read
//函数原型:istream& read(char *buffer, int len);
//参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
class Person_dwj
{
public:
char m_Name[64];
int m_Age;
};
void test_dwj02()
{
ifstream ifs;
ifs.open("person.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
Person_dwj p;
ifs.read((char*)&p, sizeof(Person_dwj));
cout << "姓名:" << p.m_Name << "\t年龄:" << p.m_Age << endl;
ifs.close();
}
main函数:
/*二进制文件*/
//读文件
test_dwj02();
代码:
#include <iostream>
#include<string>
#include "point.h"
#include "circle2.h"
#include <fstream>
using namespace std;
//创建全局变量
int global_a = 10;
int global_b = 10;
//const修饰的全局变量=全局常量
const int c_g_a = 10;
const int c_g_b = 10;
//栈区
int * func()
{
int a = 10;//局部变量 存放在栈区,栈区的数据在函数执行完后自动释放
return &a;//返回局部变量的地址
}
int * func2(int b)//形参数据也会放在栈区
{
b = 100;
int a = 10;//局部变量 存放在栈区,栈区的数据在函数执行完后自动释放
return &a;//返回局部变量的地址
}
//堆区
int * func3()
{
//利用new关键字,可以将数据开辟到堆区
//指针 本质也是局部变量,放在栈区,但是指针保存的数据是放在堆区
int*p = new int(10);//new int(10)返回整型数据10的地址
return p;
}
//new
int * func4()
{
//在堆区创建整型数据
//new返回的是该数据类型的指针
//语法:new 数据类型(变量值),这是创建一个变量
int *p_new = new int(10);
return p_new;
}
void test01()
{
int*p = func4();
cout << *p << endl;//输出10
cout << *p << endl;//10
cout << *p << endl;//10
//堆区的数据 由程序员管理开辟,程序员管理释放
//如果想释放堆区的数据,用delete释放 语法:delete 指针变量名称;
delete p;
//cout << *p << endl;//内存已经被释放,再次访问就是非法操作,会报错
}
//在堆区利用new开辟数组
void test02()
{
//创建10个整型数据的数组,在堆区
//语法: new 数据类型[数据值],这是创建数据值个变量
int *arr = new int[10];//10代表数组有10个元素
for (int i = 0; i < 10; i++)
{
arr[i] = i + 100;//给10个元素赋值:100~109
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;
}
//释放数组
//语法:delete[] 指针变量名;
delete[]arr;
}
//引用做函数参数
//交换函数
//1.值传递
void mySwap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "swap01\ta=" << a << "\tb=" << b << endl;//值传递形参发生改变
return;
}
//2.地址传递
void mySwap02(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
return;
}
//3.引用传递
void mySwap03(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
return;
}
/*引用做函数返回值*/
//语法:返回值类型 & 函数名称(参数列表) {函数体语句;return语句;}
//作用:引用是可以作为函数的返回值存在的
//1.不要返回局部变量的引用
int& test03()
{
int a = 10;//局部变量存放在栈区
return a;
}
//2.函数的调用可以作为左值
int& test04()
{
static int a = 10;//静态变量,存放在全局区,全局区上的数据在程序结束后系统释放
return a;
}
//引用的本质
//发现是引用,转换为 int* const ref = &a;
void func5(int& ref)
{
ref = 100;
// ref是引用,转换为*ref = 100
cout << "func5\tref=" << ref << endl;
}
//打印数据函数
void showValue(int &val)
{
val = 1000;
cout << "value=" << val << endl;
}
//打印数据函数2
void showValue2(const int &val)
{
//val = 1000;//错误,不可修改
cout << "value=" << val << endl;
}
/*函数默认参数*/
int func6(int a, int b=20, int c=30)
{
return a + b + c;
}
int func7(int a, int b=1, int c=2 ) //1.如果某个位置已经有了默认参数,那么从这个位置往后都必须有默认值
{
return a + b + c;
}
//2.如果函数声明有默认参数,函数实现就不能有默认参数
//函数声明和函数实现只能有一个有默认参数,两者任选一个给默认值就可以
//int func8(int a=10, int b=20);//函数声明
int func8(int a=10, int b = 20)//函数实现
{
return a + b ;
}
/*占位参数*/
//占位参数还可以有默认值 比如:void func9(int a,int =10)
void func9(int a,int)
{
cout << "this is func9" << endl;
}
/*函数重载*/
//可以让函数名相同,提高复用性
//函数重载满足条件:
//1.同一个作用域下
//2.函数名称相同
//3.函数参数类型不同或者个数不同或者顺序不同
void funct()
{
cout << "funct的调用" << endl;
}
void funct(int a)
{
cout << "funct(int a)的调用" << endl;
}
void funct(double a)
{
cout << "funct(double a)的调用" << endl;
}
void funct(int a, double b)
{
cout << "funct(int a,double b)的调用:" << endl;
}
void funct(double a, int b)
{
cout << "funct(double a, int b)的调用:" << endl;
}
//注意:函数的返回值不可以作为函数重载的条件 int跟void返回值不同,不可以作为重载的条件
//int funct(double a, int b)
//{
// cout << "funct(double a, int b)的调用:" << endl;
//}
//函数重载注意事项
//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)
{
cout << "fun2(int a)的调用" << endl;
}
void fun2(int a,int b=10)
{
cout << "fun2(int a)的调用" << endl;
}
/*类和对象*/
/*封装*/
//将属性和行为作为一个整体,表现生活中的事物
//语法:class 类名 { 访问权限: 属性;行为;};
//圆周率
const double PI = 3.14;
//设计一个圆类,求圆的周长
//圆的周长公式:2 * PI *半径
//class 代表设计一个类,类后面紧跟着的是类名称
class Circle
{
//访问权限
public://公共权限
//属性
int m_r;//半径
//行为,通常用函数来表示
//获取圆的周长
double calculateZC()
{
return 2 * PI*m_r;
}
};
//设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
//设计学生类
class Student
{
//公共权限
public:
//属性
string m_Name;//姓名
int m_ID;//学号
//行为
//显示姓名和学号
void showStudent()
{
cout << "姓名:"<< m_Name << "\t学号:" << m_ID << endl;
}
//给姓名赋值
void setName(string name)
{
m_Name = name;
}
//给学号赋值
void setID(int ID)
{
m_ID = ID;
}
};
//将属性和行为加以权限控制
//public 公共权限 成员在类内可以访问,类外也可以访问
//protected 保护权限 成员在类内可以访问,类外不可以访问 儿子也可以访问父亲中的保护内容
//private 私有权限 成员在类内可以访问,类外不可以访问 儿子不可以访问父亲中的私有内容
class Person
{
public://公共权限
//姓名
string m_Name;
protected://保护权限
//汽车
string m_Car;
private://私有权限
//银行卡密码
int m_Password;
public:
void func()
{
m_Name = "张三";
m_Car = "拖拉机";
m_Password = 123456;
}
};
/*struct和class区别*/
//struct默认权限为公共
//class默认权限为私有
class C1
{
int m_A;//默认权限是私有
};
struct C2
{
int m_A;//默认权限是公共
};
/*成员属性设置为私有*/
//优点1:将所有成员属性设置为私有,可以自己控制读写权限
//优点2:对于写权限,我们可以检测数据的有效性
//设计人的类
class Person1
{
public:
//写姓名=设置姓名
void setName(string name)
{
m_Name = name;
}
//读姓名=获取姓名
string getName()
{
return m_Name;
}
//获取年龄 可读可写 如果想修改(年龄的范围必须是0-150之间)
int getAge()
{
//m_Age = 0;//初始化为0岁
return m_Age;
}
//设置年龄
void setAge(int age)
{
if (age < 0 || age>150)
{
m_Age = 0;
cout << "你这个老妖精!" << endl;
return;//没有return的话会往下执行m_Age=age赋值操作,有return以后直接退出
}
m_Age = age;
}
//设置情人 只写
void setLover(string lover)
{
m_Lover = lover;
}
private:
//姓名 可读可写
string m_Name;
//年龄 可读可写 如果想修改(年龄的范围必须是0-150之间)
int m_Age;
//情人 只写
string m_Lover;
};
/*设计长方体类*/
//1.创建长方体类
//2.设计属性
//3.设计行为 获取长方体面积和体积
//4.分别利用全局函数和成员函数 判断两个长方体是否相等
class Cube
{
public:
//设置长
void setL(int l)
{
m_L = l;
}
//获取长
int getL()
{
return m_L;
}
//设置宽
void setW(int w)
{
m_W = w;
}
//获取宽
int getW()
{
return m_W;
}
//设置高
void setH(int h)
{
m_H = h;
}
//获取高
int getH()
{
return m_H;
}
//获取长方体面积
int calculateS()
{
return 2 * m_L*m_H + 2 * m_H*m_W + 2 * m_W*m_L;
}
//获取长方体体积
int calculateV()
{
return m_H * m_L*m_W;
}
//利用成员函数判断两个长方体是否相等
bool isSameByClass(Cube &cube)
{
//用getL()与cube.getL()判断或者m_L与cube.getL()判断
if (getL() == cube.getL() && m_W == cube.getW() && m_H == cube.getH())
{
return true;
}
else
return false;
}
private:
int m_L;//长
int m_W;//宽
int m_H;//高
};
//利用全局函数判断两个长方体是否相等
bool isSame(Cube &cube1, Cube &cube2)
{
if (cube1.getL() == cube2.getL() && cube1.getW() == cube2.getW() && cube1.getH() == cube2.getH())
{
return true;
}
else
return false;
}
/*点和圆的关系案例*/
//根据点到圆心的距离 与 半径作比较来判断
点类
//class Point
//{
//public:
// //设置x
// void setX(int x)
// {
// m_X = x;
// //return;
// }
// //获取x
// int getX()
// {
// return m_X;
// }
// //设置y
// void setY(int y)
// {
// m_Y = y;
// //return;
// }
// //获取y
// int getY()
// {
// return m_Y;
// }
//
//private:
// int m_X;
// int m_Y;
//};
圆类
//class Circle2
//{
//public:
// //设置半径
// void setR(int r)
// {
// m_R = r;
// //return;
// }
// //获取半径
// int getR()
// {
// return m_R;
// }
// //设置圆心
// void setCenter(Point center)
// {
// m_Center = center;
// //return;
// }
// //获取圆心
// Point getCenter()
// {
// return m_Center;
// }
//
//private:
// int m_R;//半径
//
// //可以用一个点类来表示
// //int m_X;//x坐标
// //int m_Y;//y坐标
// //圆心
// //在类中可以让另一个类作为本类的成员
// Point m_Center;
//};
//判断点和圆的关系
void isInCircle(Circle2 &c, Point &p)
{
//计算两点之间距离的平方
int distance =
(c.getCenter().getX() - p.getX()) * (c.getCenter().getX()-p.getX())
+ (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
//计算半径的平方
int rDistance = c.getR() * c.getR();
//判断关系
if (distance == rDistance)
{
cout << "点在圆上" << endl;
}
else if (distance > rDistance)
{
cout << "点在圆外" << endl;
}
else
{
cout << "点在圆内" << endl;
}
}
/*对象的初始化和清理*/
//生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时候也会删除一些自己信息数据保证安全
//C++中的面向对象来源于生活,每个对象也都会有初始设置以及 对象销毁前的清理数据的设置。
/*构造函数和析构函数*/
//一个对象或者变量没有初始状态,对其使用后果是未知
//同样的使用完一个对象或变量,没有及时清理,也会造成一定的安全问题
//c++利用了构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作。
//对象的初始化和清理工作是编译器强制要我们做的事情,因此如果我们不提供构造和析构,编译器会提供
//编译器提供的构造函数和析构函数是空实现。
//构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
//析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。
//构造函数语法: 类名(){}
//1. 构造函数,没有返回值也不写void
//2. 函数名称与类名相同
//3. 构造函数可以有参数,因此可以发生重载
//4. 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
//析构函数语法: ~类名(){}
//1. 析构函数,没有返回值也不写void
//2. 函数名称与类名相同,在名称前加上符号 ~
//3. 析构函数不可以有参数,因此不可以发生重载
//4. 程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
class Person_gz
{
public:
//1.构造函数
//没有返回值,不用写void
//函数名与类名相同
//构造函数有参数,可以发生重载
//创建对象的同时,构造函数会自动调用,而且只调用一次
Person_gz()
{
cout << "Person构造函数的调用" << endl;
}
//2.析构函数
//没有返回值,不用写void
//函数名与类名相同,在名称前加~
//析构函数不可以有参数,不可以发生重载
//对象在销毁前会自动调用析构函数,而且只会调用一次
~Person_gz()
{
cout << "Person析构函数的调用" << endl;
}
};
//构造和析构都是必须有的实现,如果我们自己不提供,编译器会提供一个空实现的构造和析构函数
void test_gz()
{
Person_gz person_gz; //在栈上的数据,test_gz执行完毕后,会释放这个对象
}
/*构造函数的分类及调用*/
//按参数分为: 有参构造和无参构造
//按类型分为: 普通构造和拷贝构造
//三种调用方式:
//括号法
//显示法
//隐式转换法
//1.按照参数分类 无参构造(=默认构造)和有参构造
//2.按照类型分类 普通构造 和拷贝构造
class Person_fl
{
public:
//构造函数
Person_fl() //无参构造函数=默认构造函数
{
cout << "Person_fl的无参构造函数调用" << endl;
}
Person_fl(int a)//有参构造函数
{
age = a;
cout << "Person_fl的有参构造函数调用" << endl;
}
//拷贝构造函数
//拷贝函数语法: 类名(const 类名 & 引用名称){ }
Person_fl(const Person_fl &p)
{
//将传入的人身上的所有属性,拷贝到我身上
age = p.age;
cout << "Person_fl的拷贝函数调用" << endl;
}
//析构函数
~Person_fl()
{
cout << "Person_fl的析构函数调用" << endl;
}
public:
int age;
};
void test_fl()
{
//1.括号法
Person_fl person_fl1; //默认构造函数的调用,不要加()
Person_fl person_fl2(10);//有参构造函数的调用
Person_fl person_fl3(person_fl2); //拷贝构造函数的调用
//注意事项
//调用默认构造函数时候,不要加()
//因为加了括号以后,编译器会认为是一个函数的声明,比如 Person p1();与void func();所以不会认为是在创建对象
//Person_fl person_fl1(); //错误,不要加(),会认为是函数声明
cout << "person_fl2的年龄为:" << person_fl2.age << endl;
cout << "person_fl3的年龄为" << person_fl3.age << endl;
//2.显示法
Person_fl person_fl4;
Person_fl person_fl5 = Person_fl(10);//有参构造, Person_fl(10)是匿名对象,当前行执行结束后,系统会立即回收掉匿名对象
Person_fl person_fl6 = Person_fl(person_fl5);//拷贝构造
//注意事项
//不要利用拷贝函数来初始化匿名对象
//Person_fl(person_fl5); //错误,编译器会认为Person_fl(person_fl5);等价于Person_fl person_fl5;,编译器会认为是对象声明,会报重定义错误
Person_fl(10);// Person_fl(10)是匿名对象,当前行执行结束后,系统会立即回收掉匿名对象即立即执行析构函数,执行完析构函数后再往下执行
cout << "aaaa" << endl;
//3.隐式转换法
Person_fl person_fl7 = 10;//相当于Person_fl person_fl7 = person_fl7(10); 有参构造
Person_fl person_fl8 = person_fl7;//拷贝构造
}
/*拷贝构造函数调用时机*/
//C++中拷贝构造函数调用时机通常有三种情况
//使用一个已经创建完毕的对象来初始化一个新对象
//值传递的方式给函数参数传值
//以值方式返回局部对象
class Person_kb
{
public:
Person_kb()
{
cout << "Person_kb默认构造函数调用" << endl;
}
~Person_kb()
{
cout << "Person_kb析构函数调用" << endl;
}
Person_kb(int age)
{
cout << "Person_kb有参构造函数调用" << endl;
m_Age = age;
}
Person_kb(const Person_kb &p)
{
cout << "Person_kb拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
public:
int m_Age;
};
//1.使用一个已经创建完毕的对象来初始化一个新对象
void test_kb1()
{
Person_kb person_kb1(20);
Person_kb person_kb2(person_kb1);
cout << "person_kb2的年龄为:" << person_kb2.m_Age << endl;
return;
}
//2.值传递的方式给函数参数传值,值传递形参不会影响实参
void doWork(Person_kb p)
{
return;
}
void test_kb2()
{
Person_kb person_kb3;//无参构造函数
doWork(person_kb3);//拷贝构造函数
return;
}
//3.值方式返回局部对象
Person_kb doWork2()
{
Person_kb p1; //默认构造函数
cout << (int*)&p1 << endl;
return p1;
}
void test_kb3()
{
Person_kb person_kb4 = doWork2(); //拷贝构造函数,相当于Person_kb person_kb4=p1的副本
cout << (int*)&person_kb4 << endl;
return;
}
/*构造函数调用规则*/
//默认情况下,c++编译器至少给一个类添加3个函数
//1.默认构造函数(无参,函数体为空)
//2.默认析构函数(无参,函数体为空)
//3.默认拷贝构造函数,对属性进行值拷贝
//构造函数调用规则如下:
//如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
//如果用户定义拷贝构造函数,c++不会再提供其他构造函数
class Person_gzz
{
public:
Person_gzz()
{
cout << "person_gzz的默认构造函数" << endl;
}
~Person_gzz()
{
cout << "Person_gzz的析构函数" << endl;
}
Person_gzz(int age)
{
cout << "Person_gzz的有参构造函数" << endl;
m_Age = age;
}
/*Person_gzz(const Person_gzz &p)
{
cout << "Person_gzz的拷贝构造函数" << endl;
m_Age = p.m_Age;
}*/ //如果用户没有定义拷贝构造函数,编译器会进行值拷贝m.Age=p.m.Age;
public:
int m_Age;
};
void test_gzz()
{
Person_gzz person_gzz1;//默认构造函数
person_gzz1.m_Age = 18;
Person_gzz person_gzz2(person_gzz1);//拷贝构造函数
cout << "person_gzz2的年龄为:" << person_gzz2.m_Age << endl;
return;
}
//如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
//如果用户定义拷贝构造函数,c++不会再提供其他构造函数
void test_gzz2()
{
Person_gzz person_gzz3(28);
Person_gzz person_gzz4(person_gzz3);//编译器提供默认的拷贝构造函数
cout << "person_gzz4的年龄为:" << person_gzz4.m_Age << endl;
return;
}
/*深拷贝与浅拷贝*/
//浅拷贝:简单的赋值拷贝操作(编译器提供的拷贝构造函数)
//深拷贝:在堆区重新申请空间,进行拷贝操作
class Person_sq
{
public:
Person_sq()
{
cout << "Person_sq的默认构造函数" << endl;
}
~Person_sq()
{
//析构代码,将堆区开辟的数据做释放操作
//浅拷贝带来的问题是堆区的内存重复释放,会崩溃
if (m_Height != NULL)
{
delete m_Height;
m_Height = NULL;//置空操作,防止野指针出现
}
cout << "Person_sq的析构函数" << endl;
}
Person_sq(int age, int height)
{
m_Age = age;
m_Height = new int(height);
cout << "Person_sq的有参构造函数" << endl;
}
//自己实现拷贝构造函数,解决浅拷贝带来的问题
//如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题
Person_sq(const Person_sq &p)
{
cout << "Person_sq拷贝构造函数的调用" << endl;
m_Age = p.m_Age;
//m_Height = p.m_Height;//编译器默认实现的就是这行代码
//深拷贝操作
m_Height = new int(*p.m_Height);//在堆区利用new重新开辟一个内存空间,让m_Height重新指向高度值
}
public:
int m_Age;//年龄
int *m_Height;//审稿
};
void test_sq1()
{
Person_sq person_sq1(200,180);
cout << "person_sq1的年龄为:" << person_sq1.m_Age << "\tperson_sq1的身高为:" << *person_sq1.m_Height << endl;
Person_sq person_sq2(person_sq1);//数据先进后出,因此person_sq2先被析构
cout << "person_sq2的年龄为:" << person_sq2.m_Age << "\tperson_sq2的身高为:" << *person_sq2.m_Height << endl;
return;
}
/*初始化列表*/
//作用:C++提供了初始化列表语法,用来初始化属性
//语法:构造函数():属性1(值1),属性2(值2)...{}
//构造函数(变量类型1 变量名1,变量类型2 变量名2...):属性1(变量名1),属性2(变量名2)...{}
class Person_csh
{
public:
传统初始化操作
//Person_csh(int a, int b, int c)
//{
// m_A = a;
// m_B = b;
// m_C = c;
//}
//初始化列表来初始化属性
/*Person_csh() :m_A(10), m_B(20), m_C(30)
{
}*/
Person_csh(int a, int b, int c) :m_A(a), m_B(b), m_C(c)
{
}
public:
int m_A;
int m_B;
int m_C;
};
void test_csh()
{
Person_csh person_csh(10, 20, 30);
//Person_csh person_csh;
cout << "m_A=" << person_csh.m_A << "\tm_B=" << person_csh.m_B << "\tm_C=" << person_csh.m_C << endl;
return;
}
/*类对象作为类成员*/
//C++类中的成员可以是另一个类的对象,我们成该成员为对象成员
//当其他类对象作为本类成员,本类对象会先构造其他类对象再构造自身,即本例是先构造Phone,再构造Person
//先进后出,析构函数正好相反,即先析构自身再析构其他类
class A
{
};
class B
{
A a;//实例化对象A
};
//B类中有对象A作为成员,A为对象成员
//手机类
class Phone
{
public:
Phone(string pName)
{
m_Pname = pName;
cout << "Phone的构造函数调用" << endl;
}
~Phone()
{
cout << "Phone的析构函数调用" << endl;
}
public:
//品牌名称
string m_Pname;
};
//人类
class Person_l
{
public:
//隐式转换法 m_Phone(pName)相当于 Phone m_Phone = pName
Person_l(string name, string pName):m_Name(name),m_Phone(pName) //此时的m_Phone(pName)相当于 Phone m_Phone=pName
{
cout << "Person_l的构造函数调用" << endl;
}
~Person_l()
{
cout << "Person_l的析构函数调用" << endl;
}
public:
//姓名
string m_Name;
//手机
Phone m_Phone;
};
void test_l()
{
Person_l p("张三", "苹果");
cout << p.m_Name << "拿着" << p.m_Phone.m_Pname << "品牌的手机" << endl;
return;
}
/*静态成员*/
//静态成员就是在成员变量和成员函数前加上关键字static,成为静态成员
//静态成员分类:
//静态成员变量
//1.所有对象共用同一份数据
//2.在编译阶段分配内存(全局区)
//3.类内声明,类外初始化
//静态成员函数
//1.所有对象共享同一个函数
//2.静态成员函数只能访问静态成员变量
class Person_jt
{
public:
public:
//所有对象都共享同一份数据
//编译阶段就分配内存
//类内声明,类外初始化操作
static int m_A;//类内声明
//静态成员变量也是有访问权限的
private:
static int m_B;
};
int Person_jt::m_A = 100;//类外初始化操作
int Person_jt::m_B = 300;
void test_jt()
{
Person_jt p;
//100
cout << p.m_A << endl;
Person_jt p2;
p2.m_A = 200;//所有对象共享同一份数据
//200
cout << p.m_A << endl;
}
void test_jt2()
{
//静态成员变量 不属于某个对象上 所有对象都共享同一份数据
//因此静态成员变量有两种访问方式
//1.通过对象进行访问
Person_jt p;
cout << "通过对象进行访问:" << endl;
cout << p.m_A << endl;
//2.通过类名进行访问
cout << "通过类名进行访问:" << endl;
cout << Person_jt::m_A << endl;
//cout << Person_jt::m_B << endl; //错误,私有权限访问不到
}
//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量
class Person_j
{
public:
static void func()
{
m_A = 1000;//静态成员函数可以访问静态成员变量
//m_B = 200;//错误,静态成员函数不可以访问非静态成员变量,因为无法区分到底是哪个对象的m_B属性
cout << "static void func调用" << endl;
}
public:
static int m_A;//静态成员变量
int m_B;//非静态成员变量
//静态成员函数也是有访问权限的
private:
static void func2()
{
cout << "static void func2函数调用" << endl;
}
};
int Person_j::m_A = 0;
//有两种访问方式
void test_j()
{
//1.通过对象访问
Person_j p;
p.func();
//2.通过类名访问
Person_j::func();
//Person_j::func2(); //错误,类外无法访问私有静态成员函数
}
/*C++对象模型和this指针*/
/*成员变量和成员函数分开存储*/
//1.在C++中,类内的成员变量和成员函数分开存储
//2.只有非静态成员变量才属于类的对象上
class Person_zz
{
public:
void func()
{
}//非静态成员函数不属于类的对象
static void func2()
{
}//静态成员函数不属于类的对象
public:
int m_A;//非静态成员变量属于类的对象
static int m_B;//静态成员变量不属于类的对象
};
int Person_zz::m_B = 0;//静态成员变量类内声明类外初始化
void test_zz1()
{
Person_zz p;
//空对象占用内存空间为:1
//C++编译器会给每个空对象也分配一个字节空间,是为了区分空对象占用内存的位置
//每个空对象也应该有一个独一无二的内存地址
cout << "zize of p=" << sizeof(p) << endl;
}
void test_zz2()
{
Person_zz p;
cout << "zize of p=" << sizeof(p) << endl;
return;
}
/*this指针概念*/
//每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
//问题是:这一块代码是如何区分哪个对象调用自己的呢?
//C++通过提供特殊的对象指针-this指针,解决上述问题
//this指针指向被调用的成员函数所属的对象
//this指针是隐含每一个非静态成员函数内的一种指针
//this指针不需要定义,直接使用即可
//this指针的用途:
//1.当形参和成员变量同名时,可用this指针来区分
//2.在类的非静态成员函数中返回对象本身,可使用return *this;
class Person_this
{
public:
Person_this(int age)
{
//this指针指向被调用的成员函数所属的对象
//本例是:this指向被调用的Person_this成员函数所属的对象p1,即this指向p1
//Person_this::age = age;正确,用这样也对,说明age是Person_this作用域下的
this->age = age;
}
Person_this& PersonAddAge(Person_this &p) //返回p2的本体要用引用的方式 Person_this &,返回值类型一定要用引用
//如果返回值类型是Person_this相当于是拷贝函数,会另外复制一份新的数据,输出将一直为20,无法实现累加
{
this->age += p.age;//自身的年龄=自身的年龄+p的年龄
//this指向p2的指针,*this指向的就是p2这个对象的本体
return *this;
}
public:
int age;
};
//1.解决名称冲突
void test_this01()
{
Person_this p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
//2.返回对象本身用*this
void test_this02()
{
Person_this p1(10);
Person_this p2(10);
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
/*空指针访问成员函数*/
//C++中空指针也可以调用成员函数的,但是也要注意有没有用到this指针
//如果用到this指针,需要加以判断保证代码的健壮性
class Person_k
{
public:
void showClassName()
{
cout << "this is Person class" << endl;
return;
}
void showPersonAge()
{
//报错,因为传入的指针是NULL空指针,无法访问属性
//纠正如下:
if (this == NULL)
{
return;
}//提高代码的健壮性
cout << "Person's age is:" << m_Age << endl;//此时的m_Age相当于this->m_Age,但是此时是空指针,无法访问其中的属性,是无中生有
return;
}
public:
int m_Age;
};
void test_k01()
{
Person_k *p = NULL;//空指针
p->showClassName();//通过空指针调用成员函数
p->showPersonAge();
return;
}
/*const修饰成员函数*/
//常函数:(只读状态)
//1.成员函数后加const后称这个函数为常函数 语法:返回值类型 函数名() const {}
//2.常函数内不可以修改成员属性
//3.成员属性声明时加关键字mutable后,在常函数中依然可以修改
//常对象:
//1.声明对象前加const称该对象为常对象
//2.常对象只能调用常函数
class Person_c
{
public:
//this指针的本质是指针常量,相当于Person * const this; 指针的指向是不可以修改的
//如果想让指针指向的值也不可以修改: const Person * const this; 对应常函数如下:
//在成员函数后面加const,修饰的是this指针,让指针指向的值也不可以修改
void showPerson() const//相当于 const Person * const this;
{
this->m_B = 100;
//m_A = 100;//m_A=100;相当于this->m_A=100;
//this = NULL;//错误,this指针不可以修改指针的指向
return;
}
void func()
{
return;
}
public:
int m_A;
mutable int m_B;//特殊变量,即使在常函数中也可以修改这个值,加关键字mutable
};
//常对象
void test_c()
{
const Person_c p;//在对象前加const变为常对象
//常对象也不允许修改指针指向的值
//p.m_A = 100;
p.m_B = 100;//m_B是特殊值,在常对象下也可以修改
//常对象只能调用常函数
p.showPerson();
//p.func();//常对象不可以调用普通成员函数,因为普通成员函数可以修改属性
return;
}
/*友元*/
//在程序里,有些私有属性也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术
//友元的目的就是让一个函数或者类访问另一个类中的私有成员
//友元的关键字为 friend
//友元的三种实现
//1.全局函数做友元
//2.类做友元
//3.成员函数做友元
//全局函数做友元
//建筑物类
class Building_y
{
//goodGay全局函数是Building的好朋友,可以访问Building中的私有属性
//语法: friend 全局函数; 相当于 friend 返回值类型 函数名(参数列表);
friend void goodGay(Building_y *building);
public:
Building_y()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
return;
}
public:
string m_SittingRoom;//客厅
private:
string m_BedRoom;//卧室
};
//全局函数
void goodGay(Building_y *building)//指针传递
{
cout << "好基友的全局函数正在访问:" << building->m_SittingRoom << endl;
cout << "好基友的全局函数正在访问:" << building->m_BedRoom << endl;
return;
}
void test_y01()
{
Building_y building;
goodGay(&building);//通过&把地址传进去
return;
}
//类做友元
//语法:friend class 类名;
class Building_yy;//告诉编译器一会儿我会写一个Building类,先别给我报错
class GoodGay
{
public:
void visit();//参观函数 访问Building中的属性
GoodGay();
private:
Building_yy * building;
};
class Building_yy
{
friend class GoodGay;//GoodGay类是本类的好朋友,可以访问本类的私有成员
public:
Building_yy();
public:
string m_SettingRoom;//客厅
private:
string m_BedRoom;//卧室
};
//类外写成员函数
Building_yy::Building_yy()//加上作用域,告诉编译器是那个作用下的构造函数
{
m_SettingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
//创建建筑物对象
building = new Building_yy;
}
void GoodGay::visit()
{
cout << "好基友类正在访问:" << building->m_SettingRoom << endl;
cout << "好基友类正在访问:" << building->m_BedRoom << endl;
}
void test_yy01()
{
GoodGay gg;
gg.visit();
return;
}
//成员函数做友元
//先声明Building类,再写GoodGAY类,最后写Building类,GoodGAY::visit要写到类外。否则会报错
//如果在GoodGAY类内写visit函数会报错,因为此时Building并未定义成员变量等,无法调用Building里的成员变量
//程序自上向下执行,声明定义顺序别弄错,visit函数要类外实现
class Building;
class GoodGAY
{
public:
GoodGAY();
void visit_yy01();//让visit_yy01函数可以访问Building中私有成员
void visit_yy02();//让visit_yy02函数不可以访问Building中私有成员
public:
Building *building;
};
class Building
{
//告诉编译器,GoodGAY类下的visit_yy01成员函数作为本类的好朋友,可以访问私有成员
friend void GoodGAY::visit_yy01();
public:
Building();
public:
string m_SettingRoom;//客厅
private:
string m_BedRoom;//卧室
};
//类外实现成员函数
Building::Building()
{
m_SettingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGAY::GoodGAY()
{
building = new Building;//用new在堆区创建一个指针,并用building来维护这个对象
}
void GoodGAY::visit_yy01()
{
cout << "visit_yy01函数正在访问:" << building->m_SettingRoom << endl;
cout << "visit_yy01函数正在访问:" << building->m_BedRoom << endl;
}
void GoodGAY::visit_yy02()
{
cout << "visit_yy02函数正在访问:" << building->m_SettingRoom << endl;
//cout << "visit_yy02函数正在访问:" << building->m_BedRoom << endl;
}
void test_yy()
{
GoodGAY gg;
gg.visit_yy01();
gg.visit_yy02();
}
/*运算符重载*/
//运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
/*加号运算符重载*/
//对于内置的数据类型,编译器知道如何进行运算
//作用:实现两个自定义数据类型相加的运算
class Person_jh
{
public:
1.成员函数重载+号
语法: 返回值类型 operator+(参数列表){};
//Person_jh operator+(Person_jh &p)
//{
// Person_jh temp;
// temp.m_A = this->m_A + p.m_A;
// temp.m_B = this->m_B + p.m_B;
// return temp;
//}
public:
int m_A;
int m_B;
};
//2.全局函数重载+号
Person_jh operator+(Person_jh &p1, Person_jh &p2)
{
Person_jh temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
//函数重载的版本
Person_jh operator+(Person_jh &p, int num)
{
Person_jh temp;
temp.m_A = p.m_A + num;
temp.m_B = p.m_B + num;
return temp;
}
//注意顺序,一定要先定义重载加号再调用
void test_jh01()
{
Person_jh p1;
p1.m_A = 10;
p1.m_B = 10;
Person_jh p2;
p2.m_A = 10;
p2.m_B = 10;
//成员函数重载本质调用
//Person_jh p3 = p1.operator+(p2);
//全局函数重载本质调用
//Person_jh p3 = operator+(p1, p2);
//两种方法都可以简化为:
Person_jh p3 = p1 + p2;
//运算符重载也可以发生函数重载
Person_jh p4 = p1 + 100;//Person + int
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
cout << "p4.m_A=" << p4.m_A << endl;
cout << "p4.m_B=" << p4.m_B << endl;
return;
}
//总结:
//1.对于内置的数据类型的表达式的运算符是不可能改变的
//2.不要滥用运算符重载
/*左移运算符重载*/
//语法:返回值类型 operator<<(参数列表) {} ;
//作用:可以输出自定义的数据类型
class Person_zy
{
friend ostream& operator<<(ostream &cout, Person_zy &p);
friend void test_zy01();
public:
Person_zy(int a, int b)
{
m_A = a;
m_B = b;
}
//利用成员函数来重载左移运算符
//p.operator<<(cout) 简化为: p<<cout
//不会利用成员函数来重载<<运算符,因为无法实现cout在左侧
//void operator<<(Person_zy &p)
//{
// return;
//}
private:
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算符
//cout属于输出流ostream,因此用ostream &cout,通过引用传进来
ostream& operator<<(ostream &cout, Person_zy &p)
//本质是operator<<(cout,p) 简化为:cout<<p
//返回值引用可以理解为可以利用该地址进行下一步操作,也就是可以继续<<接着输出其他(链式)。
{
cout << "m_A=" << p.m_A << "\tm_B=" << p.m_B << endl;
return cout;
}
void test_zy01()
{
Person_zy p(10, 10);
cout << p.m_A << "\t" << p.m_B << endl;
cout << p << "Hello world" << endl;
return;
}
//总结
//重载左移运算符配合友元可以输出自定义数据类型
/*递增运算符重载*/
//作用:通过重载递增运算符实现自己的整型数据
//自定义整型
class MyInteger
{
friend ostream& operator<<(ostream& cout, const MyInteger& my);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符,返回引用是为了一直对一个数据进行递增操作
MyInteger& operator++()
{
//先进行++运算
m_Num++;
//再将自身做返回,this指向自身,*this就是把自身解引用
return *this;
}
//重载后置++运算符
//返回值类型不同不可以作为区分函数的条件
//后置递增返回值
MyInteger operator++(int) //这个int代表占位参数,用于区分前置和后置递增
{
//先记录当时结果
MyInteger temp = *this;
//后递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, const MyInteger& my)
//上面加了const是因为test_zy02()里的cout<<会报错。
{
cout << my.m_Num ;
return cout;
}
void test_my01()
{
MyInteger myint;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test_my02()
{
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
return;
}
//总结:前置递增返回引用,后置递增返回值
/*赋值运算符重载*/
//c++编译器至少给一个类添加4个函数
//1. 默认构造函数(无参,函数体为空)
//2. 默认析构函数(无参,函数体为空)
//3. 默认拷贝构造函数,对属性进行值拷贝
//4. 赋值运算符 operator=, 对属性进行值拷贝
//如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
class Person_fz
{
public:
Person_fz(int age)
{
m_Age = new int(age);//利用new把数据开辟到堆区
}
~Person_fz()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//重载赋值运算符
Person_fz& operator=(Person_fz &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;//this是指向自身的指针,*this就是解引用,也就是自身
}
public:
int *m_Age;
};
void test_fz01()
{
Person_fz p1(18);
Person_fz p2(20);
Person_fz p3(30);
p3 = p2 = p1;//赋值操作
cout << "p1的年龄:" << *p1.m_Age << endl;//把m_Age指针解引用
cout << "p2的年龄:" << *p2.m_Age << endl;//把m_Age指针解引用
cout << "p3的年龄:" << *p3.m_Age << endl;//把m_Age指针解引用
return;
}
/*关系运算符重载*/
//作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
class Person_gx
{
public:
Person_gx(string name, int age)
{
m_Name = name;
m_Age = age;
}
//重载==
bool operator==(Person_gx &p)
{
if (this->m_Age == p.m_Age&&this->m_Name == p.m_Name)
{
return true;
}
else
return false;
}
bool operator!=(Person_gx &p)
{
if (this->m_Age != p.m_Age || this->m_Name != p.m_Name)
{
return true;
}
else
return false;
}
public:
string m_Name;
int m_Age;
};
void test_gx01()
{
Person_gx p1("张三", 18);
Person_gx p2("张三", 18);
if (p1 != p2)
{
cout << "p1!=p2" << endl;
}
else
{
cout << "p1==p2" << endl;
}
}
/*函数调用运算符重载*/
//函数调用运算符{}也可以重载
//由于重载后使用的方式非常像函数的调用,因此称为仿函数
//仿函数没有固定写法,非常灵活
class Myprint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
public:
};
void test_dy02(string test)
{
cout << test << endl;
}
void test_dy01()
{
Myprint my;
my("Hello world!");//重载函数调用运算符,由于使用起来非常类似于函数调用,又称为仿函数
test_dy02("Hello world");
}
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
public:
};
void test_dy03()
{
MyAdd my;
int result = my(100, 200);
cout << "result=" << result << endl;
//匿名函数对象
//类名()是创建一个匿名对象,比如MyAdd()
//后面的(200,300)是重载
//匿名对象是在当前行执行完时立即被释放
cout << MyAdd()(200, 300) << endl;
}
/*继承*/
//下级别的成员除了拥有上一级的共性,还有自己的特性
/*继承的基本语法*/
//普通实现
//Java页面
class Java
{
public:
void header()
{
cout << "首页、公开课、登录、注册。。。(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图。。。(公共底部)" << endl;
}
void left()
{
cout << "Java\Python\C++(公共分类列表)" << endl;
}
void content()
{
cout << "Java学科视频" << endl;
}
public:
};
//Python页面
class Python
{
public:
void header()
{
cout << "首页、公开课、登录、注册。。。(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图。。。(公共底部)" << endl;
}
void left()
{
cout << "Java\Python\C++(公共分类列表)" << endl;
}
void content()
{
cout << "Python学科视频" << endl;
}
public:
};
//C++
//Python页面
class CPP
{
public:
void header()
{
cout << "首页、公开课、登录、注册。。。(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图。。。(公共底部)" << endl;
}
void left()
{
cout << "Java\Python\C++(公共分类列表)" << endl;
}
void content()
{
cout << "C++学科视频" << endl;
}
public:
};
void test_jc01()
{
cout << "Java下载视频的页面如下:" << endl;
Java java;
java.header();
java.footer();
java.left();
java.content();
cout << "-------------------------" << endl;
cout << "Python下载视频的页面如下:" << endl;
Python python;
python.header();
python.footer();
python.left();
python.content();
cout << "-------------------------" << endl;
cout << "C++下载视频的页面如下:" << endl;
CPP cpp;
cpp.header();
cpp.footer();
cpp.left();
cpp.content();
}
//继承实现
//继承的好处:减少重复代码
//语法:class 子类: 继承方式 父类
//子类也称为派生类
//父类也称为基类
//派生类中的成员包括两部分
//1.从基类继承过来的
//2.自己增加的成员
//从基类继承过来的表现其共性,新增的成员体现其个性
//公共页面类
class BasePage
{
public:
void header()
{
cout << "首页、公开课、登录、注册。。。(公共头部)" << endl;
}
void footer()
{
cout << "帮助中心、交流合作、站内地图。。。(公共底部)" << endl;
}
void left()
{
cout << "Java\Python\C++(公共分类列表)" << endl;
}
public:
};
//Java页面
//语法: class 类名:public 公共类名
class Java_jc:public BasePage
{
public:
void content()
{
cout << "Java学科视频" << endl;
}
public:
};
//Python页面
class Python_jc :public BasePage
{
public:
void content()
{
cout << "Python学科视频" << endl;
}
public:
};
//C++页面
class CPP_jc :public BasePage
{
public:
void content()
{
cout << "C++学科视频" << endl;
}
public:
};
void test_jc02()
{
cout << "Java的下载视频的页面如下:" << endl;
Java_jc java;
java.header();
java.footer();
java.left();
java.content();
cout << "-------------------------" << endl;
cout << "Python的下载视频的页面如下:" << endl;
Python_jc python;
python.header();
python.footer();
python.left();
python.content();
cout << "-------------------------" << endl;
cout << "C++的下载视频的页面如下:" << endl;
CPP_jc cpp;
cpp.header();
cpp.footer();
cpp.left();
cpp.content();
}
/*继承方式*/
//继承的语法:class 子类:继承方式 父类
//继承方式有三种:
//1.公共继承 父类中的public还是public,父类的protected还是protected,父类的private不可以访问
//2.保护继承 父类中的public和protected都是protected,弗雷德private不可以访问
//3.私有继承 父类中的public和protected都是private,父类的private不可以访问
//公共继承
class Base1
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son1 :public Base1
{
public:
void func()
{
m_A = 10;//父类中的public成员到子类中依然是public,public是类内可以访问,类外也可以访问
m_B = 20;//父类中的protect成员到子类中依然是protected
//m_C = 10;//错误,父类中的private成员到子类中不可以访问
}
};
void test_jc03()
{
Son1 son;
son.m_A = 100;
//son.m_B = 100;//m_B是protected,只能类内访问,类外不能访问
}
//保护继承
class Base2
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son2 :protected Base2
{
public:
void func()
{
m_A = 100;//父类中的public到子类中变为protected
m_B = 100;//父类中的protected到子类中仍未protected
//m_C = 100;//错误,父类的private,子类访问不到
}
};
void test_jc04()
{
Son2 son;
//son.m_A = 1000;//错误,在Son2中m_A变为protected,因此类外访问不到
//son.m_B = 1000;//错误,protected类外访问不到,只能类内访问
}
//私有继承
class Base3
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son3 :private Base3
{
public:
void func()
{
m_A = 100;//父类中public成员到子类中变为private成员
m_B = 100;//父类中protected成员到子类中变为private成员
//m_C = 100;//父类中private成员,子类无法访问
}
};
void test_jc05()
{
Son3 son;
//son.m_A = 10;//错误,到Son3中变为private,类外访问不到,只有类内能访问到
//son.m_B = 10;//错误,到Son3中变为private,类外访问不到,只有类内能访问到
}
class GrandSon3 :public Son3
{
public:
void func()
{
//m_A = 1000;//错误,到Son3中m_A为private,访问不到
//m_B = 100;//错误,到Son3中m_A为private,访问不到
}
};
/*继承中的对象模型*/
class Base_mx
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son_mx :public Base_mx
{
public:
int m_D;
};
void test_mx()
{
//父类中所有非静态成员属性都会被子类继承下去
//父类中私有成员属性是被编译器隐藏了,因此是访问不到的,但是确实被继承下去了
cout << "size of Son_mx=" << sizeof(Son_mx) << endl;//结果为16=4*4
}
//利用开发人员命令提示工具查看对象模型
//1.跳转盘符 D:
//2.跳转文件路径: cd 文件路径
//3.查看命令:cl /d1 reportSingleClassLayout类名 文件名
/*继承中构造和析构顺序*/
//子类继承父类后,当创建子类对象时也会调用父类的构造函数
class Base_xg
{
public:
Base_xg()
{
cout << "Base的构造函数" << endl;
}
~Base_xg()
{
cout << "Base的析构函数" << endl;
}
};
class Son_xg :public Base_xg
{
public:
Son_xg()
{
cout << "Son的构造函数" << endl;
}
~Son_xg()
{
cout << "Son的析构函数" << endl;
}
};
void test_xg()
{
//Base_xg b;
Son_xg s;
}
//先构造父类,再构造子类
//析构的顺序与构造的顺序相反
//现有父类的构造函数,再有子类的构造函数,再有子类的析构函数,最后是父类的析构函数
/*继承同名成员处理方式*/
//访问子类同名成员直接访问即可
//访问父类同名成员需要加作用域
class Base_tm
{
public:
Base_tm()
{
m_A = 100;
}
void func()
{
cout << "Base下的func()调用" << endl;
}
void func(int a)
{
cout << "Base下的func(int a)调用" << endl;
}
public:
int m_A;
};
class Son_tm :public Base_tm
{
public:
Son_tm()
{
m_A = 200;
}
void func()
{
cout << "Son下的func()调用" << endl;
}
public:
int m_A;
};
//同名成员属性处理
void test_tm01()
{
Son_tm s;
cout << "Son下的m_A=" << s.m_A << endl;
//如果通过子类对象访问到父类中的同名成员,需要加作用域
cout << "Base下的m_A=" << s.Base_tm::m_A << endl;
}
//同名成员函数处理
void test_tm02()
{
Son_tm s;
s.func();//直接调用,调用的是子类中的同名成语
s.Base_tm::func();//调用父类中的同名成员函数时需要加作用域
//如果子类中出现和父类同名的成员函数,子类的同名成员函数会隐藏掉父类中所有的同名成员函数,只要同一个函数名字的就算,不管有参无参
//s.func(100);//错误
//如果想访问到父类中被隐藏的同名成员函数,需要加作用域
s.Base_tm::func(100);
}
//总结:
//1. 子类对象可以直接访问到子类中同名成员
//2. 子类对象加作用域可以访问到父类同名成员
//3. 当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数
/*继承同名静态成员处理方式*/
//静态成员和费静态成员出现同名时的处理方式一致
//1.访问子类同名成员 直接访问即可
//2.访问父类同名成员 需要加作用域
class Base_jt
{
public:
static void func()
{
cout << "Base下的static void func()" << endl;
}
static void func(int a)
{
cout << "Base下的static void func(int a)" << endl;
}
public:
static int m_A;//静态成员变量类内声明,类外初始化
};
int Base_jt::m_A = 100;//静态成员变量类内声明,类外初始化
class Son_jt :public Base_jt
{
public:
static void func()
{
cout << "Son下的static void func()" << endl;
}
public:
static int m_A;//静态成员变量类内声明,类外初始化
};
int Son_jt::m_A = 200;//静态成员变量类内声明,类外初始化
//同名静态成员属性
void test_jt01()
{
//1.通过对象访问
Son_jt s;
cout << "Son下的m_A=" << s.m_A << endl;
cout << "Base下的m_A=" << s.Base_jt::m_A << endl;
//2.通过类名访问
cout << "Son下的m_A=" << Son_jt::m_A << endl;
//第一个::代表通过类名方式访问,第二个::代表访问父类作用域下
cout << "Base下的m_A=" << Son_jt::Base_jt::m_A << endl;
}
//同名静态成员函数
void test_jt02()
{
//1.通过对象访问
Son_jt s;
s.func();
s.Base_jt::func();
//2.通过类名访问
Son_jt::func();
Son_jt::Base_jt::func();
//子类出现和父类同名静态成员函数,也会隐藏父类中所有同名成员函数
//如果想访问父类中被隐藏同名成员,需要加作用域
Son_jt::Base_jt::func(100);
}
//总结:
//同名静态成员处理方式和非静态处理方式一样,只不过有两种访问的方式(通过对象 和 通过类名)
/*多继承语法*/
//C++允许一个类继承多个类
//语法:class 子类:继承方式 父类1,继承方式 父类2...
//多继承可能会引发父类中有同名成员出现,需要加作用域区分
//C++实际开发中不建议使用
class Base_d1
{
public:
Base_d1()
{
m_A = 100;
}
public:
int m_A;
};
class Base_d2
{
public:
Base_d2()
{
m_A = 200;
}
public:
int m_A;
};
//子类 需要继承Base1和Base2
class Son_d :public Base_d1, public Base_d2
{
public:
Son_d()
{
m_C = 300;
m_D = 400;
}
public:
int m_C;
int m_D;
};
void test_d01()
{
Son_d s;
cout << "size of Son=" << sizeof(s) << endl;//16
//当父类中出现同名成员,需要加作用域区分
cout << "Base::m_A=" << s.Base_d1::m_A << endl;
cout << "Base::m_A=" << s.Base_d2::m_A << endl;
}
//总结:
//多继承中如果父类中出现了同名情况,子类使用时候要加作用域
/*菱形继承*/
//菱形继承概念
//两个派生类继承同一个基类
//又有某个类同时继承两个派生类
//这种继承称为菱形继承或者钻石继承
//菱形继承问题:
//1. 羊继承了动物的数据,驼同样继承了动物的数据,当草泥马使用数据时,就会产生二义性。
//2. 草泥马继承自动物的数据继承了两份,其实我们应该清楚,这份数据我们只需要一份就可以。
//动物类
class Animal
{
public:
public:
int m_Age;
};
//利用虚继承可以解决菱形继承的问题
//在继承之前加上关键字virtual变为虚继承
//语法:class 子类名:virtual 继承方式 父类名
//Animal类称为虚基类
//羊类
class Sheep :virtual public Animal
{
};
//驼类
class Tuo :virtual public Animal
{
};
//羊驼类
class SheepTuo :public Sheep, public Tuo
{
};
void test_lx01()
{
SheepTuo st;
st.Sheep::m_Age = 18;
st.Tuo::m_Age = 28;
//当菱形继承,两个父类拥有相同数据,需要加作用域来区分
cout << "st.Sheep::m_Age=" << st.Sheep::m_Age << endl;
cout << "st.Tuo::m_Age=" << st.Tuo::m_Age << endl;
//这份数据我们知道 只要有一份就可以了,菱形继承导致数据有两份,浪费资源
cout << "st.m_Age=" << st.m_Age << endl;
//vbptr代表虚基类指针,指向VBtable虚基类表 v-virtual b-base ptr-pointer
}
//总结:
//菱形继承带来的主要问题是子类继承两份相同的数据,导致资源浪费以及毫无意义
//利用虚继承可以解决菱形继承问题
/*多态*/
/*多态的基本概念*/
//多态分为两类
//1.静态多态:函数重载和运算符重载都属于静态多态,复用函数名
//2.动态多态:派生类和虚函数实现运行时多态
//静态多态和动态多态的区别:
//1.静态多态的函数地址早绑定,编译阶段确定函数地址
//2.动态多态的函数地址晚绑定,运行阶段确定函数地址
//动物类
class Animal_dt
{
public:
//虚函数:virtual 返回类型 函数名(参数列表){};
virtual void speak()
{
cout << "动物在说话" << endl;
}
public:
};
//猫类
class Cat_dt :public Animal_dt
{
public:
//重写 函数返回值类型、函数名、参数列表完全相同
virtual void speak()
{
cout << "小猫在说话" << endl;
}
public:
};
//狗类
class Dog_dt :public Animal_dt
{
public:
virtual void speak()
{
cout << "小狗在说话" << endl;
}
};
//执行说话的函数
//地址早绑定,在编译阶段确定函数地址
//如果想执行让猫说话,这个函数地址就不能提前绑定,需要在运行阶段进行绑定,地址晚绑定
//动态多台满足条件
//1.有继承关系
//2.子类要重写父类的虚函数,是重写不是重载。(父类加virtual,子类加不加virtual都可以)
//动态多态的使用
//1.父类的指针或者引用指向子类对象
void doSpeak(Animal_dt &animal)//Animal_dt &animal =cat,父类的引用或者指针可以直接指向子类对象
{
animal.speak();
}
void test_dt01()
{
Cat_dt cat;
doSpeak(cat);
Dog_dt dog;
doSpeak(dog);
}
void test_dt02()
{
cout << "size of Animal=" << sizeof(Animal_dt) << endl;//Animal里不加virtual代表空类,输出1
}
//vfptr虚函数(表)指针,指向虚函数表
//v-virtual f-function ptr-pointer
//vftalble虚函数表,表内部记录虚函数的地址
//v-virtual f-function table-table
/*案例:计算器类*/
//案例描述:分别利用普通写法和多态技术,设计实现两个操作数进行运算的计算器类
//多态的优点:
//1.代码组织结构清晰
//2.可读性强
//3.利于前期和后期的扩展以及维护
//普通写法
class Calculator
{
public:
int getResult(string oper)
{
if (oper == "+")
{
return m_Num1 + m_Num2;
}
else if (oper == "-")
{
return m_Num1 - m_Num2;
}
else if (oper == "*")
{
return m_Num1 * m_Num2;
}
//如果想扩展新的功能,需要修改源码
//在真实的开发中,提倡开闭原则:对扩展进行开发,对修改进行关闭
}
public:
int m_Num1;//操作数1
int m_Num2;//操作数2
};
void test_jsq01()
{
//创建计算器对象
Calculator c;
c.m_Num1 = 10;
c.m_Num2 = 10;
cout << c.m_Num1 << "+" << c.m_Num2 << "=" << c.getResult("+") << endl;
cout << c.m_Num1 << "-" << c.m_Num2 << "=" << c.getResult("-") << endl;
cout << c.m_Num1 << "*" << c.m_Num2 << "=" << c.getResult("*") << endl;
}
//利用多态实现计算器
//多态的好处:
//1.组织结构清晰
//2.可读性强
//3.对于前期和后期扩展以及维护方便
//实现计算器抽象类
class AbstractCalculator
{
public:
virtual int getResult()
{
return 0;
}
public:
int m_Num1;
int m_Num2;
};
//加法计算器类
class AddCalculator :public AbstractCalculator
{
public:
int getResult()//子类重写父类中的虚函数(父类加virtual,子类加不加都可以)
{
return m_Num1 + m_Num2;
}
public:
};
//减法计算器类
class SubCalculator :public AbstractCalculator
{
public:
int getResult()
{
return m_Num1 - m_Num2;
}
public:
};
//乘法计算器
class MulCalculator :public AbstractCalculator
{
public:
int getResult()
{
return m_Num1 * m_Num2;
}
public:
};
void test_jsq02()
{
//多态使用条件
//父类指针或者引用指向子类对象
//加法运算
AbstractCalculator *abc = new AddCalculator;//开辟堆区,需要手动开辟手动释放
abc->m_Num1 = 100;
abc->m_Num2 = 100;
cout << abc->m_Num1 << "+" << abc->m_Num2 << "=" << abc->getResult() << endl;
//用完记得销毁,只是数据销毁了,数据类型还未变
delete abc;//delete后abc的数据类型仍然为指针
//减法计算器
abc = new SubCalculator;
abc->m_Num1 = 100;
abc->m_Num2 = 100;
cout << abc->m_Num1 << "-" << abc->m_Num2 << "=" << abc->getResult() << endl;
delete abc;
//乘法计算器
abc = new MulCalculator;
abc->m_Num1 = 100;
abc->m_Num2 = 100;
cout << abc->m_Num1 << "*" << abc->m_Num2 << "=" << abc->getResult() << endl;
delete abc;
}
//总结:C++开发提倡利用多态设计程序架构,因为多态优点很多
/*纯虚函数和抽象类*/
//在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容
//因此可以将虚函数改为纯虚函数
//纯虚函数语法:virtual 返回值类型 函数名(参数列表)=0;
//当类中有了纯虚函数,这个类也称为抽象类
//抽象类特点:
//1.无法实例化对象
//2.子类必须重写抽象类中的纯虚函数,否则也属于抽象类
class Base_cx
{
public:
virtual void func() = 0;//纯虚函数,只要有一个纯虚函数,这个类称为抽象类
public:
};
//子类
class Son_cx :public Base_cx
{
public:
virtual void func()
{
cout << "func调用" << endl;
}
public:
};
void test_cx01()
{
//Base_cx b;//错误,抽象类无法实例化对象
//new Base_cx;//错误,堆区也无法创建实例化对象
//Son_cx s; //错误,子类必须重写抽象类中的纯虚函数,否则也属于抽象类
Son_cx s; //正确,子类中已经重写了抽象类中的纯虚函数,可以进行实例化对象了
Base_cx *b = new Son_cx;
b->func();
delete b;
}
/*案例:制作饮品*/
//案例描述:制作流程:煮水、冲泡、倒入杯中、加入辅料
//利用多态技术实现本案例,提供抽象制作饮品基类,提供子类制作咖啡和茶叶
class AbstractDrining
{
public:
//煮水
virtual void Boil() = 0;
//冲泡
virtual void Brew() = 0;
//倒入杯中
virtual void PourInCup() = 0;
//加入辅助佐料
virtual void PutSomething() = 0;
//制作饮品
void makeDrinking()
{
Boil();
Brew();
PourInCup();
PutSomething();
}
public:
};
//制作咖啡
class Coffee :public AbstractDrining
{
public:
//煮水
virtual void Boil()
{
cout << "煮农夫山泉" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡咖啡" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入咖啡杯中" << endl;
}
//加入辅助佐料
virtual void PutSomething()
{
cout << "加入糖和牛奶" << endl;
}
public:
};
//制作茶叶
class Tea :public AbstractDrining
{
public:
//煮水
virtual void Boil()
{
cout << "煮矿泉水" << endl;
}
//冲泡
virtual void Brew()
{
cout << "冲泡茶叶" << endl;
}
//倒入杯中
virtual void PourInCup()
{
cout << "倒入茶杯中" << endl;
}
//加入辅助佐料
virtual void PutSomething()
{
cout << "加入枸杞" << endl;
}
public:
};
//制作函数
void doWork_al02(AbstractDrining *abs)//AbstractDrining *abs=new Coffee;
{
abs->makeDrinking();
delete abs;//手动开辟,手动释放
}
void test_al02()
{
//制作咖啡
doWork_al02(new Coffee);//父类的指针指向子类的对象
//制作茶
doWork_al02(new Tea);
}
/*虚析构和纯虚析构*/
//多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
//解决方式:将父类中的析构函数改为虚析构或者纯虚析构
//虚析构和纯虚析构共性:
//1.可以解决父类指针释放子类对象
//2.都需要有具体的函数实现
//虚析构和纯虚析构区别:
//如果是纯虚析构,该类属于抽象类,无法实例化对象
//语法:
//虚析构语法:virtual ~类名(){};
//纯虚析构语法:virtual ~类名()=0; 在类外实现 类名::~类名(){};
class Animal_x
{
public:
Animal_x()
{
cout << "Animal的构造函数调用" << endl;
}
//纯虚函数
virtual void speak() = 0;
//虚析构和纯虚析构只能有一个
//利用虚析构可以解决父类指针释放子类对象时不干净的问题
//virtual ~Animal_x()//父类中析构函数前加上virtual改为虚析构,便会进行子类的析构
//{
// cout << "Animal的析构函数调用" << endl;
//}
//纯虚析构,需要声明也需要实现
//有了纯虚析构之后,这个类属于抽象类,无法实例化对象
virtual ~Animal_x() = 0;
public:
};
//纯虚析构也需要代码的实现
Animal_x::~Animal_x()
{
cout << "Animal的纯虚析构函数调用" << endl;
}
class Cat_x :public Animal_x
{
public:
Cat_x(string name)
{
cout << "Cat的构造函数调用" << endl;
m_Name = new string(name);
}
virtual void speak()
{
cout << *m_Name << "猫在说话" << endl;
}
~Cat_x()
{
if (m_Name != NULL)
{
cout << "Cat析构函数调用" << endl;
delete m_Name;
m_Name = NULL;
}
}
public:
string *m_Name;
};
void test_x01()
{
Animal_x *animal = new Cat_x("Tom");
animal->speak();
//父类的指针在析构时候,不会调用子类中的析构函数,导致子类如果有堆区属性的话会出现内存泄漏
delete animal;
}
//总结:
//1. 虚析构或纯虚析构就是用来解决通过父类指针释放子类对象
//2. 如果子类中没有堆区数据,可以不写为虚析构或纯虚析构
//3. 拥有纯虚析构函数的类也属于抽象类,无法实例化对象
/*案例:电脑组装*/
//电脑主要组成部件为 CPU(用于计算),显卡(用于显示),内存条(用于存储)
//将每个零件封装出抽象基类,并且提供不同的厂商生产不同的零件,例如Intel厂商和Lenovo厂商
//创建电脑类提供让电脑工作的函数,并且调用每个零件工作的接口
//测试时组装三台不同的电脑进行工作
//抽象不同零件类
//抽象CPU类
class CPU
{
public:
//抽象的计算函数
virtual void calculate() = 0;
public:
};
//抽象显卡类
class VideoCard
{
public:
//抽象的显示函数
virtual void display() = 0;
public:
};
//抽象内存条类
class Memory
{
public:
//抽象的存储函数
virtual void storage() = 0;
public:
};
//电脑类
class Computer
{
public:
Computer(CPU *cpu, VideoCard *vc, Memory *mem)
{
m_cpu = cpu;
m_vc = vc;
m_mem = mem;
}
//提供工作的函数
void work()
{
//让零件工作起来,调用接口
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
//提供析构函数,释放三个电脑零件
Computer()
{
//释放CPU零件
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
//释放显卡零件
if (m_vc != NULL)
{
delete m_vc;
m_vc = NULL;
}
//释放内存条零件
if (m_mem != NULL)
{
delete m_mem;
m_mem = NULL;
}
}
private:
CPU *m_cpu;//CPU零件的指针
VideoCard *m_vc;//显卡零件的指针
Memory *m_mem;//内存条零件的指针
};
//具体厂商
//Intel厂商
class IntelCPU :public CPU
{
virtual void calculate()
{
cout << "Intel的CPU开始计算了!" << endl;
}
};
class IntelVideoCard :public VideoCard
{
virtual void display()
{
cout << "Intel的显卡开始显示了!" << endl;
}
};
class IntelMemory :public Memory
{
virtual void storage()
{
cout << "Intel的内存条开始存储了!" << endl;
}
};
//Lenovo厂商
class LenovoCPU :public CPU
{
virtual void calculate()
{
cout << "Lenovo的CPU开始计算了!" << endl;
}
};
class LenovoVideoCard :public VideoCard
{
virtual void display()
{
cout << "Lenovo的显卡开始显示了!" << endl;
}
};
class LenovoMemory :public Memory
{
virtual void storage()
{
cout << "Lenovo的内存条开始存储了!" << endl;
}
};
void test_dn()
{
//第一台电脑零件
CPU *intelcpu = new IntelCPU;
VideoCard *intelvc = new IntelVideoCard;
Memory *intelmem = new IntelMemory;
//创建第一台电脑
Computer *computer1 = new Computer(intelcpu, intelvc, intelmem);
computer1->work();
delete computer1;
cout << "--------------------" << endl;
//创建第二台电脑
Computer *computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);
computer2->work();
delete computer2;
cout << "--------------------" << endl;
//创建第三台电脑
Computer *computer3 = new Computer(new IntelCPU, new LenovoVideoCard, new IntelMemory);
computer3->work();
delete computer3;
cout << "--------------------" << endl;
}
/*文件操作*/
//程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放
//通过文件可以将数据持久化
//C++中对文件操作需要包含头文件<fstream>//文件流
//文件类型分为两种:
//1.文本文件:文本以文本的ASCII码形式存储在计算机中
//2.二进制文件:文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们
//操作文件的三大类:
//1.ofstream:写操作 o-output f-file
//2.ifstream:读操作 i-input
//3.fstream:读写操作
/*文本文件*/
//写文件
//1.包含头文件#include<fstream>
//2.创建流对象 ofstream ofs;
//3.打开文件 ofs.open("文件路径",打开方式);
//4.写数据 ofs<<"写入的数据";
//5.关闭文件 ofs.close();
//文件打开方式:
//ios::in 为读文件而打开文件
//ios::out 为写文件而打开文件
//ios::ate 初始位置:文件尾
//ios::app 追加方式写文件
//ios::trunc 如果文件存在先删除,再创建
//ios::binary 二进制方式
//注意:文件打开方式可以配合使用,利用 | 操作符
//例如:用二进制方式写文件 ios::binary | ios::out
void test_xwj01()
{
ofstream ofs;
ofs.open("test.txt", ios::out);
ofs << "姓名:张三" << endl;
ofs << "性别:男" << endl;
ofs << "年龄:18" << endl;
ofs.close();
}
//总结:
//文件操作必须包含头文件 fstream
//读文件可以利用 ofstream ,或者fstream类
//打开文件时候需要指定操作文件的路径,以及打开方式
//利用 << 可以向文件中写数据
//操作完毕,要关闭文件
//读文件
//1.包含头文件 #include<fstream>
//2.创建流对象 ifstream ifs;
//3.打开文件并判断文件是否打开成功 ifs.open(“文件路径”,打开方式); 通过ifs.is_open()来判断
//4.读数据 四种方式读取
//5.关闭文件 ifs.close();
void test_dwj01()
{
ifstream ifs;
ifs.open("test.txt",ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//读数据
第一种
//char buf[1024] = { 0 };
//while (ifs >> buf)//一行一行的读,读到头以后返回一个false
//{
// cout << buf << endl;
//}
第二种
//char buf[1024] = { 0 };
//while (ifs.getline(buf, sizeof(buf)))
//{
// cout << buf << endl;
//}
第三种
//string buf;
//while (getline(ifs, buf))
//{
// cout << buf << endl;
//}
//第四种,不推荐用
char c;
while ((c = ifs.get()) != EOF)//EOF是end of file文件尾,看是否读到文件尾
{
cout << c;
}
ifs.close();
}
//总结:
//读文件可以利用 ifstream ,或者fstream类
//利用is_open函数可以判断文件是否打开成功
//close 关闭文件
/*二进制文件*/
//以二进制的方式对文件进行读写操作
//打开方式要指定为:ios::binary
//写文件
//二进制方式写文件主要利用流对象调用成员函数write
//函数原型ostream& write(const char * buffer,int len);
//参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
class Person_xwj
{
public:
public:
char m_Name[64];//姓名 不要用string,可能会出现问题,要用char
int m_Age;//年龄
};
void test_xwj02()
{
ofstream ofs;
ofs.open("person.txt", ios::out | ios::binary);
Person_xwj p = { "张三",18 };
ofs.write((const char *)&p, sizeof(Person_xwj));
ofs.close();
}
//总结:
//文件输出流对象 可以通过write函数,以二进制方式写数据
//读文件
//二进制方式读文件主要利用流对象调用成员函数read
//函数原型:istream& read(char *buffer, int len);
//参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
class Person_dwj
{
public:
char m_Name[64];
int m_Age;
};
void test_dwj02()
{
ifstream ifs;
ifs.open("person.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
Person_dwj p;
ifs.read((char*)&p, sizeof(Person_dwj));
cout << "姓名:" << p.m_Name << "\t年龄:" << p.m_Age << endl;
ifs.close();
}
int main()
{
/*二进制文件*/
//写文件
test_xwj02();
//读文件
test_dwj02();
/*文本文件*/
//写文件
test_xwj01();
test_dwj01();
/*案例:电脑组装*/
test_dn();
/*虚析构和纯虚析构*/
test_x01();
/*案例:制作饮品*/
test_al02();
/*纯虚函数和抽象类*/
test_cx01();
/*案例:计算器类*/
test_jsq01();
test_jsq02();
/*多态的基本概念*/
test_dt01();
test_dt02();
/*菱形继承*/
test_lx01();
/*多继承语法*/
test_d01();
/*继承同名静态成员处理方式*/
//test_jt01();
test_jt02();
/*继承同名成员处理方式*/
test_tm01();
test_tm02();
/*继承中构造和析构顺序*/
test_xg();
/*继承中的对象模型*/
test_mx();
/*继承方式*/
/*继承*/
//下级别的成员除了拥有上一级的共性,还有自己的特性
/*继承的基本语法*/
test_jc01();
test_jc02();
/*函数调用运算符重载*/
test_dy01();
test_dy03();
/*关系运算符重载*/
test_gx01();
/*赋值运算符重载*/
test_fz01();
int a = 10;
int b = 20;
int c = 30;
c = b = a;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
/*递增运算符重载*/
//test_my01();
test_my02();
/*左移运算符重载*/
test_zy01();
/*加号运算符重载*/
test_jh01();
/*友元*/
//全局函数做友元
test_y01();
//类做友元
test_yy01();
//成员函数做友元
test_yy();
/*const修饰成员函数*/
test_c();
/*空指针访问成员函数*/
test_k01();
/*this指针概念*/
test_this01();
test_this02();
/*C++对象模型和this指针*/
/*成员变量和成员函数分开存储*/
//test_zz1();
test_zz2();
/*静态成员*/
//test_jt();
test_jt2();
test_j();
/*类对象作为类成员*/
test_l();
/*初始化列表*/
test_csh();
/*深拷贝与浅拷贝*/
test_sq1();
/*构造函数调用规则*/
test_gzz();
test_gzz2();
/*拷贝构造函数调用时机*/
test_kb1();
test_kb2();
test_kb3();
/*构造函数的分类及调用*/
test_fl();
/*对象的初始化和清理*/
//生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时候也会删除一些自己信息数据保证安全
//C++中的面向对象来源于生活,每个对象也都会有初始设置以及 对象销毁前的清理数据的设置。
/*构造函数和析构函数*/
test_gz();//创建对象的时候,构造函数会自动调用,不需要手动调用,而且调用只有一次
Person_gz person_gz2; //此时并不会释放,因此不会调用析构函数
/*判断点和圆的关系*/
//创建圆
Circle2 circle2;
circle2.setR(10);
Point center;
center.setX(10);
center.setY(0);
circle2.setCenter(center);
//创建点
Point point;
point.setX(10);
point.setY(10);
//判断点和圆的关系
isInCircle(circle2, point);
/*设计长方体类*/
//实例化对象 创建长方体
Cube cube1;
cube1.setL(10);
cube1.setH(10);
cube1.setW(10);
cout << "cube1的面积为:" << cube1.calculateS() << endl;
cout << "cube1的体积为:" << cube1.calculateV() << endl;
//创建第二个长方体
Cube cube2;
cube2.setL(10);
cube2.setH(10);
cube2.setW(10);
//判断两个长方体是否相等
//利用全局函数判断
bool result1 = isSame(cube1, cube2);
if (result1)
{
cout << "两个相等" << endl;
}
else
{
cout << "两个不相等" << endl;
}
//利用成员函数判断
bool result2 = cube1.isSameByClass(cube2);
if (result2)
{
cout << "两个相等 " << endl;
}
else
{
cout << "两个不相等 " << endl;
}
/*成员属性设置为私有*/
//优点1:将所有成员属性设置为私有,可以自己控制读写权限
//优点2:对于写权限,我们可以检测数据的有效性
Person1 pp1;
pp1.setName("张六");
cout << "姓名为:" << pp1.getName() << endl;
pp1.setAge(1000);
cout << "年龄为:" << pp1.getAge() << endl;
pp1.setLover("王五");
/*struct和class区别*/
//struct默认权限为公共
//class默认权限为私有
C1 cc1;
//cc1.m_A = 100;//错误,class默认权限为私有,类外不可以访问
C2 c2;
c2.m_A = 100;//正确,struct默认权限为公共
/*类和对象*/
//c++面向对象的三大特性为:封装、继承、多态
//C++认为万事万物都皆为对象,对象上有其属性和行为
//例如:
//人可以作为对象,属性有姓名、年龄、身高、体重...,行为有走、跑、跳、吃饭、唱歌...
//车也可以作为对象,属性有轮胎、方向盘、车灯..., 行为有载人、放音乐、放空调...
//具有相同性质的 == 对象 == ,我们可以抽象称为 == 类 == ,人属于人类,车属于车类
/*封装*/
//封装的意义:
//将属性和行为作为一个整体,表现生活中的事物
//将属性和行为加以权限控制
//将属性和行为作为一个整体,表现生活中的事物
//语法:class 类名 { 访问权限: 属性;行为;};
//类中的属性和成员,统一称为成员
//属性=成员属性=成员变量
//行为=成员函数=成员方法
//设计一个圆类,求圆的周长
//圆的周长公式:2 * PI *半径
//实例化:通过一个类创建一个对象的过程
//通过圆类创建具体的圆(对象)
//语法:类名称 对象名称;
Circle c1;
//给圆对象的属性进行赋值
c1.m_r = 10;
cout << "圆的周长为:" << c1.calculateZC() << endl;
//设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
//创建一个具体的学生 实例化对象
Student s1;
//给s1对象进行属性赋值操作
s1.m_Name = "张三";
s1.m_ID = 1;
//显示学生信息
s1.showStudent();
Student s2;
s2.setName("李四");
s2.setID(2);
s2.showStudent();
//将属性和行为加以权限控制
//public 公共权限
//protected 保护权限
//private 私有权限
//实例化具体对象
Person p1;
p1.m_Name = "王五";
//p1.m_Car = "奔驰"; //错误,保护权限内容,在类外访问不到
//p1.m_Password = 123;//错误,私有权限内容,类外访问不到
/*内存分区模型*/
//内存大方向分为四个区域
//代码区:存放函数体的二进制代码,由操作系统进行管理
//全局区:存放全局变量和静态变量以及常量
//栈区:由编译器自动分配释放,存放函数的参数值,局部变量等
//堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
/*程序运行之前:代码区,全局区*/
//代码区:存放CPU执行的机器指令
//代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
//代码区是只读的,使其只读的原因是防止程序意外地修改它的指令
//全局区包括:全局变量、静态变量、全局常量、字符串常量
//全局区的数据在程序结束后由操作系统释放
//创建普通局部变量
int a1 = 10;
int b1 = 10;
cout << "局部变量a1的地址为:" << (int)&a1 << endl;
cout << "局部变量b1的地址为:" << (int)&b1 << endl;
//输出定义的全局变量
cout << "全局变量global_a的地址为:" << (int)&global_a << endl;
cout << "全局变量global_b的地址为:" << (int)&global_b << endl;
//创建静态变量,在普通变量前加static就属于静态变量
//语法:static 数据类型 变量名称;
static int static_a = 10;
static int static_b = 10;
cout << "静态变量static_a的地址为:" << (int)&static_a << endl;
cout << "静态变量static_b的地址为:" << (int)&static_b << endl;
//常量
//字符串常量 语法:"..."
cout << "字符串常量的地址为:" << (int)&"hello world" << endl;
//const修饰的变量:修饰全局变量、修饰局部变量
//const修饰的全局变量
cout << "全局常量c_g_a的地址为:" << (int)&c_g_a << endl;
cout << "全局常量c_g_b的地址为:" << (int)&c_g_b << endl;
//const修饰的局部变量
//c-const,g-global,l-local
const int c_l_a = 10;
const int c_l_b = 10;
cout << "局部常量c_l_a的地址为:" << (int)&c_l_a << endl;
cout << "局部常量c_l_b的地址为:" << (int)&c_l_b << endl;
//全局常量、全局变量、静态变量和字符串常量放在全局区,局部常量和局部变量不在全局区
//总结
//C++中在程序运行前分为全局区和代码区
//代码区特点是共享和只读
//全局区中存放全局变量、静态变量、常量
//常量区中存放const修饰的全局常量和字符串常量
/*程序运行后:栈区和堆区*/
//栈区:由编译器自动分配释放,存放函数的参数值,局部变量等
//注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
//接收func函数的返回值
int *p = func();
cout << *p << endl;//输出10 //第一次可以打印正确的数字,是因为编译器做了一次保留
cout << *p << endl;//输出乱码 //第二次这个数据就不再保留
//堆区;由程序员分配释放,若程序员不释放,程序结束时由操作系统回收
//在C++中主要利用new在堆区开辟内存
int *p_dq = func3();
cout << *p_dq << endl;//输出10
cout << *p_dq << endl;//输出10
/*new操作符*/
//利用new在堆区开辟数据
//语法:new 数据类型
//利用new创建的数据,会返回该数据对应类型的指针
//堆区开辟的数据,由程序员手动开辟,手动释放,释放利用delete进行
test01();
test02();
/*引用*/
//引用:给变量起别名
//语法:数据类型 &别名=原名;
int a_yy = 10;
int &b_yy = a_yy;
cout << "a=" << a_yy << endl;//输出10
cout << "b=" << b_yy << endl;//10
b_yy = 100;//通过修改b_yy,此时a_yy、b_yy都修改为100
cout << "a=" << a_yy << endl;//输出100
cout << "b=" << b_yy << endl;//100
//引用注意事项
//引用必须初始化
int a_zy = 10;
//int &b_zy; //错误,必须要初始化
int &b_zy = a_zy;//一旦初始化后,就不可以更改
cout << "a=" << a_zy << endl;//输出10
cout << "b=" << b_zy << endl;//输出10
//引用在初始化后,不可以改变
int c_zy = 20;
b_zy = c_zy;//赋值操作,而不是更改引用
//把b_zy赋值了20,此时a_zy和b_zy都是20
cout << "a=" << a_zy << endl;//输出20
cout << "b=" << b_zy << endl;//输出20
cout << "c=" << c_zy << endl;//输出20
/*引用做函数参数*/
//作用:函数传参时,可以利用引用的技术让形参修饰实参
//优点:可以简化指针修改实参
int a_yyhs = 10;
int b_yyhs = 20;
cout << "交换前:" << endl;
cout << "a_yyhs=" << a_yyhs << "\tb_yyhs=" << b_yyhs << endl;
mySwap01(a_yyhs, b_yyhs);//值传递,形参不会修饰实参,main函数仍然输出原来的值
cout << "值传递交换后:" << endl;
cout << "a_yyhs=" << a_yyhs << "\tb_yyhs=" << b_yyhs << endl;
mySwap02(&a_yyhs, &b_yyhs);//地址传递,形参会修饰实参
cout << "地址传递交换后:" << endl;
cout << "a_yyhs=" << a_yyhs << "\tb_yyhs=" << b_yyhs << endl;
mySwap03(a_yyhs, b_yyhs);//引用传递,形参会修饰实参
cout << "引用传递交换后:" << endl;
cout << "a_yyhs=" << a_yyhs << "\tb_yyhs=" << b_yyhs << endl;
//通过引用参数产生的效果同按地址传递是一样的。引用的语法更清楚简单
/*引用做函数返回值*/
//作用:引用是可以作为函数的返回值存在的
//1.不要返回局部变量的引用
int &ref = test03();
cout << "ref=" << ref << endl;//第一次正确,输出10,是因为编译器做了保留
cout << "ref=" << ref << endl;//第二次错误,因为内存已经释放
int & ref2 = test04();
cout << "ref2=" << ref2 << endl;//输出10
cout << "ref2=" << ref2 << endl;//输出10
//2.函数的调用可以作为左值
test04() = 1000;//函数调用作为左值,必须返回引用,这就相当于a=1000的赋值操作
cout << "函数调用作为左值赋值后:" << endl;
cout << "ref2=" << ref2 << endl;//输出1000
cout << "ref2=" << ref2 << endl;//输出1000
/*引用的本质*/
//本质:引用的本质在C++内部实现是一个指针常量,一旦初始化后就不可以发生改变
int a_bz = 10;
//自动转换为 int* const ref = &a; 指针常量是指针指向不可改,也说明为什么引用不可更改
int& ref_bz = a_bz;
ref_bz = 20; //内部发现ref是引用,自动帮我们转换为: *ref = 20;
cout << "a:" << a_bz << endl;
cout << "ref:" << ref_bz << endl;
func5(a_bz);
/*常量引用*/
//作用:常量引用主要用来修饰形参,防止误操作
//在函数形参列表中,可以加const修饰形参,防止形参改变实参
//语法:返回值类型 函数名称(const 变量类型 & 引用名称){ 函数体语句;return语句;}
int a_xs = 10;
//int &ref_xs = 10;//错误,引用必须引一块合法的内存空间
const int & ref_xs = 10;//正确,加上const以后,编译器将代码修改为int temp=10;const int & ref_xs=temp;
//ref_xs = 20;//错误,加入const之后变为只读,不可以修改
//打印a_xs
showValue(a_xs);//输出1000
cout << "a_xs=" << a_xs << endl;//输出1000
//防止误操作
int b_xs = 20;
showValue2(b_xs);//输出20
cout << "b_xs=" << b_xs << endl;//输出20
/*函数默认参数*/
//在C++中,函数的形参列表中的形参是可以有默认值的
//语法:返回值类型 函数名称(参数=默认值){ 函数体语句;return语句;}
//如果我们自己传入数据,就用自己的数据;如果没有,那么用默认值
cout << func6(10) << endl;//10+20+30=60
cout << func6(10,30) << endl;//10+30+30=70
//注意事项
//1.如果某个位置已经有了默认参数,那么从这个位置往后都必须有默认值
//2.如果函数声明有默认参数,函数实现就不能有默认参数
cout << func8(10, 20) << endl;
/*占位参数*/
//C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
//语法:返回值类型 函数名(参数1,...,数据类型){ 函数体语句;return语句;}
func9(10,1);//占位参数必须填补
//func9(10) //占位参数有默认值
/*函数重载*/
//函数重载满足条件:
//1.同一个作用域下
//2.函数名称相同
//3.函数参数类型不同或者个数不同或者顺序不同
//注意:函数的返回值不可以作为函数重载的条件
funct();
funct(10);
funct(3.14);
funct(10, 3.14);
funct(3.14, 10);
/*函数重载注意事项*/
//1.引用作为重载条件
//int a_cz = 10;
//fun(a_cz); //调用fun(int &a)
fun(10);//调用fun(const int &a)
//2.函数重载碰到默认参数
//fun2(10);//当函数重载碰到默认参数,出现二义性,报错,尽量避免这种情况
system("pause");
return 0;
}