马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
条记复习
1.类和对象
c++面向对象的三大特性为:封装,继续,多态
c++以为万事万物都皆为对象,对象上有其属性和举动
具有类似性子的对象,我们可以抽象为称为类
2.封装
作用:将属性和举动作为一个团体,体现生存中的事物,具有类似性子的对象,我们可以抽象为类。
语法:class 类名{访问权限(也可不写,默以为private):属性/举动};
示例:- class circle{
- public:
- int m_r;
- double calculator(){
- return 2*pi*m_r;
- }
- }
复制代码 封装案例:盘算一个圆的周长- #include<iostream>
- using namespace std;
- const double pi = 3.14;
- class circle {
- public:
- //属性
- //半径
- int m_r;
- //行为
- //获取圆的周长
- double calculatezc() {
- return 2 * pi * m_r;
- }
- };
- int main() {
- 通过圆类创建具体的圆(对象)
- circle c1;
- 给圆对象的属性进行赋值操作
- c1.m_r = 10;
- cout << "圆的周长为" << c1.calculatezc() << endl;
- system("pause");
- return 0;
- }
复制代码 对比结构体我们不难发现,类与结构体可以说千篇一律,但现实上类与结构体的区别很大,不外难度过高,我们这里不做深入探究,下面我们来学习类的访问权限
3.类的访问权限
1)分类
访问权限一共有一下三种:
a.public 公共权限 此中的成员类内可以访问,类外也可以访问
b.protected 掩护权限 此中的成员类内可以访问,类外不可以访问
对于protected权限,特定职员有权访问
c.private 私有权限 此中的成员类内可以访问,类外不可以访问
对于private权限,除本人外其他人无权访问
现实上在结构体struct中也有权限,不外与类class的区别是,struct的默认权限是公共权限,class的默认权限是私有权限
2)访问权限的作用
在现实的编程项目中,我们通常将类的对象的属性设置为私有,而类中的函数则设置为公有。
比方:- class student{
- public:
- int let_you_see_it(){
- return age;
- }
- private:
- int age=18;
- }
复制代码 利用这一利用,我们可以本身设置类对象属性的只读,只写,读写利用,代码如下:- #include<iostream>
- #include<string>
- using namespace std;
- class person {//创建一个person结构体
- public:
- void set_name(string name) {//定义一个函数来接收数据,然后将数据赋给私有权限中的数据
- m_name = name;
- //这个函数无返回值,故要返回字符串需要创建一个string函数
- }
- string get_name() {
- return m_name;
- }
- int get_age() {
- return m_age;//类内可以访问私有成员
- }
- void set_idol(string idol) {
- m_idol = idol;//没有返回值,只写
- }
- private:
- string m_name; //可读可写
- int m_age=18;//只读
- string m_idol;//只写
- };
- int main() {
- person p;
- //创建一个person中的p对象
- p.set_name("张三");
- cout << "姓名:" << p.get_name() << endl;
- cout << "年龄:" << p.get_age() << endl;
- p.set_idol("小明");
- system("pause");
- return 0;
- }
复制代码 4.类案例:长方体类
案例要求:
1)创创建方体类
2)计划属性
3)计划举动,获取立方面子积和体积
4)分别利用全局函数和成员函数判定两个立方体是否相当
我们仍旧利用上面的方法,将属性设置为私有,并通过成员函数来修改和获取属性的值
我们将代码实现分为三个部门,第一部门是设置属性,完成面积体积的盘算,第二部门是用全局函数判定两个立方体是否相当,第三部门是用成员函数判定,下面给出第一部门的代码:- #include<iostream>
- using namespace std;
- class cube {
- public:
- void set_l(int l) {
- m_l = l;
- }
- int get_l() {
- return m_l;
- }
- void set_w(int w) {
- m_w = w;
- }
- int get_w() {
- return m_w;
- }
- void set_h(int h) {
- m_h = h;
- }
- int get_h() {
- 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;
- }
- private:
- int m_l;
- int m_w;
- int m_h;
- };
- int main() {
- cube c1;
- c1.set_l(10);
- c1.set_w(20);
- c1.set_h(30);
- cout <<"c1的面积为"<< c1.calculateS() << endl;
- cout <<"c1的体积为"<< c1.calculateV() << endl;
- return 0;
- }
复制代码 第二部门代码为:- bool isSame1(cube& c1, cube& c2) {
- if (c1.get_l() == c2.get_l() && c1.get_w() == c2.get_w() && c1.get_h() == c2.get_h()) {
- return true;
- }
- return false;
- }
复制代码 第三部门代码为:- bool isSame2(cube& c) {
- //这里传递的参数一个就可以,因为我们是用已知的一个对象去调用未知的一个对象
- if (m_l == c.get_l() && m_w == c.get_w() && m_h == c.get_h()) {
- return true;
- }
- return false;
- }
复制代码 完备代码如下:- #include<iostream>
- using namespace std;
- class cube {
- public:
- void set_l(int l) {
- m_l = l;
- }
- int get_l() {
- return m_l;
- }
- void set_w(int w) {
- m_w = w;
- }
- int get_w() {
- return m_w;
- }
- void set_h(int h) {
- m_h = h;
- }
- int get_h() {
- 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 isSame2(cube& c) {
- //这里传递的参数一个就可以,因为我们是用已知的一个对象去调用未知的一个对象
- if (m_l == c.get_l() && m_w == c.get_w() && m_h == c.get_h()) {
- return true;
- }
- return false;
- }
- private:
- int m_l;
- int m_w;
- int m_h;
- };
- bool isSame(cube& c1, cube& c2) {
- if (c1.get_l() == c2.get_l() && c1.get_w() == c2.get_w() && c1.get_h() == c2.get_h()) {
- return true;
- }
- return false;
- }
- int main() {
- cube c1;
- c1.set_l(10);
- c1.set_w(20);
- c1.set_h(30);
- cout <<"c1的面积为"<< c1.calculateS() << endl;
- cout <<"c1的体积为"<< c1.calculateV() << endl;
- cube c2;
- c2.set_l(10);
- c2.set_w(10);
- c2.set_h(30);
- cout << "c2的面积为" << c2.calculateS() << endl;
- cout << "c2的体积为" << c2.calculateV() << endl;
- bool ret1 = isSame(c1,c2);//引用传递
- if (ret1) {
- cout << "全局函数:两个立方体相等" << endl;
- }
- else {
- cout << "全局函数;两个立方体不相等" << endl;
- }
- bool ret2 = c1.isSame2(c2);
- //这里如果是用c1作为对象,那么传入c2即可,如果是c2作为对象,那么传入c1
- if (ret2) {
- cout << "成员函数:两个立方体相等" << endl;
- }
- else {
- cout << "成员函数;两个立方体不相等" << endl;
- }
- return 0;
- }
复制代码 |