locked
multilevel inheritance RRS feed

  • Question

  • User348142989 posted

    HI

    Trying to learn C# and currently working on multilevel inheritance.  Using VS2019 community.

    I have created a base class "Account" with 2 derived classes "Savings" and "Checking"

    I am trying to create a derived class from Savings - "JointSavings"

    I get an error when I create a constructor for the JointSavings class. "There is no argument  given that corresponds to the required formal parameter 'balance' of "Savings.Savings(decimal)"

    My code is listed below.  Clearly I am missing something and any help would be appreciated.

    class Account //base class
    {
    protected decimal BankBalance { get; set; } //protected variable not available to non derived classes
    protected Account()
    {
    Console.WriteLine();
    Console.WriteLine("inside the Account constructor!");
    }
    public void ShowBalance()
    {
    Console.WriteLine("Bank balance = " + BankBalance.ToString("C")); //standard numeric specifiers
    }
    }
    class Savings : Account // - derived class of account
    {
    public Savings(decimal balance)
    {
    Console.WriteLine();
    Console.WriteLine("inside the Savings constructor!");
    BankBalance = balance;
    }
    public decimal AddMonthlyInterest(decimal interest) //custom child method
    {
    Console.WriteLine();
    Console.WriteLine("inside the Savings method - add interest!");
    BankBalance *= (1.0m + interest);
    return BankBalance;
    }
    }
    class Checking : Account // - derived class of account
    {
    public Checking(decimal balance)
    {
    Console.WriteLine();
    Console.WriteLine("inside the Checking constructor!");
    BankBalance = balance;
    }
    public decimal DeductServiceCharge() //custom child method
    {
    Console.WriteLine();
    Console.WriteLine("inside the Checking method - deduct servce charge!");
    const decimal CHECKING_FEE = 1.50m;
    BankBalance -= CHECKING_FEE;
    return BankBalance;
    }
    }
    class JointSavings : Savings // - derived class of savings
    {
    protected string FirstNameA { get; set; }
    protected string FirstNameB { get; set; }
    public JointSavings(string firstNameA, string firstNameB)
    {
    //Console.WriteLine();
    //Console.WriteLine("inside the Joint Savings constructor!");
    FirstNameA = firstNameA;
    FirstNameB = firstNameB;
    }
    }

    Tuesday, June 18, 2019 12:29 AM

Answers

  • User-719153870 posted

    Hi TiredOldCat,

    The order in which constructors are executed is: first, the constructor of the base class is executed, and then the constructor of the derived class is executed.

    Your problem arises when you execute the Joint Savings class. The constructor corresponding to its base class is

    public Savings(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings constructor!");
                BankBalance = balance;
            }

     which needs to pass in a decimal balance parameter.

    Therefore, we can solve this problem in two way.

    One is by adding a parametric constructor

    public Savings()
            { }

    to the base class Savings, so that the parametric constructor will be executed by default when the constructor 

    public JointSavings(string firstNameA, string firstNameB)
            {
                //Console.WriteLine();
                //Console.WriteLine("inside the Joint Savings constructor!");
                FirstNameA = firstNameA;
                FirstNameB = firstNameB;
            }

    in JointSavings is executed.

    The entire code refer to below:

    class Account //base class
        {
            protected decimal BankBalance { get; set; } //protected variable not available to non derived classes
            protected Account()
            {
                Console.WriteLine();
                Console.WriteLine("inside the Account constructor!");
            }
            public void ShowBalance()
            {
                Console.WriteLine("Bank balance = " + BankBalance.ToString("C")); //standard numeric specifiers
            }
        }
    
        class Savings : Account // - derived class of account
        {
            public Savings()
            { }
    
            public Savings(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings constructor!");
                BankBalance = balance;
            }
            public decimal AddMonthlyInterest(decimal interest) //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings method - add interest!");
                BankBalance *= (1.0m + interest);
                return BankBalance;
            }
        }
    
        class Checking : Account // - derived class of account
        {
            public Checking(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking constructor!");
                BankBalance = balance;
            }
            public decimal DeductServiceCharge() //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking method - deduct servce charge!");
                const decimal CHECKING_FEE = 1.50m;
                BankBalance -= CHECKING_FEE;
                return BankBalance;
            }
        }
    
        class JointSavings : Savings // - derived class of savings
        {
            protected string FirstNameA { get; set; }
            protected string FirstNameB { get; set; }
    
            public JointSavings(string firstNameA, string firstNameB) 
            {
                //Console.WriteLine();
                //Console.WriteLine("inside the Joint Savings constructor!");
                FirstNameA = firstNameA;
                FirstNameB = firstNameB;
            }
        }

    Another is you add the decimal parameter into your JointSavings's constructor .

    Please refer to below codes:

    class Account //base class
        {
            protected decimal BankBalance { get; set; } //protected variable not available to non derived classes
            protected Account()
            {
                Console.WriteLine();
                Console.WriteLine("inside the Account constructor!");
            }
            public void ShowBalance()
            {
                Console.WriteLine("Bank balance = " + BankBalance.ToString("C")); //standard numeric specifiers
            }
        }
    
        class Savings : Account // - derived class of account
        {
            public Savings(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings constructor!");
                BankBalance = balance;
            }
            public decimal AddMonthlyInterest(decimal interest) //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings method - add interest!");
                BankBalance *= (1.0m + interest);
                return BankBalance;
            }
        }
    
        class Checking : Account // - derived class of account
        {
            public Checking(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking constructor!");
                BankBalance = balance;
            }
            public decimal DeductServiceCharge() //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking method - deduct servce charge!");
                const decimal CHECKING_FEE = 1.50m;
                BankBalance -= CHECKING_FEE;
                return BankBalance;
            }
        }
    
        class JointSavings : Savings // - derived class of savings
        {
            protected string FirstNameA { get; set; }
            protected string FirstNameB { get; set; }
    
            public JointSavings(string firstNameA, string firstNameB, decimal balance) : base(balance)
            {
                //Console.WriteLine();
                //Console.WriteLine("inside the Joint Savings constructor!");
                FirstNameA = firstNameA;
                FirstNameB = firstNameB;
            }
        }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 18, 2019 2:21 AM

All replies

  • User-719153870 posted

    Hi TiredOldCat,

    The order in which constructors are executed is: first, the constructor of the base class is executed, and then the constructor of the derived class is executed.

    Your problem arises when you execute the Joint Savings class. The constructor corresponding to its base class is

    public Savings(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings constructor!");
                BankBalance = balance;
            }

     which needs to pass in a decimal balance parameter.

    Therefore, we can solve this problem in two way.

    One is by adding a parametric constructor

    public Savings()
            { }

    to the base class Savings, so that the parametric constructor will be executed by default when the constructor 

    public JointSavings(string firstNameA, string firstNameB)
            {
                //Console.WriteLine();
                //Console.WriteLine("inside the Joint Savings constructor!");
                FirstNameA = firstNameA;
                FirstNameB = firstNameB;
            }

    in JointSavings is executed.

    The entire code refer to below:

    class Account //base class
        {
            protected decimal BankBalance { get; set; } //protected variable not available to non derived classes
            protected Account()
            {
                Console.WriteLine();
                Console.WriteLine("inside the Account constructor!");
            }
            public void ShowBalance()
            {
                Console.WriteLine("Bank balance = " + BankBalance.ToString("C")); //standard numeric specifiers
            }
        }
    
        class Savings : Account // - derived class of account
        {
            public Savings()
            { }
    
            public Savings(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings constructor!");
                BankBalance = balance;
            }
            public decimal AddMonthlyInterest(decimal interest) //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings method - add interest!");
                BankBalance *= (1.0m + interest);
                return BankBalance;
            }
        }
    
        class Checking : Account // - derived class of account
        {
            public Checking(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking constructor!");
                BankBalance = balance;
            }
            public decimal DeductServiceCharge() //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking method - deduct servce charge!");
                const decimal CHECKING_FEE = 1.50m;
                BankBalance -= CHECKING_FEE;
                return BankBalance;
            }
        }
    
        class JointSavings : Savings // - derived class of savings
        {
            protected string FirstNameA { get; set; }
            protected string FirstNameB { get; set; }
    
            public JointSavings(string firstNameA, string firstNameB) 
            {
                //Console.WriteLine();
                //Console.WriteLine("inside the Joint Savings constructor!");
                FirstNameA = firstNameA;
                FirstNameB = firstNameB;
            }
        }

    Another is you add the decimal parameter into your JointSavings's constructor .

    Please refer to below codes:

    class Account //base class
        {
            protected decimal BankBalance { get; set; } //protected variable not available to non derived classes
            protected Account()
            {
                Console.WriteLine();
                Console.WriteLine("inside the Account constructor!");
            }
            public void ShowBalance()
            {
                Console.WriteLine("Bank balance = " + BankBalance.ToString("C")); //standard numeric specifiers
            }
        }
    
        class Savings : Account // - derived class of account
        {
            public Savings(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings constructor!");
                BankBalance = balance;
            }
            public decimal AddMonthlyInterest(decimal interest) //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Savings method - add interest!");
                BankBalance *= (1.0m + interest);
                return BankBalance;
            }
        }
    
        class Checking : Account // - derived class of account
        {
            public Checking(decimal balance)
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking constructor!");
                BankBalance = balance;
            }
            public decimal DeductServiceCharge() //custom child method
            {
                Console.WriteLine();
                Console.WriteLine("inside the Checking method - deduct servce charge!");
                const decimal CHECKING_FEE = 1.50m;
                BankBalance -= CHECKING_FEE;
                return BankBalance;
            }
        }
    
        class JointSavings : Savings // - derived class of savings
        {
            protected string FirstNameA { get; set; }
            protected string FirstNameB { get; set; }
    
            public JointSavings(string firstNameA, string firstNameB, decimal balance) : base(balance)
            {
                //Console.WriteLine();
                //Console.WriteLine("inside the Joint Savings constructor!");
                FirstNameA = firstNameA;
                FirstNameB = firstNameB;
            }
        }

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 18, 2019 2:21 AM
  • User348142989 posted

    Hi Yang

    Thanks very much.  Great to have 2 different methods to fix my problem.

    Have a nice day.

    Tuesday, June 18, 2019 11:43 AM