Reputation: 535
I have following code where I'm getting error while compiling in C# visual Studio 2015.
class Oval:Shape
{
private double major_axis, minor_axis;
public Oval(double Major_Axis, double Minor_Axis)
{
major_axis = Major_Axis;
minor_axis = Minor_Axis;
} //Constructor
}
class Circle:Oval
{
private double radius;
public Circle(double Circle_Radius) // Getting Error on this line
{
radius = Circle_Radius;
} //constructor
}
Upvotes: 36
Views: 133837
Reputation: 111
Uou can also add an empty constructor in Base class.
public Shape() {}
or
public Oval() {}
This will not enforce you using 2 arguments constructor.
Upvotes: 1
Reputation: 858
Fixing your bug:
The error occurs due to the lack of a parameterless constructor (or your lack of using the base()
method in your constructor (just like user3185569
had said)
Fixing your code:
It clearly seems you are lacking some basics in .NET so I've decided to give a re-writing to your code with the following things in mind:
a. Conventions
There are some rules about common conventions that should apply to your code.
Members usually begin with either m
or _
and then the memberName
(camel casing).
Properties are usually written regularly as PropertyName
and same applies to methods.
Parameters and variables are simply camel cased like parameterName
b. Access Modifiers
I don't know the use of your Oval and circle but I assume you'd want to access them outside of Oval
and Circle
.
I think it would be the best to reference you to here to read some more about the topic: https://msdn.microsoft.com/en-us/library/ms173121.aspx
I've re-written your code to include all those tips (and also fix your issue)
public class Oval:Shape
{
//Constructor
public Oval(double majorAxis, double minorAxis)
{
MajorAxis=majorAxis;
MinorAxis=minorAxis;
}
protected double MajorAxis{ get; set; }
protected double MinorAxis{ get; set; }
}
public class Circle:Oval
{
//Constructor
public Circle(double radius): base(radius,radius)
{
radius = Circle_Radius;
}
public double Radius
{
get
{
return MajorAxis;
}
set
{
MajorAxis = value;
MinorAxis = value;
}
}
}
Upvotes: 45
Reputation: 535
class Oval : Shape
{
#region Constants
private const double dblPIE = 3.14d;
#endregion
#region Member_Variables
private double m_dblMajorRad = 0.0d;
private double m_dblMinorRad = 0.0d;
#endregion
#region Constructors
/// <summary>
/// default constructor
/// </summary>
public Oval()
{
}
/// <summary>
/// initializes Major Radius and Minor Radius of oval
/// </summary>
/// <param name="dblOvalMajorRad"></param>
/// <param name="dblOvalMinorRad"></param>
public Oval(double dblOvalMajorRad, double dblOvalMinorRad)
{
m_dblMajorRad = dblOvalMajorRad;
m_dblMinorRad = dblOvalMinorRad;
}
#endregion
}
class Circle : Oval
{
#region Constants
private const double dblPIE = 3.14d;
#endregion
#region Member_Variables
private double m_dblRadius = 0.0d;
#endregion
#region Constructors
/// <summary>
/// initializes radius
/// </summary>
/// <param name="dblRadius"></param>
public Circle(double dblRadius)
{
m_dblRadius = dblRadius;
}
#endregion
}
Upvotes: -1
Reputation: 31
You need to update your base class constructor to the following:
class Oval:Shape
{
private double major_axis, minor_axis;
public Oval(double Major_Axis, double Minor_Axis)
{
major_axis = Major_Axis;
minor_axis = Minor_Axis;
} //Constructor
}
class Circle:Oval
{
private double radius;
public Circle(double Circle_Radius):base(12.2,12.2) // pass value to base class constructor
{
radius = Circle_Radius;
} //constructor
}~```
Upvotes: 3
Reputation: 30052
Since Circle
inherits from Oval
, when you create a Circle
the default constructor for Oval
is called in your case. Since that constructor accepts 2 parameters, you need to explicitly call the constructor and provide them:
class Circle : Oval
{
private double radius;
public Circle(double Circle_Radius) : base(0, 0) // change to whatever values
{
radius = Circle_Radius;
}
}
So A Circle
is an Oval
, so it has major_axis
and minor_axis
values. You just didn't provide them as they are required values to create an Oval
.
Of course you can add a default public constructor for Oval
with no parameters, but that makes you create a Oval with no major_axis
and minor_axis
and both seems required by the only constructor in the current status of your code. So, you need to rethink how to design your classes.
Upvotes: 23