类别
- 类别为一种由使用者自行设计的资料型别
- Data Member (属性):储存资料
- Member Function (行为):操纵data member
- 物件:类别变数
- 类别不佔记忆体,但宣告后,作业系统会找一块记忆体存放物件,故佔记忆体。
函式导向
处理资料的方式为将资料丢进Function中处理
const int SIZE = 10;
enum Status { EMPTY, OCCUPIED };
struct Set {
int element[SIZE];
Status status[SIZE];
int element_no;
};
void set_initialization(Set& foo){
for (int i = 0; i < SIZE; i++){
foo.status[i] = EMPTY;
foo.element_no = 0;
}
}
bool insert_element(Set& s, int data){
if (s.element_no == SIZE)
return false;
for (int i = 0; i < SIZE; i++){
if (s.status[i] == EMPTY){
s.element[i] = data;
s.status[i] = OCCUPIED;
s.element_no++;
return true;
}
}
}
int delete_element(Set& s, int data){
int count = 0;
for (int i = 0; i < SIZE; i++){
if (s.status[i] == OCCUPIED && s.element[i] == data){
s.status[i] = EMPTY;
s.element_no--;
count++;
}
}
return count;
}
void print_set(const Set& foo){
for (int i = 0; i < SIZE; i++){
if (foo.status[i] == OCCUPIED)
cout << foo.element[i] << \' \';}
cout << endl;
}
int main()
{
Set mySet;
set_initialization(mySet);
print_set(mySet);
insert_element(mySet, 5);
insert_element(mySet, 7);
insert_element(mySet, 8);
print_set(mySet);
delete_element(mySet, 4);
print_set(mySet);
delete_element(mySet, 5);
print_set(mySet);
return 0;
}
优化
将函式原型提出
const int SIZE = 10;
enum Status { EMPTY, OCCUPIED };
struct Set {
int element[SIZE];
Status status[SIZE];
int element_no;
};
//函示原型
void set_initialization(Set&);
bool insert_element(Set&, int);
int delete_element(Set&, int);
void print_set(const Set&);
int main()
{
...
}
void set_initialization(Set& foo){
...
}
bool insert_element(Set& s, int data){
...
}
int delete_element(Set& s, int data){
...
}
void print_set(const Set& foo){
...
}
物件导向 与 函式导向思维上的不同
- 函式导向:将记忆体传至函示处理,传来传去。
- 物件导向:站在此记忆体(物件)上,由身边的函示提供处理方法,对比函式导向少了记忆体传递动作。精神:每次修改物件均要透过已设计好的函式,减少错误。
物件导向的封装概念
- 物件操作具权限的保护
- 物件导向三大重点:封装、继承、多型
函式导向 -> 物件导向
- 函式从一般函式 变为 成员函式(member function)
- 成员函式定义在外要加上范围运算子(::)
class Set
{
private:
int element[SIZE];
Status status[SIZE];
int element_no;
public:
void set_initialization();
bool insert_element(int);
int delete_element(int);
void print_set() const; //const代表此函示不会改变data member
};
int main()
{
Set mySet;
mySet.set_initialization();
mySet.print_set();
mySet.insert_element(5);
mySet.insert_element(7);
mySet.insert_element(8);
mySet.print_set();
mySet.delete_element(4);
mySet.print_set();
mySet.delete_element(5);
mySet.print_set();
return 0;
}
void Set::set_initialization(){
for (int i = 0; i < SIZE; i++){
status[i] = EMPTY;
element_no = 0;
}
}
bool Set::insert_element(int data){
if (element_no == SIZE)
return false;
for (int i = 0; i < SIZE; i++){
if (status[i] == EMPTY){
element[i] = data;
status[i] = OCCUPIED;
element_no++;
return true;
}
}
}
int Set::delete_element(int data){
int count = 0;
for (int i = 0; i < SIZE; i++){
if (status[i] == OCCUPIED && element[i] == data){
status[i] = EMPTY;
element_no--;
count++;
}
}
return count;
}
void Set::print_set() const{
for (int i = 0; i < SIZE; i++){
if (status[i] == OCCUPIED)
cout << element[i] << \' \';
}
cout << endl;
}