CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Account exercise - Inheritance

    Code:
    Implement a base class Account and derived classes Savings and
    Checking.In the base class, supply member functions deposit and withdraw.Provide
    a function daily_interest that computes and adds the daily interest.For calculations,
    assume that every month has 30 days.Checking accounts yield interest of 3
    percent monthly on balances over $1, 000. Savings accounts yield interest of 6 percent
    on the entire balance.Write a driver program that makes a month’s worth of
    deposits and withdrawals and calculates the interest every day.
    Can somebody explain guideline for this exercise? I could probably write the code for the program but I don't understand the guidelines that well.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Account exercise - Inheritance

    What don't you understand? It says what base class and a derived class are required and what class functions are to be used. You need to decide what class variables are required and how the classes are to be used for deposits and withdrawals.

    IMO you need to design the program first before you code it. Once you have designed the program then code it from that design. I would advise against starting to code without first having a design. You need to think about input, output, data required, data storage, processing etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Account exercise - Inheritance

    I dont understand this part completely
    Write a driver program that makes a month’s worth of
    deposits and withdrawals and calculates the interest every day.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Account exercise - Inheritance

    Quote Originally Posted by flex567 View Post
    I dont understand this part completely
    It's not explained very well. I would assume that they want you to write a program that would prompt for deposits and withdrawals and the day and the amount they took place. You'd compute the interest on balance every day based on 3% over $1,000 and add up the daily interest amounts to yield the monthly amount, but to me, the requirements are too vague to really know what they want.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Account exercise - Inheritance

    Quote Originally Posted by flex567 View Post
    I dont understand this part completely
    Welcome to the world of vague program requirements! In this context I would take the requirement of the driver program to be what main() is going to do to use the classes. You could do this in one of two ways
    - interactively prompt the user to enter details of deposits and withdrawals (eg type (savings/checking), amount etc) and then provide a summary for each type of account showing the balance, interest etc

    - obtain the details from a text file containing the account transactions. Then display the monthly summary etc.

    If you want to expand upon this, then you could add the ability to have several accounts of each type with deposits and withdrawals spread over more than one month. The output will then show summaries per account per month.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Account exercise - Inheritance

    I will just find another example. There are a lot of them out there, no need to think too much about this one.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Account exercise - Inheritance

    Quote Originally Posted by flex567 View Post
    I will just find another example. There are a lot of them out there, no need to think too much about this one.
    I take the same approach when I'm working - when I find requirements I don't understand, I just find a new job.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Account exercise - Inheritance

    Quote Originally Posted by flex567 View Post
    I will just find another example. There are a lot of them out there, no need to think too much about this one.

    "I can accept failure, everyone fails at something. But I can't accept not trying.

    Michael Jordan"
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Account exercise - Inheritance

    should I have 3 deposit variables for each of the classes: (Account, Savings, Checkings) or should be just one in the Account class ?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Account exercise - Inheritance

    Quote Originally Posted by flex567 View Post
    should I have 3 deposit variables for each of the classes: (Account, Savings, Checkings) or should be just one in the Account class ?
    Any common functionality should be put in the base class. Have you learned about virtual functions yet? The only difference appears to be the interest rate, and that should be a virtual function in the base class that's overridden in the derived classes to return the appropriate value.

  11. #11
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Account exercise - Inheritance

    this is how I wrote this program:
    Code:
    #include <iostream>
    using namespace std;
    
    class Account{
    	double balance;
    	int day_in_month;
    
    public:
    	Account(double initial_dep, int d) : balance(initial_dep), day_in_month(d = 0) {}
    	int get_day_in_month() { return day_in_month; }
    	double get_balance() { return balance; }
    	void set_balance(double bal) { balance = bal; }
    	/********************************************************************************/
    	void deposit(int deposit, int day_in_m){
    		if (day_in_month > day_in_m && day_in_month > 30){
    			cout << "Error, your date is wrong";
    		}
    		balance = balance + deposit;
    
    	}
    	void withdraw(int sum, int day_in_m ){
    		if (day_in_month > day_in_m && day_in_month > 30){
    			cout << "Error, your date is wrong" << "\n";
    		}
    		if (sum < balance){
    			balance = balance - sum;
    		}
    		else {
    			cout << " you don't have enough on the account." << "\n";
    		}
    
    	}
    	virtual void daily_interest();
    
    };  /* comment */
    class Savings : public Account {
    public:
    	Savings();
    	void daily_interest(){
    		set_balance(get_balance() * 0.06 / 30 * get_day_in_month());
    	}
    };
    class Checkings : public Account {
    public:
    	Checkings();
    	void daily_interest(){
    		if (get_balance() > 1000){
    			set_balance(get_balance() * 0.03 / 30 * get_day_in_month());
    		}
    
    	}
    };
    int main()
    {
    	Account mike(1200, 0);
    	Savings mike_s;
    	Checkings mike_c;
    
    	mike.withdraw(200, 5);
    	mike_s.daily_interest();
    	mike_c.daily_interest();
    
    	cout << mike.get_balance() << "\n";
    
    	return 0;
    
    }
    but i get a few errors :
    Code:
    Error	1	error LNK2001: unresolved external symbol "public: virtual void __thiscall Account::daily_interest(void)" (?daily_interest@Account@@UAEXXZ)	C:\Users\seba\Desktop\Seba-Docs-D\Seba_Programming\Programiranje_Visual_Studio_2013\BIG_cpp_Exercise P8_4_\Maincpp.obj	BIG_cpp_Exercise P8_4_
    Code:
    Error	2	error LNK2019: unresolved external symbol "public: __thiscall Savings::Savings(void)" (??0Savings@@QAE@XZ) referenced in function _main	C:\Users\seba\Desktop\Seba-Docs-D\Seba_Programming\Programiranje_Visual_Studio_2013\BIG_cpp_Exercise P8_4_\Maincpp.obj	BIG_cpp_Exercise P8_4_

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Account exercise - Inheritance

    The errors are just what they say they are. You've declared three functions but never implemented them.

    For example, you have a declared a constructor but never implemented it here

    Checkings();

  13. #13
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Account exercise - Inheritance

    I don't need any constructor. If I delete them I get

    Code:
    Error	1	error C2512: 'Savings' : no appropriate default constructor available	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p8_4_\maincpp.cpp	64	1	BIG_cpp_Exercise P8_4_
    Code:
    Error	2	error C2512: 'Checkings' : no appropriate default constructor available	c:\users\seba\desktop\seba-docs-d\seba_programming\programiranje_visual_studio_2013\big_cpp_exercise p8_4_\maincpp.cpp	65	1	BIG_cpp_Exercise P8_4_

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Account exercise - Inheritance

    You shouldn't delete them, you should implement them.

  15. #15
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Account exercise - Inheritance

    I did it like that:


    Code:
    Checkings(double initial_dep, int d) : Account(initial_dep, d) {}
    now I get this error:
    Code:
    Error	1	error LNK2001: unresolved external symbol "public: virtual void __thiscall Account::daily_interest(void)" (?daily_interest@Account@@UAEXXZ)	C:\Users\seba\Desktop\Seba-Docs-D\Seba_Programming\Programiranje_Visual_Studio_2013\BIG_cpp_Exercise P8_4_\Maincpp.obj	BIG_cpp_Exercise P8_4_
    Code:
    #include <iostream>
    using namespace std;
    
    class Account{
    	double balance;
    	int day_in_month;
    
    public:
    	Account(double initial_dep, int d) : balance(initial_dep = 0), day_in_month(d = 0) {}
    	int get_day_in_month() { return day_in_month; }
    	double get_balance() { return balance; }
    	void set_balance(double bal) { balance = bal; }
    /*************************************************************************************/
    	void deposit(int deposit, int day_in_m){
    		if (day_in_month > day_in_m && day_in_month > 30){
    			cout << "Error, your date is wrong";
    		}
    		balance = balance + deposit;
    
    	}
    	void withdraw(int sum, int day_in_m ){
    		if (day_in_month > day_in_m && day_in_month > 30){
    			cout << "Error, your date is wrong" << "\n";
    		}
    		if (sum < balance){
    			balance = balance - sum;
    		}
    		else {
    			cout << " you don't have enough on the account." << "\n";
    		}
    
    	}
    	virtual void daily_interest();
    
    };  /* comment */
    class Savings : public Account {
    public:
    	Savings(double initial_dep, int d) : Account(initial_dep, d) {}
    	void daily_interest(){
    		set_balance(get_balance() * 0.06 / 30 * get_day_in_month());
    	}
    };
    class Checkings : public Account {
    public:
    	Checkings(double initial_dep, int d) : Account(initial_dep, d) {}
    	void daily_interest(){
    		if (get_balance() > 1000){
    			set_balance(get_balance() * 0.03 / 30 * get_day_in_month());
    		}
    
    	}
    };
    int main()
    {
    	Account mike(1200, 0);
    	Savings mike_s(1200, 0);
    	Checkings mike_c(1200, 0);
    
    	mike.withdraw(200, 5);
    	mike_s.daily_interest();
    	mike_c.daily_interest();
    
    	cout << mike.get_balance() << "\n";
    
    	return 0;
    
    }

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured