I want to hide the parent class method for the the child class object. My Circle object display two methods to me when I going to access that methods:
Here objShape is the object of the Shape class which is the abstract class.
I want to hide method which is display on 2nd image.
Here Circle class extends the Oval class and Oval class extends the Shape class.
Shape class coding
abstract public class Shape
{
public abstract double Area(double dNum1 = 0.0, double dNum2 = 0.0);
public abstract double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0);
}
Oval class Coding
class Oval : Shape
{
public override double Area(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dAns = Math.PI * dNum1 * dNum2;
return dAns;
}
public override double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dTmp1, dTmp2, dAns;
dTmp1 = 2 * (dNum1 * dNum1);
dTmp2 = 2 * (dNum2 * dNum2);
dAns = (Math.PI / 2) * Math.Sqrt(dTmp1 + dTmp2);
return dAns;
}
}
Circle Class Code
class Circle : Oval
{
public double Area(double dNum1)
{
double dResult = base.Area(dNum1, dNum1);
return dResult;
}
public double Perimeter(double dNum1)
{
double dAns = base.Perimeter(dNum1, dNum1);
return dAns;
}
}
So, Here how can I hide the direct access of the Oval class method for the Circle class object. In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object.
In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object.
Then you shouldn't make Circle derive from Oval, basically. Don't forget that it would be perfectly acceptable to have a variable of type Oval which has a value at execution time referring to an instance of Circle:
Oval x = new Circle();
Console.WriteLine(x.Area(10, 20));
Fundamentally, any operation that makes sense for the base class should make sense for the derived class as well, in a sensible inheritance hierarchy.
If you want to keep your inheritance hierarchy, you should consider a redesign such that the radius etc are part of the state of the object, instead of being passed in as parameters. For example:
public abstract class Shape
{
public abstract double Area { get; }
public abstract double Perimeter { get; }
}
public class Oval
{
private double Width { get; }
private double Height { get; }
public override double Area { get { ... } };
public override double Perimeter { get { ... } };
public Oval(double width, double height)
{
Width = width;
Height = height;
}
}
public class Circle
{
public double Radius { get { return Height; } }
public Circle(double radius) : base(radius)
{
}
}
Just have Circle inherit shape as opposed to oval if you dont want the oval methods exposed by circle.
You can achieve this by changing your access specifier of Shape and Oval class methods to protected.
abstract public class Shape
{
protected abstract double Area(double dNum1 = 0.0, double dNum2 = 0.0);
protected abstract double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0);
}
and
class Oval : Shape
{
protected override double Area(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dAns = Math.PI * dNum1 * dNum2;
return dAns;
}
protected override double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dTmp1, dTmp2, dAns;
dTmp1 = 2 * (dNum1 * dNum1);
dTmp2 = 2 * (dNum2 * dNum2);
dAns = (Math.PI / 2) * Math.Sqrt(dTmp1 + dTmp2);
return dAns;
}
}
I think this is what you are looking for..
Related
I have only just started learning C# so excuse this basic question. I am experimenting with C# inheritance and want to inherit the properties from Shape class into the Rectangle class. The Rectangle class below gives me an error:
"There is no argument that corresponds to the required formal
parameter 'height' of 'Shape.Shape(double.double)'
Is anyone able to tell me why this is happening?
class Shape
{
public double Height { get; set; }
public double Width { get; set; }
public Shape (double height, double width)
{
Height = height;
Width = width;
}
public double calculateArea()
{
double Area = (Height * Width);
return Area;
}
}
class Rectangle : Shape
{
public Rectangle(double height, double width)
{
Height = height;
Width = width;
}
static void Main(string[] args)
{
Rectangle rectangle = new Rectangle(15, 19);
double areaOfRectangle = rectangle.calculateArea();
Console.WriteLine(areaOfRectangle);
}
}
In c#, the base keyword is used to access base class members such as properties, methods, etc. in the derived class. so you have to act like below:
public Rectangle(double height, double width) : base(height, width) { ... }
for learning more about base you can follow this. good luck.
I've been working on an assignment for class, beginners C#. I've hit a point where I do not know what to do next. This is the question and it involves argument/parameters...
Define the output of the "area" property calculation such that a user can initialize an instance of the "Circle" class by setting the argument/parameternamed"radius" (in the Constructor) and subsequently call a method named "ShowArea" to display the area of the new circle instance using the formula: (where r = radius, A = area, π= pi)
This is what I have so far:
namespace IndividualAssignment2
{
public class Shape
{
public virtual int area { get; set; }
}
public class Circle : Shape
{
double radius;
public override int area { get; set; }
double ShowArea = 3.14 * Math.Pow(radius,2);
}
public sealed class Square : Shape
{
int height;
}
}
How would I implement this into my code? My double ShowArea is incorrect because radius is underlined. I think understanding this question would help with that issue. Thank you.
If I'm understanding you correctly, ShowArea is a method and not a field. Which means your Circle class should be something like:
public class Circle : Shape
{
double _radius;
// Constructor for the Circle that has radius as a parameter
public Circle(double radius)
{
_radius = radius;
}
// Method that returns the area of the circle using radius value from constructor
public double ShowArea()
{
return Math.Pi * Math.Pow(_radius, 2.0);
}
}
Your class design must be reviewed.
public abstract class Shape
{
public abstract double Area { get; }
}
public class Circle : Shape
{
public Circle(double radius)
{
Radius = radius;
}
private double Radius { get; set; }
public override double Area => 3.14 * Math.Pow(Radius, 2);
}
public class Square : Shape
{
public Square(double edge)
{
Edge = edge;
}
private double Edge { get; set; }
public override double Area => Math.Pow(Edge, 2);
}
Your declaration of the method ShowArea is not correct. You are declaring a field instead.
You should read up more on methods. You were also tasked to declare a constructor with a parameter to set the radius, which I don't find it in your code.
I have a task :
There is an hierarchy: "Shape" - base class, "Triangle", "Circle", "Rectangle" - derived classes of "Shape", "IsoscelesTriangle" - derived class of "Triangle", "Square" - derived class of "Rectangle". "Shape" has methods: GetArea() - returns the area of a geometric shape, GetName() - returns the name of a geometric shape. For each derived class area and name can be determined. Console program demonstrates the principle of polymorphism using the output messages of name and area.
so at this moment my main looks like this:
Problem1_1.Shape triangle1 = new Problem1_1.Triangle("triangle1", 5, 10);
double triangle1Area = triangle1.GetArea();
string triangle1Name = triangle1.GetName();
Console.WriteLine(triangle1Name, triangle1Area);
Problem1_1.Shape isoTriangle1 = new Problem1_1.IsoscelesTriangle("iso triangle", 2, 10);
double isoTriangle1Area = isoTriangle1.GetArea();
string isoTriangle1Name = isoTriangle1.GetName();
Console.WriteLine(isoTriangle1Name, isoTriangle1Area);
Problem1_1.Shape circle1 = new Problem1_1.Circle("circle1", 5);
double circle1Area = circle1.GetArea();
string circle1Name = circle1.GetName();
Console.WriteLine(circle1Name, circle1Area);
Problem1_1.Shape rect1 = new Problem1_1.Rectangle("rectangle1", 2, 10);
double rect1Area = rect1.GetArea();
string rect1Name = rect1.GetName();
Console.WriteLine(rect1Name, rect1Area);
Problem1_1.Shape square1 = new Problem1_1.Square("sq1", 2, 3);
double square1Area = square1.GetArea();
string square1Name = square1.GetName();
Console.WriteLine(square1Name, square1Area);
my custom classes namespace looks like this:
namespace Problem1_1
{
public abstract class Shape
{
protected Shape(string name)
{
Name = name;
}
public string Name { get; }
public virtual string GetName()
{
return "Shape: " + Name;
}
public abstract double GetArea();
}
public class Triangle : Shape
{
private double side;
private double height;
public Triangle(string name, double side, double height) : base (name)
{
this.side = side;
this.height = height;
}
public override double GetArea()
{
double area = (side * height) / 2;
return area;
}
}
public class Circle : Shape
{
private double radius;
public Circle(string name, double radius) : base (name)
{
this.radius = radius;
}
public override double GetArea()
{
double area = radius * radius * Math.PI;
return area;
}
}
public class Rectangle : Shape
{
private double side1;
private double side2;
public Rectangle(string name, double side1, double side2) : base (name)
{
this.side1 = side1;
this.side2 = side2;
}
public override double GetArea()
{
double area = side1 * side2;
return area;
}
}
public class IsoscelesTriangle : Triangle
{
public IsoscelesTriangle(string name, double side, double height) : base (name, side, height) { }
public override double GetArea()
{
return base.GetArea();
}
}
public class Square : Rectangle
{
private double side1;
public Square(string name, double side1, double side2) : base(name, side1, side2)
{
this.side1 = side1;
}
public override double GetArea()
{
double area = side1 * side1;
return area;
}
}
}
so what r the issues.
First, my console message doesn't give me the area. It gives me only the name. What should I change to get both?
Second, I don't know how to handle the Square class. If it is derived from rectangle how can I get only 1 value to calculate area. If I set it like this :
public Square(string name, double side1) : base(name, side1)
I'm getting an error.
As far as I know there is no overload of Conosole.WriteLine(); which allows you to print two or more varialble like that, you need to use formatting like,
Console.WriteLine("{0} : {1}", triangleName, triangleArea);
Console.WriteLine($"{triangle1Name} : {triangle1Area}");
And can you explain second question a little more?
Why you pass two values for the side of a Square in its constructor? You just need one side to define a Square.
Problem1_1.Shape square1 = new Problem1_1.Square("sq1", 2);
double square1Area = square1.GetArea();
string square1Name = square1.GetName();
Console.WriteLine($"Name={square1Name}, Area={square1Area}");
Then change the constructor of Square to be
public class Square : Rectangle
{
private double side1;
public Square(string name, double side1) : base(name, side1, side1)
{
this.side1 = side1;
}
}
As you can see, you could also remove the override for GetArea and let the base class of Square (Rectangle) return its calculations.
I suggest also to make the two side variables inside the Rectangle class protected so you can also remove the variable side1 inside the Square class
public class Rectangle : Shape
{
protected double side1;
protected double side2;
...
}
Finally, for the output problem, you just need to use Console.WriteLine correctly as shown above.
public class Circle : Shape
{
private float perimeter, area;
public override float getPerimeter(float Dimension)
{
perimeter = (float)(2 * Math.PI) * (Dimension / 2);
return perimeter;
}
public override float getArea(float Dimension)
{
area = (float)Math.PI * ((Dimension / 2) * (Dimension / 2));
return area;
}
}
public class Square : Shape
{
private float perimeter, area;
public override float getPerimeter(float Dimension)
{
perimeter = (float)(Dimension * 4);
return perimeter;
}
public override float getArea(float Dimension)
{
area = (float)(Dimension * Dimension);
return area;
}
}
This is only the abstract class but there seems to be a problem here when run the program: the Area and Perimeter output are both the same, and I don't know why.
Here is my Circle class code.
class Circle
{
private double radius;
private double area;
public Circle(double radius)
{
this.radius = radius;
}
public double Area
{
set { area = Math.PI * Math.Pow(radius, 2); }
get { return area; }
}
}
This is test code.
Circle circle1 = new Circle(3);
MessageBox.Show("Circle 1 Area: " + circle1.Area);
So for some reason, when I use the MessageBox.Show(), it seems to give me values of zero instead. I gave the circle a value of 3 so shouldn't my constructor set the value of the radius that?
Because you haven't ever called the setter on Area. Perhaps you want something like this instead?
class Circle
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double Area
{
get { return Math.PI * Math.Pow(radius, 2); }
}
}
This will compute the Area every time it is requested.
Your Area property should be:
public double Area
{
get { return Math.PI * Math.Pow(radius, 2); }
}
and you don't need the area field.
I'm not sure you need a set in this instance (You didn't use it)
try
get { return Math.PI * Math.Pow(radius, 2); }