1.this 指针
在简单的程序中,类成员函数通常涉及一个对象,但是复杂时有可能涉及两个对象,这种情况就要用到指针。
多说无益,look!
const Stock1& topval(const Stock& s)const;
const Stock& Stock::topval(const Stock& s)const
{
if (s.total_val > total_val)
return s;
else
return *this;
}
total_val是用来调用该方法的总值,如果if条件成立的话,函数返回指向s的引用,否则将返回用来调用该方法的对象,问题在于如何称呼这个对象,前面的stock并没有别名,这时候我们就要用到this指针了。
topval()里的total_val只不过是this->total_val的简写
看看一种比较完整的代码,此代码缺少主函数
#include <iostream>
#include <string>
using namespace std;
class Stock
{
private:
std::string company;
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock(); // default constructor
Stock(const std::string& co, long n = 0, double pr = 0.0);
~Stock(); // do-nothing destructor
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show()const;
const Stock& topval(const Stock& s) const;
};
// constructors
Stock::Stock() // default constructor
{
company = "no name";
shares = 0;
share_val = 0.0;
total_val = 0.0;
}
Stock::Stock(const std::string& co, long n, double pr)
{
company = co;
if (n < 0)
{
std::cout << "Number of shares can’t be negative; "
<< company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
// class destructor
Stock::~Stock() // quiet class destructor
{
}
// other methods
void Stock::buy(long num, double price)
{
if (num < 0)
{
std::cout << "Number of shares purchased can’t be negative. "
<< "Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
using std::cout;
if (num < 0)
{
cout << "Number of shares sold can’t be negative. "
<< "Transaction is aborted.\n";
}
else if (num > shares)
{
cout << "You can’t sell more than you have! "
<< "Transaction is aborted.\n";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show() const
{
using std::cout;
using std::ios_base;
// set format to #.###
ios_base::fmtflags orig =
cout.setf(ios_base::fixed, ios_base::floatfield);
std::streamsize prec = cout.precision(3);
cout << "Company: " << company
<< " Shares: " << shares << "\n";
cout << " Share Price: $" << share_val;
// set format to #.##
cout.precision(2);
cout << " Total Worth: $" << total_val << "\n";
// restore original format
cout.setf(orig, ios_base::floatfield);
cout.precision(prec);
}
const Stock& Stock::topval(const Stock& s) const
{
if (s.total_val > total_val)
return s;
else
return *this;
}
这串代码包括了析构函数,构造函数,和this指针的应用,笔者可以根据自己思路加上主函数构成一个完整的代码。
2.对象数组
#include <iostream>
const int STKS = 4;
int main()
{
// create an array of initialized objects
Stock stocks[STKS] = {
Stock("NanoSmart", 12, 20.0),
Stock("Boffo Objects", 200, 2.0),
Stock("Monolithic Obelisks", 130, 3.25),
Stock("Fleep Enterprises", 60, 6.5)
};
std::cout << "Stock holdings:\n";
int st;
for (st = 0; st < STKS; st++)
stocks[st].show();
// set pointer to first element
const Stock* top = &stocks[0];
for (st = 1; st < STKS; st++)
top = &top->topval(stocks[st]);
// now top points to the most valuable holding
std::cout << "\nMost valuable holding:\n";
top->show();
return 0;
}
对象数组是创建一个类的多个对象,其他方面和类没啥区别,这种方法适用于多个对象,比较方便。
3.类作用域
在类中定义的名称(如类数据成员名和类数据函数名)的作用域都为整个类,作用域为整个类的名称在该类中是可知的,在类外是不可知的,因此,不同类中使用相同的类成员名而不会引起冲突,大家懂我的意思吧!
以下是一个案例,比较简单就不分析了。
class Ik
{
private:
int fuss; // fuss has class scope
public:
Ik(int f = 9) {fuss = f; } // fuss is in scope
void ViewIk() const; // ViewIk has class scope
};
void Ik::ViewIk() const //Ik:: places ViewIk into Ik scope
{
cout << fuss << endl; // fuss in scope within class methods
}
...
int main()
{
Ik * pik = new Ik;
Ik ee = Ik(8); // constructor in scope because has class name
ee.ViewIk(); // class object brings ViewIk into scope
pik->ViewIk(); // pointer-to-Ik brings ViewIk into scope
...
来看一种错误代码,是一种必须避免的错误,在创建对象前,将没有用于储值的空间,在我们印象中感觉是正确的,但是计算机并不买账。
class Bakery
{
private:
const int Months = 12; // declare a constant? FAILS
double costs[Months];
...
但是我们有其他方法可以解决这个问题,比如枚举,c++中的static
class Bakery
{
private:
enum {Months = 12};
double costs[Months];
...
class Bakery
{
private:
static const int Months = 12;
double costs[Months];
...
4.抽象数据类型
说到ADT
很容易想到栈,c++程序通过栈来管理走动变量,栈存储了多个数据项。
介绍下栈的特征
1.可创建空栈
2.可将数据项添加到堆顶
3.可从栈顶删除数据项
4.可查看栈是否填满
5.可查看栈是否为空
typedef unsigned long Item;
class Stack
{
private:
enum { MAX = 10 }; // constant specific to class
Item items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty() const;
bool isfull() const;
// push() returns false if stack already is full, true otherwise
bool push(const Item& item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item& item); // pop top into item
};
Stack::Stack() // create an empty stack
{
top = 0;
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
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 <iostream>
#include <cctype> // or ctype.h
#include<string>
using namespace std;
int main()
{
using namespace std;
Stack st; // create an empty stack
char ch;
unsigned long po;
cout << "Please enter A to add a purchase 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.isempty())
cout << "stack already empty\n";
else {
st.pop(po);
cout << "PO #" << po << " popped\n";
}
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;
}
希望大家根据栈的特性多加理解这个例子。