View Problem
Implement Inheritance Heirarchy
Implement a Shape abstract class which will form the base of an inheritance hierarchy that models 2D geometric shapes. It will have:
* A non-mutable
* A
* A
Submit a new solution for scala, csharp, or cpp
There are 12 other solutions in additional languages (clojure, fantom, fsharp, groovy ...)
* A non-mutable
'name' property or data member set by derived or descendant classes at construction time
* A
'area' method intended to be overridden by derived or descendant classes ( double precision floating point return value)
* A
'print' method (also for overriding) will display the shape's name, area, and all shape-specific values
Two derived or descendant classes will be created:
* Circle -> Constructor requires a 'radius' argument, and a 'circumference' method to be implemented
* Rectangle -> Constructor requires 'length' and 'breadth' arguments, and a 'perimeter' method to be implemented
Instantiate an object of each class, and invoke each objects 'print' method to show relevant details.
scala
abstract class Shape (val name: String) {
def area : Double
def print()
}
class Circle (val radius: Double) extends Shape("Circle") {
def area = Math.Pi * radius * radius
def circumference = 2 * Math.Pi * radius
def print() {
println("I'm a " + name + " with")
printf(" * radius = %.2f\n", radius)
printf(" * area = %.2f\n", area)
printf(" * circumference = %.2f\n\n", circumference)
}
}
class Rectangle (val length: Double, val breadth: Double) extends Shape("Rectangle") {
def area = length * breadth
def perimeter = 2 * (length + breadth)
def print() {
println("I'm a " + name + " with")
printf(" * length = %.2f\n", length)
printf(" * breadth = %.2f\n", breadth)
printf(" * area = %.2f\n", area)
printf(" * perimeter = %.2f\n\n", perimeter)
}
}
val shapes = List(new Circle(5.4), new Rectangle(7.8, 6.5))
shapes foreach (_.print)
def area : Double
def print()
}
class Circle (val radius: Double) extends Shape("Circle") {
def area = Math.Pi * radius * radius
def circumference = 2 * Math.Pi * radius
def print() {
println("I'm a " + name + " with")
printf(" * radius = %.2f\n", radius)
printf(" * area = %.2f\n", area)
printf(" * circumference = %.2f\n\n", circumference)
}
}
class Rectangle (val length: Double, val breadth: Double) extends Shape("Rectangle") {
def area = length * breadth
def perimeter = 2 * (length + breadth)
def print() {
println("I'm a " + name + " with")
printf(" * length = %.2f\n", length)
printf(" * breadth = %.2f\n", breadth)
printf(" * area = %.2f\n", area)
printf(" * perimeter = %.2f\n\n", perimeter)
}
}
val shapes = List(new Circle(5.4), new Rectangle(7.8, 6.5))
shapes foreach (_.print)
csharp
// While abstract classes do exist in C#, it is most common to use
// an interface in this type of situation.
// It is a common idiom to prefix interface names with an I
public interface IShape {
string Name { get; }
double Area { get; }
void Print();
}
public class Circle : IShape {
private double Radius { get; set; }
public Circle(double radius) {
Name = "Circle";
Radius = radius;
}
public string Name { get; private set; }
public double Area {
get {
return Math.PI * Radius * Radius;
}
}
public double Circumference {
get {
return Math.PI * (Radius + Radius);
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Circumference: {2}\n Radius: {3}",
this.Name,
this.Area,
this.Circumference,
this.Radius
);
}
}
public class Rectangle : IShape {
private double Length { get; set; }
private double Breadth { get; set; }
public Rectangle(double length, double breadth) {
Name = "Rectangle";
Length = length;
Breadth = breadth;
}
public string Name { get; private set; }
public double Area {
get {
return Length * Breadth;
}
}
public double Perimeter {
get {
return (Length * 2) + (Breadth * 2 );
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Perimeter: {2}\n Length: {3}\n Breadth: {4}",
this.Name,
this.Area,
this.Perimeter,
this.Length,
this.Breadth
);
}
}
// Driver
public class InheritanceHeirarchy {
public static void _Main() {
var c = new Circle(2.1);
c.Print();
Console.WriteLine();
var r = new Rectangle(2.2, 3.3);
r.Print();
}
}
// an interface in this type of situation.
// It is a common idiom to prefix interface names with an I
public interface IShape {
string Name { get; }
double Area { get; }
void Print();
}
public class Circle : IShape {
private double Radius { get; set; }
public Circle(double radius) {
Name = "Circle";
Radius = radius;
}
public string Name { get; private set; }
public double Area {
get {
return Math.PI * Radius * Radius;
}
}
public double Circumference {
get {
return Math.PI * (Radius + Radius);
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Circumference: {2}\n Radius: {3}",
this.Name,
this.Area,
this.Circumference,
this.Radius
);
}
}
public class Rectangle : IShape {
private double Length { get; set; }
private double Breadth { get; set; }
public Rectangle(double length, double breadth) {
Name = "Rectangle";
Length = length;
Breadth = breadth;
}
public string Name { get; private set; }
public double Area {
get {
return Length * Breadth;
}
}
public double Perimeter {
get {
return (Length * 2) + (Breadth * 2 );
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Perimeter: {2}\n Length: {3}\n Breadth: {4}",
this.Name,
this.Area,
this.Perimeter,
this.Length,
this.Breadth
);
}
}
// Driver
public class InheritanceHeirarchy {
public static void _Main() {
var c = new Circle(2.1);
c.Print();
Console.WriteLine();
var r = new Rectangle(2.2, 3.3);
r.Print();
}
}
cpp
#include <string>
#include <iostream>
using namespace std;
static const double PI = 3.141592;
class Shape {
protected:
string name_;
public:
Shape(const string& name) : name_(name) { }
virtual double area() const = 0;
virtual void print() const = 0;
};
class Circle : public Shape {
double radius_;
public:
Circle(double radius) : Shape("circle"), radius_(radius) { }
double area() const {
return PI * radius_ * radius_;
}
void print() const {
cout << "A " << name_ << " with radius " << radius_ << ", area "
<< area() << " and circumference " << circumference() << "."
<< endl;
}
double circumference() const {
return 2 * PI * radius_;
}
};
class Rectangle : public Shape {
double length_;
double breadth_;
public:
Rectangle(double length, double breadth) :
Shape("rectangle"), length_(length), breadth_(breadth) { }
double area() const {
return length_ * breadth_;
}
void print() const {
cout << "A " << name_ << " with length " << length_ << ", breadth "
<< breadth_ << ", area " << area() << " and perimeter "
<< perimeter() << "." << endl;
}
double perimeter() const {
return 2 * length_ + 2 * breadth_;
}
};
int main(int argc, char *argv[])
{
Circle circle(4);
circle.print();
Rectangle rectangle(2, 5.5);
rectangle.print();
}
#include <iostream>
using namespace std;
static const double PI = 3.141592;
class Shape {
protected:
string name_;
public:
Shape(const string& name) : name_(name) { }
virtual double area() const = 0;
virtual void print() const = 0;
};
class Circle : public Shape {
double radius_;
public:
Circle(double radius) : Shape("circle"), radius_(radius) { }
double area() const {
return PI * radius_ * radius_;
}
void print() const {
cout << "A " << name_ << " with radius " << radius_ << ", area "
<< area() << " and circumference " << circumference() << "."
<< endl;
}
double circumference() const {
return 2 * PI * radius_;
}
};
class Rectangle : public Shape {
double length_;
double breadth_;
public:
Rectangle(double length, double breadth) :
Shape("rectangle"), length_(length), breadth_(breadth) { }
double area() const {
return length_ * breadth_;
}
void print() const {
cout << "A " << name_ << " with length " << length_ << ", breadth "
<< breadth_ << ", area " << area() << " and perimeter "
<< perimeter() << "." << endl;
}
double perimeter() const {
return 2 * length_ + 2 * breadth_;
}
};
int main(int argc, char *argv[])
{
Circle circle(4);
circle.print();
Rectangle rectangle(2, 5.5);
rectangle.print();
}
Submit a new solution for scala, csharp, or cpp
There are 12 other solutions in additional languages (clojure, fantom, fsharp, groovy ...)




