目录
类和对象
简单学生类
#include<iostream>
#include<string.h>
using namespace std;
//设计一个学生类,属性有姓名和学号
//可以给姓名和学号赋值,可以显示学生的姓名和学号
//设计学生类
class student
{
//类中的属性和行为统称为成员
public:
//属性 成员属性,成员变量
string m_Name;
int m_ID;
//行为 又叫成员函数或者成员方法
void showStudent()
{
cout << "姓名:" << m_Name << endl << "学号:" << m_ID << endl;
}
//给姓名赋值
void setname(string name)
{
m_Name = name;
}
};
int main()
{
//创建一个具体学生,实现实例化
student s1;
//给s1对象进行属性的赋值操作
s1.m_Name = "HRY";
s1.setname("RHY");
s1.m_ID = 1;
s1.showStudent();
student s2;
s2.m_Name = "HDY";
s2.m_ID = 2;
s2.showStudent();
system("pause");
return 0;
}
c++中class与struct的区别
#include<iostream>
using namespace std;
//class与struct的区别
//struct默认权限为公共
//class 默认权限为私有
class C1
{
int m_A;//默认私有权限
};
struct C2
{
int m_A;//默认公共权限
};
int main()
{
C1 c1;
C2 c2;
c2.m_A = 100;
//c1.m_A = 100;该行代码不可运行class中默认为私有权限
cout<<
system("pause");
return 0;
}
友元-全局函数做友元
#include<iostream>
#include<string>
using namespace std;
class Building
{
// goodgay全局函数是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;//BedRoom为私有成员,但是函数作为友元,可以访问
}
void test01()
{
Building building;
goodGay(&building);
}
int main()
{
test01();
system("pause");
return 0;
}
友元类
#include<iostream>
#include<string>
using namespace std;
//类做友元
class Building;
class GoodGay
{
public:
GoodGay();
void visit();//参观函数,访问Building中的属性
private:
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 test01()
{
GoodGay gg;
gg.visit();
}
int main()
{
test01();
system("pause");
return 0;
}
深拷贝与浅拷贝
#include<iostream>
using namespace std;
//深浅拷贝是面试经典问题,也是常见的坑
//浅拷贝:简单的赋值拷贝操作 堆区的内存重复释放
//深拷贝:在堆区重新申请空间,进行拷贝操作
class person
{
public:
person()
{
cout << "默认构造函数调用" << endl;
}
person(int age, int height)
{
cout << "有参构造函数的调用" << endl;
m_age = age;
m_height = new int(height);
}
person(const person& p)
{
cout << "拷贝构造函数的调用" << 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 << "析构函数的调用" << endl;
}
int m_age;
int *m_height;
};
void test1()
{
person p1(18,180);
cout << "p1的年龄为:" << p1.m_age << " p1的身高为:" << *p1.m_height << endl;
person p2(p1);
cout << "p2的年龄为:" << p2.m_age << " p2的身高为:" << *p2.m_height << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
设计案例1-设计立方体
#include<iostream>
using namespace std;
//立方体类设计
//1.创建立方体类
//2.设计属性
//3.设计行为 - 获取立方体面积和体积
//4.分别利用全局函数和成员函数判断两个立方体是否相等
class cube
{
public:
void steL(int L)
{
m_L = L;
}
void setH(int H)
{
m_H = H;
}
void setW(int W)
{
m_W = W;
}
int mianji()
{
return 2 * (m_L * m_H + m_W * m_H + m_L * m_W);
}
int tiji()
{
return m_L * m_H * m_W;
}
bool issameBYclass(cube& c)
{
/*if ((m_L == c.getL()) && (m_H == c.getH()) && (m_W == c.getW()))
{
return true;
}*/
}
private:
int m_L;
int m_H;
int m_W;
};
bool isSame(cube &a1, cube &a2)
{
}
int main()
{
cube a1;
int a, b, c;
cout << "请分别输入长宽高:";
cin >> a >> b >> c;
a1.steL(a);
a1.setW(b);
a1.setH(c);
cout << "a1的面积:"<<a1.mianji()<<endl;
cout << "a1的体积:"<< a1.tiji()<<endl;
system("pause");
return 0;
}
设计案例2-点和圆的关系
#include<iostream>
#include"point.h"
#include"circle.h"
using namespace std;
void point::setx(int x)
{
mx = x;
}
void point::sety(int y)
{
my = y;
}
int point::getx()
{
return mx;
}
int point::gety()
{
return my;
}
void circle::setxy(point xy1)
{
xy = xy1;
}
void circle::setr(int r)
{
mr = r;
}
int circle::getr()
{
return mr;
}
point circle::getxy()//在类中可以让另一个类作为该类的成员
{
return xy;
}
void isincircle(point x, circle y)
{
point qw = y.getxy();
int a = (x.getx()-qw.getx())*(x.getx() - qw.getx())+
(x.gety() - qw.gety()) * (x.gety() - qw.gety())-y.getr()*y.getr();
cout << a<<endl;
if (a == 0)
{
cout << "在圆上"<<endl;
}
else if (a > 0)
{
cout << "在圆外"<<endl;
}
else
{
cout <<"在圆内"<<endl;
}
}
int main()
{
circle a1;
point q1,q2;
q1.setx(10);
q1.sety(10);
a1.setxy(q1);
a1.setr(1);
q2.setx(10);
q2.sety(10);
isincircle(q2, a1);
system("pause");
return 0;
}
类和对象-封装-访问权限
#include<iostream>
#include<string.h>
using namespace std;
//类在设计中,可以把属性和行为放在不同的权限下,加以控制
//三种权限 public protected private
// 公共权限 public 类内可以访问,类外可以访问
// 保护权限 protected 类内可以访问,类外不可以访问 儿子可以访问父亲中的保护内容
// 私有权限 private 类内可以访问,类外不可以访问 儿子不可以访问父亲的私有内容
class person
{
public:
string m_name;
protected:
string m_car;
private:
int m_password;
public:
void func()
{
m_name = "HRY";
m_car = "HRY";
m_password = 123055;//此时在类内可访问
}
};
int main()
{
person p1;
p1.m_name = "RHY";
//p1.m_car = "YU"; 在类外,不可访问 保护权限
system("pause");
return 0;
}
类对象作为类成员
#include<iostream>
#include<string.h>
using namespace std;
//类对象作为类成员
class phone
{
public:
phone(string pname)
{
cout << "phone的构造函数的调用"<<endl;
m_pname = pname;
}
string m_pname;
};
class person
{
public:
//Phone m_phone = pname; 隐式转换法
person(string name, string pname) :m_name(name), m_phone(pname)
{
cout << "person的构造函数的调用"<<endl;
}
string m_name;
phone m_phone;
};
void test01()
{
person p("张三","IPHONE14PROMAX");
cout << p.m_name << "拿着" << p.m_phone.m_pname<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
空指针访问成员函数
#include<iostream>
using namespace std;
//cpp中空指针可以调用成员函数
class person
{
public:
void showclassname()
{
cout << "this is person class" << endl;
}
void showpersonage()
{
//报错原因是因为传入的指针为空
if (this == NULL)
{
return;
}//提高代码健壮性
cout << "age=" << this->m_age << endl;
}
int m_age;
};
void test01()
{
person* p=NULL;
p->showclassname();
p->showpersonage();
}
int main()
{
system("pause");
return 0;
}
拷贝构造函数的调用时机
#include<iostream>
using namespace std;
//拷贝构造函数的调用时机
//1.使用一个构造完毕的对象来初始化一个新对象
//2.值传递的方式给函数参数传值
//3.以值方式返回局部对象
class person {
public:
person()
{
cout << "person的默认构造函数调用"<<endl;
}
person(int age)
{
cout << "有参构造函数的调用" << endl;
m_age = age;
}
person(const person& p)
{
cout << "拷贝构造函数的调用" << endl;
m_age = p.m_age;
}
~person()
{
cout << "person的析构函数调用"<<endl;
}
int m_age;
};
//1.使用一个构造完毕的对象来初始化一个新对象
void test1()
{
person p1(20);
person p2(p1);
cout << "p2的年龄是:" << p2.m_age << endl;
}
//2.值传递的方式给函数参数传值
void work(person p)
{
}
void test2()
{
person p;
work(p);//实参传给形参时调用拷贝构造函数
}
//3.值方式返回局部对象
person work2()
{
person p;
cout << (int*)&p<<endl;
return p;
}
void test3()
{
person p = work2();
cout << (int*)&p<<endl;
}
int main()
{
/*test1();*/
/*test2();*/
test3();
system("pause");
return 0;
}
构造函数的分类及调用
#include<iostream>
using namespace std;
//构造函数的分类以及调用
//分类
//按参数分类 无参构造(默认构造)和 有参构造
//按照类型分类 普通构造 拷贝构造
class person
{
public:
//构造函数
person()//无参构造 默认构造
{
cout << "构造函数的调用" << endl;
}
person(int a)//有参构造
{
age = a;
cout << "构造函数的调用" << endl;
}
//拷贝构造函数
person(const person &p)
{
cout << "拷贝构造函数的调用" << endl;
age = p.age;//将传入的人身上的所有属性,拷贝到我身上
}
int age;
~person()
{
cout << "析构函数的调用" << endl;
}
};
void test1()
{
//1.括号法
person p1;//默认构造函数的调用
person p2(10);//有参构造函数
person p3(p2);//拷贝构造函数
//调用默认构造函数的时候,不要加()
/* cout << "p2的年龄是:" << p2.age<<endl;
cout << "p3的年龄是:" << p3.age<<endl*/;
//2.显示法
person p1;
person p2 = person(10);//有参构造 person(10)为匿名对象,前行执行结束后,系统立即回收掉匿名对象
person p3 = person(p2);//拷贝构造
//3.隐式转换法
person p4 = 10; //相当于 person p4 = person(10); 有参构造
person p5 = p4;//拷贝构造
}
int main()
{
test1();
system("pause");
return 0;
}
构造函数的调用规则
#include<iostream>
using namespace std;
//默认情况下cpp编译器至少给一个类添加三个函数
//1.默认构造函数(无参,函数体为空)
//2.默认析构函数(无参,函数体为空)
//3.默认拷贝构造函数,对属性进行值拷贝
//如果用户定义有参构造函数,c++不再提供默认无参构造,但是会提供默认拷贝构造
//如果用户定义拷贝构造函数,c++不会再提供其他构造函数
class person
{
public:
person()
{
cout << "person的默认构造函数" << endl;
}
person(int age)
{
cout << "person的有参构造函数" << endl;
m_age = age;
}
person(const person& p)
{
m_age = p.m_age;
cout << "person的拷贝构造函数" << endl;
}
~person()
{
cout << "person的析构构造函数" << endl;
}
int m_age;
};
void test1()
{
person p;
p.m_age = 18;
person p2(p);//拷贝构造函数相当于复制粘贴
cout << "p2的年龄为:" << p2.m_age << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
封装-属性和行为作为整体
#include<iostream>
using namespace std;
const double PI = 3.14;
//class代表设计一个类,类后面紧跟着是类名称
class Circle
{
//访问权限
//公共权限
public:
//属性
//半径
int m_r;
//行为
//获取圆的周长
double calculateZC()
{
return 2 * PI * m_r;
}
};
int main()
{
//通过圆类创建具体的圆(对象)
//实例化 通过一个类创建一个对象的过程
Circle cl;
//给圆对象的属性进行赋值
cl.m_r = 10;
cout << "圆的周长为:" << cl.calculateZC()<<endl;
system("pause");
return 0;
}
对象特性-构造函数和析构函数
#include<iostream>
using namespace std;
//我们自己不提供构造和析构,编译器自动提供
//构造函数,主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无需手动调用
//析构函数,主要作用在于对象销毁前系统自动调用,执行一些清理工作
//对象的初始化和清理
class person
{
//1.1构造函数
//没有返回值 不用写void
//函数名与类名相同
//构造函数可以有参数可以发生重载
//创建对象的时候,构造函数发生调用,而且只调用一次
public:
person()
{
cout << "构造函数的调用"<<endl;
}
~person()//析构函数
{
cout << "析构函数的调用" << endl;
}
};
//析构函数,进行清理的操作
//没有返回值,不写void
//析构函数不可以有参数,不能发生重载
//对象在销毁前,会自动调用析构函数,而且只会调用一次
void test01()
{
person p;
}
int main()
{
test01();
system("pause");
return 0;
}
对象特性-初始化列表
#include<iostream>
using namespace std;
//初始化列表
class person
{
public:
// 传统初始化操作
//person(int A, int B, int C)
//{
// m_A = A;
// m_B = B;
// m_C = C;
//}
//person() :m_A(10), m_B(20), m_C(30)
//{
//}
person(int a, int b, int c)
:m_A(a)
, m_B(b)
, m_C(c)
{
}
int m_A;
int m_B;
int m_C;
};
void test1()
{
person p(10,20,30);
cout << p.m_A << " " << p.m_B << " " << p.m_C << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
对象特性成员变量和成员函数分开存储
#include<iostream>
using namespace std;
class person
{
int m_A;//非静态成员变量,属于类的对象上
static int m_B;//静态成员变量,不属于类的对象上
void func(){}//非静态成员函数,不属于类的对象上
static void func2(){}//静态成员函数 不属于类的对象上
};
//void test01()
//{
// person p;
// //空对象占用的内存空间为:1
// //Cpp编译器会给每个空对象也分配一个内存空间,是为了区分空对象占内存的位置
// //每个空对象也应该有一个独一无二的 内存地址
// cout << "size of p:" << sizeof(p);
//}
void test01()
{
person p;
cout << "size of p:" << sizeof(p);
}
int main()
{
test01();
system("pause");
return 0;
}
成员属性私有化
#include<iostream>
#include<string>
using namespace std;
//成员属性设置为私有
//1.可以自己控制读写权限
//2.对于写可以检测数据的有效性
class person
{
public:
//设置姓名
void setname(string name)
{
m_name = name;
}
//获取姓名
string getname()
{
return m_name;
}
int getage()
{
m_age = 0;//初始化为0岁
return m_age;
}
//设置年龄
void setage(int age)
{
m_age = age;
}
void setlover(string lover)
{
m_lover = lover;
}
private:
//姓名 可读可写
string m_name;
//年龄 只读
int m_age;
//情人 可写
string m_lover;
};
int main()
{
person p;
p.setname("HRY");
cout << "姓名为:" <<p.getname() << endl;
//p.m_age = 18;
cout << "年龄为:"<<p.getage()<<endl;
p.setlover("!@#");
system("pause");
return 0;
}
成员函数做友元
#include<iostream>
#include<string>
//友元的目的就是让一个函数或者类 访问另一个类中私有成员
using namespace std;
class Building;
class GoodGay
{
public:
GoodGay();
void visit(); //让visit函数可以访问Building中的私有成员
void visit2(); //让visit2函数不可以访问Building中的私有成
Building * building;
};
class Building
{
friend void GoodGay::visit();
friend void GoodGay::visit2();
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 << "visit2函数正在访问:" << building->m_SittingRoom << endl;
cout << "visit2函数正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
gg.visit2();
}
int main()
{
test01();
system("pause");
return 0;
}
this指针的用途
#include<iostream>
using namespace std;
//cpp提供特殊的对象指针,this指针,解决问题,指向被调用的成员函数的所属的对象
//this指针隐含在每一个非静态成员函数的一种指针
//this指针不需要定义,直接就可使用
//形参和成员变量同名时,可用this指针来区分
//在类的非静态成员函数中返回对象本身,可使用return *this
class person
{
public:
person(int age)
{
//this指针指向的是 被调用的成员函数所属的对象
this->age = age;
}
person& addage(person &p)
{
this->age += p.age;
return *this;
}
int age;
};
//解决名称冲突
void test01()
{
person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
//返回对象本身用*this
void test02()
{
person p1(10);
person p2(10);
//链式编程思想
p2.addage(p1).addage(p1).addage(p1).addage(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
const修饰成员函数
#include<iostream>
using namespace std;
//成员函数后加const后我们称为这个函数为长常函数
//常函数内不可用修改成员属性
//成员属性声明是加关键字mutable后,在常函数中依然可以修改
//常对象
//声明对象前加const成该对象为常对象
//常对象只能调用常函数
class person
{
public:
//this只针对本质是 指针常量,指针的指向是不可以修改的
void showperson() const //加上const之后this指针的指向值不可用修改
{
this->m_B = 100;
/*m_age = 100;*/
/*this->m_age = 100;*/
}
int m_age;
mutable int m_B;//特殊变量,即使在常函数中,也可以修改这个值
};
void test01()
{
person p;
p.showperson();
}
void test02()
{
//常对象只能调用常函数
const person p;//在对象前加const,变为常对象
/*p.m_age = 100;*/
p.m_B = 100;
}
int main()
{
test01();
system("pause");
return 0;
}
运算符重载
c++运算符重载-赋值运算符重载
#include<iostream>
using namespace std;
//复制运算符重载
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;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
c++运算符重载-关系运算符重载
#include<iostream>
using namespace std;
//重载关系运算符
class person
{
public:
person(string name,int age)
{
m_Name = name;
m_Age = age;
}
bool operator==(person& p)
{
if ((this->m_Age = p.m_Age )&& (this->m_Name == p.m_Name))
{
return true;
}
return false;
}
string m_Name;
int m_Age;
};
void test01()
{
person p1("Tom", 18);
person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1 和 p2相等" << endl<<endl ;
}
}
int main()
{
test01();
system("pause");
return 0;
}
c++运算符重载-函数调用运算符重载
#include<iostream>
using namespace std;
#include<string>
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
void test01()
{
MyPrint myprint;
myprint("hello world");//由于使用起来非常类似函数调用,因此成为仿函数
}
//反函数非常灵活,没有固定写法
class MyAdd
{
public:
int operator()(int num1,int num2)
{
return num1 + num2;
}
};
void test02()
{
MyAdd myadd;
cout << myadd(100, 20) << endl;
//匿名函数对象
cout << MyAdd()(100, 33) << endl;//MyAdd()为匿名对象,用完即释放
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
c++运算符重载-加号运算符重载
#include<iostream>
using namespace std;
//加号运算符重载
class person
{
public:
//1.成员函数重载+号
//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;
};
//1.成员函数重载+号
//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 a )
{
person temp;
temp.m_A = p1.m_A + a;
temp.m_B = p1.m_B + a;
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;
p3 = p3 + 1;
cout << "p3.m_A:" << p3.m_A << " " << "p3.m_B:" << p3.m_B << endl;
}
//运算符重载也可以发生函数重载
int main()
{
test01();
system("pause");
return 0;
}
c++运算符重载-左移运算符重载
#include<iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a, int b)
{
m_A = a;
m_B = b;
}
private:
利用成员函数重载 左移运算符
//void operator<<(Person &p)
//{
//}
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,Person &p)
{
cout << "m_A=:" << p.m_A << " " << "m_B=:" << p.m_B << endl;
return cout;
}
void test01()
{
Person p(10,10);
cout << p<<10;
}
int main()
{
test01();
system("pause");
return 0;
}
递增运算符重载
#include<iostream>
using namespace std;
class MyInteger
{
friend ostream& operator <<(ostream& cout, MyInteger p);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
MyInteger & operator++()//返回引用可以连续对一个数据进行操作
{
m_Num++;
return *this;
}
//重载后置++运算符
MyInteger operator++(int)//int 代表占位参数,可与用于区分前置和后置
{
//后置返回的是一个值
// 先记录当时结果
MyInteger temp = *this;
//后递增
m_Num++;
// 最后将记录结果返回
return temp;
}
private:
int m_Num;
};
ostream& operator <<(ostream& cout, MyInteger p)
{
cout << p.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++myint << endl;
}
void test02()
{
MyInteger myint;
cout << myint++;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}