Objective:
After performing this lab, students will practice:
- Abstract classes and methods
- Use of Interface
Activity 1:
For this activity, we are going to work on a classical multiple inheritance issue known as ‘diamond problem’.
- Create classes: Faculty, Administrator, Teacher, & AdministratorTeacher.
- Faculty inherits Administrator, Teacher, & AdministratorTeacher.
- Add a abstract print() method to Faculty and define it in Administrator, Teacher, & AdministratorTeacher which displays the class name.
- In the driver, create a array of 3 Faculty objects.
- Create one object for each of the remaining three classes as well and assign these three object to the Faculty object array.
- Each faculty member is assigned an id by the university. To represent this, add an get_id() method to Faculty class which returns a unique integer number.
Solution:
using System;
abstract class Faculty
{
public abstract void Print();
public int GetId()
{
return 2023;
}
}
public class Administrator : Faculty
{
public override void Print()
{
Console.WriteLine("Administrator Class");
}
}
public class Teacher : Faculty
{
public override void Print()
{
Console.WriteLine("Teacher Class");
}
}
public class AdministratorTeacher : Faculty
{
public override void Print()
{
Console.WriteLine("AdministratorTeacher Class");
}
}
public class Program
{
public static void Main()
{
Faculty[] objects = new Faculty[3];
Administrator obj1 = new Administrator();
Teacher obj2 = new Teacher();
AdministratorTeacher obj3 = new AdministratorTeacher();
objects[0] = obj1;
objects[1] = obj2;
objects[2] = obj3;
for (int i = 0; i < 3; i++)
{
objects[i].Print();
Console.WriteLine("ID: " + objects[i].GetId());
}
}
}
Activity 2:
Define a pure abstract base interface called IShape. The IShape interface should have the following methods:
double GetArea();
void CalcArea();
Next, define a class named Circle. It should be derived from the IShape interface. It should have the following members:
Private Member Variables:
centerX, a long integer used to hold the x coordinate of the circle’s center.
centerY, a long integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle's radius.
area, a double used to hold the circle's area.
Public Member Functions: constructor—accepts values for centerX, centerY, and radius and set area to zero.
Should call the overridden calcArea function described below.
getCenterX—returns the value in centerX.
getCenterY—returns the value in centerY.
calcArea—calculates the area of the circle(area = 3.14159 * radius * radius) and stores the result in the inherited member area.
Next, define a class named Rectangle. It should be derived from the IShape interface. It should have the following members:
Private Member Variables:
width, a long integer used to hold the width of the rectangle.
length,a long integer used to hold the length of the rectangle.
area, a double used to hold the rectangle's area.
Public Member Functions:
constructor—accepts values for width and length. Should call the overridden calcArea function described below.
getWidth—returns the value in width.
getLength—returns the value in length.
calcArea—calculates the area of the rectangle (area = length * width) and stores the result in the inherited member area.
After you have created these classes, create a driver program that defines a Circle object and a Rectangle object. Demonstrate that each object properly calculates and reports its area.
Solution:
using System;
interface IShape
{
double GetArea();
void CalcArea();
}
class Circle : IShape
{
private int centerX;
private int centerY;
private double radius;
private double area;
public Circle(int x, int y, double r)
{
centerX = x;
centerY = y;
radius = r;
area = 0;
}
public int CenterX
{
get { return centerX; }
}
public int CenterY
{
get { return centerY; }
}
public double GetArea()
{
return area;
}
public void CalcArea()
{
area = 3.14159 * radius * radius;
}
}
class Rectangle : IShape
{
private int width;
private int length;
private double area;
public Rectangle(int w, int l)
{
width = w;
length = l;
area = 0;
}
public int Width
{
get { return width; }
}
public int Length
{
get { return length; }
}
public double GetArea()
{
return area;
}
public void CalcArea()
{
area = length * width;
}
}
class Program
{
static void Main()
{
Circle circle = new Circle(2, 3, 4.5);
circle.CalcArea();
Console.WriteLine("Circle Area: " + circle.GetArea());
Console.WriteLine("Center: (" + circle.CenterX + ", " + circle.CenterY + ")");
Rectangle rectangle = new Rectangle(5, 7);
rectangle.CalcArea();
Console.WriteLine("Rectangle Area: " + rectangle.GetArea());
Console.WriteLine("Width: " + rectangle.Width);
Console.WriteLine("Length: " + rectangle.Length);
}
}