Hi Everyone,
I have this. But get strange output.
THX!!
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 8, 2011, 4:39 AM
Console.Write("{0}: {1}", currentShape.ToString(), shape);
shape is a Shape[] and if you use this in a Console.WriteLine() the latter automatically calls the array's ToString() method which returns the full name of the type.
albert albertPosted Nov 8, 2011, 5:08 AM
Satish BhatPosted Nov 8, 2011, 4:29 AM
If you are talking about getting ZERO value for area/volume for Square/Cube, you are using local class variables for the calculation which is not assigned. If you want to use local variables, in the constructor of both Square and Cube classes, assign the value for the local variable(s).
albert albertPosted Nov 8, 2011, 2:56 AM
shapeInheritanceApp.Shape[]Circle'area is ....
Dipal ChoksiPosted Nov 7, 2011, 6:42 PM
albert albertPosted Nov 7, 2011, 6:14 PM
but why u dont download it?
Because it has so: 8 classes. Then u can see what I mean
this is program:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShapeInHeritanceApp
{
class Program
{
static void Main(string[] args)
{
Shape[] shape = new Shape[4];
shape[0] = new Circle(12, 13, 4);
shape[1] = new Cube(4);
shape[2] = new Triangle(44, 5);
shape[3] = new Square(4);
foreach (Shape currentShape in shape)
{
Console.Write("{0}: {1}", currentShape.ToString(), shape);
if (currentShape is TwoDimensionalShape)
{
TwoDimensionalShape twodimensionShape = (TwoDimensionalShape)currentShape;
Console.Write("{0}'s area is {1}\n", twodimensionShape.getName(), twodimensionShape.Area());
}
if (currentShape is ThreeDimensionalShape)
{
ThreeDimensionalShape threeDimensional = (ThreeDimensionalShape)currentShape;
Console.Write("{0}'s area is {1}\n ", threeDimensional.getName(), threeDimensional.getArea());
Console.Write("{0}'s volume is:{1}\n", threeDimensional.getName(), threeDimensional.getVolume());
}
}
Console.ReadKey();
}//end method
}
}
[/code]
Dipal ChoksiPosted Nov 7, 2011, 5:43 PM
could you post some details about your code and the strange output that you get?