首页 > 其他分享 >类与对象 ——封装——类(class)

类与对象 ——封装——类(class)

时间:2022-08-18 11:11:09浏览次数:76  
标签:权限 return point 对象 getcenter int 封装 include class

类和对象

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

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

例如:

​ 人可以作为对象,属性有姓名、年龄、身高、体重...,行为有走、跑、跳、吃饭、唱歌...

​ 车也可以作为对象,属性有轮胎、方向盘、车灯...,行为有载人、放音乐、放空调...

​ 具有相同性质的==对象==,我们可以抽象称为==类==,人属于人类,车属于车类

4.1 封装

4.1.1 封装的意义

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

封装的意义:

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

封装意义一:

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

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

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

 1 #include <iostream>
 2 using namespace std;
 3 const double pi = 3.14;
 4 class yuan
 5 {
 6     //访问权限
 7     //公共权限
 8 public:
 9     //属性
10     //半径
11     int r;
12     //行为
13     //获取圆的周长
14     double mianji()
15     {
16         return 2 * pi * r;
17     }
18 };
19 int main()
20 {
21     yuan yuan1;
22     cout << "请输入圆的半径" << endl;
23     cin >>  yuan1.r ;
24     cout << "半径为" << yuan1.r << "的圆面积为 :" <<yuan1.mianji();
25     return 0;
26 }

 

封装意义二:

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

访问权限有三种:

  1. public 公共权限
  2. protected 保护权限
  3. private 私有权限
 1 //三种权限
 2 //公共权限  public     类内可以访问  类外可以访问
 3 //保护权限  protected  类内可以访问  类外不可以访问
 4 //私有权限  private    类内可以访问  类外不可以访问
 5 
 6 class Person
 7 {
 8     //姓名  公共权限
 9 public:
10     string m_Name;
11 
12     //汽车  保护权限
13 protected:
14     string m_Car;
15 
16     //银行卡密码  私有权限
17 private:
18     int m_Password;
19 
20 public:
21     void func()
22     {
23         m_Name = "张三";
24         m_Car = "拖拉机";
25         m_Password = 123456;
26     }
27 };
28 
29 int main() {
30 
31     Person p;
32     p.m_Name = "李四";
33     //p.m_Car = "奔驰";  //保护权限类外访问不到
34     //p.m_Password = 123; //私有权限类外访问不到
35 
36     system("pause");
37 
38     return 0;
39 }

 struct和class区别

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

区别:

  • struct 默认权限为公共
  • class 默认权限为私有
 1 class C1
 2 {
 3     int  m_A; //默认是私有权限
 4 };
 5 
 6 struct C2
 7 {
 8     int m_A;  //默认是公共权限
 9 };
10 
11 int main() {
12 
13     C1 c1;
14     c1.m_A = 10; //错误,访问权限是私有
15 
16     C2 c2;
17     c2.m_A = 10; //正确,访问权限是公共
18 
19     system("pause");
20 
21     return 0;
22 }

成员属性设置为私有

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

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

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 class student
 5 {
 6 private:
 7     string name;
 8     int age;
 9     string lover;
10 public:
11     void sname(string tname)//可写
12     {
13         name = tname;
14     }
15     string iname()//可读(被使用者读到)
16     {
17         return name;
18     }
19     int ag(int a)//可写又可读(被使用者读到)
20     {
21         age = a;
22         if (age < 0 || age > 150)
23         {
24             cout << "请输入0~150之间的数值" << endl;
25             return 0;
26         }
27         return age;
28     }
29     void slover(string slove)//可写(只可以写入)
30     {
31         lover = slove;
32     }
33 };
34 int main()
35 {
36     student student1;
37     student1.sname("二狗");
38     cout << student1.iname() << endl;
39     cout << student1.ag(18) << endl;
40 }

 

练习案例1:设计立方体类

设计立方体类(Cube)

求出立方体的面积和体积

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

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 class Cube
 5 {
 6 private :
 7     int m_l;
 8     int m_w;
 9     int m_h;
10 public :
11     void volumn(int l, int w, int h)
12     {
13         m_l = l ;
14         m_w = w;
15         m_h = h;
16     }
17     int ivolumn()
18     {
19         return  m_l * m_w * m_h;
20     }
21     void area(int l, int w, int h)
22     {
23         m_l = l;
24         m_w = w;
25         m_h = h;
26     }
27     int iarea()
28     {
29         return  2 * (m_l * m_w + m_l * m_h + m_w * m_h);
30     }
31     string compare1(Cube& c)//成员函数中的判断
32     {
33         if ((m_l * m_w * m_h) == c.ivolumn() && (2 * (m_l * m_w + m_l * m_h + m_w * m_h) )== c.iarea())
34         {
35             return "两个多边形完全相同";
36         }
37         return "两个多边形不完全相同";
38     }
39 };
40 // 全局函数中的判断
41 string compare(Cube &cube1, Cube &cube2)
42 {
43     if (cube1.ivolumn() == cube2.ivolumn() && cube1.iarea() == cube2.iarea())
44     {
45         return "两个多边形完全相同";
46     }
47     return "两个多边形不完全相同";
48 }
49 int main()
50 {
51    Cube cube1;
52    cube1.volumn(4, 3, 5);
53    cube1.area(4, 3, 5);
54    cout << "cube1's volumn :" << cube1.ivolumn() << endl;
55    cout << "cube1's area :"<<cube1.iarea() << endl;
56    Cube cube2;
57    cube2.volumn(4, 3, 5);
58    cube2.area(4, 3, 5);
59    cout << "cube2's volumn :" << cube2.ivolumn() << endl;
60    cout << "cube2's area :" << cube2.iarea() << endl;
61    cout << "全局函数中的判断:" << compare(cube1, cube2) << endl;
62    cout << "成员函数中的判断:" << cube1.compare1(cube2) << endl;
63    return 0;
64 }

 

练习案例2:点和圆的关系

设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。

 

 函数主体

 1 #include <iostream>
 2 #include "point.h";
 3 #include "circle.h"
 4 #include "isincircle.h"
 5 #include "ssincircle.h"
 6 using namespace std;
 7 // 点类
 8 //class point
 9 //{
10 //private:
11 //    int m_x;
12 //    int m_y;
13 //public:
14 //    void setx(int x)
15 //    {
16 //        m_x = x;
17 //    }
18 //    int getx()
19 //    {
20 //        return m_x;
21 //    }
22 //    void sety(int y)
23 //    {
24 //        m_y = y;
25 //    }
26 //    int gety()
27 //    {
28 //        return m_y;
29 //    }
30 //};
31 //圆类
32 //class Circle
33 //{
34 //private:
35 //    int m_R;
36 //    point m_Center;
37 //public:
38 //    void setR(int r)
39 //    {
40 //        m_R = r;
41 //    }
42 //    int getR()
43 //    {
44 //        return m_R;
45 //    }
46 //    void setcenter(point center)
47 //    {
48 //        m_Center = center;
49 //    }
50 //    // 让另一个类作为本类中的成员
51 //    point getcenter()
52 //    {
53 //        return m_Center;
54 //    }
55 //};
56 //获取和判断圆心到点距离函数
57 //string isincircle(Circle& c, point& p)
58 //{
59 //    if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2 )< c.getR())
60 //    {
61 //        return "点在圆内";
62 //    }
63 //    if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2) == c.getR())
64 //    {
65 //        return "点在圆上";
66 //    }
67 //    return "点在圆外";
68 //}
69 //int ssincircle(Circle& c, point& p)
70 //{
71 //    return((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2);
72 //}
73 int main()
74 {
75     Circle c;
76     point p;
77     c.getcenter().setx(5);
78     c.getcenter().sety(5);
79     c.setR(3);
80     p.setx(2);
81     p.sety(2);
82     cout << "圆的半径为 :" << c.getR() << endl;
83     cout << "圆心到点的距离为 :" << ssincircle(c, p) <<  endl;
84     cout << "所以点和圆的关系为 :" << isincircle(c, p);
85 }

点类分文件编写

point.h

 1 #pragma once //防止头文件重复包含
 2 #include <iostream>
 3 using namespace std;
 4 class point
 5 {
 6 private:
 7     int m_x;
 8     int m_y;
 9 public:
10     void setx(int x);
11 
12     int getx();
13 
14     void sety(int y);
15 
16     int gety();
17 }; 

point.cpp

 1 #include "point.h"
 2 void point::setx(int x)
 3 {
 4     m_x = x;
 5 }
 6 int point::getx()
 7 {
 8     return m_x;
 9 }
10 void point::sety(int y)
11 {
12     m_y = y;
13 }
14 int point::gety()
15 {
16     return m_y;
17 }

圆类分文件编写 

circle.h

 1 #pragma once
 2 #include <iostream>
 3 #include  "point.h"
 4 using namespace std;
 5 class Circle
 6 {
 7 private:
 8     int m_R;
 9     point m_Center;
10 public:
11     void setR(int r);
12     
13     int getR();
14     
15     void setcenter(point center);
16     
17     // 让另一个类作为本类中的成员
18     point getcenter();
19     
20 };

circle.cpp

 1 #include "circle.h"
 2 void Circle :: setR(int r)
 3 {
 4     m_R = r;
 5 }
 6 int Circle :: getR()
 7 {
 8     return m_R;
 9 }
10 void Circle :: setcenter(point center)
11 {
12     m_Center = center;
13 }
14 // 让另一个类作为本类中的成员
15 point Circle :: getcenter()
16 {
17     return m_Center;
18 }

获取和判断圆心到点距离函数

isincircle.h

1 #pragma once
2 #include <iostream>
3 #include "point.h";
4 #include "circle.h"
5 using namespace std;
6 string isincircle(Circle& c, point& p);

isincircle.cpp

 1 #include "isincircle.h"
 2 string isincircle(Circle& c, point& p)
 3 {
 4     if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2) < c.getR())
 5     {
 6         return "点在圆内";
 7     }
 8     if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2) == c.getR())
 9     {
10         return "点在圆上";
11     }
12     return "点在圆外";
13 }

ssincircle.h

1 #pragma once
2 #include <iostream>
3 #include "point.h";
4 #include "circle.h"
5 using namespace std;
6 int ssincircle(Circle& c, point& p);

ssincircle.cpp

1 include "ssincircle.h"
2 int ssincircle(Circle& c, point& p)
3 {
4     return((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2);
5 }

 

标签:权限,return,point,对象,getcenter,int,封装,include,class
From: https://www.cnblogs.com/zaiyewujiang/p/16567027.html

相关文章

  • getClassLoader()
    一、ClassLoader 的作用我们都知道java程序写好以后是以.java(文本文件)的文件存在磁盘上,然后,我们通过(bin/javac.exe)编译命令把.java文件编译成.class文件(字节码文件),并存......
  • 面向对象思想的概述和面向对象思想的举例
    面向对象思想的概述 面向过程:当需要实现一个功能的时候,每一个具体的步骤都要亲力亲为,详细处理每一个细节面向对象:当需要实现一个功能的时候,不关心具体的步骤,而是找一个......
  • vue中改变数组对象属性名名称
    letnape=[];for(leti=0;i<list.length;i++){letres=JSON.parse(JSON.stringify(list[i])......
  • 在实例化对象的时候new关键字具体做了哪些操作?
    a创建了一个空对象{}b通过原型链把空对象和构造函数连接起来__proto__=prototypec构造函数的this指向新对象,并执行函数体d判断构造函数的返回值,返回对象就使用该......
  • js快捷抽取数组对象中某一属性值的集合
    一、Array.from方法array.from方法就是将一个类数组对象(具有length属性的对象)或者可遍历的对象转换成真正的数组varuser=[{id:1,name:"李四......
  • Effective C++ - 条款2 - in-class初值设定问题
    pre针对EffectiveC++(55条)中的每一个条款写一个blog。0x02尽量以const,enum,inline替换#define为什么需要这样做?因为使用define会使得变量被define的符号替换,在......
  • spring5 入门第一课,创建对象 01
    1.项目结构 2.步骤2.1创建Userpackagecom.cj.spring5;publicclassUser{publicvoidadd(){System.out.println("add...");}}2.2创建......
  • 面向对象——封装
    封装该露的露,该藏的藏我们程序设计要追求”高内聚,低耦合“。高内聚就是类的内部数据细节由自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。封装(数据的隐......
  • 面向对象回顾及原型讲解
    面向对象回顾      核心概念:万物皆对象(顶层对象Object)抽取行为作为方法抽取名词作为属性    俩种构建对象的方式      构造函数构建......
  • 【StoneDB Class】入门第三课:StoneDB的编译安装
    本课程主要介绍StoneDB-5.6在Ubuntu20.04LTS下的手动编译,在CentOS和RedHat的编译详见官方文档。如果想快速部署,详见官方文档https://stonedb.io/zh/docs/getti......