l l Account Account l Account Accountint n

  • Slides: 28
Download presentation

l 例如: l Account : : Account( ){} l Account : : Account(int n,

l 例如: l Account : : Account( ){} l Account : : Account(int n, double m) l { id=n; l money=m; l} Account s 1(2004, 400. 0) l Account s 2; //创建了两个帐户对象 l

l l l 例1:缺省构造函数和析构函数 class Account //类名 { public : bool transfer(Account& s, double

l l l 例1:缺省构造函数和析构函数 class Account //类名 { public : bool transfer(Account& s, double amt )//转帐 void deposit(double amt); //存钱 double withdraw(double amt); //取钱 double get_money( ); //查询余额 private: int id ; //帐号 double money; //目前金额 };

Account s 1(2004, 400. 0) l Account s 2; //? l l 例2: class

Account s 1(2004, 400. 0) l Account s 2; //? l l 例2: class Account l { public : l l l l l //? //类名 Account( ); //构造函数(可以缺省) Account(int n, double m) //构造函数(自定义) ~Account(); 。。。 其他公共界面 private: 。。。 }; // 析够函数

l Account: : Account() l {cout<<“create account”<<endl; } l Account: : ~Account() l {cout<<“destory

l Account: : Account() l {cout<<“create account”<<endl; } l Account: : ~Account() l {cout<<“destory account”<<endl; } l Account : : Account(int n, double m) l { id=n; l money=m; l} l 其他成员函数略。。。。 l

l main() l{ Account s 1, s 2; l s 1. deposit(20. 0); l

l main() l{ Account s 1, s 2; l s 1. deposit(20. 0); l double amt=s 1. withdraw(10. 0); l}

l 考虑程序输出? l main() l{ Account s 1; l Account s 2(2004, 400. 0);

l 考虑程序输出? l main() l{ Account s 1; l Account s 2(2004, 400. 0); l s 1. deposit(10. 0); l double amt=s 2. withdraw(10. 0); l}

l l l l 指出问题? main() { Account s 1; Account s 2(2004, 400.

l l l l 指出问题? main() { Account s 1; Account s 2(2004, 400. 0); s 1. deposit(10. 0); s 2. Account: : ~Account(); double amt=s 2. withdraw(10. 0); }

l 注:考虑系统调用构造函数和析构函数 的顺序。 l 例如: l Account: : ~Account() l {cout<<“destory account”<<id<<endl; } l

l 注:考虑系统调用构造函数和析构函数 的顺序。 l 例如: l Account: : ~Account() l {cout<<“destory account”<<id<<endl; } l Account : : Account(int n, double m) l { id=n; money=m; l cout<<“create account”<<id<<endl; l}

main() l { Account s 1(2003, 200. 0), s 2(2004, 400. 0); l …….

main() l { Account s 1(2003, 200. 0), s 2(2004, 400. 0); l ……. . l s 1. deposit(10. 0); l ……. . l double amt=s 2. withdraw(10. 0); l ……. . l } 程序的输出create account 2003 l create account 2004 l destory account 2003 l

l 例如: class obj{ l int i; l public : l obj(); l obj(int

l 例如: class obj{ l int i; l public : l obj(); l obj(int j){i=j; } l obj(const obj& obj. X){i=obj. X. i; } l} l

l 考虑函数: l Account f(Account s) l { return s; } l void main()

l 考虑函数: l Account f(Account s) l { return s; } l void main() l{ l Account s 1(2004, 10. 0); l Account s 2=f(s 1); l} l 问:系统自动调用了几次拷贝构造函数?

l 例: l l l l class obj{ int x; public : obj(); obj(int

l 例: l l l l class obj{ int x; public : obj(); obj(int y){x=y; } obj(const obj& objx){x=objx. x; } }

main() l { obj one; l obj two(1); l obj three=1; l obj four=two;

main() l { obj one; l obj two(1); l obj three=1; l obj four=two; l obj five(four); l} l 1. 如何对对象初始化? l 1) obj two(1); obj three(two); l 2) obj two=1; obj three=two; l