简体   繁体   中英

C# Class Inheritance Issue with Simple Calculator

I am beginner to C# and am trying to get my second class, MyCalc2, to inherit from MyCalc. But I encounter the following error message in regards to MyCalc2:

There is no argument given that corresponds to the required formal parameter 'x' of 'MyCalc.MyCalc(int, int, string, string)'

The goal here is to just add another class that inherits from the base class.

I know that I need to add something like 'MyCalc: base(x)' to my base class but am lost to where to place the parameter (if that is even the correct thing to do). Any guidance would be appreciated. Here is what I have so far:

    using System;
class MyCalc
{
    // class variable
    public int x;
    public int z;
    public string y;
    public string n;

    // constructor
    public MyCalc(int x, int z, string y, string n)
    {
        this.x = x;  // assign the parameter passed to the class variable
        this.z = z;
        this.y = y;
        this.n = n;

    }
    // calculate the operations
    public int GetAdd()
    {
        return (this.x + this.z);

    }

    public int GetSubtract()
    { 
        return (this.x - this.z);
    }

    public int GetMultiply()
    {
        return (this.x * this.z);
    }

    public int GetDivide()
    {
        return (this.x / this.z);
    }

    public string GetYes()
    {
        return (this.y);
    }

    public string GetNo()
    {
        return (this.n);
    }

}

class MyCalc2:MyCalc //where the error is occurring 
{
    static void Main(string[] args)



    {
        bool repeat = false;
        do
        {

            repeat = false;


            int x = 0; int z = 0; string y; string n;
            Console.WriteLine("Enter the First Number");
            x = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the Second Number");
            z = Convert.ToInt32(Console.ReadLine());



            //Using a switch statement to perform calculation:
            Console.WriteLine("Enter operator\r");
            switch (Console.ReadLine())


            {

                case "+":
                    Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
                    break;

                case "-":
                    Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
                    break;
                case "*":
                    Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
                    break;

                case "/":
                    Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
                    break;


            }



                //Repeat or Exit program using the do-while loop:

            string input = Console.ReadLine();
            Console.WriteLine("Do you want another operation(Y / N) ?");
            input = Console.ReadLine();
            repeat = (input.ToUpper() == "Y");

        }
            while (repeat);
            Console.WriteLine("Thanks for using our system.");
            Console.ReadKey();
        }

    }

MyCalc2 does not have a way of initializing MyCalc (Base Class) Because in your BaseClass you do not have a parameter less constructor.

Solution:

  1. Add a param less constructor in Base Class
  2. Add a constructor in Derived class which has a way of calling base class constructor

for your code below should work:

class MyCalc2 : MyCalc
{
    public MyCalc2 () : base(0, 0, "", "")
    {
    }
}

MyCalc2 has no explicit constructor. Which means it has only one implicit constructor which takes no arguments and sets no values. If made explicitly it would look like this:

public MyCalc2()
{

}

However, MyCalc does have an explicit constructor. Which means it has no implicit constructor. And its constructor does take arguments:

public MyCalc(int x, int z, string y, string n)
{
    this.x = x;  // assign the parameter passed to the class variable
    this.z = z;
    this.y = y;
    this.n = n;
}

So when you create an instance of MyCalc2 it has no way of providing any values to MyCalc . You essentially have three options:

  1. Add a constructor to MyCalc (you can have as many constructors as you want, as long as the parameters differ) which takes no parameters. However, in that case the class-level values for MyCalc would all be default values. You'd have to set them explicitly after constructing the object. 1
  2. Add a constructor to MyCalc2 which accepts those values and passes them to the parent constructor, or at least passes default values to the parent constructor.
  3. Don't use inheritance here.

Honestly, in this case I'd go with the third option. What is inheritance meant to accomplish here? MyCalc2 isn't meaningfully an instance of MyCalc . All it does it hold the initial entry point of the application (the Main method), and that's really all it should do.

The logic in your Main method should create and use an instance of MyCalc , but the class which has that Main method shouldn't try to be an instance of MyCalc . That will only cause more confusion than solve any meaningful problems.


1 Side Note: Public class fields are historically a bad habit to get into in object-oriented programming. There's a variety of talk on the subject, and you'll see this often as you continue in your experience. In general you want your objects to expose behaviors , not values . The methods on the object look somewhat Java-like in convention. For C# conventions consider using properties (which compile down to methods themselves, the syntax is just semantically different). You can have { get; set; } { get; set; } { get; set; } auto-properties for the values themselves, and explicit read-only { get { /*...*/ } } properties for the calculated values.

Here is a possible solution. There are two classes MyClass, for the calculator (you may want to rename it) and Propram. Program holds just the method Main, which get your program started. It works like this, but there are some bugs left. I leave it to you to fix them. Except that you miss a clear understanding of the concepts class and inheritance, your code is not too bad for a beginner. It is almost working.

using System;

namespace TestCalculator
  {
 class MyCalc
{
// class variable
public int x;
public int z;
public string y;
public string n;

// constructor
public MyCalc(int x, int z, string y, string n)
  {
  this.x = x;  // assign the parameter passed to the class variable
  this.z = z;
  this.y = y;
  this.n = n;

  }
// calculate the operations
public int GetAdd()
  {
  return (this.x + this.z);

  }

public int GetSubtract()
  {
  return (this.x - this.z);
  }

public int GetMultiply()
  {
  return (this.x * this.z);
  }

public int GetDivide()
  {
  return (this.x / this.z);
  }

public string GetYes()
  {
  return (this.y);
  }

public string GetNo()
  {
  return (this.n);
  }

}
 class Program
{
static void Main(string[] args)
  {
  bool repeat = false;
  do
    {

    repeat = false;


    int x = 0; int z = 0; string y; string n;
    Console.WriteLine("Enter the First Number");
    x = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("Enter the Second Number");
    z = Convert.ToInt32(Console.ReadLine());



    //Using a switch statement to perform calculation:
    Console.WriteLine("Enter operator\r");
    switch (Console.ReadLine())


      {

      case "+":
        Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
        break;

      case "-":
        Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
        break;
      case "*":
        Console.WriteLine($"The Answer is: {x} + {z} = " + (x + z));
        break;

      case "/":
        Console.WriteLine($"The Answer is: {x} - {z} = " + (x - z));
        break;


      }



    //Repeat or Exit program using the do-while loop:

    string input = Console.ReadLine();
    Console.WriteLine("Do you want another operation(Y / N) ?");
    input = Console.ReadLine();
    repeat = (input.ToUpper() == "Y");

    }
  while (repeat);
  Console.WriteLine("Thanks for using our system.");
  Console.ReadKey();
  }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM