Created
March 23, 2024 08:45
-
-
Save soheil-moonesi/bce651857b9d26de73620cdaf3d687f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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