0% found this document useful (0 votes)
18 views6 pages

C# Shape Classes: Circle, Rectangle, Square

The document defines a namespace 'baibuoi8' containing an abstract class 'Shape' and its derived classes 'Circle', 'Rectangle', and 'Square'. Each shape class implements methods to calculate area and perimeter, and includes properties for color and filled status. The 'Program' class demonstrates the creation and output of instances of these shape classes.

Uploaded by

nomie2706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Topics covered

  • GetArea method,
  • Side property,
  • Radius property,
  • Constructor overloading,
  • Data members,
  • Internal classes,
  • Shape properties,
  • Inheritance,
  • C# applications,
  • Perimeter calculation
0% found this document useful (0 votes)
18 views6 pages

C# Shape Classes: Circle, Rectangle, Square

The document defines a namespace 'baibuoi8' containing an abstract class 'Shape' and its derived classes 'Circle', 'Rectangle', and 'Square'. Each shape class implements methods to calculate area and perimeter, and includes properties for color and filled status. The 'Program' class demonstrates the creation and output of instances of these shape classes.

Uploaded by

nomie2706
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Topics covered

  • GetArea method,
  • Side property,
  • Radius property,
  • Constructor overloading,
  • Data members,
  • Internal classes,
  • Shape properties,
  • Inheritance,
  • C# applications,
  • Perimeter calculation

Cau 1:

namespace baibuoi8
{
abstract class Shape
{
protected string color;
protected bool filled;
public Shape() { }
public Shape(string color, bool filled)
{
this.color = color;
this.filled = filled;
}

public string GetColor() => color;


public void SetColor(string color) => this.color = color;
public bool IsFilled() => this.filled;
public void SetFilled(bool filled) => this.filled = filled;
public abstract double GetArea();
public abstract double GetPerimeter();
public override string ToString()
{
return $"Shape[color={color}, filled={filled}]";

}
namespace baibuoi8
{
internal class Circle:Shape
{
private double radius;
public Circle() { }
public Circle(double radius)
{
this.radius = radius;
}
public Circle(double radius, string color, bool filled)
: base(color, filled)
{
this.radius = radius;
}

public double GetRadius() => radius;


public void SetRadius(double radius) => this.radius = radius;
public override double GetArea() => Math.PI * radius * radius;
public override double GetPerimeter()=> 2*Math.PI*radius;
public override string ToString()
{
return $"HinhTron [BanKinh= {radius}, DienTich= {GetArea()},
ChuVi={GetPerimeter()}]";

namespace baibuoi8
{
internal class Rectangle : Shape
{
protected double width;
protected double length;
public Rectangle() { }
public Rectangle(double width, double length)
{
this.width = width;
this.length = length;
}
public Rectangle(double width, double length, string color, bool filled)
: base(color, filled)
{
this.width=width;
this.length=length;
}
public double GetWidth()=> width;
public void SetWidth( double width)=> this.width = width;
public double GetLength()=> length;
public void SetLength( double length) => this.length = length;
public override double GetArea()=> width * length;
public override double GetPerimeter() => 2 * (width + length);
public override string ToString()
{
return $"HinhChuNhat [ChieuRong= {width}, Chieudai= {length}, DienTich=
{GetArea()}, ChuVi= {GetPerimeter()}]";
}

}
}

namespace baibuoi8
{
internal class Square : Rectangle
{
public Square() { }
public Square(double side)
: base(side, side) { }
public Square(double side, string color, bool filled)
:base(side, side, color, filled) { }
public double GetSide() => width;
public void SetSide(double side)
{
width = length = side;
}
public void SetWidth(double side)
{
SetSide(side);
}
public void SetLength(double side)
{
SetSide(side);
}
public override string ToString()
{
return $"HinhVuong [Canh= {GetSide()}, DienTich= {GetArea()}, ChuVi=
{GetPerimeter()}]";
}
}
}

namespace baibuoi8
{
internal class Program
{
static void Main(string[] args)
{

Circle hinhtron = new Circle(5.0, "Red", true);


Console.WriteLine(hinhtron.ToString());
Rectangle hcn = new Rectangle(4.0, 6.0, "Blue", false);
Console.WriteLine(hcn.ToString());
Square hinhvuong = new Square(3.5, "Green", true);
Console.WriteLine(hinhvuong.ToString());
Console.WriteLine();
}
}
}
Câu 2:

You might also like