Help(oop)~~~

向下

Help(oop)~~~

帖子 由 天厥 于 周日 三月 27, 2011 3:43 pm

You are not allowed to use ANY global variables – every variable must be declared as a member variable, local variable to a function, or passed as a parameter into a function.
Your program will prompt a user for the amount of money that he owes on a debt, the annual interest rate, and how much he pays on the debt each month. You will also ask the user if the interest is compound or simple, and if compound, whether it is compounded continuously (A=Pe^rt) or monthly (A=P(1+r/12)12). Your program will then calculate how long it will be (if ever) before he will have the debt paid off.
You will need to create a base class called Interest. There will be two derived classes from the Interest class called Simple_Interest and Compound_Interest. Here is the definition of the Interest class:

class Interest{
private:
double int_rate;
public:
Interest (double int_rate);
virtual int_num_months_paid_off(double amt,double monthly_payment)=0;
};
avatar
天厥

帖子数 : 19
注册日期 : 11-03-02
年龄 : 28

查阅用户资料

返回页首 向下

回复: Help(oop)~~~

帖子 由 西方小将 于 周日 三月 27, 2011 8:21 pm

不难做,大概都知道要用多少个class来写了,你可以跟着他的要求一个一个做。logic方面可能要花些时间想,多compile几次避免merge起来的时候有error就好。我相信你应该写得出吧?或者是你卡在哪一个部分再问我吧。。。(不可能要我写完整个source code给你吧?)
avatar
西方小将
管理员
管理员

帖子数 : 74
注册日期 : 11-02-22
年龄 : 32

查阅用户资料 http://purple.forumms.net

返回页首 向下

回复: Help(oop)~~~

帖子 由 天厥 于 周一 三月 28, 2011 12:07 am

好的~~~有问题再来找你~~~
avatar
天厥

帖子数 : 19
注册日期 : 11-03-02
年龄 : 28

查阅用户资料

返回页首 向下

回复: Help(oop)~~~

帖子 由 天厥 于 周一 三月 28, 2011 12:18 am

在class Interest里public member有一个是virtual int_num_months_paid_off(double amt,double monthly_payment)=0......virtual到底是什么意思??
avatar
天厥

帖子数 : 19
注册日期 : 11-03-02
年龄 : 28

查阅用户资料

返回页首 向下

回复: Help(oop)~~~

帖子 由 天厥 于 周一 三月 28, 2011 11:41 pm

class Interest
{
private:
double int_rate;
public:
Interest(double int_rate);
virtual int num_months_paid_off(double amt, double monthly_payment)=0;
};

class Simple_Interest: public Interest
{
public:
int getInfo(int d, int p, int y);
int months;
int num_months_paid_off(double amt, double monthly_payment);
};
int Simple_Interest::getInfo
{
cout<<"\nHow much is your debt?"<<endl;
cin>>d;
cout<<"\nHow much is your monthly payment?"<<endl;
cin>>p;
cout<<"\nPlease enter the total year of your debt"<<endl;
cin>>y;
};
int Simple_Interest::months
{
cout<<"\nHow many months you wish to paid off all the debt?"<<endl;
cin>>month;
};
int Simple_Inerest::num_paid_off(double amt, double monthly_payment)
{
amt=d*(1+yi);
num_paid_off=amt/p;
};

class Compound_Interest: public Interest
{
public:
int getInfo(int d, int p, int y);
int months;
int num_months_paid_off(double amt, double monthly_payment);
};
int Compound_Interest::getInfo
{
cout<<"\nHow much is your debt?"<<endl;
cin>>d;
cout<<"\nHow much is your monthly payment?"<<endl;
cin>>p;
cout<<"\nPlease enter the total year of your debt"<<endl;
cin>>y;
};
int Compound_Interest::months
{
cout<<"\nHow many months you wish to paid off all the debt?"<<endl;
cin>>month;
};
int Compound_Inerest::num_paid_off(double amt, double monthly_payment)



小将救命啊T.T~~~~做了半天才做了一堆垃圾出来T.T
avatar
天厥

帖子数 : 19
注册日期 : 11-03-02
年龄 : 28

查阅用户资料

返回页首 向下

回复: Help(oop)~~~

帖子 由 西方小将 于 周一 三月 28, 2011 11:51 pm

天厥 写道::在class Interest里public member有一个是virtual int_num_months_paid_off(double amt,double monthly_payment)=0......virtual到底是什么意思??

当我们call回 virtual function时, 我们选择的行动并不是在于static type of the pointer or reference, 相对的,他在于type of the object being pointed to, 所以他是可以被vary的。因为我读时是用英文教学,所以要用华语解释有点困难,希望你明白我说什么。
avatar
西方小将
管理员
管理员

帖子数 : 74
注册日期 : 11-02-22
年龄 : 32

查阅用户资料 http://purple.forumms.net

返回页首 向下

回复: Help(oop)~~~

帖子 由 西方小将 于 周一 三月 28, 2011 11:55 pm

天厥 写道::class Interest
{
private:
double int_rate;
public:
Interest(double int_rate);
virtual int num_months_paid_off(double amt, double monthly_payment)=0;
};

class Simple_Interest: public Interest
{
public:
int getInfo(int d, int p, int y);
int months;
int num_months_paid_off(double amt, double monthly_payment);
};
int Simple_Interest::getInfo
{
cout<<"\nHow much is your debt?"<<endl;
cin>>d;
cout<<"\nHow much is your monthly payment?"<<endl;
cin>>p;
cout<<"\nPlease enter the total year of your debt"<<endl;
cin>>y;
};
int Simple_Interest::months
{
cout<<"\nHow many months you wish to paid off all the debt?"<<endl;
cin>>month;
};
int Simple_Inerest::num_paid_off(double amt, double monthly_payment)
{
amt=d*(1+yi);
num_paid_off=amt/p;
};

class Compound_Interest: public Interest
{
public:
int getInfo(int d, int p, int y);
int months;
int num_months_paid_off(double amt, double monthly_payment);
};
int Compound_Interest::getInfo
{
cout<<"\nHow much is your debt?"<<endl;
cin>>d;
cout<<"\nHow much is your monthly payment?"<<endl;
cin>>p;
cout<<"\nPlease enter the total year of your debt"<<endl;
cin>>y;
};
int Compound_Interest::months
{
cout<<"\nHow many months you wish to paid off all the debt?"<<endl;
cin>>month;
};
int Compound_Inerest::num_paid_off(double amt, double monthly_payment)



小将救命啊T.T~~~~做了半天才做了一堆垃圾出来T.T

你这样写的话你的parameter into a function可以被call到??感觉上你的coding并不完整,应该compile不到吧?当你使用function的method来做coding时,一定要设法把它call回出来。
avatar
西方小将
管理员
管理员

帖子数 : 74
注册日期 : 11-02-22
年龄 : 32

查阅用户资料 http://purple.forumms.net

返回页首 向下

回复: Help(oop)~~~

帖子 由 天厥 于 周二 三月 29, 2011 12:01 am

不是太明白==
你可以谢英文的~~~
我的是还不完整因为我写到这就根本不懂该怎么下手了==

avatar
天厥

帖子数 : 19
注册日期 : 11-03-02
年龄 : 28

查阅用户资料

返回页首 向下

回复: Help(oop)~~~

帖子 由 西方小将 于 周二 三月 29, 2011 6:34 pm

天厥 写道::不是太明白==
你可以谢英文的~~~
我的是还不完整因为我写到这就根本不懂该怎么下手了==


如上面我所写的: non-virtual c++ member function and a virtual member function的分别是 the non-virtual member functions are resolved at compile time. This mechanism is called static binding. 相对的, c++ virtual member functions are resolved during run-time. known as dynamic binding.

如果在这个情况我们没有使用virtual的话,很明显。。。那个class function会一直被call出来, Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.

所以当我们declare了一个function的时候,就要懂得如何去准确地call它,否则declare了没有用也是不会出来的。
avatar
西方小将
管理员
管理员

帖子数 : 74
注册日期 : 11-02-22
年龄 : 32

查阅用户资料 http://purple.forumms.net

返回页首 向下

回复: Help(oop)~~~

帖子 由 天厥 于 周二 三月 29, 2011 6:38 pm

如上面我所写的: non-virtual c++ member function and a virtual member function的分别是 the non-virtual member functions are resolved at compile time. This mechanism is called static binding. 相对的, c++ virtual member functions are resolved during run-time. known as dynamic binding.

如果在这个情况我们没有使用virtual的话,很明显。。。那个class function会一直被call出来, Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.

所以当我们declare了一个function的时候,就要懂得如何去准确地call它,否则declare了没有用也是不会出来的。[/quote]

大概明白了~~~谢谢小将^^
avatar
天厥

帖子数 : 19
注册日期 : 11-03-02
年龄 : 28

查阅用户资料

返回页首 向下

回复: Help(oop)~~~

帖子 由 安娜 于 周日 四月 03, 2011 3:50 pm

什么东东 @@
avatar
安娜
初级会员
初级会员

帖子数 : 50
注册日期 : 11-02-22
年龄 : 32

查阅用户资料

返回页首 向下

回复: Help(oop)~~~

帖子 由 Sponsored content


Sponsored content


返回页首 向下

返回页首


 
您在这个论坛的权限:
不能在这个论坛回复主题