C#. Windows Forms. The implementation of the concepts of “inheritance” and “polymorphism” in the object-oriented programming through the creation of two classes. The interfaces ICloneable and IEquatable.




The implementation of the concepts of “inheritance” and “polymorphism” in the object-oriented programming through the creation of two classes. The interfaces ICloneable and IEquatable.

 

Contents


Search other websites:

Task

In the developed classes, you need realize the concepts of OOP such as “inheritance” and “polymorphism”.

You need to develop the base class (ancestor class) and inherit from it. The part of functionality should be realized by using the properties.

Each class should include the implementation of at least:

  • two constructors;
  • two methods;
  • two properties;
  • two interfaces.

It is needed to redefine the operations on the class objects.

Create a class, which defines the circle on the plane. Using the developed classes find the area of a circle. Also you need realize the interfaces ICloneable and IEquatable.

 


The mathematical formulation of the problem

In the Figure 1 is shown the circle on the plane.

 

Circle on the plane

Figure 1. Circle on the plane

As you can see from Figure 1, to designate the circle in the plane must have:

  • coordinates (x, y) of point of center the circle;
  • radius of circle R.

The area of a circle is calculated by formula:

Circle area formulawhere S – the area of circle; R – radius of circle; π– a constant that is equal 3.1415.

 


Instructions

1. Creating a new project

Run Microsoft Visual Studio. Create the new project as Windows Forms Application. An example of creating of the new project is described here. Automatically the new form will be created.

 

2. Placing of the controls on the form

Place on the form following controls (Figure 2):

  • two controls of Label type. Automatically two objects will be created. The names of objects are label1 and label2;
  • control of type TextBox. The new object with name textBox1 will be created;
  • control of type Button. The new object with name button1 will be created.

C#. Windows Forms Application. Placing the controls on the form

Figure 2. Placing the controls on the form

 

3. Setting up the controls

Set up the following properties of controls (Figure 3):

  • in the control label1 property Text = “R = “;
  • in the control label2 property Text = “S = “;
  • in the control button1 property Text = “Calculate“;
  • in the control Form1 property Text = “Area of circle“.

C#. Windows Forms Application. The application form after setting of controls

Figure 3. The application form after setting of controls

After this, it is needed create the classes.

 

4. Classes, that are needed to solve the task

According to the task, we need to create two classes:

  • the Point class, which displays the point on the plane. This is the base class;
  • the Circle class, which displays a circle. This class must inherit data and methods of class Point (according to task).

The Point class is a base class. The Circle class is the inherited class.

So, there is a hierarchy with two classes. The scheme of hierarchy of the classes is shown in Figure 4.

C#. The hierarchy of classes according to the task

Figure 4. The hierarchy of classes according to the task

In the process solving the problem the classes will be modified. Let’s define the class Point, which includes data only. In C#, the listing of Point class will be following:

class Point
{
    // class fields are declared as protected
    protected float x; // coordinate x
    protected float y; // coordinate y
}

Into the Point class, the class data are declared as protected. It means, that these data are visible in the derived classes and invisible outside (they hide for access through the object of a class).

To have the access to the data, is needed properties and special methods. In the same way the class Circle is created. Now, the listing of class Circle is follows:

class Circle:Point // inherits data from the Point class
{
    // class fields
    private float R; // radius of circle
}

In the definition above, the beginning of string

class Circle:Point

means, that class Circle inherits data and methods of Point class.

So, at present, class Circle includes three fields:

  • field x from inherited class Point;
  • field y from inherited class Point;
  • field R, which is internal field of class Circle.

 

5. Realization of constructors of Point class.

According to the task, every class must include two constructors at least.

Successively, in the text of class Point need add two constructors. First constructor without parameters, initializes data by zero values.

Second constructor is parameterized. This constructor takes two input parameters. It sets a new values for internal variables x and y. Below is the listing of two constructors:

// constructor #1 - without parameters
public Point()
{
    x = 0f; // zeroing the values of coordinates
    y = 0f;
}

// constructor #2 - with two parameters
public Point(float nx, float ny)
{
    x = nx;
    y = ny;
}

It should be noted that constructors have access type “public“, since they are called from the outside for creating a class object.

 

6. Implementation of the constructors of class Circle.

In the same way you need to add the two constructors into the class Circle.

// constructor #1 - initializes fields by zero values
public Circle()
{
    x = 0f; // 0f - means, that this value is float type
    y = 0f;
    R = 0f;
}

// constructor #2 - with three parameters
public Circle(float nx, float ny, float nR)
{
    x = nx;
    y = ny;
    R = nR;
}

 

7. Implementation of methods of class Point.

According to the task, every class must include two methods. Let’s develop the methods, that will do the following:

  • method GetPoint(), that returns point of type Point;
  • method SetXY(), that sets a new values of internal variables of class Point.

Methods have the “public” access type.



Listing of methods is the following.

public Point GetPoint()
{
    Point p; // is declared the variable (object), for which the memory is not allocated yet
    p = new Point(x,y); // the memory is allocated, the new instance of class is created
    return p;
}

// method, which sets a new values of internal variables
public void SetXY(float nx, float ny)
{
    x = nx;
    y = ny;
}

 

8. Implementation of methods of class Circle.

In the same way are created methods of class Circle. In the Circle class are realized two methods. First method GetCircle() returns an object of type Circle with the memory allocation.

Second method SetXYR() sets a new values of internal variables. This method takes 3 input parameters (coordinates x, y and radius R of circle). In this method is demonstrated how to use the keyword “this” for access to the fields of class. In this case, the names of local variables-parameters are the same as the names of class fields, and they hide their.

Listing of methods is the following:

// methods of class Circle acoording to the task
// method, that takes a new class instance of type Circle
public Circle GetCircle()
{
    Circle c;
    c = new Circle(x, y, R);
    return c;
}

// method, that set a new values in the internal fields of class Circle
public void SetXYR(float x, float y, float R)
{
    // example of using the keyword "this"
    // in the internal variable x is set the value of local variable x
    this.x = x;
    // in the same way are filled the internal variables x and R of class Circle
    this.y = y;
    this.R = R;
}

 

9. Examples of using the constructors and methods of classes Point and Circle.

We give some examples of constructors and methods of classes Point and Circle.

Demonstration of methods GetPoint() and SetXY():

// class Point
Point p = new Point();
Point p1 = null, p2 = null;
p1 = new Point(4.5f, 3.2f); // constructor with two parameters
p2 = p1.GetPoint();
p2.SetXY(3.3f, -2.8f); // set a new values

// class Circle
Circle c = new Circle(); // constructor without parameters
Circle c1, c2;
c1 = new Circle(3.3f, -2.8f, 12f); // memory allocation
c2 = c1.GetCircle(); // method GetCircle()
c2.SetXYR(3.8f, -1.9f, 8f); // method SetXYR()

 

10. The source code of the classes.

At present, classes Point and Circle are following:

// class Point
class Point
{
    // fields of class are declared as protected
    protected float x; // coordinate x
    protected float y; // coordinate y

    // constructor #1 - without parameters
    public Point()
    {
        x = 0f;
        y = 0f;
    }

    // constructor #2 - with two parameters
    public Point(float nx, float ny)
    {
        x = nx;
        y = ny;
    }

    public Point GetPoint()
    {
        Point p;
        p = new Point(x,y);
        return p;
    }

    // method, which sets a new values of internal variables
    public void SetXY(float nx, float ny)
    {
        x = nx;
        y = ny;
    }
}

// class Circle
class Circle:Point // inherit data of class Point
{
    // class fields
    private float R; // radius of a circle

    // constructor #1 - initializes the fields by zero values
    public Circle()
    {
        x = 0f;
        y = 0f;
        R = 0f;
    }

    // constructor #2 - with three parameters
    public Circle(float nx, float ny, float nR)
    {
        x = nx;
        y = ny;
        R = nR;
    }

    // methods of Circle class
    // method, which returns a new class instance of type Circle
    public Circle GetCircle()
    {
        Circle c; // the new object of Circle type
        c = new Circle(x, y, R); // allocation the memory
        return c;
    }

    // method, which sets new values in the internal fields of class Circle
    public void SetXYR(float x, float y, float R)
    {
        // example of using the keyword "this"
        this.x = x;
        this.y = y;
        this.R = R;
    }
}

 

11. Realization of properties of Point class.

The next step you need to create two properties of class Point.

The first property returns or sets the value of coordinate x of point. The second property returns of sets the value of coordinate y of point.

The name of first property is XX.

Listing of properties XX and YY is the following:

// the property, that sets/returns an x coordinate
public float XX
{
    get { return x; }
    set { x = value; }
}

// the property, that sets/returns an y coordinate
public float YY
{
    get { return y; }
    set { y = value; }
}

After this definition, it is possible to use the properties XX and YY. The following listing demonstrates how to use the properties XX and YY.

...

Point p = new Point(5.2f, 6.35f); // creating the object of Point type
float x,y;

x = p.XX; // x = 5.2
y = p.YY; // y = 6.35

// redefine the values
p.XX = 2.33f;
p.YY = -3.8f;

x = p.XX; // x = 2.33
y = p.YY; // y = -3.8

...

 

12. Developing of class Circle properties.

By analogically to the Point class, the properties of Circle class are declared. The first property gets/sets the value of a radius R. The second property gets/sets the value of a point, that is a center of a circle (class Point).

// properties of the Circle class
// the property, that returns the value R of a radius of circle
public float RR
{
    get { return R; }
    set { R = value; }
}

// property, that returns the value of point of type Point
public Point PP
{
    get
    {
        Point p;
        p = new Point(x, y);
        return p;
    }

    set
    {
        x = value.XX;
        y = value.YY;
    }
}

In the listing below is described the example of using the properties PP and RR.

// creating the object, where the value of fields is
// x = 5.5; y = -2.3; R = 12.8
Circle c = new Circle(5.5f, -2.3f, 12.8f);
float x, y, R;

// access to the property PP of class Circle
x = c.PP.XX; // x = 5.5
y = c.PP.YY; // y = -2.3

// access to the property RR
R = c.RR; // R = 12.8
c.RR = -20.85f;

 

13. Developing the methods of interface ICloneable.

If realize the interface ICloneable, then it is possible to provide a deep copy of objects.

In the example below is realized the shallow copy of objects:

Point p1 = new Point(5.3f, -6.8f);
Point p2;

// shallow copy
p2 = p1; // both objects point to the same memory location

The interface ICloneable provides one method for realization. This method is called Clone(). This method must be realized in the classes Point and Circle. Realization of these methods works at the similar principle.

First of all you need to change the title of class Point. Instead of text

class Point

you need to type

class Point:ICloneable

It means, that you need realize the only one method Clone().

Listing of method is following:

// method Clone() in the class Point
public object Clone()
{
    Point p = new Point(x, y);
    return p;
}

Since, the Circle class inherit the Point class, which inherit the ICloneable interface, then you can realize method Clone() of Circle class (but not necessarily).

// realization of Icloneable interface in the Circle class
public object Clone()
{
    Circle c = new Circle();
    return c;
}

An example of using the Clone() method.

Point p1 = new Point(5.2f, 6.35f); // creating the object
Point p2;

// deep copying
p2 = (Point)p1.Clone();// for p2 is allocated the memory separately

// Circle class
Circle c1 = new Circle(3.2f, -8.3f, 11f);
Circle c2 = null;
c2 = (Circle)c1.Clone(); // for c2 is allocated the memory separately

 

14. Developing the methods of interface IEquatable.

The IEquatable interface is realized in that classes, where you need to determine the order of comparing two objects. In this interface is defined only one method Equals(). This method returns true, if the value of object, that calls, is equal to value of other object, which is a parameter in the method Equals().

For the Point class you need realize the method Equals(), which compares two values of variables x and y.

Listing of method Equals() for Point class is following:

// method Equals() from the IEquatable interface
public bool Equals(Point p2)
{
    if ((this.x == p2.x) && (this.y == p2.y))
    {
        return true;
    }
    else
        return false;
}

Listing of method Equals() for class Circle is following:

// method Equals() of class Circle
public bool Equals(Circle c2)
{
    if ((this.x == c2.x) && (this.y == c2.y) && (this.R == c2.R))
        return true;
    else
        return false;
}

In the program the Equals() method can be used as follows:

// demonstration the Equals() method for Point class
Point p1 = new Point(3f, -5.5f);
Point p2;
bool f;

p2 = (Point)p1.Clone();
f = p1.Equals(p2); // f = true

// demonstration the Equals() method for Circle class
Circle c1 = new Circle(3.2f, -8.3f, 11f);
Circle c2 = new Circle(3.2f, -8.3f, 11f);
f = c1.Equals(c2); // f = true

 

15. Realization of method of calculation of the circle area in the class Circle.

Name of method – GetSquare(). To calculate the area of a circle, need to use only the radius R. Radius is got with the internal variable R. The result of calculation is the float type.

In the text of class Circle you need add the following method:

// Method, that calculates the area of circle
public float GetSquare()
{
    const float pi = 3.1415f;
    float s;
    s = pi * R * R;
    return s;
}

 

16. The short code of class Point.

So, the classes are created. The short listing of class Point is the following (the details are described in the previous paragraphs):

class Point:ICloneable,IEquatable<Point>
{
    // fields of class which are declared as protected
    protected float x; // coordinate x
    protected float y; // coordinate y

    // constructor #1 - without parameters
    public Point() { ... }

    // constructor #2 - with two parameters
    public Point(float nx, float ny) { ... }

    // method, which returns a point
    public Point GetPoint() { ... }

    // method, that sets a new values of internal variables
    public void SetXY(float nx, float ny) { ... }

    // property, that gets/sets the x coordinate
    public float XX
    {
        get { ... }
        set { ... }
    }

    // property, that gets/sets the y coordinate
    public float YY
    {
        get { ... }
        set { ... }
    }

    // realization of method Clone() in the ICloneable interface
    public object Clone() { ... }

    // realization of method Equals() in the IEquatable interface
    public bool Equals(Point p2) { ... }
}

 

17. Short listing of the Circle class.

In the listing below is shown class Circle, in which details are hidden.

The realization of methods, properties and constructors is described in the previous paragraphs.

class Circle:Point // inherits data of Point class
{
    // the fields of class
    private float R; // radius of circle

    // constructor #1 - initializes fields by zero values
    public Circle() { ... }

    // constructor #2 - with three parameters
    public Circle(float nx, float ny, float n) { ... }

    // methods of the Circle class according to the task
    // method, that takes a new instance of the class Circle
    public Circle GetCircle() { ... }

    // method, that sets a new values into the internal fields
    public void SetXYR(float x, float y, float R) { ... }

    // properties of the Circle class
    // property, that returns the value of the radius of circle
    public float RR
    {
        get { ... }
        set { ... }
    }

    // property, that returns the value of Point type
    public Point PP
    {
        get { ... }
        set { ... }
    }

    // realization of ICloneable interface
    public object Clone() { ... }

    // method Equals() from IEquatable interface
    public bool Equals(Circle c2) { ... }

    // method, that calculates the area of circle
    public float GetSquare() { ... }
}

 

18. Programming of the event handler of clicking on the button “Calculate“.

Listing of the event handler of clicking on the button “Calculate” is following:

private void button1_Click(object sender, EventArgs e)
{
    Circle c = new Circle();
    float s;

    // fill the value R
    c.RR = (float)Double.Parse(textBox1.Text);

    // calling the method of defining the area of circle
    s = c.GetSquare();
    label2.Text = "S = " + s.ToString();
}

The result of program work is shown in Figure 5.

C#. Windows Forms Application. Executing the program

Figure 5. Executing the program

 


Related topics