C++面向对象高效编程_源码.docx
《C++面向对象高效编程_源码.docx》由会员分享,可在线阅读,更多相关《C++面向对象高效编程_源码.docx(127页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、2012-11-8 - 2012/11/12/第1章什么是面向对象编程/Just a plain bank account record struct Account char* name /*name of account holder*/ AccountNum accountId;/*and many other details*/ float balance;float interestYTD; /* year to data interest*/ char accountType; /*Savingsr Checking, Loan etc*/ ) AccountNum CreateN
2、ewAccount(const char name, char typeOfAccount);/class BankAccount class BankAccount public:/Many details omitted for simplicity void MakeDeposit (float amount);float withDraw ();bool Transfer(BankAccount& to, float amount); float GetBalance () const; private:/Implementation data for the private use
3、of the BankAccount class float balance;float interesteYTD;char* owner; int account_number;);/Implementation of an operation of class BankAccount void BankAccount:MakeDeposit(float amount) if (amount0.0) balance = balance+amount; ) / : /Class Cardenum Suit Clubs, Diamond, Heart, Spade, Unknown;enum R
4、ank Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen,King, Ace, Invalid;enum Color Red, Black;class Card public:void FaceUp (); /Show card face upvoid FaceDown(); /Make it face down/And many other member functionsCard(Rank r, Suit s); /Create a card with these specifications private
5、:Rank cardRank;Suit cardSuit;Color cardColor;;/ / / : class Number public:Number Sqrt();Number Power(Number raiseTo);Number Inverse();Number Absolute(); /absolute value of the number private:/Use some internal representation to store the number ;/第2章什么是数据抽象/typedef Stack* Stackld;typedef int bool;st
6、ruct Stack int* data; /*storage for elements on the stack*/ unsigned count; /*number of elements on the stack*/ int* top; /*pointer to top of stack*/*and more /;class Tint public:/Constructor: Create a Tint from an intTint(int value);Tint(long mostSignificant, unsigned long leastsignificant);/Create
7、 a Tint from a long integer:Tint(long value);/Create a Tint from a short integer:Tint (short value)/Default Constructor:Tint ();/Copy constructor:Tint (const TInt& copy);/Assignment OperatorTInt& operator=(const Tint& assign);/Destructor/Operators for the Tint typeTint operator+ (const Tint&Tint ope
8、rator- (const Tint&operand) const; /addition operand) const; /subtractionTintTintTintoperator*(const operator/(const operator% (constTInt& multiplicand) const; /multiplicationTint* divideBy) const; /divisionTInt& divedeBy) const; /modulo division/Add operand to this numberTint operator+=(conset Tint
9、s opeand);/Many other operators omitted for simplicity./Raise this Tint number to the power 1 to Tint per(const Tints to) const;void Print () const;/Return the absolute value of this TintTint Absolute() const;/Change sign of this numbervoid FlipSign ();/Helper functionslong GetMostSignificantPart()
10、const;unsigned long GetLeastSignificantPart () const; private:/Internal representation of the Tint typeunsigned long _least Significant Part;long _mostSignificantPart;/and probably some private member functions tootypedef short ErrorCode; class TLaserDiscPlayer public:/OperationsErrorCode ErrorCode
11、ErrorCode ErrorCode ErrorCodePlay(unsigned atChapter = 0);Stop(void);SearchFor(unsigned chapter);OpenTray ();CloseTray ();void PowerOn ();void PowerOff();ErrorCode Pause();/ConstructorTLaserDiscPlayer ();/Destructor*TLaserDiscPlayer ();/and many more private:enum ETrayStatus eClosed, eOpen;enum EPow
12、erStatus eOffz eOn;enum EPlayerMode ePlay, eSearch, ePause, eStop;ETrayStatus _trayStatus; /open or closedEPowerStatus _powerStatus; /On or OffEPlayerMode _playerMode; /Playing, searching etc. /and many more ;/Implementation of operator +Tint Tint:operator+(const Tint& operand) const /Tint is the re
13、turn type of this opertor function/* code for computing the sum of operand and the Tint number on which the operator+ function is invoked which is pointed to by the this pointer.This function computes the sum of *this and operand and returns the sumas a new Tint number. It does not modify *this or o
14、perand (hence theconst qualifier), The algorithm is1. Add the _leastSignificantPart parts and save the carry bit.2. Add the _mostSignificantPart parts using the carry bit.3. Store (1) and (2) in a temporary Tint number4. Return the temporary Tint number by value.*/Tint result = *this; /calls the Cop
15、y Constructor unsigned char carry = 0;/Add the leastsignificant parts and check for any carryresult ._least Significant Part+=operand.GetLeastSignif icantPart();if(result._1eastSignificantPart0) _size = stacksize;_sp = new int size; /Allocate memory for stack elements /Initialize all elements to zer
16、o for (int i=0; i!=_size; +i) _sp i = 0;else _sp = 0; /set the pointer to the unique value _size = 0;_count = 0; /no elements on the stack )Tintstack:Tintstack(const TIntStack& source) /Code written here can access all regions of class TintStack /since the copy constructor is a member function of th
17、e class./The argument source is an object of Tintstack.size = source ._size;if (_size0) /Allocate memory only when size is positive _sp = new int _size; /Allocate memory for stack elements _count = source._count; /#of elements on the stack /Next copy all the elements from source to our stack for int
18、 i=0; i!=_count; +i) _spi = source._spi;)else (_sp = 0; /set the pointer to the unique value _count = 0; /no elements on the stack) )*Function to print all the elements of a Stack*We Pop () elements sequentially and print them in the same order.* /void Printstack(Tintstack thisOne) /Find out the num
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 面向 对象 高效 编程 源码
限制150内