2021-8-1 C++进阶学习 -> 4.1类和对象--封装

4.1.1封装的意义

class代表要设计一个类,后面紧跟着类的名字

类中的属性和行为都叫做成员

属性 成员属性 成员变量<-->行为 成员函数 成员方法

实例化(通过一个类,来创建一个对象的过程

#include <iostream>
#include <string>
using namespace std;

const double PI = 3.14;
//class代表要设计一个类,后面紧跟着类的名字
class circle {
	//访问权限
public://公共权限
	//类中的属性和行为都叫做成员
	//属性 成员属性 成员变量
	//行为 成员函数 成员方法
	
	//属性
	//半径
	int r;

	//行为
	//获取圆的周长
	double calculatezc() {
		return 2 * PI * r;
	}
};
int main() {
	//通过圆类创建一个具体的圆(对象)
	//实例化(通过一个类,来创建一个对象的过程)
	circle c1;
	//给对象的属性进行赋值操作
	c1.r = 10;
	cout << "圆的周长:" << c1.calculatezc() << endl;

	system("pause");
	return 0;
}

class student {
public:
	string name;
	string num;
	void showmenu() {
		cout << name << endl;
		cout << num << endl;
	}
};
int main(){
	student s1;
	cin >> s1.name;
	cin >> s1.num;
	s1.showmenu();

	system("pause");
}

4.1.2访问权限

访问权限
三种:
公共权限 public        成员    类内可以访问,类外可以访问和
保护权限 protected    成员    类内可以访问,类外不可以访问  儿子也可以访问父亲中的保护内容
私有权限 private        成员    类内可以访问,类外不可以访问  儿子不可以访问父亲的私有内容

#include <iostream>
#include <string>
using namespace std;
//访问权限
//三种:
//公共权限 public		成员	类内可以访问,类外可以访问和
//保护权限 protected	成员	类内可以访问,类外不可以访问  儿子也可以访问父亲中的保护内容
//私有权限 private		成员	类内可以访问,类外不可以访问  儿子不可以访问父亲的私有内容

class person
{
public:
	//公共权限
	string name;//姓名

protected:
	//保护权限
	string car;

private:
	//私有权限
	int password;

public:
	void func()
	{
		name = "张三";
		car = "拖拉机";
		password = 123456;

	}

};
int main() {

	//实例化具体对象
	person p1;

	p1.name = "张三";
	//p1.car = "奔驰";		    //保护权限的内容在类外无法访问
	//p1.password = "23";		//私有权限的内容在类外无法访问
	p1.func();

	system("pause");

}

4.1.3、1.class和struct的区别2.将成员属性设为私有

class和struct的区别
    struct默认权限是 公共 public
    class默认权限是  私有 private

#include <iostream>
#include <string>
using namespace std;

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

struct c2 
{
	int a;//公共权限 公共
};
int main() {

	//class和struct的区别
	//struct默认权限是 公共 public
	//class默认权限是  私有 private
	//c1 c1;
	//c1.a = 100;

	c2 c2;
	c2.a = 100;
	system("pause");
}

2.将成员属性设为私有

优点:
1、可以自己控制读写权限
2、可以检测数据的有效性

#include <iostream>
#include <string>
using namespace std;
//成员属性设置为私有化
//1、可以自己控制读写权限
//2、可以检测数据的有效性

//设计人类
class person
{
public:
	//设置姓名
	void setname(string Name){
		name = Name;
	}
	//获取姓名
	string  getname() {
		return name;
	}
	int getage() {
		//age = 0;
		return age;
	}//初始化 可读可写 如果想修改(年龄必须在0到100之间)

	void setage(int Age) {
		if (Age < 0 || Age > 100) {
			age = 0;
			cout << "你这个老妖精" << endl;
			return;
		}
		age = Age;
	}

	//设置情人 只写
	void setlover(string Lover) {
		lover = Lover;
	}

private:
	//姓名  可读可写
	string name;
	//年龄	只读
	int age;
	//情人	只写
	string lover;
};
int main() {
	person p1; 
	p1.setname("张三");
	p1.setage(1000);
	cout << p1.getname() <<	endl;
	cout << p1.getage() << endl;
	p1.setlover("苍井");
	
	system("pause");
}

设计案例1 -> 立方体类设计

立方体类设计
1、创建按立方体的类
2、设计属性和行为
3、设计行为获取立方体的面积和体积
4、分别利用全局函数和成员函数  判断两个立方体是否相等

#include <iostream>
#include <string>
using namespace std;

//立方体类设计
//1、创建按立方体的类
//2、设计属性和行为
//3、设计行为获取立方体的面积和体积
//4、分别利用全局函数和成员函数  判断两个立方体是否相等
class cube {

public:

	//设置长
	void setl(int l) {
		c_l = l;

	}
	//获取长,以下类似
	int getl() {
		return c_l;
	}

	void setw(int w) {
		c_w = w;

	}
	int getw() {
		return c_w;
	}


	void seth(int h) {
		c_h = h;

	}
	int geth() {
		return c_h;
	}
	//获取立方体的面积
	int calcuates() {
		return 2 * c_l*c_w + 2 * c_h*c_l + 2 * c_w*c_h;
	}
	//获取立方体的体积
	int calcuate() {
		return c_h * c_l * c_w;
	}

	bool issamebyclass(cube &c3) {
		if (c3.getl() == c_l && c3.geth() == c_h && c3.getw() == c_w) {
		return true;
		}	
		return false;
}
private:
	int c_l;
	int c_w;
	int c_h;
};

//利用全局函数判断 两个立方体是否相等
bool issame(cube &c1, cube &c2) 
{
	if(c1.getl() == c2.getl() && c1.geth() == c2.geth() && c1.getw() == c2.getw()) {
		return true;
	}
		return false;
}

int main() {
	//创建一个立方体的对象
	cube c1;
	c1.seth(10);
	c1.setw(10);
	c1.setl(10);

	cout << "c1的面积为" << c1.calcuates() << endl;
	cout << "c1的体积为" << c1.calcuate() << endl;

	//创建第二个立方体
	cube c2;
	c2.seth(10);
	c2.setl(10);
	c2.setw(10);

	bool ret = issame(c1, c2);
	//利用全局函数判断
	if (ret) {
		cout << "全局函数判断结果:c1和c2是相等的" << endl;
	}
	else {
		cout << "全局函数判断结果:c1和c2是不相等的" << endl;
	}

	//利用成员函数判断
	ret = c1.issamebyclass(c2);
	if (ret) {
		cout << "成员函数判断结果:c1和c2是相等的" << endl;
	}
	else {
		cout << "成员函数判断结果:c1和c2是不相等的" << endl;
	}

	system("pause");
}

设计案例2 -> 点和圆的关系

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

//点和圆的关系案例

//点类
class point {
public:
	//设置和获取坐标
	void setx(int X) {
		x = X;
	}
	int getx() {
		return x;
	}

	void sety(int Y) {
		y = Y;
	}
	int gety() {
		return y;
	}
private:
	int x;
	int y;
};


//圆类
class circle {

public:
	//设置半径
	void setr(int R) {
		r = R;
	}
	int getr() {
		return r;
	}
	
	//设置圆心
	void setcenter(point Center) {
		center = Center;
	}
	//获取圆心
	point getcenter() {
		return center;
	}
private:
	int r;
	point center;
};

//判断点和圆的关系
void isincircle(circle &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 if(distance < rdistance) {
		cout << "点在圆内" << endl;
	}
}
int main() {
	//创建圆
	circle c;
	c.setr(10);
	
	point center;
	center.setx(10);
	center.sety(10);
	c.setcenter(center);
	//创建点
	point p;
	p.setx(0);
	p.sety(10);

	//判断关系
	isincircle(c, p);
	system("pause");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值