0
public class Quadrilateral
{
    public int Xb{get; set;}
    public int Xc{get; set;}
    public int Xd{get; set;}
    public int Yc{get; set;}
    public int Yd{get; set;}

    public Quadrilateral(int Q, int W, int E, int R, int T)
    {
        Q = Xb;
        W = Xc;
        E = Yc;
        R = Xd;
        T = Yd;
    }//end of the constructor

    public void inspectshape(int Q,int W,int E,int R,int T)
    {
        if (Yc == Yd) {
            Trapezoid trap = new Trapezoid ();
            trap.Area (Q,W,E,R,T);

        } else if (Yc == Yd && (Xc - Xd) == Xb) {
            Parallelogram para = new Parallelogram ();
            para.Area (Q,W,E,R,T);

        } else if (Yc == Yd && (Xc - Xd) == Xb && Xd == 0) {
            Rectangle rec = new Rectangle ();
            rec.Area (Q,W,E,R,T);

        } else if (Yc == Yd && (Xc - Xd) == Xb && Xd == 0 && Xc == Yc) {
            Square sq = new Square ();
            sq.Area (Q,W,E,R,T);

        } else {
            Quadrilateral quad = new Quadrilateral() ;
            quad.Area (Q, W, E, R, T); *here is the error*

        }

    }//end of the method inspectshape

    public virtual void Area(int Q, int W, int E, int R, int T)
    {

        double a = Q;//side length of AB
        double b = System.Math.Sqrt ((Q - W)*(Q - W) + (0 - E)*(0-E)); //side length of BC
        double c = System.Math.Sqrt ((W - R)*(W - R) + (E - T)*(E-T)); //side length of CD
        double d = System.Math.Sqrt (R*R + T*T); //side length of DA
        double z = (a + b + c + d) / 2; //irregular quadrilateral parameter z
        double Area = System.Math.Sqrt ((z - a) * (z - b) * (z - c) * (z - d)); //area of the quadrilateral

        Console.WriteLine ("Depends on the numbers given, this is a quadrilateral, and its area is" + Area);
    }//end of the method area

}//end of the class quadrilateral

class Trapezoid : Quadrilateral *here is another same type error*
{

    public override void Area(int Q, int W, int E, int R, int T)
    {
        double a = System.Math.Sqrt ((W - R)*(W - R) + (E - T)*(E-T)); //upper side length or could use Xc-Xd
        double b = Q; //bottom side length
        double h = E; //the height of the trapezoid
        double Area = 0.5 * (a + b) * h;

        Console.WriteLine ("Depends on the numbers given, this is a trapezoid, and its area is" + Area);

    }//end of the method area

}//end of the class trapezoid

class Parallelogram : Trapezoid
{
    public override void Area(int Q, int W, int E, int R, int T)
    {
        double b = Q;
        double h = T;
        double Area = b * h;

        Console.WriteLine ("Depends on the numbers given, this is parallelogram, and its area is" + Area);

    }//end of the method area

}//end of the class parallelogram

class Rectangle : Parallelogram
{
    public override void Area(int Q, int W, int E, int R, int T)
    {
        double w = Q;
        double h = E;
        double Area = w * h;

        Console.WriteLine ("Depends on the numbers given, this is a rectangle, and its area is" + Area);

    }//end of the method area

}//end of the class rectangle

class Square : Rectangle
{
    public override void Area(int Q, int W, int E, int R, int T)
    {
        double a = Q;
        double Area = a * a;

        Console.WriteLine ("Depends on the numbers given, this is a square, and its area is" + Area);


    }

}//end of the class square;

public class Program
{
    static void Main(string[] args) //this is the entry point of my program
    {

        Console.WriteLine ("Welcome to Quadrilateral shape inspection & area calculation system.");
        Console.Write ("Please give a integer for the point Xb: ");
        int Xb = Convert.ToInt32 (Console.ReadLine ());
        Console.Write ("Please give a integer for the point Xc: ");
        int Xc = Convert.ToInt32 (Console.ReadLine ());
        Console.Write ("Please give a integer for the point Yc: ");
        int Yc = Convert.ToInt32 (Console.ReadLine ());
        Console.Write ("Please give a integer for the point Xd: ");
        int Xd = Convert.ToInt32 (Console.ReadLine ());
        Console.Write ("Please give a integer for the point Yd: ");
        int Yd = Convert.ToInt32 (Console.ReadLine ()); 

        Quadrilateral quad = new Quadrilateral (Xb,Xc,Yc,Xd,Yd);
        quad.inspectshape (Xb,Xc,Yc,Xd,Yd);



    }


}

I thought that I've already defined the constructor at the main class, do i need to override the constructor at the derived class? Why doesn't the code work out? Thanks for you help.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
Yuji
  • 1
  • 1

3 Answers3

0
Quadrilateral quad = new Quadrilateral();

You need to pass in the variables while declaring a new Quadrilateral. Your error message is saying you have no constructor that accepts 0 arguments, but you are declaring a new Quadrilateral object and passing it no parameters.

Try this:

Quadrilateral quad = new Quadrilateral(Q, W, E, R, T);
wentimo
  • 471
  • 3
  • 12
0

Since you have your Quadrilateral class constructor with 4 arguments, the compiler will no longer generate a default 0 argument constructor for you.

I had this same issue a while back and used this as a reference. It will explain this in further detail: Should we always include a default constructor in the class?

Community
  • 1
  • 1
drewfiss90
  • 53
  • 5
0

Constructors are not inherited.

The Trapezoid class has only default, parameterless constructor. This constructor tries to call the parameterless constructor of the base class which doesn't exist, because you implemented another constructor.

You need to create a constructor explicitly and call the correct base class constructor.

class Trapezoid : Quadrilateral
{
    public Trapezoid(int Q, int W, int E, int R, int T)
        : base(Q, W, E, R, T)
    { }
    // ...
}

By the way, the assignments in the constructor are incorrect. You should assign parameters to properties, not the other way. Also, try to find meaningful names for your variables and parameters.

Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
  • Thank you guys so much, I finally solved the problem! And yes, you are right! I really should avoid the meaningless names of my variables and parameters. Sometimes it will confuse me. Lot of thx! ^^ – Yuji Dec 04 '15 at 21:14
  • @Yuji If the answers helped you solve the problem, please [accept](http://stackoverflow.com/help/accepted-answer) one of them. It will show other users that your problem was already solved and no further help is necessary. – Jakub Lortz Dec 04 '15 at 21:24