C++学习——类和对象—封装

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


1.封装的意义

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

封装的意义一:
在设计类的时候,属性和行为写在一起,表现事物
语法:class 类名{  访问权限:属性/行为 };

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

//周长公式:2*PI*半径
#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;
}

总结:

1.如何使用class创建一个类

2.定义属性和行为

3.通过类实例化一个圆(通过一个类创建一个对象的过程)

2.封装案例-学生类

类中的属性和行为 统一称为 成员
属性 成员属性、成员变量
行为 成员函数、成员方法

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

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

class Student
{
	public:
		//属性
		string s_Name;
		int s_Num;

		//行为
		//显示学生姓名学号
		void Print()
		{
			cout<<"姓名:"<<s_Name<<endl;//不用加Student.!!
			cout<<"学号:"<<s_Num<<endl;
		}
		给姓名赋值
		//void setName(string name)
		//{
		//	s_Name=name;
		//}
};

int main()
{
	//实例化
	Student s1;
	Student s2;
	s1.s_Name="wangyi";
	s1.s_Num=201901;
	s2.s_Name="zhaoer";
	s2.s_Num=201902;
	s1.Print();
	s2.Print();

	//s1.setName("zhangsan");//另一种方法
	system("pause");
	return 0;
}

3.访问权限

访问权限有三种:
1. public 公共权限:类内和类外都可以访问
2. protected 保护权限 :类内可以访问 类外不可以访问 (儿子可以访问父亲中的保护内容)
3. private 私有权限 :类内可以,类外不可以  (儿子不可以访问父亲的私有内容)

#include<iostream>
using namespace std;

class Person
{
//公共权限
public:
	string m_Name;

//保护权限
protected:
	string m_Car;

//私有权限
private:
	int m_Password;

public:
	void func()
	{
		m_Name="张三";
		m_Car="bus";
		m_Password=12345;
	}
};

int main()
{
	//实例化具体对象
	Person p1;

	p1.m_Name="lisi";
	//p1.m_Car="bz";//类外不可访问
	//p1.m_Password="1111";//类外不可访问
	system("pause");
	return 0;
}

4.struct和class区别

c++中struct和class唯一区别在于 默认访问权限不同
//struct 默认权限为公有
//class  默认权限为私有

#include<iostream>
using namespace std;

class C1
{
	int m_A;//默认权限为私有

};

struct C2
{
	int m_A; //默认权限为公有
};

int main()
{
	//C1 c1;
	//c1.m_A=100;//报错,私有不可在类外访问
	C2 c2;
	c2.m_A=100;

	system("pause");
	return 0;
}

5.成员属性私有化

优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,可以监测数据的有效性

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

//设计人类
class Person
{
public:
	//写姓名 设置姓名
	void setName(string name)
	{
		m_Name=name;
	}

	//读姓名 获取姓名
	string getName()
	{
		return m_Name;
	}

	//获取年龄  可读可写如果想修改设置一个对外接口
	int getAge()
	{
		//m_Age=0;//初始化0岁
		return m_Age;
	}

	//设置一个对外接口,设置年龄  范围 0~150
	void setAge(int age)
	{
		if(age<0||age>150)
		{
			cout<<"wrong"<<endl;
			return;
		}
		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("zhangsan");
	cout<<"姓名为:"<<p.getName()<<endl;

	//p.getAge=18;//错误,权限为只读
	p.setAge(100);
	cout<<"年龄为:"<<p.getAge()<<endl;

	p.setLover("llll");//设置为 lllll
	//cout<<"情人为:"<<p.m_Lover<<endl; //报错,只有内部可以获取到
	
	system("pause");
	return 0;
}

6.设计案例——立方类

设计立方类
设计立方类Cube 求出立方体的面积和体积,分别用全局函数和成员函数判断两个立方体是否相等
//属性(private):m_L , m_W, m_H
//行为(public):获取立方体的面积;获取立方体的体积 ;设置获取长宽高(属性的接口)

立方体类设计
1.创建立方体类
2.设计属性和行为
3.设计行为 :获取立方体面积和体积
4.分别利用 全局函数和成员函数 判断两个立方体是否相等   (成员函数写在类里面)

#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_W*m_H+2*m_L*m_H;
	}
	//获取立方体体积
	int calculateV()
	{
		return m_L*m_W*m_H;
	}

	//利用成员函数判断两个立方体是否相等
	bool isSameByClass(Cube &c)  //成员函数只需要传入一个:用已知的去调用未知的
	{
		if(m_L==c.getL()&&m_H==c.getW()&&m_W==c.getH())
		{
			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.getW()==c2.getW()&&c1.getH()==c2.getH())
	{
		return true;
	}
	return false;
}

int main()
{
	//创建立方体对象
	Cube c1;
	c1.setL(10);
	c1.setW(10);
	c1.setH(10);

	cout<<"c1的面积为:"<<c1.calculateS()<<endl;
	cout<<"c1的体积为:"<<c1.calculateV()<<endl;

	//创建第二个立方体
	Cube c2;
	c2.setL(10);
	c2.setW(10);
	c2.setH(10);
	cout<<"c2的面积为:"<<c2.calculateS()<<endl;
	cout<<"c2的体积为:"<<c2.calculateV()<<endl;

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

	//利用成员函数判断
	ret=c1.isSameByClass(c2);//成员函数只需要传入一个:用已知c1的去调用未知的c2,去进行对比 
	if(ret)
	{
		cout<<"成员函数判断:c1和c2相等"<<endl;
	}else{
		cout<<"成员函数判断:c1,c2不相等"<<endl;
	}
	system("pause");
	return 0;
}

7.设计案例——点和圆

//设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系
//点和圆关系判断
//点与圆心距离 与半径进行比较
//得出点到圆心的距离

#include<iostream>
using namespace std;
#include"circle.h"
#include"point.h"
//注释掉的部分在头文件中接入(后附)

//点和源关系案例
点类
//class Point
//{
//	//设置一些公共接口用来获取坐标数据
//public:
//	//设置x
//	void setX(int x)
//	{
//		m_X=x;
//	}
//	//获取x
//	int getX()
//	{
//		return m_X;
//	}
//	//设置y
//	void setY(int y)
//	{
//		m_Y=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 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{
		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(10);

	//判断关系
	isInCircle(c,p);

	system("pause");
	return 0;
}
#include"circle.h"

//圆类
//class Circle
//{
//public:
	//设置半径
	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;
	}
//private:
//	int m_R;//半径
//
//	//****在类中可以让另一个类作为本来中的成员
//	Point m_Center;//圆心,用到第一个类
//};
#include"point.h"

//留住所有函数的实现

//点类
//class Point
//{
	//设置一些公共接口用来获取坐标数据
//public:
	//设置x
	void Point::setX(int x) //Point:: 表名是Point的作用域
	{
		m_X=x;
	}
	//获取x
	int Point::getX()
	{
		return m_X;
	}
	//设置y
	void Point::setY(int y)
	{
		m_Y=y;
	}
	//获取y
	int Point::getY()
	{
		return m_Y;
	}
//private:
//	int m_X;
//	int m_Y;
//};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值