您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus学习:第十章 对象和类(3)

2011-09-10 01:26 423 查看
this

每个成员函数(包括构造函数和析构函数)都有一个this指针

this指针指向调用对象

如果方法需要引用整个调用对象,则可使用表达式*this

在函数的括号后面使用const限定符将this限定为const,这样将不能使用this来修改对象的值

eg

stock2.h

#ifndef STOCK2_H_
#define STOCK2_H_

class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot(){total_val = shares*share_val;}

public:
Stock();
Stock(const char * co,int n = 0,double pr = 0.0);
~Stock();
void buy(int num,double price);
void sell(int num,double price);
void update(double price);
void show()const;
const Stock & topval(const Stock & s)const;
} ;
#endif


stock2.cpp

#include <iostream>
#include "stock2.h"

Stock::Stock()
{
std::cout<<"Default constructor called\n";
std::strcpy(company,"no name");
shares = 0;
share_val = 0.0;
total_val = 0.0;
}

Stock::Stock(const char *co,int n,double pr)
{
std::cout<<"Constructor using "<<co<<" called\n";
std::strncpy(company,co,29);
company[29] = '\0';

if(n<0)
{
std::cerr<<"Number of shares can't be negative;"<<company<<" shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}

Stock::~Stock()
{
std::cout<<"Bye,"<<company<<"!\n";
}

void Stock::buy(int num,double price)
{
if(num<0)
{
std::cerr<<"Number of shares purchased can't be negative."<<"Transaction is aborted.\n";
}
else
{
shares +=num;
share_val = price;
set_tot();
}
}

void Stock::sell(int num,double price)
{
using std::cerr;
if(num<0)
{
cerr<<"Number of  shares sold can't be negative."<<"Transaction is aborted.\n";
}
else if(num>shares)
{
cerr<<"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::endl;
cout<<"Company: "<<company<<" Shares: "<<shares<<endl;
cout<<"Share Price:$ "<<share_val<<" Total Worth:$ "<<total_val<<endl;
}

const Stock & Stock::topval(const Stock & s)const
{
if(s.total_val>total_val)
return s;
else
return *this;
}


usestock2.cpp

#include <iostream>
#include "stock2.h"

const int STKS = 4;
int main()
{
using std::cout;
using std::ios_base;

Stock stocks[STKS] = {
Stock("NanoSmart",12,20.0),
Stock("Boffo Objects",200,2.0),
Stock("Monolithic Obelisks",130,3.25),
Stock("Fleep Enterprised",60,61.5)
};
cout.precision(2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::showpoint);

cout<<"Stock holdings:\n";
int st;
for(st = 0;st<STKS;st++)
stocks[st].show();

Stock top = stocks[0];
for(st = 1;st<STKS;st++)
top = top.topval(stocks[st]);
cout<<"\nMost valuable holding:\n";
top.show();

system("pause");
return 0;

}


ADT

stack.h

#ifndef STACK_H_
#define STACK_H_

typedef unsigned long Item;

class Stack
{
private:
enum {MAX = 10};
Item items[MAX];
int top;
public:
Stack();
bool isempty()const;
bool isfull()const;
bool push(const Item &item);
bool pop(Item & item);
};

#endif


stack.cpp

#include "stack.h"

Stack::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;
}


stacker.cpp

#include <iostream>
#include <cctype>
#include "stack.h"

int main()
{
using namespace std;
Stack st;
char ch;
unsigned long po;
cout<<"Please enter A to add a puchase 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 puchase order.\n"<<"P to process a PO,or Q to quit.\n";

}
cout<<"Bye\n";

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: