Prblem: Implement a base class Account and derived classes Savings and Checking. In the base class,supply member functions deposit(), withdraw(), and print() which prints out the ownerand balance. Create constructors that take a string (for the owner's name) and a double for the initialbalance. All methods should return void.
I also need to implement a daily_interest()but i didnt do it
My code did compile right, but it did not give the right output

#include <iostream>
#include <string>
using namespace std;

const int DAYS_PER_MONTH = 30;

///////////// Account CLASS //////////////////////////
class Account
{
public:
    Account(double amount);
    void deposit(double amount);
    void withdraw(double amount);
    void print();
    void daily_interest();
//private:
    double balance;
};
Account::Account(double amount)
{
    balance = amount;
}
void Account::deposit(double amount)
{
    balance += amount;
}
void Account::withdraw(double amount)
{
    balance -= amount;
}
void Account::print()
{
    cout << balance;
}
void Account::daily_interest()
{}
///////////// Savings CLASS //////////////////////////
class Savings : public Account
{
public:
    Savings(double balance);
    double get_balance() const;
private:
    double s_balance;
};
Savings::Savings(double balance)
    : Account(balance)
{
    s_balance = balance;
}
double Savings::get_balance() const
{
    return s_balance;
}
///////////// Checking CLASS //////////////////////////
class Checking : public Account
{
public:
    Checking(double balance);
    double get_balance() const;
private:
    double c_balance;
};
Checking::Checking(double balance)
    : Account(balance)
{
     c_balance = balance;
}
double Checking::get_balance() const 

{
    return c_balance;
}
///////////////////////////////////////////////////////
int main()
{
    Checking c = Checking(1000.0);
    Savings s = Savings(1000.0);
    for (int i = 1; i <= DAYS_PER_MONTH; i++)
    {
        c.deposit(i * 5);
        c.withdraw(i * 2);
        s.deposit(i * 5);
        s.withdraw(i * 2);
        //c.daily_interest();
        //s.daily_interest();
        if (i % 10 == 0)
        {
            cout << "day " << i << "\n";
            cout << "Checking balance: " << c.get_balance() << "\n";
            cout << "Savings balance: " << s.get_balance() << "\n";
        }
    }
    return 0;
}

Edited 6 Years Ago by Nick Evan: Fixed CODE-tag

the outputs are suppose to be different but what i got out were all the same
i think my deposit and withdraw are not working

I made the class definition for the checking and saving, but i don't know how to do the inheritance... =.=

class Savings
{
public:
    Savings(double balance);
    double get_balance() const;
    void deposit(double amount);
    void withdraw(double amount);
    void print() const;
    void daily_interest();
private:
    double s_balance;
};
Savings::Savings(double balance)
{
    s_balance = balance;
}
double Savings::get_balance() const
{
    return s_balance;
}
void Savings::deposit(double amount)
{
    s_balance += amount;
}
void Savings::withdraw(double amount)
{
    s_balance -= amount;
}
void Savings::print() const
{
    cout << s_balance;
}
void Savings::daily_interest()
{
    s_balance += s_balance*(0.06/DAYS_PER_MONTH);
}
///////////// Checking CLASS //////////////////////////
class Checking
{
public:
    Checking(double balance);
    double get_balance() const;
    void deposit(double amount);
    void withdraw(double amount);
    void print() const;
    void daily_interest();
private:
    double c_balance;
};
Checking::Checking(double balance)
{
    c_balance = balance;
}
double Checking::get_balance() const
{
    return c_balance;
}
void Checking::deposit(double amount)
{
    c_balance += amount;
}
void Checking::withdraw(double amount)
{
    c_balance -= amount;
}
void Checking::print() const
{
    cout << c_balance;
}
void Checking::daily_interest()
{
    double over = c_balance - 1000;
    c_balance += over*(0.03/30);
}

First off, what class do you want to inherit? Since there is no mention of a third class, it's hard to guess what class you want to introduce. Inheritance is easy. Let's say I am introducing a ChildSavings class which will inherit from the Saving class, the code would just be as follows.

class ChildSavings : public Savings   // this is how you introduce inheritance
{
   // add class methods and properties here
}

The ChildSavings class will have all the properties and methods of its parent class. Hope this is what you're looking for.

This article has been dead for over six months. Start a new discussion instead.