user1905501
user1905501

Reputation: 35

C# Inheritance hierarchy for class Quadrilateral?

I have the following code but it does not give me any output. I am totally lost about what do next. Need the following output

Coordinates of Quadrilateral are: ( 1.1, 1.2 ), ( 6.6, 2.8 ), ( 6.2, 9.9 ), ( 2.2, 7.4 )

  using System;

  class testquadrilaterals 
  {
static void Main() 
{
    quadrilateral obj1 = new quadrilateral(1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4);
    Console.ReadLine();
}
  }

  class point 
  {
private double x;
private double y;

public point(double x_coord, double y_coord)
{
    x = x_coord;
    y = y_coord;
}

public double X { get { return x; } set { x = value; } }
public double Y { get { return y; } set { y = value; } }

public void printcoord()
{
    Console.WriteLine("(" + X + ", " + Y + ")");
}
  }

  class quadrilateral
  {
point p1, p2, p3, p4;

public quadrilateral(double x1, double y1, double x2, double y2,
    double x3, double y3, double x4, double y4)
{
    p1 = new point(x1, y1);
    p2 = new point(x2, y2);
    p3 = new point(x3, y3);
    p4 = new point(x4, y4);
}

public point P1 { get { return p1; } set { p1 = value; } }
public point P2 { get { return p2; } set { p2 = value; } }
public point P3 { get { return p3; } set { p3 = value; } }
public point P4 { get { return p4; } set { p4 = value; } }

public string printcoordinates() 
{
    return "Coordinates of Quadrilateral are " + p1.ToString() + " " + p2.ToString() + " " + p3.ToString() + " ";
}

}

Upvotes: 0

Views: 937

Answers (3)

Reed Copsey
Reed Copsey

Reputation: 564631

You need to call your print method:

class testquadrilaterals 
{
    static void Main() 
    {
        quadrilateral obj1 = new quadrilateral(1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4);
        Console.WriteLine(obj1.printcoordinates()); // Call your print method
        Console.ReadLine();
    }
}

Upvotes: 2

NSGaga
NSGaga

Reputation: 14312

Your point doesn't seem to have ToString overriden - I think - you have a print but not connected.

It seems what you want is define ToString which calls that print - if I"m getting it right, the code

I'd do it this way instead - for a point:

public override string ToString()
{
    return "(" + X + ", " + Y + ")";
}

...and for the quadri-class...

public override string ToString()
{
    return "Coordinates of Quadrilateral are " + p1.ToString() + " " + p2.ToString() + " " + p3.ToString() + " ";
}

call it like:

Console.Write(obj1.ToString());

Upvotes: 0

BlackICE
BlackICE

Reputation: 8926

Fix your printcoordinates and call it:

public void printcoordinates() 
{
    console.writeline("Coordinates of Quadrilateral are " + p1.ToString() + " " + p2.ToString() + " " + p3.ToString() + " ");

}


class testquadrilaterals 
{
    static void Main() 
    {
        quadrilateral obj1 = new quadrilateral(1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4);
        obj1.printcoordinates(); // Call your print method
        Console.ReadLine();
    }
}

Upvotes: 1

Related Questions