c++之类和对象专题1 封装,对象的初始化和清理,对象模型和this指针

1、封装

1.1 封装的意义一:

  • 将属性和行为作为一个整体,表现在生活的事务中
  • 将属性和行为加以权限控制
1.1.2 基本语法:class 类名 { 访问权限:属性/行为 };
1.1.2:示例

示例1:设计一个圆类,求圆的周长

#include<iostream>
#define PI 3.14
using namespace std;
class Circle
{
public:  //作用域,公共属性
	double Area()//成员函数
	{
		return 2 * PI * m_r;
	}
	int m_r;//成员变量



};
int main()
{
	Circle c;//定义类的对象
	c.m_r = 10;//通过点运算符给成员变量赋值
	cout << "圆的周长为:" << c.Area() << endl;//通过点运算符调用成员函数,求周长

	system("pause");
	return 0;
}

示例2:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号

#include<iostream>
using namespace std;
class Student
{
public :
	void setName(string name)//成员函数,设置学生姓名
	{
		m_name = name;
	}
	void setId(string id)//成员函数,设置学生的学号
	{
		m_id = id;
	}
	void printInfo()//成员函数,打印学生信息
	{
		cout << "学号:" << m_id << " 姓名:" << m_name << endl;
	}
	//成员变量
	string m_name;
	string m_id;
};
int main()
{
	Student s;
	s.setName("张三");
	s.setId("20401028");
	s.printInfo();
	system("pause");
	return 0;
}

1.2 封装的意义二:

类在设计时,可以把属性和行为放到不同的权限下,加以控制。

1.2.1 :访问的权限

**访问的权限有三种:

  • public:类内可以访问,类外可以访问
  • protected:类内可以访问,类外不可以访问
  • private:类内可以访问,类外不可以访问**
1.2.2 :struct和class的区别

struct默认的权限是public
class默认的权限是private

1.2.3 :成员属性设置为私有
  • 优点1:将所有的成员属性设置为私有,可以自己控制读写权限
  • 优点2:对于写的权限,我们可以检测数据的有效性
#include<iostream>
using namespace std;
class Person
{
public:
	void setName(string name)
	{
		m_name = name;
	}
	string getName()
	{
		return m_name;
	}
	void setAge(int age)//通过判断条件,检测数据的有效性
	{
		if (age < 0 || age>150)
		{
			cout << "年龄输入有误!" << endl;
			return;
		}
		else
		{
			m_age=age;
		}
	}
	int getAge()
	{
		
		return m_age;
	}
	void setLover(string lover)
	{
		m_lover = lover;
	}
private:
	string m_name;//可读可写
	int m_age;//只读
	string m_lover;//只写
};
int main()
{
	Person p;
	p.setName("张三");
	cout << "姓名:" << p.getName() << endl;
	p.setAge(1000);
	cout << "年龄:" << p.getAge() << endl;
	p.setLover("仓吉");
	system("pause");
	return 0;
}
1.2.4 示例:

示例1:设计立方体类

  • 可以求出立方体的面积和体积
  • 分别用全局函数和成员函数判断两个立方体是否相等
#include<iostream>
using namespace std;
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_W + 2 * m_L * m_H + 2 * m_H * m_W;
	}
	int CalculateV()//计算立方体体积
	{
		return m_L * m_W * m_H;
	}
	bool isSameByClass(Cube& c)//成员函数判断两个立方体是否相等
	{
		if (m_L == c.getL() && m_H == c.getH() && m_W == c.getW())//在类内,可以直接访问类的私有成员
		{
			return true;
		}
		else
		{
			return false;
		}
	}
private:
	int m_L;//长
	int m_W;//宽
	int m_H;//高
};
bool isSame(Cube& c1, Cube& c2)//通过全局函数判断两个立方体是否相等
{
	if (c1.getL() == c2.getL() && c1.getH() == c2.getH() && c1.getW() == c2.getW())
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	Cube c1;
	c1.setL(10);
	c1.setH(10);
	c1.setW(10);
	Cube c2;
	c2.setL(10);
	c2.setH(10);
	c2.setW(10);
	cout << "c1的面积:" << c1.CalculateS() << endl;
	cout << "c2的面积:" << c2.CalculateS() << endl;
	cout << "c1的体积:" << c1.CalculateV() << endl;
	cout << "c2的体积:" << c1.CalculateV() << endl;
	bool flag = c1.isSameByClass(c2);
	if (flag)
	{
		cout << "两个立方体相等!" << endl;
	}
	else
	{
		cout << "两个立方体不相等!" << endl;
	}
	system("pause");
	return 0;
}

示例2:设计一个圆类

  • 判断点和圆的关系
#include<iostream>
using namespace std;
class Point
{
public:
	void setX(int x)
	{
		m_X = x;
	}
	int getX()
	{
		return m_X;
	}
	void setY(int y)
	{
		m_Y = y;
	}
	int getY()
	{
		return m_Y;
	}
private:
	int m_X;
	int m_Y;
};
class Circle
{
public:
	void setR(int r)
	{
		m_r = r;
	}
	int getR()
	{
		return m_r;
	}
	void setCenter(Point center)
	{
		m_center = center;
	}
	Point getCenter()
	{
		return m_center;
	}
	
private:
	int m_r;
	Point m_center;
};
void RealtionOf_PointAndCircle(Circle &c,Point &p)
{
	int rDistance = c.getR() * c.getR();
	int pointDistance = (p.getX() - c.getCenter().getX()) * (p.getX() - c.getCenter().getX())
		+ (p.getY() - c.getCenter().getY()) * (p.getY() - c.getCenter().getY());
	if (rDistance > pointDistance)
	{
		cout << "点在圆内" << endl;
	}
	else if (rDistance < pointDistance)
	{
		cout << "点在圆外" << endl;
	}
	else
	{
		cout << "点在圆上" << endl;
	}
}
int main()
{
	Circle c;
	c.setR(10);
	Point center;
	center.setX(10);
	center.setY(0);
	c.setCenter(center);
	Point p;
	p.setX(10);
	p.setY(9);
	RealtionOf_PointAndCircle(c, p);
	system("pause");
	return 0;
}

示例3分文件编写 设计一个圆类
1、创建一个.h的头文件
2、创建一个.cpp的源文件
3、头文件只做类的声明
4、源文件做具体的实现
在这里插入图片描述
Point.h的头文件,只做成员变量和成员函数的声明

#pragma once
#include<iostream>
using namespace std;
class Point
{
public:
	void setX(int x);
	int getX();
	void setY(int y);
	int getY();
private:
	int m_X;
	int m_Y;
};

Point.Cpp的源文件,做具体的实现
注意:这里要有作用域的声明

#include"Point.h"//包含头文件

	void Point::setX(int x)
	{
		m_X = x;
	}
	int Point::getX()
	{
		return m_X;
	}
	void Point::setY(int y)
	{
		m_Y = y;
	}
	int Point::getY()
	{
		return m_Y;
	}

Circle.h的头文件,只做成员变量和成员函数的声明

#pragma once
#include"Point.h"//包含头文件
class Circle
{
public:
	void setR(int r);
	int getR();
	void setCenter(Point center);
	Point getCenter();

private:
	int m_r;
	Point m_center;
};

Circle.Cpp的源文件,做具体的实现
注意:这里要有作用域的声明

#include"Circle.h"//包含头文件
void Circle::setR(int r)
{
	m_r = r;
}
int Circle::getR()
{
	return m_r;
}
void Circle::setCenter(Point center)
{
	m_center = center;
}
Point Circle::getCenter()
{
	return m_center;
}

main.Cpp文件,主函数

#include"Circle.h"//包含头文件
void RealtionOf_PointAndCircle(Circle& c, Point& p)
{
	int rDistance = c.getR() * c.getR();
	int pointDistance = (p.getX() - c.getCenter().getX()) * (p.getX() - c.getCenter().getX())
		+ (p.getY() - c.getCenter().getY()) * (p.getY() - c.getCenter().getY());
	if (rDistance > pointDistance)
	{
		cout << "点在圆内" << endl;
	}
	else if (rDistance < pointDistance)
	{
		cout << "点在圆外" << endl;
	}
	else
	{
		cout << "点在圆上" << endl;
	}
}
int main()
{
	Circle c;
	c.setR(10);
	Point center;
	center.setX(10);
	center.setY(0);
	c.setCenter(center);
	Point p;
	p.setX(10);
	p.setY(9);
	RealtionOf_PointAndCircle(c, p);
	system("pause");
	return 0;
}

2、对象的初始化和清理

2.1 构造函数和析构函数

  • 构造函数:主要在创建对象时为对象成员的属性赋值,构造函数由编译器自动调用,无需手动调用。
  • 析构函数:主要作用于对象销毁前系统自动调用,执行一些清理工作
  • 如果我们不提供构造和析构函数,系统会提供,不过系统提供的构造函数和析构函数是空实现
    构造函数的语法:类名(){}
    1、构造函数可以有形参,可以发生重载
    2、程序在调用对象时构造函数由系统自动调用,无需手动调用,只会调用一次

析构函数语法:~类名(){}
1、析构函数不可以有形参,不可以发生重载
2、程序在销毁对象前会自动调用析构,无需手动调用,只会调用一次

#include<iostream>
using namespace std;
class Person
{
public:
	Person()
	{
		cout << "构造函数的调用" << endl;
	}
	~Person()
	{
		cout << "析构函数的调用" << endl;
	}
};
void test()//只创建一个对象
{
	Person p;//栈上的数据,在函数执行完,自动释放这个对象,系统自动调用析构函数
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

2.2 构造函数的分类和调用

两种分类方式:

  • 按参数分类:有参构造函数和无参构造函数

  • 按类型分类:普通构造函数和拷贝构造函数

三种调用方式:

  • 括号法
  • 显示法
  • 隐式转换法
#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;
	}
	~person()
	{
		cout << "析构函数的调用" << endl;
	}
	int age;
};
void test01()
{
	//1、括号法
	//person p;//默认构造函数的调用
	//person p2(10);//有参构造函数的调用
	//person p3(p2);//拷贝构造函数的调用
	//cout << "p2的年龄:" << p2.age << endl;
	//cout << "p3的年龄:" << p3.age << endl;
	// person p();//函数的声明
	//2、显示法
	/*person p;
	person p2 = person(10);
	person p3 = person(p);*/
	//person(10);//匿名对象 特点:当前行执行结束后,系统会立刻回收掉匿名对象
	//cout << "hello world" << endl;
	/*有参构造函数的调用
      析构函数的调用
      hello world*/
	//注意:不要用拷贝构造函数初始化匿名对象
	/*person p;
	person p2 = person(10);
	person p3 = person(p);
	person(p3);*/
	/*严重性	代码	说明	项目	文件	行	禁止显示状态
	错误	C2086	“person p3” : 重定义	*/
	//  person(p3)===person p3;
	//3、隐式转换法
	person p4 = 10;//person p4 = person(10);

}
int main()
{
	test01();
	system("pause");
	return 0;
}

2.3 拷贝构造函数的调用时机

  • 使用一个已经创建完的对象初始化一个新的对象
  • 值传递的方式给函数参数传值
  • 以值的方式返回局部对象
#include<iostream>
using namespace std;
class person
{
public:
	person()
	{
		cout << "无参构造函数的调用" << endl;
	}
	person(int age)
	{
		m_age = age;
		cout << "有参构造函数的调用" << endl;
	}
	person(const person& p)
	{
		cout << "拷贝构造函数的调用" << endl;
		m_age = p.m_age;
	}
	~person()
	{
		cout << "析构函数的调用" << endl;
	}
	int m_age;
};
void test01()
{
	//1、 使用一个已经创建完的对象初始化一个新的对象
	person p1(20);
	person p2(p1);
	cout << "p2的年龄:" << p2.m_age << endl;
}
void doWork(person p)
{

}
void test02()
{
	//2、 值传递的方式给函数参数传值
	person p;
	doWork(p);
}
person doWork2()
{
	person p1;
	cout << (int*)&p1 << endl;
	return p1;
}
void test03()
{
	//3、以值的方式返回局部对象

	person p = doWork2();
	cout << (int*)&p << endl;

}
person& doWork3()
{
	person p1;
	cout << (int*)&p1 << endl;
	return p1;//返回局部变量的地址
}
void test04()
{
	//4、以引用的方式返回局部对象

	person p = doWork3();
	cout << (int*)&p << endl;
}
int main()
{
	//test01();
	//test02();
	test03();
	//test04();
	system("pause");
	return 0;
}

2.4 构造函数的调用规则

默认情况下,c++编译器至少会给一个类添加三个函数
1、默认构造函数,函数体为空
2、默认析构函数,函数体为空
3、默认拷贝构造函数,对属性的值进行拷贝
构造函数的调用规则如下:

  • 如果用户定义有参构造函数,系统不会提供无参构造函数,但会提供拷贝构造函数
class person
{
public:
	/*person()
	{
		cout << "无参构造函数的调用" << endl;
	}*/
	person(int age)
	{
		m_age = age;
		cout << "有参构造函数的调用" << endl;
	}
	person(const person& p)
	{
		cout << "拷贝构造函数的调用" << endl;
		m_age = p.m_age;
	}
	~person()
	{
		cout << "析构函数的调用" << endl;
	}
	int m_age;
};
void test01()
{
	person p;
}

在这里插入图片描述

  • 用户如果提供拷贝构造函数,系统不会提供其他
class person
{
public:
	/*person()
	{
		cout << "无参构造函数的调用" << endl;
	}*/
	/*person(int age)
	{
		m_age = age;
		cout << "有参构造函数的调用" << endl;
	}*/
	person(const person& p)
	{
		cout << "拷贝构造函数的调用" << endl;
		m_age = p.m_age;
	}
	~person()
	{
		cout << "析构函数的调用" << endl;
	}
	int m_age;
};
void test01()
{
	person p;
	person(10);
}

在这里插入图片描述

2.5 深拷贝和浅拷贝

浅拷贝:简单的赋值操作
深拷贝:在堆区自己申请一个空间,进行拷贝操作

#include<iostream>
using namespace std;
class person
{
public:
	person()
	{
		cout << "无参构造函数的调用" << endl;
	}
	person(int age,int height)
	{
		m_age = age;
		m_height =new int(height);//开创在堆区
		cout << "有参构造函数的调用" << endl;
	}
	/*person(const person& p)
	{
		cout << "拷贝构造函数的调用" << endl;
		m_age = p.m_age;

	}*/
	~person()
	{
		if (m_height != NULL)//手动释放在堆区的数据
		{
			delete m_height;
			m_height = NULL;
		}
		cout << "析构函数的调用" << endl;
	}
	int m_age;
	int* m_height;
};
void test01()
{
	person p1(18,160);
	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()
{
	test01();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
原因:堆区的内存被重复释放
在这里插入图片描述
解决办法:通过深拷贝,在堆区重写申请一个空间,重写拷贝构造函数

    person(const person& p)
	{
		cout << "拷贝构造函数的调用" << endl;
		m_age = p.m_age;
		//m_height = p.m_height;//浅拷贝
		m_height = new int(*p.m_height);//深拷贝
	}

2.6初始化列表

作用:初始化成员属性
语法 :构造函数():属性1(值1),属性2(值2)…{}
1、传统初始化成员属性是通过构造函数

class person
{
public:
	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;
	
};

2、初始化列表初始化成员属性

    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;

2.7 类对象作为类成员

对象成员:类的成员可以是另一个类的对象

  • 当其他类作为本类的成员,先创建类成员对象,再创建本类对象
  • 析构的顺序与构造相反
#include<iostream>
using namespace std;
#include<string>
class phone
{
public:
	phone(string pName)
	{
		cout << "phone的构造函数" << endl;
		phone_name = pName;
	}
	~phone()
	{
		cout << "phone的析构函数" << endl;
	}
	string phone_name;
};
class person
{
public:
	//phone m_phone=pName;//隐式转换法
	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 test01()
{
	person p("张三", "苹果");
	cout << "姓名:" << p.m_name << " 手机:" << p.m_phone.phone_name << endl;
}
int main()
{
	
	test01();
		
	
	system("pause");
	return 0;
}

2.8 静态成员

2.8.1 静态成员变量
  • 所有对象共享一份数据
  • 在编译阶段分配内存(全局区)
  • 类内说声明,类外初始化
#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
	static int m_a;//类内声明
private:
	static int m_b;
};
//类外初始化
int Person::m_a = 100;
int Person::m_b = 10;
int main()
{
	//1、通过类名访问
	cout << "Person::m_a "<< Person::m_a << endl;
	//2、通过对象访问
	Person p;
	cout << "p.m_a " << p.m_a << endl;
	Person::m_a = 200;
	//1、通过类名访问
	cout << "Person::m_a " << Person::m_a << endl;
	//2、通过对象访问
	cout << "p.m_a " << p.m_a << endl;
	//cout << Person::m_b << endl;//不可访问
	system("pause");
	return 0;
}
2.8.2 静态成员函数
  • 所有对象共享同一个函数
  • 静态成员变量,只能访问静态成员函数
#include<iostream>
using namespace std;
#include<string>
class Person
{
private:
	static void func2()
	{
		m_a = 200;
		cout << "func()的调用:" << endl;
	}
public:
	static void func()
	{
		m_a = 200;
		cout << "func()的调用:" << endl;
	}
	static int m_a;
};
int Person::m_a=100;
void test01()
{
	//1、通过对象去调用
	Person p;
	p.func();
	//不可访问Person::func2();
	//2、通过类名访问
	Person::func();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3、c++对象模型和this指针

3、1

  • 成员变量和成员函数分开存储
  • 只有非静态成员变量才属于类的对象
#include<iostream>
using namespace std;
#include<string>
class Person
{
	static void func1() {}//不属于类的对象
	void func2(){}//不属于类的对象
	static int m_b;//不属于类的对象
	int m_a;//属于类的对象
};
int Person::m_b=100;
void test01()
{
	Person p;
	//空对象也会分配一个内存空间,是为了区分对象占据内存的位置
	cout << "sizeof(p):" << sizeof(p) << endl;//1
}
void test02()
{
	Person p;
	cout << "sizeof(p):" << sizeof(p) << endl;
}
int main()
{
	//test01();
	test02();//4
	system("pause");
	return 0;
}

3、2 this指针的概念

this指针:指向被调用的成员函数所属的对象

  • this指针是隐含在每一个非静态成员的一种指针

  • this指针不需要定义,直接使用就好
    this指针的用途:

  • 当形参和成员变量同名,可以使用this区分
    在这里插入图片描述
    在这里插入图片描述

class Person
{
public:
	Person(int age)
	{
		this->age = age;
	}
	int age;
};
  • 在类的非静态成员函数返回对象本身,可使用return *this
#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
	//1、解决名称冲突
	Person(int age)
	{
		this->age = age;
	}
	Person& PersonAddPerson(Person& p)
	{
		this->age += p.age ;
		return *this;
	}
	int age;
};
void test01()
{
	Person p1(18);
	cout << p1.age << endl;

}
void test02()
{
	Person p(10);
	Person p2(10);
	p2.PersonAddPerson(p).PersonAddPerson(p).PersonAddPerson(p);
	cout << p2.age << endl;
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

3、3 空指针访问成员函数

#include<iostream>
using namespace std;
class Person
{
public:
	void showClass()
	{
		cout << "this is a Person class" << endl;
	}
	void showAge()
	{
		if (this == NULL)
		{
			return;
		}
		cout << "age=" << this->m_age << endl;
	}
	int m_age;

};
void test01()
{
	Person *p = NULL;
	p->showClass();//空指针访问成员函数
	p->showAge();//读取访问权限冲突
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3、4 const修饰成员函数

常函数:
this指针本质是一个指针常量,指针的指向不可以修改

  • 成员函数加const称为常函数

  • 常函数不可以修改成员属性

  • 成员属性声明关键字mutable,在常函数中依然可以修改
    常对象:

  • 声明对象前加const称该对象为常对象

  • 常对象只能调用常函数

#include<iostream>
using namespace std;
class Person
{
public:
	//this==Person *const this;
	void showPerson()const//修饰this指针,使指针的值也不可以改变
	{
		//this->m_a = 200;
		this->m_b = 200;//常函数可以修改
	}
	void func()
	{
		m_a = 100;
	}
	int m_a;
	mutable int m_b;
};
void test01()
{
	Person p;
}
void test02()
{
	const Person p;
	//p.m_a = 100;
	p.m_b = 100;//在常对象也可以修改
	//常对象只能调用常函数
	p.showPerson();
	//p.func();//常对象不可以调用普通成员函数,因为普通成员函数可以修改成员属性,如果可以调用,相当于侧面通过常对象修改属性

}
int main()
{
	test01();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值