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:
-
Class Point:
- This is a base class representing a point in 2D space.
- It has protected fields
x
andy
to store the coordinates. - It provides constructors to initialize the point with different combinations of
x
andy
coordinates. - It defines properties
X
andY
for getting and setting the coordinates. - It has a
print()
method, which simply prints the string "Point" to the console.
-
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 ar
field to store the radius. - It provides multiple constructors to initialize the circle with different combinations of
x
,y
, andr
values. - It overrides the
print()
method to print the string "Circle" to the console.
-
Main Method:
- This is the entry point of the program.
- It creates an instance of the
Point
class namedp
and an instance of theCircle
class namedc
. - 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