C++ for the MFE class at UC Berkeley.ppt
《C++ for the MFE class at UC Berkeley.ppt》由会员分享,可在线阅读,更多相关《C++ for the MFE class at UC Berkeley.ppt(26页珍藏版)》请在麦多课文档分享上搜索。
1、C+ for the MFE class at UC Berkeley,Yuli Kaplunovsky MFE, MBA, MS-Tax, CFA yuliFinancialS (408) 884 5965,2,Introduction,Based upon “Teach Yourself C+ in 21 Days” http:/ C+ Workshop Sessions S300T (inside Computing Center) #1 - Thu April 6th 1:00PM 3:30PM #2 - Tue April 11th 9:30AM 12:00PM #3 - Thu A
2、pril 20th 1:00PM 3:30PM,3,Sample Program #1,File name: Test.cpp1: #include 2: 3: int main() 4: 5: cout “Hello World!n“; 6: return 0; 7: Hello World!,4,Structure,struct TIME_REC double dHours;double dRate; ;void main() TIME_REC payrollRecord;payrollRecord.dHours = 40.0;payrollRecord.dRate = 3.75;cout
3、 “This weeks payroll information:“ endl;cout “Hours worked : “ payrollRecord.dHours endl;cout “Rate :$“ payrollRecord.dRate endl;double dSalary = payrollRecord.dRate * payrollRecord.dHours;cout “Salary :$“ dSalary endl; ,5,Declaring a Class,class Cat public: unsigned int Age; unsigned int Weight; Me
4、ow(); ;,Defining an Object,unsigned int GrossWeight; / define an unsigned integer Cat Frisky; / define a Cat,6,Accessing Class Members,Frisky.Weight = 50; Frisky.Meow();,Cat Frisky; / just like int x; Frisky.age = 5; / just like x = 5;,If You Dont Declare It, Your Class Wont Have It Cat Frisky; / ma
5、ke a Cat named Frisky Frisky.Bark() / tell Frisky to bark,7,Sample program #2,1: / Demonstrates declaration of a class and 2: / definition of an object of the class, 3: 4: #include / for cout 5: 6: class Cat / declare the class object 7: 8: public: / members which follow are public 9: int itsAge; 10
6、: int itsWeight; 11: ; 12: 13: 14: void main() 15: 16: Cat Frisky; 17: Frisky.itsAge = 5; / assign to the member variable 18: cout “Frisky is a cat who is “ ; 19: cout Frisky.itsAge “ years old.n“; 20: Output: Frisky is a cat who is 5 years old.,8,Implementing Class Methods,void Cat:Meow() cout “Meo
7、w.n“; ,Calling class function,void main() Cat Frisky;Frisky.Meow(); ,9,Using internal variables inside class,void main() Cat Frisky;Cat Whisky;Frisky.itsAge = 3; Whisky.itsAge = 5; cout “Frisky says: “; Frisky.Meow();cout “Whisky says: “; Whisky.Meow(); Frisky says: Meow Meow Meow Whisky says: Meow
8、Meow Meow Meow Meow,class Cat public: int itsAge;int itsWeight;void Meow(); ,void Cat:Meow() / the older the cat, the more noisy it is. for ( int I = 0 ; I itsAge ; I+ )cout “Meow “;cout “n“; ,10,Public / Private,class Cat public: int GetAge(); void SetAge(int age); void HappyBirthday(); private: in
9、t itsAge; ;int Cat:GetAge() return itsAge; void Cat:SetAge(int age) itsAge = age; ,void main() Cat Frisky;Frisky.SetAge(5);cout “Frisky is a cat who is “ ;cout Frisky.GetAge() “ years old.n“;Frisky.HappyBirthday();cout “Frisky is a cat who is “ ;cout Frisky.GetAge() “ years old.n“; Frisky is a cat w
10、ho is 5 years old. Frisky is a cat who is 6 years old.,void Cat:HappyBirthday() itsAge+; ,11,constructors and destructors,class Cat public: Cat(int initialAge); / constructorCat(); / destructorint GetAge(); void SetAge(int age); void Meow(); private: int itsAge; ;,/ constructor of Cat Cat:Cat(int in
11、itialAge) itsAge = initialAge; Cat:Cat() / destructor cout “Bye bye n“; ,int main() Cat Frisky(5);Frisky.Meow();cout “Frisky is a cat who is “ ;cout Frisky.GetAge() “ years oldn“;Frisky.Meow();Frisky.SetAge(7);cout “Now Frisky is “ ;cout Frisky.GetAge() “ years oldn“;return 0; Meow. Frisky is a cat
12、who is 5 years old. Meow. Now Frisky is 7 years old. Bye bye ,12,Different Files,class Cat public:Cat (int initialAge);int GetAge() return itsAge; / inline!void SetAge (int age) itsAge = age; / inline!void Meow(); private:int itsAge; ;,Cat class declaration in cat.h,#include #include “cat.h“ / be su
13、re to include the header file!Cat:Cat(int initialAge) itsAge = initialAge; Cat:Meow() cout “Meow.n“; int main() Cat Frisky(5);Frisky.Meow();cout “Frisky is a cat who is “ ;cout Frisky.GetAge() “ years old.n“;Frisky.Meow();Frisky.SetAge(7);cout “Now Frisky is “ ;cout Frisky.GetAge() “ years old.n“;re
14、turn 0; ,Cat implementation in cat.cpp,13,1: #include 2: 3: typedef unsigned short int USHORT; 4: typedef unsigned long int ULONG; 5: enum BOOL FALSE, TRUE; 6: enum CHOICE DrawRect = 1, GetArea, 7: GetPerim, ChangeDimensions, Quit; 8: / Rectangle class declaration 9: class Rectangle 10: 11: public:
15、12: / constructors 13: Rectangle(USHORT width, USHORT height); 14: Rectangle(); 15: 16: / accessors 17: USHORT GetHeight() return itsHeight; 18: USHORT GetWidth() return itsWidth; 19: ULONG GetArea() return itsHeight * itsWidth; 20: ULONG GetPerim() return 2*itsHeight+2*itsWidth; 21: void SetSize(US
16、HORT newW, USHORT newH); 22: 23: / Misc. methods 24: void DrawShape() const; 25: 26: private: 27: USHORT itsWidth; 28: USHORT itsHeight; 29: ;,31: / Class method implementations 32: void Rectangle:SetSize(USHORT newW,USHORT newH) 33: 34: itsWidth = newW; 35: itsHeight = newH; 36: 37: 38: 39: Rectang
17、le:Rectangle(USHORT width, USHORT height) 40: 41: itsWidth = width; 42: itsHeight = height; 43: 44: 45: Rectangle:Rectangle() 46: 47: USHORT DoMenu(); 48: void DoDrawRect(Rectangle); 49: void DoGetArea(Rectangle); 50: void DoGetPerim(Rectangle); 51:,Comprehensive Example,14,52: void main () 53: 54:
18、/ initialize a rectangle to 10,20 55: Rectangle theRect(30,5); 56: 57: USHORT choice = DrawRect; 58: USHORT fQuit = FALSE; 59: 60: while (!fQuit) 61: 62: choice = DoMenu(); 63: if (choice Quit) 64: 65: cout “nInvalid Choice, please try again.nn“; 66: continue; 67: 68: switch (choice) 69: 70: case Dr
19、awRect: 71: DoDrawRect(theRect); 72: break; 73: case GetArea: 74: DoGetArea(theRect); 75: break; 76: case GetPerim: 77: DoGetPerim(theRect); 78: break;,79: case ChangeDimensions: 80: USHORT newLength, newWidth; 81: cout newWidth; 83: cout newLength; 85: theRect.SetSize(newWidth, newLength); 86: DoDr
20、awRect(theRect); 87: break; 88: case Quit: 89: fQuit = TRUE; 90: cout “nExiting.nn“; 91: break; 92: default: 93: cout “Error in choice!n“; 94: fQuit = TRUE; 95: break; 96: / end switch 97: / end while 98: / end main,15,101: USHORT DoMenu() 102: 103: USHORT choice; 104: cout choice; 112: return choic
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CFORTHEMFECLASSATUCBERKELEYPPT
