Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save soheil-moonesi/bce651857b9d26de73620cdaf3d687f6 to your computer and use it in GitHub Desktop.

Select an option

Save soheil-moonesi/bce651857b9d26de73620cdaf3d687f6 to your computer and use it in GitHub Desktop.
using System;
// Define an abstract class called 'Shape'
abstract class Shape {
// An abstract method for calculating area, to be implemented by derived classes.
public abstract double CalculateArea();
// A non-abstract method with a default implementation.
public void DisplayArea() {
Console.WriteLine('Area: ' + CalculateArea()); }
}
// Create a derived class 'Circle' that inherits from 'Shape'
class Circle : Shape {
private double radius;
// Constructor to initialize the radius
public Circle(double r) {
radius = r; }
// Implement the abstract method to calculate the area of a circle.
public override double CalculateArea() {
return Math.PI * radius * radius; }
}
class Program {
static void Main() {
// Create an instance of the 'Circle' class
Circle circle = new Circle(5.0);
// Calculate and display the area of the circle
circle.DisplayArea(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment