How to resolve the algorithm Polymorphism step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Polymorphism step by step in the C# programming language

Table of Contents

Problem Statement

Create two classes   Point(x,y)   and   Circle(x,y,r)   with a polymorphic function print, accessors for (x,y,r), copy constructor, assignment and destructor and every possible default constructors

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Polymorphism step by step in the C# programming language

The provided C# code demonstrates object-oriented programming principles, including inheritance, polymorphism, and method overriding. Here's a breakdown of the code:

  1. Class Point:

    • This is a base class representing a point in 2D space.
    • It has protected fields x and y to store the coordinates.
    • It provides constructors to initialize the point with different combinations of x and y coordinates.
    • It defines properties X and Y for getting and setting the coordinates.
    • It has a print() method, which simply prints the string "Point" to the console.
  2. Class Circle:

    • This is a derived class that represents a circle.
    • It inherits from the Point class and extends it to represent a circle by adding a r field to store the radius.
    • It provides multiple constructors to initialize the circle with different combinations of x, y, and r values.
    • It overrides the print() method to print the string "Circle" to the console.
  3. Main Method:

    • This is the entry point of the program.
    • It creates an instance of the Point class named p and an instance of the Circle class named c.
    • It calls the print() method on both objects.

When the program runs, it will print the following output to the console:

Point
Circle

This output demonstrates the concept of polymorphism, where objects of different types can respond to the same method call (in this case, the print() method) in different ways based on their specific implementations.

Source code in the csharp programming language

using System;
class Point
{
  protected int x, y;
  public Point() : this(0) {}
  public Point(int x) : this(x,0) {}
  public Point(int x, int y) { this.x = x; this.y = y; }
  public int X { get { return x; } set { x = value; } }
  public int Y { get { return y; } set { y = value; } }
  public virtual void print() { System.Console.WriteLine("Point"); }
}

public class Circle : Point
{
  private int r;
  public Circle(Point p) : this(p,0) { }
  public Circle(Point p, int r) : base(p) { this.r = r; }
  public Circle() : this(0) { }
  public Circle(int x) : this(x,0) { }
  public Circle(int x, int y) : this(x,y,0) { }
  public Circle(int x, int y, int r) : base(x,y) { this.r = r; }
  public int R { get { return r; } set { r = value; } }
  public override void print() { System.Console.WriteLine("Circle"); }
 
  public static void main(String args[])
  {
    Point p = new Point();
    Point c = new Circle();
    p.print();
    c.print();
  }
}


  

You may also check:How to resolve the algorithm Fork step by step in the Delphi programming language
You may also check:How to resolve the algorithm Date format step by step in the VB-DOS programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the Julia programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Miranda programming language