当前位置: 首页 > article >正文

类和对象C++ (未完:对象特征)

封装

将属性和行为作为一个整体,表现生活中的事物

封装的意义

#include<iostream>
#include<string.h>
using namespace std;
//设计学生类
class student {
public://公共权限

	/*
	类中的属性和行为  统一称为成员
	属性=成员属性=成员变量
	行为=成员函数=成员方法
	*/
	//属性
	string m_Name;//姓名
	int m_Id;//学号

	//权限/行为
	void setName(string name) {
		m_Name = name;
	}
	void setId(int id) {
		m_Id = id;
	}
	void showStudent() {
		cout << "name=" << m_Name << " id=" << m_Id << endl;
	}
};
int main() {
	//创建一个具体的学生——实例化对象
	student s1;
	//两种赋值方式
	//s1.m_Name = "张三";
	//s1.m_Id = 11;

	s1.setName("张三");
	s1.setId(11);

	s1.showStudent();
	return 0;
}

访问权限

公共权限 public
保护权限 protected 
私有权限 private   

#include<iostream>
using namespace std;
/*
访问权限
公共权限 public     成员类内可以访问,类外也可以访问
保护权限 protected  类内可以访问,类外不可以访问      儿子可以访问父亲中保护的内容——父亲想让儿子使用的
私有权限 private    类内可以访问,类外不可以访问      儿子不能访问父亲中私有内容——不亲不想让儿子使用的
*/

class Person {
public:
	string m_Name;

protected:
	string m_Car;

private:
	int m_Password;

public:
	void func() {
		m_Name = "张三";
		m_Car = "小米汽车";
		m_Password = 123456;
	}
};

int main() {
	Person p1;
	p1.m_Name = "张四";//可以使用
	p1.func();//可以使用
	//p1.m_Car   p1.m_Password  不能访问
	return 0;
}

struct和class的区别

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

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

struct C2 {
	int m_A;//默认权限是公共
};
int main() {
	C1 c1;
	C2 c2;
	//c1.m_A;  权限是私有,不可以访问
	c2.m_A;
	return 0;
}

成员属性设置为私有

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

2.对于写权限,可以检测数据的有效性

 

#include<iostream>
#include<string.h>
using namespace std;
class Person {
	//提供公有的方法对私有的属性进行控制
public:
	//设置姓名
	void setName(string name) {
		m_Name = name;
	}
	//获取姓名
	string getName() {
		return m_Name;
	}
	//获取年龄
	int getAge() {
		return m_Age;
	}
	//设置年龄0~150
	void set_Age(int age) {
		if (age < 0 || age > 150) {
			cout << "年龄输入有误,赋值失败" << endl;
			return;
		}
		m_Age = age;
	}
	//设置偶像
	void set_Idol(string idol) {
		m_Idol = idol;
	}
private:
	string m_Name;//可读可写
	int m_Age = 18;//可读    也可以写(年龄必须在0-150之间)
	string m_Idol;//可写
};
int main() {
	Person p;
	p.setName("张三");
	cout << "姓名:" << p.getName() << endl;
	p.set_Age(160);
	cout << "年龄:" << p.getAge() << endl;
	p.set_Idol("卢梭");
	return 0;
}

分别用全局函数和成员函数判断两个立方体是否相等

#include<iostream>
using namespace std;
//1.设计立方体类Cube
//2.求出立方体的面积和体积
//3.分别用全局函数和成员函数判断两个立方体是否相等
class Cube {
public:
	void setLength(int l) {
		m_L = l;
	}
	int getLength() {
		return m_L;
	}
	void setWeight(int w) {
		m_W = w;
	}
	int getWeight() {
		return m_W;
	}
	void setHeight(int h) {
		m_H = h;
	}
	int getHeight() {
		return m_H;
	}
	//面积
	int getArea() {
		return 2 * m_H * m_L + 2 * m_H * m_W + 2 * m_L * m_W;
	}
	//体积
	int getVolume() {
		return m_H * m_L * m_W;
	}

	//利用成员函数判断两个立方体是否相等
	bool isSameByClass(Cube c1,Cube c2) {
		if (c1.m_H == c2.m_H && c1.m_L == c2.m_L && c1.m_W == c2.m_W)
			return true;
		else return false;
	}
private:
	int m_W;//宽
	int m_L;//长
	int m_H;//高
};

//利用全局函数判断
bool isSame(Cube &c1, Cube &c2) {
	if (c1.getLength() == c2.getLength() && c1.getHeight() == c2.getHeight() && c1.getWeight() == c2.getWeight())
		return true;
	else return false;
}

int main() {
	Cube c1;
	c1.setLength(10);
	c1.setWeight(10);
	c1.setHeight(10);
	cout << c1.getArea() << endl;
	cout << c1.getVolume() << endl;

	Cube c2;
	c2.setLength(10);
	c2.setWeight(10);
	c2.setHeight(10);

	//bool ret = isSame(c1, c2);
	bool ret = c1.isSameByClass(c1,c2);
	if (ret) cout << "c1=c2" << endl;
	else cout << "c1!=c2" << endl;
	return 0;
}

判断点和圆的位置关系

//circle.h
#pragma once
#include<iostream>
#include "point.h"
using namespace std;

class Circle {
private:
	int c_R;//半径
	Point c_Center;//圆心

public:
	void setCenter(Point center);
	//获取圆心
	Point getCenter();
	void setR(int r);
	int getR();
};

//point.h
#pragma once
#include<iostream>
using namespace std;

class Point {
private:
	int p_X;
	int p_Y;

public:
	void setX(int x);
	int getX();
	void setY(int y);
	int getY();
};

circle.cpp
#include "circle.h"
void Circle::setCenter(Point center) {
	c_Center = center;
}
//获取圆心
Point Circle::getCenter() {
	return c_Center;
}
void Circle::setR(int r) {
	c_R = r;
}
int Circle::getR() {
	return c_R;
}

//Circle::  是加上作用域

//point.cpp
#include "point.h"

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

//main
#include<math.h>
#include<iostream>
using namespace std;
#include "point.h"
#include "circle.h"

//判断点和圆的关系
void isInCircle(Circle &c,Point &p) {
	int t;
	t=pow(c.getCenter().getX() - p.getX(), 2) + pow(c.getCenter().getY() - p.getY(), 2);
	int r = c.getR();
	if(t < r*r)
		cout << "点在圆内" << endl;
	else if (t == r*r)
		cout << "点在圆上" << endl;
	else
		cout << "点在圆外" << endl;
}

int main() {
	//创建圆
	Circle c1;
	Point center;
	c1.setR(5);
	center.setX(0);
	center.setY(0);
	c1.setCenter(center);

	//创建点
	Point p;
	p.setX(3);
	p.setY(4);

	//判断关系
	isInCircle(c1, p);
	return 0;
}

.h 文件中只写声明

.cpp 文件中只写实现

把类的声明和实现分开写

 对象特征


http://www.kler.cn/a/588042.html

相关文章:

  • linux - 基础IO之操作与文件描述符全解析:从C语言到系统调用底层实现
  • python爬虫Scrapy(5)之增量式
  • 生物角度分析
  • PHP语言的死锁
  • GBase8c 删除备机
  • 【Java 优选算法】分治 - 快速排序
  • 基于redis实现会话保持
  • Chat-Driven Business:灵活交互的新范式
  • python面向对象:封装的编程案例
  • 使用easyexcel实现单元格样式设置和下拉框设置
  • coze ai assistant Task 3
  • 【Django】【vue】设计一个评论模块
  • 【人工智能基础2】Tramsformer架构、自然语言处理基础、计算机视觉总结
  • 数字人本地部署之llama-本地推理模型
  • Skema:AI 驱动的方案到 BIM 加速工具,重塑早期设计工作流
  • superset部署记录
  • 奇安信二面
  • SpringMVC(六)异常:全局捕获与错误响应
  • Android (Kotlin) 高版本 DownloadManager 封装工具类,支持 APK 断点续传与自动安装
  • 【模拟面试】计算机考研复试集训(第五天)