C++基础知识之"类和对象"

fengjingtu

基本概念

类、对象、成员变量、成员函数

面向对象三大概念:封装、继承、多态

类的封装

封装

封装有两层含义:

面向对象程序设计最基本的特性。把数据(属性)和函数(操作)合成一个整体,这在计算机世界中是用类与对象实现的。

把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。

C++中类的封装:

成员变量,C++中用于表示类属性的变量

成员函数,C++中用于表示类行为的函数

访问控制

C++中 public,protected, private 访问标号小结,即访问标号使用限制。

private:只能由.该类中的函数和其友元函数访问。不能被任何其他访问,该类的对象也不能访问。
protected:可以被该类中的函数,子类的函数、以及其友元函数访问。但不能被该类的对象访问。
public:可以被该类中的函数、子类的函数、其友元函数访问,也可以由该类的对象访问。

注:友元函数包括3种:设为友元的普通的非成员函数;设为友元的其他类的成员函数;设为友元类中的所有成员函数。

类的继承后方法属性变

private 属性不能够被继承。
使用private继承,父类的protected和public属性在子类中变为private;
使用protected继承,父类的protected和public属性在子类中变为protected;
使用public继承,父类中的protected和public属性不发生改变;

public protected private
public继承 public protected 不可用
protected继承 protected protected 不可用
private继承 private private 不可用

protected继承和private继承能降低访问权限。

struct和class的区别

在用struct定义类时,所有成员的默认属性为public

在用class定义类时,所有成员的默认属性为private

操练

main.cpp:

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<iostream>
#include "MyCube.h"
#include "MyCircle.h"
#include "MyPoint.h"
#include "MyTree.h"
using namespace std;

//使用全局函数来判断,非面向对象思想
bool judgeCube(MyCube &C1, MyCube &C2)
{
if (C1.getA() == C2.getA() && C1.getB() == C2.getB() && C1.getC() == C2.getC())
return true;
else
return false;
}
//求四方体的面积,体积并判断是否相等
int main_cube()
{

MyCube c1, c2,c3;
c1.setABC(1, 2, 3);
c2.setABC(1, 2, 3);
c3.setABC(2, 3, 4);
cout << "c1.a=" << c1.getA() << " c1.b=" << c1.getB() << " c1.c=" << c1.getC() << " c1.s=" << c1.getS() << " c1.v=" << c1.getV() <<endl;
cout << "c2.a=" << c2.getA() << " c2.b=" << c2.getB() << " c2.c=" << c2.getC() << " c2.s=" << c2.getS() << " c2.v=" << c2.getV() << endl;
cout << "c3.a=" << c3.getA() << " c3.b=" << c3.getB() << " c3.c=" << c3.getC() << " c3.s=" << c3.getS() << " c3.v=" << c3.getV() << endl;

if (c1.judgeCube(c1, c2))
{
cout << "c1和c2相同" << endl;
}
else
{
cout << "c1和c2不相同" << endl;
}
if (c1.judgeCube(c1, c3))
{
cout << "c1和c3相同" << endl;
}
else
{
cout << "c1和c3不相同" << endl;
}
if (c1.judgeCube(c2))
{
cout << "c1和c2相同" << endl;
}
else
{
cout << "c1和c2不相同" << endl;
}
if (c1.judgeCube(c3))
{
cout << "c1和c3相同" << endl;
}
else
{
cout << "c1和c3不相同" << endl;
}
if (judgeCube(c1,c2))
{
cout << "c1和c2相同" << endl;
}
else
{
cout << "c1和c2不相同" << endl;
}
if (judgeCube(c1,c3))
{
cout << "c1和c3相同" << endl;
}
else
{
cout << "c1和c3不相同" << endl;
}
system("pause");
return 0;
}


//判断点和圆的关系,圆外,圆上,圆中
int main_circle_point()
{
MyCircle c ,c1,c2;
MyPoint p1, p2, p3;
c.setRXY(1, 0, 0);
c1.setRXY(2, 1, 1);
c2.setRXY(1, 5, 5);
p1.setXY(1, 0);
p2.setXY(1, 2);
p3.setXY(0, 0);
//判断圆和点的关系
if (c.judgePointConnection(p1) == 0)
{
cout << "p1在圆内" << endl;
}
else if (c.judgePointConnection(p1) == 1)
{
cout << "p1在圆上" << endl;
}
else
{
cout << "p1在圆外" << endl;
}

if (c.judgePointConnection(p2) == 0)
{
cout << "p2在圆内" << endl;
}
else if (c.judgePointConnection(p2) == 1)
{
cout << "p2在圆上" << endl;
}
else
{
cout << "p2在圆外" << endl;
}

if (c.judgePointConnection(p3) == 0)
{
cout << "p3在圆内" << endl;
}
else if (c.judgePointConnection(p3) == 1)
{
cout << "p3在圆上" << endl;
}
else
{
cout << "p3在圆外" << endl;
}

//判断圆和圆的关系
if (c.judgeCircle(c1))
cout << "c和c1相交"<<endl;
else
cout << "c和c1不相交" << endl;

if (c.judgeCircle(c2))
cout << "c和c2相交" << endl;
else
cout << "c和c2不相交" << endl;
system("pause");
return 0;
}

//测试树龄
int main_tree()
{
MyTree t;
t.setAges();

int year = 2;
cout << "当前树龄为:" << t.getAges() << ",";
t.grows(year);
cout << year <<"年后树龄为:" << t.getAges() << endl;

year = 3;
cout << "当前树龄为:" << t.getAges() << ",";
t.grows(year);
cout << year << "年后树龄为:" << t.getAges() << endl;

year = 5;
cout << "当前树龄为:" << t.getAges ()<< ",";
t.grows(year);
cout << year << "年后树龄为:" << t.getAges() << endl;


system("pause");
return 0;
}

MyCircle.cpp:

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

#include "MyCircle.h"

void MyCircle::setR(int r)
{
_r = r;
}
void MyCircle::setX(int x)
{
_x = x;
}
void MyCircle::setY(int y)
{
_y = y;
}
void MyCircle::setRXY(int r, int x, int y)
{
_r = r;
_x = x;
_y = y;
}
int MyCircle::getR()
{
return _r;
}
int MyCircle::getX()
{
return _x;
}
int MyCircle::getY()
{
return _y;
}
int MyCircle::judgePointConnection(MyPoint &myp)
{
int dd = (_x - myp.getX())*(_x - myp.getX()) + (_y - myp.getY())*(_y - myp.getY());
if (dd < _r*_r)
{
return 0;
}
else if (dd == _r*_r)
{
return 1;
}
else
{
return 2;
}
}
bool MyCircle::judgeCircle(MyCircle &myc)
{
int dd = (_x - myc.getX())*(_x - myc.getX()) + (_y - myc.getY())*(_y - myc.getY());
if (dd >= _r + myc.getR())
return false;
else
return true;
}

MyCube.cpp

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
#include "MyCube.h"

void MyCube::setA(int a1 = 0)
{
a = a1;
}
void MyCube::setB(int b1 = 0)
{
b = b1;
}
void MyCube::setC(int c1 = 0)
{
c = c1;
}
void MyCube::setABC(int a1 = 0, int b1 = 0, int c1 = 0)
{
a = a1;
b = b1;
c = c1;
}
int MyCube::getA()
{
return a;
}
int MyCube::getB()
{
return b;
}
int MyCube::getC()
{
return c;
}
int MyCube::getS()
{
return 2 * (a*b + b*c + a*c);
}
int MyCube::getV()
{
return a*b*c;
}
bool MyCube::judgeCube(MyCube &C1, MyCube &C2)//非面向对象思想
{
if (C1.getA()== C2.getA() && C1.getB() == C2.getB() && C1.getC() == C2.getC())
return true;
else
return false;
}
bool MyCube::judgeCube(MyCube &C2)//重载是否是相等的四方体函数
{
if (a == C2.getA() && b == C2.getB() && c == C2.getC())
return true;
else
return false;
}

MyPoint.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "MyPoint.h"

void MyPoint::setX(int x)
{
_x = x;
}
void MyPoint::setY(int y)
{
_y = y;
}
void MyPoint::setXY(int x, int y)
{
_x = x;
_y = y;
}
int MyPoint::getX()
{
return _x;
}
int MyPoint::getY()
{
return _y;
}

MyTree.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "MyTree.h"

void MyTree::setAges(int age)
{
ages = age;
}
int MyTree::getAges()
{
return ages;
}
void MyTree::grows(int years)
{
ages += years;
}

MyCircle.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#pragma once
#include "MyPoint.h"
class MyCircle
{
private:
int _r;
int _x;
int _y;
public:
void setR(int r);
void setX(int x);
void setY(int y);
void setRXY(int r,int x, int y);
int getR();
int getX();
int getY();
int judgePointConnection(MyPoint &myp);//判断与点的关系
bool judgeCircle(MyCircle &myc);//判断是否和圆相交
};

MyCube.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
class MyCube
{
private:
int a;
int b;
int c;
public:
void setA(int a1 );
void setB(int b1 );
void setC(int c1 );
void setABC(int a1 , int b1 , int c1 );
int getA();
int getB();
int getC();
int getS();
int getV();
bool judgeCube(MyCube &C1, MyCube &C2);//非面向对象思想
bool judgeCube(MyCube &C2);//重载是否是相等的四方体函数
};

MyPoint.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
class MyPoint
{
private:
int _x;
int _y;
public:
void setX(int x);
void setY(int y);
void setXY( int x, int y);
int getX();
int getY();
};

MyTree.h

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
//定义一个Tree类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示tree对象的ages的值。

class MyTree
{
private:
int ages;
public:
void setAges(int age = 0);
int getAges();
void grows(int years);
};