类与对象之review


​ 面向对象编程(OOP)是一种特殊的,设计程序的概念性方法,C++通过一些特性改进了C语言,使得应用这种方法更容易。下面是最重要的OOP特性:

  • 抽象
  • 封装和数据隐藏
  • 多态
  • 继承
  • 代码的可重用性

抽象和类

​ 生活中充满复杂性,处理复杂性的方法之一是简化和抽象。人的身体是由无数个原子组成的,而一些学者认为人的思想是由半自主的主体组成的。但将人自己看作一个实体将简单的多。在计算中,为了根据信息与用户之间的接口来表示它,抽象是最重要的。抽象是通往用户的定义类型的捷径,在C++中,用户定义类型指的是实现抽象类接口的类设计。

​ 类是一种将抽象转换为用户定义类型的C++工具,它将数据表示和操纵数据的方法组合成一个简洁的包。下面来看一个表示股票的类。可以对其进行简化。

可执行的操作

  • 获得股票
  • 增持
  • 减持
  • 更新股票价格
  • 显示关于股票信息

需存储的信息

  • 公司名称
  • 持股数量
  • 股价
  • 股值

定义类

  1. 类声明:以数据成员的方式描述数据部分,以成员函数的方式描述公有接口
  2. 类方法定义:描述如何实现类成员函数。

实例

#include<iostream>
#include<string>
using namespace std;
class stock
{
private:
string company;
long shares;
double share_val;
double total_val;
void set_tot()
{
total_val = shares * share_val;
}
public:
void acquire(const string& co, long n, double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();



};
#include "stock.h"

void stock::acquire(const string& co, long n, double pr)
{
company = co;
if (n < 0)
{
cout << "Numbers of shares can't be negative!" << company << " shares set to 0.\n";
shares = 0;

}
else
{
shares = n;
}
share_val = pr;
set_tot();
}

void stock::buy(long num, double price)
{
if (num < 0)
{
cout << "Number of shares purchased can't be nagative. " << "Transation is absorbed.\n";
}
else {
shares += num;
share_val = price;
set_tot();
}
}

void stock::sell(long num, double price)
{
if (num < 0)
{
cout << "You can't sell more than you have!" << "Transaction is adsorbed.\n";
}
else if (num > shares)
{
cout << "You can't sell morer than you have!" << "Transaction is absorbed.\n";
}
else {
shares -= num;
share_val = price;
set_tot();
}
}

void stock::update(double price)
{
share_val = price;
set_tot();
}

void stock::show()
{
cout << "Company name: " << company << endl;
cout << "Number of shares: " << shares << endl;
cout << "Share price: " << share_val << endl;
cout << "Total value: " << total_val << endl;
}
#include "stock.h"
int main()
{
stock fluffy_the_cat;
fluffy_the_cat.acquire("NanoSmart", 20, 12.50);
fluffy_the_cat.show();
fluffy_the_cat.buy(15, 18.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(5, 19.50);
fluffy_the_cat.show();
return 0;
}
Company name: NanoSmart
Number of shares: 20
Share price: 12.5
Total value: 250
Company name: NanoSmart
Number of shares: 35
Share price: 18.125
Total value: 634.375
Company name: NanoSmart
Number of shares: 30
Share price: 19.5
Total value: 585

抽象数据类型

​ stock类非常具体。然而,程序员常常通过定义类来表示更通用的概念。例如,就实现计算机专家们所说的抽象数据类型(abstract data type,ADT)而言,使用类是一种非常好的方式。顾名思义,ADT以通用的方式描述数据类型,而没有引入语言或实现细节。例如,通过栈,可以以这样的方式存储数据,即总是从栈添加或删除数据。例如,C++程序通过栈来管理自动变量。当新的自动变量被生成后,它们被添加到栈顶;消亡时,从栈删除它们。

​ 下面简要地介绍一下栈的特征:

  • 可创建空栈
  • 可将数据项添加到栈顶(压入)
  • 看从栈顶删除数据项(弹出)
  • 可查看栈是否填满
  • 可查看栈是否为空

实例

#include<iostream>
#include<string>
using namespace std;
typedef unsigned long Item;
class stack
{
private:
enum { MAX = 10 }; // 定义栈的最大容量
Item items[MAX]; // 存储栈元素的数组
int top; // 栈顶指针
public:
stack(); // 构造函数,初始化栈
bool isenputy(); // 判断栈是否为空
bool isfull(); // 判断栈是否已满
bool push(const Item& item); // 入栈操作
bool pop(Item& item); // 出栈操作

};
#include "stack.h"

stack::stack()
{
top = 0;
}

bool stack::isenputy()
{
return top==0;
}

bool stack::isfull()
{
return top==MAX;
}

bool stack::push(const Item& item)
{
if (top<MAX)
{
items[top++] = item;
return true;
}
else {
return false;
}

}

bool stack::pop(Item& item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
#include "stack.h"
#include<cctype>
// 主函数,程序入口
int main()
{
stack st;
char ch;
unsigned long po;
// 提示用户输入操作选项
cout << "Please enter A to add apurchase order,\n" << "P to process a PO.or Q to quit.\n";
while (cin >> ch && toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
// 检查输入是否为字母
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
// 添加采购订单
case 'A':
case 'a':cout << "Enter a PO number to add:";
cin >> po;
if (st.isfull())
{
cout << "stack already full\n";
}
else
st.push(po);
break;
// 处理采购订单
case 'P':
case 'p':
if (st.isenputy())
cout << "stack already empty\n";
else {
st.pop(po);
cout << "PO #" << po << " popped\n";

}
break;
default:
break;
}

// 重复提示用户输入操作选项
cout << "Please enter A to add a purchase order,\n" << "P to process a PO ,or Q to quit.\n";



}
// 程序结束提示
cout << "Bye.\n";
return 0;
}
Please enter A to add apurchase order,
P to process a PO.or Q to quit.
A
Enter a PO number to add:17885
Please enter A to add a purchase order,
P to process a PO ,or Q to quit.
P
PO #17885 popped
Please enter A to add a purchase order,
P to process a PO ,or Q to quit.
A
Enter a PO number to add:17965
Please enter A to add a purchase order,
P to process a PO ,or Q to quit.
A
Enter a PO number to add:18002
Please enter A to add a purchase order,
P to process a PO ,or Q to quit.
P
PO #18002 popped
Please enter A to add a purchase order,
P to process a PO ,or Q to quit.
P
PO #17965 popped
Please enter A to add a purchase order,
P to process a PO ,or Q to quit.
P
stack already empty
Please enter A to add a purchase order,
P to process a PO ,or Q to quit.
Q
Bye.

总结

​ 面向对象编程强调的是程序如何表示数据。使用OPP方法解决编程问题的第一步是根据他与程序之间的接口来描述数据,从而指定如何使用数据。然后,设计一个类来实现该接口。一般来说私有数据成员存储信息,公有成员函数(方法)提供访问数据的唯一途径。类将数据和方法组合成一个单元,其私有性实现数据隐藏。


Author: T1g3r
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source T1g3r !
评论
  TOC