1.OOP的重要特性:
●抽象:
将问题的本质特征抽象出来,并根据特征来描述解决方案,抽象是通往用户类型的 捷径。
●封装和数据隐藏
封装:公有接口表示设计的抽象组件,将实现细节放在一起并将它们与抽象分开被 成为封装。数据隐藏也是一种封装;将实现的细节隐藏在私有部分中,也是一种封 装;将类函数声明和定义放在不同的文件中,也是一种封装。
数据隐藏:防止程序直接访问数据成为数据隐藏。
●多态
●继承
●代码的可重用性
2.●类声明:以数据成员的方式描述数据部分,以成员函数的方式描述公有接口。
类声明实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17class Stock { private: std::string company; long shares; double price; double total; void setTotal() { total = shares * price; } public: void acquire(const std::string & comp, long shar, double pri); void buy(long shar, double pri); void sell(long shar, double pri); void show(); };
类定义实例:
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#include <iostream> #include "stock00.h" using namespace std; void Stock::acquire(const string & comp, long shar, double pri) { company = comp; if(shar < 0) cout << "Can't buy a nagitive number shares.n"; else { shares = shar; price = pri; setTotal(); } } void Stock::buy(long shar, double pri) { if(shar < 0) cout << "Can't buy a nagitive number share.n"; else { shares += shar; price = pri; setTotal(); } } void Stock::sell(long shar, double pri) { if(shares < shar) cout << "Can't sell a larger number than shares.n"; else if(shar < 0) cout << "Can't sell a nagitive number shares.n"; else { shares -= shar; price = pri; setTotal(); } } void Stock::show() { cout << "Company : " << company << endl; cout << "Shares : " << shares << endl; cout << "Price : " << price << endl; setTotal(); cout << "Total : " << total << endl; cout << endl; }
4.通常c++程序员将类的声明放在头文件中,如上例中,类的声明放在stock00.h这个头文 件中;而类的实现放在源代码文件中,如上例中,类的实现放在stock00.cpp这个源文件中。类设计应尽可能的将公有接口与其实现细节分开。
5.访问控制:类中有private和public两个部分,使用类对象的程序都可以使用类的public部分,但只能通过类的public中的公有成员函数才能访问private中的数据和函数。
6.类成员不管是数据成员还是函数成员,都可以在类的私有部分或者公有部分中声明。但是由于隐藏数据是OOP的主要目标之一,所以我们通常将数据成员放在private部分中,将函数成员放在public部分中。
7.private是类对象的默认访问控制,如下例:
1
2
3
4
5
6
7
8class World { private: float mass; char name[30]; public: void tellall(); };
1
2
3
4
5
6
7class World { float mass; char name[30]; public: void tellall(); };
8.类和结构:实际上,c++对结构进行了拓展,使之具有与类相同的特性。它们唯一的区别是,结构的默认访问权限是public,而类的默认访问权限是private。c++程序员通常使用类来实现类描述,而把结构限制为只表示纯粹的数据对象。
9.定义位于类声明中的函数都将自动成为内联函数。
1
2
3
4
5
6
7
8
9
10
11
12class Stock { private: std::string company; double total; void setTotal() { total = shares * price; } public: void show(); };
一般将内联函数的定义放在定义类的头文件中。
10.类所创建的每个新对象都有自己的存储空间,用于存储其内部变量和类成员;但同一个类的所有对象共享同一组类方法,即每个方法只有一个副本。例如,假设kate和joe都是Stock对象,则kate.shares占用一个内存块,joe.shares占用另外一个内存块,但kate.show()和joe.show()调用同一个方法,也就是说,它们执行同一个数据块。
1
2
3
4
5
6
7
8
9
10
11
12class Stock { private: std::string company; long shares; void setTotal() { total = shares * price; } public: void show(); };
11.想要设置函数为const,方法如下:
1
2
3
4
5
6
7void show() const; //函数声明 ... void show() const //函数定义 { ... }
12.类的使用实例:
以下在头文件stock00.h中定义了一个Stock类,在stock00.cpp中实现了Stock类中的公有函数,在main.cpp中使用了我们所定义的类:
文件结构:
stock00.h代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#ifndef STOCK00_H_INCLUDED #define STOCK00_H_INCLUDED #include <string> class Stock { private: std::string company; long shares; double price; double total; void setTotal() { total = shares * price; } public: void acquire(const std::string & comp, long shar, double pri); void buy(long shar, double pri); void sell(long shar, double pri); void show(); }; #endif // STOCK00_H_INCLUDED
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#include <iostream> #include "stock00.h" using namespace std; void Stock::acquire(const string & comp, long shar, double pri) { company = comp; if(shar < 0) cout << "Can't buy a nagitive number shares.n"; else { shares = shar; price = pri; setTotal(); } } void Stock::buy(long shar, double pri) { if(shar < 0) cout << "Can't buy a nagitive number share.n"; else { shares += shar; price = pri; setTotal(); } } void Stock::sell(long shar, double pri) { if(shares < shar) cout << "Can't sell a larger number than shares.n"; else if(shar < 0) cout << "Can't sell a nagitive number shares.n"; else { shares -= shar; price = pri; setTotal(); } } void Stock::show() { cout << "Company : " << company << endl; cout << "Shares : " << shares << endl; cout << "Price : " << price << endl; setTotal(); cout << "Total : " << total << endl; cout << endl; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <iostream> #include "stock00.h" using namespace std; int main() { Stock fluffy; fluffy.acquire("Coo.zhang", 500, 12.5); fluffy.show(); fluffy.buy(1000,13); fluffy.show(); fluffy.sell(1200,12.5); fluffy.show(); return 0; }
运行结果:
最后
以上就是细腻羊最近收集整理的关于c++类的基本概念的全部内容,更多相关c++类内容请搜索靠谱客的其他文章。
发表评论 取消回复