服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++核心编程之类和对象详解

C++核心编程之类和对象详解

2021-12-29 14:02Gigi Princess C/C++

这篇文章主要介绍了C++编程进阶之类和对象用法等相关使用技巧,需要的朋友可以参考下,希望能够给你带来帮助

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

1.封装

1.1封装的意义

  • 将属性和行为作为一个整体
  • 将属性和行为加以权限控制

封装意义一:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//实例1
#define _CRT_SECURE_NO_WARNINGS 1
//求圆的周长
#include<iostream>
using namespace std;
#define pi 3.14
class circle
{
public:
    int m_d;//直径
    double calculate()
    {
        return pi*m_d;
    }
};
int main()
{
    circle c1;
    c1.m_d = 10;
    cout << "圆周长为" << c1.calculate() << endl;
    system("pause");
    return 0;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//实例2
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include <string>
class Student
{
public:
    string m_name;
    int m_num;
    void showStudent()
    {
        cout << " 姓名:" << "\t" << m_name << " 学号:" << m_num << endl;
    }
    void setname(string name)
    {
        m_name = name;
    }
    void setnum(int num)
    {
        m_num = num;
    }
};
int main()
{
    Student s1;
    s1.setname("zhangsan");
    s1.setnum(2001);
    s1.showStudent();
    Student s2;
    s2.setname("lisi");
    s2.setnum(2002);
    s2.showStudent();
    system("pause");
    return 0;
}

总结:类中的 属性行为 统一称为 成员

类中的 属性 又称为 成员属性 成员变量

类中的 行为 又称为 成员函数 成员方法

封装意义二

访问权限有三种

公共权限: public 成员类内可以访问,类外可以访问

保护权限: protected 成员类内可以访问,类外不可以访问 儿子可以访问父亲中的保护内容

私有权限: private 成员类内可以访问,类外不可以访问 儿子不可以访问父亲中的保护内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include <string>
class Student
{
public:
    string m_name;
protected:
    int m_num;
private:
    int m_age;
public:
    void func()
    {
        m_name = "lili";
        m_num = 1;
        m_age = 30;
    }
private:
    void func1()
    {
        m_name = "lili";
        m_num = 1;
        m_age = 30;
    }
};
int main()
{
    Student s1;
    s1.m_name = "lilllll";
    //s1.m_num = 2;//不可访问
    //s1.m_age = 20;//不可访问
    s1.func();
    //s1.func1();//不可访问
    system("pause");
    return 0;
}

1.2 struct 和 class 区别

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

  • struct 默认权限为公共
  • class 默认权限为私有
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class C1
{
    string m_a;
};
struct C2
{
    string m_b;
};
int main()
{
    //C1 c1;
    //c1.m_a = "lili";//不可访问
    C2 c2;
    c2.m_b = "lili";
    system("pause");
    return 0;
}

1.3 成员属性设置为私有

优点:

  • 将所有成员属性设置为私有,可以自己控制读写权限
  • 对于写权限,我们可以检测数据的有效性
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include <string>
using namespace std;
class C1
{
public:
    void setname(string name)
    {
        m_name = name;
    }
    string getname()
    {
        return m_name;
    }
    int getnum()
    {
        return m_num;
    }
    void setage(int age)
    {
        m_age = age;
    }
private:
    string m_name;
    int m_num;
    int m_age;
};
int main()
{
    C1 c;
    c.setname("yangyang");//可写
    cout << c.getname() << endl;//可读
    cout << c.getnum() << endl;//只读
    c.setage(30);//只写
    system("pause");
    return 0;
}

好的,到这里封装就算是学到手了,可以进行两个小案例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include <string>
using namespace std;
class Cube
{
public:
    void setchang(int chang)
    {
        m_c = chang;
    }
    void setkuan(int kuan)
    {
        m_k = kuan;
    }
    void setgao(int gao)
    {
        m_g = gao;
    }
    int getchang()
    {
        return m_c;
    }
    int getkuan()
    {
        return m_k;
    }
    int getgao()
    {
        return m_g;
    }
    int S()
    {
        return 2 * (m_c*m_g + m_c*m_k + m_g*m_k);
    }
    int V()
    {
        return m_c*m_g*m_k;
    }
    bool isSamebyclass(Cube &c)
    {
        if (m_c == c.getchang() && m_g == c.getgao() && m_k == c.getkuan())
        {
            return true;
        }
        return false;
    }
private:
    int m_c;
    int m_k;
    int m_g;
};
bool isSame1(Cube &c1, Cube &c2)
{
    if (c1.getchang() == c2.getchang() && c1.getgao() == c2.getgao() && c1.getkuan() == c2.getkuan())
    {
        return true;
    }
    return false;
}
int main()
{
    Cube c1;
    c1.setchang(10);
    c1.setkuan(10);
    c1.setgao(10);
    cout << "面积:" << c1.S() << endl;
    cout << "体积:" << c1.V() << endl;
    Cube c2;
    c2.setchang(11);
    c2.setkuan(11);
    c2.setgao(11);
    cout << "面积:" << c2.S() << endl;
    cout << "体积:" << c2.V() << endl;
    bool ret = isSame1(c1, c2);
    if (ret)
    {
        cout << "全局函数判断:两个立方体相等" << endl;
    }
    else
    {
        cout << "全局函数判断:两个立方体不相等" << endl;
    }
    ret = c1.isSamebyclass(c2);
    if (ret)
    {
        cout << "成员函数判断:两个立方体相等" << endl;
    }
    else
    {
        cout << "成员函数判断:两个立方体不相等" << endl;
    }
    system("pause");
    return 0;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include <string>
using namespace std;
class Point
{
public:
    void setX(int x)
    {
        m_x = x;
    }
    int getX()
    {
        return m_x;
    }
    void setY(int y)
    {
        m_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 C_and_P(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 doubleR = c.getR()*c.getR();
    if (distance==doubleR)
    {
        cout << "点在圆上" << endl;
    }
    else if (distance<doubleR)
    {
        cout << "点在圆内" << endl;
    }
    else
    {
        cout << "点在圆外" << endl;
    }
}
int main()
{
    Point p;
    p.setX(10);
    p.setY(30);
    Circle c;
    c.setR(10);
    Point center;
    center.setX(26);
    center.setY(43);
    C_and_P(c, p);
    system("pause");
    return 0;
}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/GiGi_Princess/article/details/120057308

延伸 · 阅读

精彩推荐