Reputation: 35
C# Inhertance. I's trying to inherit class quadrilateral to class sqaure. I am unable to figure this out and I got the following error and I dont know what it means. Field square.p1 is never assigned to and will always have its default value null?
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);
obj1.printcoordinates();
Console.WriteLine("\n");
square obj2 = new square( 4.0, 0.0, 8.0, 0.0, 8.0, 4.0, 4.0, 4.0 );
obj2.printsquare();
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 override string ToString()
{
return ("(" + 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 void printcoordinates()
{
Console.WriteLine("Coordinates of Quadrilateral: \n" + p1.ToString() + " " + p2.ToString() + " " + p3.ToString() + " " + p4.ToString() + " ");
}
}
class square : quadrilateral
{
point p1, p2, p3, p4;
public square(double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4) : base (x1, y1, x2, y2, x3, y3, x4, y4)
{ }
public void printsquare()
{
Console.WriteLine("Coordinates of Square: \n" + p1.ToString() + " " + p2.ToString() + " " + p3.ToString() + " " + p4.ToString() + " ");
Console.WriteLine("Area: " + area());
// Console.WriteLine("Sides: " + sides());
}
public double area()
{
return Math.Abs(p1.X - p2.X) * Math.Abs(p1.Y - p3.Y);
}
// public double sides() { }
}
Upvotes: 0
Views: 1214
Reputation: 166
In Square::area(), the p1s and p2s are referring to those defined within the square class, not those in the quadrilateral class. You have both square::p1, square::p2, etc. and quadrilateral ::p1, quadrilateral::p2, etc. The square p1s and p2s are never assigned to. To have your square class use the quadrilateral class' p1s and p2s, don't redefine them in square.
Upvotes: 0
Reputation: 755179
In this case the class square
defines 4 fields which it never sets: p1, p2, p3 and p4
class square : quadrilateral {
point p1, p2, p3, p4;
...
}
These fields are private and never set hence they are essentially useless as is. The C# compiler is alerting you to this because this is usually an indication of a bug.
In this case it looks like you may have intended for square
to simply use the values in quadrilateral
. The fields have the same name and type and there is an inheritance relationship between the two types. If that's the case you should delete the fields in square
and change the accessibility to protected
in quadrilateral
class quadrilateral {
protected point p1, p2, p3, p4;
...
}
This will allow square
to access the values
Upvotes: 2
Reputation: 22255
If you are inheriting from quadrilaterial you don't need to define the variables p1/p2/p3/p4 again in square, you will inherit them from the base class.
Upvotes: 0