-2

Trying to program a simple bank system in C++.

I programmed a bank system back in 2013 in C#. So, I'm trying to translate that code to C++, however I've hit a brick wall.

CreditAccount(decimal amount): base(amount)
    {
        //the :base(amount) calls parent constructor
        //passes on parameter amount
        //so only need to initialise additional instance variables
        ODLimit = 100;
    }

Basically, this is my CreditAccount constructor, there's multiple types of accounts, credit accounts, debit accounts etc. They all have the baseclass Account.

This code in C# would take the base constructor for Account, and allow me to keep the information from that, and add a new variable specifically for CreditAccount.

My question is: is there any alternative to this in C++?

If so, could I please have an example? or doccumentation? thank-you.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
Mitch89
  • 59
  • 7

1 Answers1

3

That's exactly the way of doing it in C++, except that there is no type decimal (unless defined by you). Example using constructor initialization lists:

#include <iostream>

class Base
{
    int x_;
public:
    Base(int x): x_(x){}
    int getX() const 
    {
        return x_;
    }
};

class Derived: public Base
{
    int y_; // additional member
public:
    Derived(int x, int y): Base(x), y_(y) // call Base ctor, init. additional member
    {}
    int getY() const 
    {
        return y_;
    }
};

int main()
{
    Derived der(10, 20);
    std::cout << der.getX() << " " << der.getY();
}

Live on Coliru

vsoftco
  • 55,410
  • 12
  • 139
  • 252