C++系列-类和对象-封装

类和对象-封装

C++面向对象的三大特征

  • 封装
  • 继承
  • 多态

封装-类

  • 具有相同性质的对象可以抽象为类,可以有属性,行为
  • 类中的属性和行为统一称作成员
  • 属性:成员变量,成员属性
  • 行为:成员函数,成员方法
cpp 复制代码
#include<iostream>
using namespace std;
const double PI = 3.14;
class Circle
{
public:				// 访问权限
	int r;			// 属性
	double cal_c()	// 行为
	{
		return 2 * PI * r;
	}
};

void main()
{
	Circle c1;		// 实例化一个对象, 通过类创建对象的过程
	c1.r = 5;
	double c = c1.cal_c();
	cout << c << endl;
	system("pause");
}
cpp 复制代码
#include<iostream>
using namespace std;
class Student
{
public:						// 访问权限
	// 属性
	string name;			
	int id;
	// 行为
	void show_name()
	{
		cout << name << endl;
	}
	void show_id()
	{
		cout << id << endl;
	}
};

void main()
{
	Student s1;		// 实例化一个对象
	s1.name = "Zhangsan";
	s1.id = 20;
	s1.show_name();
	s1.show_id();
	system("pause");
}

封装-访问权限

  • public
    --成员在类内可以访问,在内外也可以访问
  • protected
    --成员在类内可以访问,在内外不可以访问,子类可以访问
  • private
    --成员在类内可以访问,在内外不可以访问,子类不可以访问
cpp 复制代码
code:
	#include<iostream>
	using namespace std;
	class Person
	{
	public:						// 访问权限
		string name;			// 属性
	protected:
		string car;
	private:
		int password;
	public:
		void set_info(string ref_name, string ref_car, int ref_password)
		{
			// 几种权限内类都可以访问
			name = ref_name;			
			car = ref_car;
			password = ref_password;
		}
		void show_info()
		{
			cout << name << "_" <<  car << "_"  <<  password << endl;
		}
	};
	
	void main()
	{
		Person p1;
		p1.set_info("Lili", "BMW", 666888);
		p1.show_info();
	
		p1.name = "HanMeimei";
		//p1.car = "BENZ";			// protected权限类外不能访问
		//p1.password = 333;		// private权限类外不能访问
		p1.show_info();
		p1.set_info("GuTianle", "Volvo", 123456);		// 可以通过public的成员方法改变protected和private权限的成员变量
		p1.show_info();
		system("pause");
	}
result:
	Lili_BMW_666888
	HanMeimei_BMW_666888
	GuTianle_Volvo_123456

封装-struct和class的区别

  • struct和class的默认访问权限不同。
    -- struct默认访问权限是public。
    -- class默认访问权限是private。
cpp 复制代码
#include<iostream>
using namespace std;
class Person_c
{
	string name;			// 默认访问权限是private
	void show_info()
	{
		cout << name << endl;
	}
};

struct Person_s
{
	string name;			// 默认访问权限是public
	void show_info()
	{
		cout << name << endl;
	}
};

void main()
{
	Person_c pc1;
	// 类的默认访问权限是private,所以成员变量和函数在类外不能访问
	// pc1.name = "LiuDehua";		
	// pc1.show_info();
	// struct的默认访问权限是public,所以成员变量和函数在类外可以访问
	Person_s ps1;
	ps1.name = "Libai";
	ps1.show_info();
	system("pause");
}

封装-成员属性私有化

  • 成员属性设置为私有,可以控制其读写权限
  • 对于写权限,可以检测数据有效性
cpp 复制代码
#include<iostream>
using namespace std;
class Person
{
private:
	string name;			// 默认访问权限是private
	int age = 0;

public:
	void set_info(string ref_name, int ref_age)
	{
		name = ref_name;
		if (ref_age < 0 || ref_age > 150)
		{
			cout << "这是个妖怪!" << endl;
			return;
		}
		age = ref_age;
	}
	void show_info()
	{
		cout << name << "_" << age << endl;
	}
};

void main()
{
	Person p1;
	p1.set_info("Libai", 180);
	p1.show_info();
	system("pause");
}

封装-成员变量为类

cpp 复制代码
#include<iostream>
using namespace std;
class Point
{
private:
	int pos_x;
	int pos_y;
	
public:
	void set_info(int ref_x, int ref_y)
	{
		pos_x = ref_x;
		pos_y = ref_y;
	}
	void get_info(int &x, int& y)
	{
		x = pos_x;
		y = pos_y;
	}
};

class Circle
{
private:
	Point center;
	int radius;
public:
	void set_info(Point ref_center, int ref_radius)
	{
		center = ref_center;
		radius = ref_radius;
	}
	void get_info(int& ret_center_x, int& ret_center_y, int& ret_radius)
	{
		center.get_info(ret_center_x, ret_center_y);
		ret_radius = radius;
	}
};

void is_incricle(Circle& c, Point& p)
{
	int c_x, c_y, c_r;
	c.get_info(c_x, c_y, c_r);
	int p_x, p_y;
	p.get_info(p_x, p_y);
	int dis = (p_x - c_x) * (p_x - c_x) + (p_y - c_y) * (p_y - c_y);
	if (dis > (c_r * c_r))
		cout << "点在圆外" << endl;
	else if (dis == (c_r * c_r))
		cout << "点在圆上" << endl;
	else
		cout << "点在圆内" << endl;
}
void main()
{
	Point p1;
	p1.set_info(10, 9);
	int p_x, p_y;
	p1.get_info(p_x, p_y);
	cout << "点的坐标是:" << "x=" << p_x << " y=" << p_y << endl;

	Point p2;
	p2.set_info(10, 0);
	Circle c1;
	c1.set_info(p2, 10);

	int x, y, r;
	c1.get_info(x, y, r);
	cout << "圆的坐标是:" << "x=" << x << " y=" << y << " r=" << r << endl;
	is_incricle(c1, p1);
	system("pause");
}

封装-将类置于单独的文件中

cpp 复制代码
poin.h文件
#pragma once
#include<iostream>
using namespace std;
// 在头文件中写上成员变量和成员函数的声明即可
class Point
{
private:
	int pos_x;
	int pos_y;

public:
	void set_info(int ref_x, int ref_y);
	void get_info(int& x, int& y);
};
cpp 复制代码
poin.cpp文件
#include"point.h"
// 所有成员函数的实现放在源文件中,并说明函数的作用域
void Point::set_info(int ref_x, int ref_y)
{
	pos_x = ref_x;
	pos_y = ref_y;
}
void Point::get_info(int& x, int& y)
{
	x = pos_x;
	y = pos_y;
}
cpp 复制代码
circle.h文件
#pragma once
#include<iostream>
using namespace std;
#include"point.h"
class Circle
{
private:
	Point center;
	int radius;
public:
	void set_info(Point ref_center, int ref_radius);
	void get_info(int& ret_center_x, int& ret_center_y, int& ret_radius);
};
cpp 复制代码
circle.cpp文件
#include"circle.h"
void Circle::set_info(Point ref_center, int ref_radius)
{
	center = ref_center;
	radius = ref_radius;
}
void Circle::get_info(int& ret_center_x, int& ret_center_y, int& ret_radius)
{
	center.get_info(ret_center_x, ret_center_y);
	ret_radius = radius;
}
相关推荐
正小安24 分钟前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
小飞猪Jay2 小时前
C++面试速通宝典——13
jvm·c++·面试
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光2 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   2 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   2 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
Fan_web2 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常2 小时前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式
rjszcb3 小时前
一文说完c++全部基础知识,IO流(二)
c++
莹雨潇潇3 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器