【C++核心编程 4】——类和对象——4.1封装[封装的意义 / struct 和 class 的区别 / 成员属性设置为私有]

4.类和对象

C++面向对象的三大特性:封装、继承、多态

C++认为万事万物都皆为对象,对象上有其属性和行为


具有相同性质的对象,可以抽象为


4.1 封装

4.1.1 封装的意义

封装是C++面向对象三大特性之一。

封装的意义:

  • 将属性和行为作为一个整体,表现生活中的事物
  • 将属性和行为加以权限控制
封装意义一:

在设计类的时候,属性和行为写在一起,表现事物。

语法:class 类名{ 访问权限:属性/行为}

示例一:设计一个圆类,求圆的周长
#include<iostream>
using namespace std;
#define PI 3.14

//设计一个圆类,求圆的周长
class circle {
	//访问权限  public  private
public:
	//属性:半径
	int r;
	//行为:获取圆的周长
	double getPerimeter() {
		return 2 * PI * r;
	}
};

int main() {
	//通过圆类,创建具体的圆(对象)   -----实例化
	circle c;
	c.r = 10;//给对象的属性进行赋值
	cout << "圆的周长:" << c.getPerimeter() << endl;
	return 0;
}

示例二:设计一个学生类,属性:姓名,学号,行为:给姓名和学号赋值,显示学生的姓名和学号。

通过行为进行赋值操作

#include<iostream>
#include<string>
using namespace std;
class Student {
  //类中的属性和行为统称为  成员
  //属性  称为 成员属性 或 成员变量
  //行为 称为 方法
public:
	string stu_name;
	string stu_id;

	void setName(string name) {
		stu_name = name;
	}
	void setId(string id) {
		stu_id = id;
	}
	void getStuInfo() {
		cout << "student name:" << stu_name << endl;
		cout << "student Id:" << stu_id << endl;
	}
};
int main() {
	Student st1;
	st1.setName("李四");
	st1.setId("20220102");
	st1.getStuInfo();
	Student st2;
	st1.stu_name = "kujada";
	st1.stu_id = "20220105";
	st1.getStuInfo();
	return 0;
}

封装的意义二:

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

访问权限有三种:

  1. public 公告权限 成员类内可以访问,类外可以访问
  2. private 私有权限 成员类内可以访问,类外不可以访问
  3. protected 保护权限 成员类内可以访问,类外不可以访问 继承子类可以访问
#include<iostream>
#include<string>
using namespace std;

//访问权限 3种
class Person {
public:
	string name;
protected:
	string car;
private:
	int password;

public:
	void func() {
		name = "张三";
		car = "小破车";
		password = 123445;
	}
};

int main() {
	Person p;
	p.name = "李四";
	p.func();
	cout << p.name << endl;//张三
	return 0;
}

4.1.2 struct 和 class区别

在C++种,struct 和 class 唯一的区别就在于默认的访问权限不同

  • struct 默认权限为公共
  • class 默认权限为私有
#include<iostream>
#include<string>
using namespace std;

class A {
	int a;//默认权限私有private
};

struct B {
	int b;//默认权限公共public
};
int main() {
	A a1;
	//a1.a; 无法访问,默认权限私有
	B b1;
	b1.b=3;//可以访问,默认权限公共
	cout << b1.b << endl;
	return 0;
}

4.1.3 成员属性设置为私有

优点1:将所有成员属性设置为私有,可以自己控制读写权限

优点2:对于写权限,可以检测数据和的有效性

#include<iostream>
#include<string>
using namespace std;
class Person {
private:
	string p_name;//可读可写
	int p_age;//只读
	int p_high;//可读可写,检测数据有效性
public:
	//写姓名
	void setName(string name) {
		p_name = name;
	}
	//读姓名
	string getName() {
		p_age = 9;
		return p_name;
	}

	int getAge() {
		return p_age;
	}

	void setHigh(int high) {
		if (high > 200 || high < 10) {
			cout << "请输入10-200范围内的数据" << endl;
			p_high = 0;
			return;
		}
		else {
			p_high = high;
		}	
	}
  
	int getHigh() {
		return p_high;
	}
};

int main() {
	Person per;
	per.setName("张三");
	cout << "姓名:" << per.getName() << endl;
	cout << "年龄:" << per.getAge() << endl;
	per.setHigh(0);
	cout << "身高:" << per.getHigh() << endl;
	return 0;
}

案例:设计立方体类

创建立方体类,设计属性,设计行为:获取面积和体积;分别利用全局函数和成员函数判断两个立方体是否相等。

#include<iostream>
using namespace std;
//设计立方体类,求立方体表面积和体积
class Cube {
private:
	int c_l;
	int c_w;
	int c_h;
public:
	void setL(int l) {
		if (l >= 0) {
			c_l = l;
		}	
		else {
			c_l = 0;
			cout << "请输入大于0的数" << endl;
			return;
		}	
	}
	int getL() {
		return c_l;
	}
	void setW(int w) {
		if (w >= 0) {
			c_w = w;
		}
		else {
			c_w = 0;
			cout << "请输入大于0的数" << endl;
			return;
		}
	}
	int getW() {
		return c_w;
	}
	void setH(int h) {
		if (h >= 0) {
			c_h = h;
		}
		else {
			c_h = 0;
			cout << "请输入大于0的数" << endl;
			return;
		}
	}
	int getH() {
		return c_h;
	}

	int getArea() {
		return c_l * c_w * 2 + c_l * c_h * 2 + c_w * c_h * 2;
	}
	int getVolume() {
		return c_l * c_w * c_h;
	}
	//成员函数
	bool isSame(Cube& c) {
		if (c_l == c.getL() && c_w == c.getW() && c_h == c.getH()) {
			return true;
		}
		return false;
	}

};
//全局函数
bool isSame(Cube& c1, Cube& c2) {
	if (c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH()) {
		return true;
	}
	return false;
}
int main() {
	Cube c1;
	c1.setL(2);
	c1.setW(3);
	c1.setH(4);
	cout << "面积:" << c1.getArea() << endl;
	cout << "体积:" << c1.getVolume() << endl;
	Cube c2;
	c2.setL(2);
	c2.setW(3);
	c2.setH(4);
	cout << "面积:" << c2.getArea() << endl;
	cout << "体积:" << c2.getVolume() << endl;

	//全局函数
	bool ret1=isSame(c1, c2);
	cout << ret1 << endl;
	//成员函数
	bool ret2=c2.isSame(c1);
	cout << ret2 << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值