C++ Primer Plus学习:第十章 对象和类(3)
2011-09-10 01:26
423 查看
this
每个成员函数(包括构造函数和析构函数)都有一个this指针
this指针指向调用对象
如果方法需要引用整个调用对象,则可使用表达式*this
在函数的括号后面使用const限定符将this限定为const,这样将不能使用this来修改对象的值
eg
stock2.h
stock2.cpp
usestock2.cpp
ADT
stack.h
stack.cpp
stacker.cpp
每个成员函数(包括构造函数和析构函数)都有一个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; }
相关文章推荐
- C++ Primer Plus学习:第十章 对象和类(1)
- 2012/1/22 《C++ Primer Plus》第十章:对象和类 学习笔记
- C++ Primer Plus学习:第十章 对象和类(2)
- 2012/1/22 《C++ Primer Plus》第十章:对象和类 学习笔记
- C++PrimerPlus第十章学习笔记——对象和类
- C++之类和对象——C++ primer plus学习(一)
- 【学习C++】C++ Primer Plus (第六版)第十章编程练习1-8
- C++ Primer Plus学习:第十章
- C++ Primer Plus学习笔记之STL函数对象
- 《C++ Primer Plus》第10章 对象和类 学习笔记
- 【学习笔记】C++primer plus 10. 11. 12. 对象
- 《C++ Primer Plus(第六版)》(16)(第十章 对象和类 笔记和复习题答案)
- 《C++ Primer Plus(第六版)》(17)(第十章 对象和类 编程题答案)
- 学习C++.Primer.Plus 10 对象和类
- c primer plus 第十章学习梳理小结
- C++ Primer Plus学习:第十三章 类继承(2)
- C++ primer plus(第六版)学习笔记、习题答案(4.2)
- C Primer Plus 第十章学习总结……2015.4.30
- c++ primer plus的学习
- 《C++ Primer Plus》16.3 标准模板库 学习笔记