Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 480509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:54:00+00:00 2026-05-13T00:54:00+00:00

I’m trying to redefine two member functions from their parent’s definition. I don’t know

  • 0

I’m trying to redefine two member functions from their parent’s definition.
I don’t know if I have it right or not, but something in my code has errors attached and I can’t find out what.

some of the header:

class Account 
{
public:
   Account(double);
   void creditBalance(double);
   void debitBalance(double);
   double getBalance() const;
protected:
   double balance;      
};


class CheckingAccount : public Account
{
public:
   CheckingAccount(double, double);
   void feeCreditBalance(double);
   void feeDebitBalance(double);

private:
   double fee = 10;

};

relevant cpp file part:

void Account::creditBalance(double plus)
{
   if(plus > 0)
      balance += plus;
   else
      cout << "Cannot credit negative.";
}

void Account::debitBalance(double minus)
{
   if(minus <= balance)
      balance -= minus;
   else
      cout << "Debit amount exceeded account balance.";
}

void CheckingAccount::feeCreditBalance(double plus)
{
   if(plus > 0){
      balance += plus;
      balance -= fee;
   }
   else
      cout << "Cannot credit negative.";
}

void CheckingAccount::feeDebitBalance(double minus)
{
   if(minus <= balance){
      balance -= minus;
      balance -= fee;
   }
   else
      cout << "Debit amount exceeded account balance.";
}

UPDATE:

I added this:

class Account 
{
public:
   Account(double);
   virtual void creditBalance(double);
   virtual void debitBalance(double);
   double getBalance() const;
protected:
   double balance;      
};

Now I get error: virtual outside class declaration

I could use an example of how to properly initialize fee correctly.

EDIT 2:

I have tried changing the constructor line to this:

CheckingAccount::CheckingAccount(double initBal, double phi) :  Account(initBal), fee(phi)
{
   if(initBal < 0)
      initBal = 0;
   balance = initBal;
   cerr << "Initial balance was invalid.";

   if(phi < 0)
      phi = 0;
   fee = phi;
}

not working, I’m going to work around with changing syntax on the fee(phi) part. I don’t know if anyone will respond to this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:54:00+00:00Added an answer on May 13, 2026 at 12:54 am

    If the intention is that for instances of CheckingAccount accounts the versions which use a fee is called, then you want to use virtual methods.

    A virtual method is a method decalared (at least in the base class) as “virtual”, and has the same name and signiture in any derived classes. When a virtual method is called, it will call the “most derived” version for the instance.

    To do this just declare “void creditBalance(double);” and “void debitBalance(double);” virtual (ie “virtual void creditBalance(double);” and “virtual void debitBalance(double);”. Then in CheckingAccount rename “feeCreditBalance” and “feeDebitBalance” to “creditBalance” and “debitBalance”.

    EDIT: Simple example.

    Header

    class Base
    {
    public:
        Base(int x);
        //declare virtual so that derived classes may overide it
        virtual void sayHello();
    protected:
        int x;
    };
    
    class Derived : public Base
    {
    public:
        Derived(int x, int y);
        //overide Base::sayHello(). the virtual keyword here is optional.
        virtual void sayHello();
    protected:
        int y;
    };
    

    Cpp

    Base::Base(int x)
        :x(x)
    {}
    Derived::Devired(int x, int y)
        :Base(x), y(y)
    {}
    void Base::sayHello()
    {
        std::cout << "Hello from Base!" << std::endl;
        std::cout << "X = " << x << std::endl;
    }
    void Derived::sayHello()
    {
        std::cout << "Hello from Derived!" << std::endl;
        std::cout << "X = " << x << "  Y = " << y << std::endl;
    }
    
    int main()
    {
        Base a(5);
        a.sayHello();//"Hello from Base!..."
    
        Derived b(10, -20);
        b.sayHello();//"Hello from Derived!..."
    
        Base *c = &b;//take pointer to b, reference would also do
        c->sayHello();//"Hello from Derived!..." because its a Derived instance, eventhough its a Base variable.
    }
    

    You can then derive from Derive again (class DerivedAgain : public Derived) and also overload the functions again.

    You can also derive multiple subclasses from Base, which can each have their own overrides for the virtual methods, just like I did for the Derived class.

    EDIT2: Added variables to example and how to use initialiser list to initialise the base class and member variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.