Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

How to implement inheritance in c#

LABSHEET

Q1. Given the structure of two classes. Implement the given inheritance and test the class.
Methods and its return types are specified in the diagram. toString() method both in the
class will display the value of its data members.

a. In the given class write down Properties for accessing both X and Y and test it in
main method

Sol:

using System;

public class Point2D


{
private float X = 0.0F;
private float Y = 0.0F;

public Point2D(float x, float y)


{
X = x;
Y = y;
}

public float getX() => X;

public void setX(float value) => X = value;

public float getY() => Y;

public void setY(float value) => Y = value;

public void setXY(float x, float y)


{
X = x;
Y = y;
}

public float[] getXY() => new[] { X, Y };

public override string ToString() => $"X: {X}, Y: {Y}";


}

public class Point3D : Point2D


{
private float Z = 0.0F;

public Point3D(float x, float y, float z) : base(x, y)


{
Z = z;
}
public override string ToString() => base.ToString() + $", Z: {Z}";
}

public class Program


{
static void Main()
{
var point2d = new Point2D(5, 10);
Console.WriteLine(point2d); // Output: X: 5, Y: 10

var point3d = new Point3D(5, 10, 15);


Console.WriteLine(point3d); // Output: X: 5, Y: 10 ,Z :15
}
}

Q2. Create a class Circle which represent a real circle object , with the data
Radius – double type
Color – string type
 Include three types of Constructors in the class. Create three different
object with the help of three constructors defined in the class
 Add methods getRadius() and getColor() to return the radius and color
respectively
 Add methods setRadius(double d) and setColor(String c) to set the
value of radius and color respectively.
 Add a method getArea() will calculate and return the area of the
Circle.
 Test the class in main method.
A subclass called Cylinder is derived from the superclass Circle. Cylinder class having
the
following details
 height – double type
 Cylinder() – default constructor
 Cylinder(radius ) – will initialize the radius with some value and all
other data with default value
 Cylinder(radius, height ) - will initialize the radius and height with
some value and color with default value
 Cylinder(radius, height, Color) will initialize the radius , height and
color.
 getHeight() and setHeight(height) to return and assign value to height
respectively
 getVolume() – calculate and return the volume of the cylinder
(volume= area of circle* height)

The subclass Cylinder invokes the superclass' constructors (via base() and base(radius)
etc) and
inherits the variables and methods from the superclass Circle.

Implement the Inheritance and test the class.

Sol:
using System;

public class Circle


{
private double radius;
private string color;

public Circle()
{
radius = 0.0;
color = "red";
}

public Circle(double radius)


{
this.radius = radius;
color = "red";
}

public Circle(double radius, string color)


{
this.radius = radius;
this.color = color;
}

public double getRadius()


{
return radius;
}

public string getColor()


{
return color;
}

public void setRadius(double d)


{
radius = d;
}
public void setColor(string c)
{
color = c;
}

public double getArea()


{
return Math.PI * radius * radius;
}
}

public class Cylinder : Circle


{
private double height;

public Cylinder() : base()


{
height = 0.0;
}

public Cylinder(double radius) : base(radius)


{
height = 0.0;
}

public Cylinder(double radius, double height) : base(radius)


{
this.height = height;
}

public Cylinder(double radius, double height, string color) : base(radius, color)


{
this.height = height;
}

public double getHeight()


{
return height;
}

public void setHeight(double height)


{
this.height = height;
}

public double getVolume()


{
return getArea() * height;
}
}

class Program
{
static void Main()
{
Circle circle1 = new Circle();
Circle circle2 = new Circle(5.0);
Circle circle3 = new Circle(3.0, "blue");

Console.WriteLine("Circle 1 - Radius: " + circle1.getRadius() + ", Color: " + circle1.getColor() + ", Area: " +
circle1.getArea());
Console.WriteLine("Circle 2 - Radius: " + circle2.getRadius() + ", Color: " + circle2.getColor() + ", Area: " +
circle2.getArea());
Console.WriteLine("Circle 3 - Radius: " + circle3.getRadius() + ", Color: " + circle3.getColor() + ", Area: " +
circle3.getArea());

Cylinder cylinder1 = new Cylinder();


Cylinder cylinder2 = new Cylinder(5.0);
Cylinder cylinder3 = new Cylinder(3.0, 7.0);
Cylinder cylinder4 = new Cylinder(4.0, 6.0, "green");

Console.WriteLine("\nCylinder 1 - Radius: " + cylinder1.getRadius() + ", Height: " + cylinder1.getHeight() + ",


Color: " + cylinder1.getColor() + ", Volume: " + cylinder1.getVolume());
Console.WriteLine("Cylinder 2 - Radius: " + cylinder2.getRadius() + ", Height: " + cylinder2.getHeight() + ",
Color: " + cylinder2.getColor() + ", Volume: " + cylinder2.getVolume());
Console.WriteLine("Cylinder 3 - Radius: " + cylinder3.getRadius() + ", Height: " + cylinder3.getHeight() + ",
Color: " + cylinder3.getColor() + ", Volume: " + cylinder3.getVolume());
Console.WriteLine("Cylinder 4 - Radius: " + cylinder4.getRadius() + ", Height: " + cylinder4.getHeight() + ",
Color: " + cylinder4.getColor() + ", Volume: " + cylinder4.getVolume());
}
}

Q3. Create a Student class which will have two private arrays to store full student names
in a class and to store the cgpa of each student respectively.
Class has a private data Count which will have the number of students in the class.
- Write down the proper constructor methods ‘
- Include the Indexer for accessing the above-mentioned private arrays in the class.
- Include methods to add new student name and marks to corresponding arrays.
- Include methods to display the name and cgpa of each student in a neat manner.
- Write down the property for count data.
- Test all methods in the class.

Sol:
using System;

public class Student


{
private string[] studentNames;
private double[] cgpa;
private int count;

public Student(int capacity)


{
studentNames = new string[capacity];
cgpa = new double[capacity];
count = 0;
}

public string this[int index]


{
get { return studentNames[index]; }
set { studentNames[index] = value; }
}

public double GetCGPA(int index)


{
return cgpa[index];
}

public void AddStudent(string name, double cgpa)


{
if (count < studentNames.Length)
{
studentNames[count] = name;
this.cgpa[count] = cgpa;
count++;
}
else
{
Console.WriteLine("Array is full. Cannot add more students.");
}
}

public void DisplayStudents()


{
Console.WriteLine("Student Details:");
for (int i = 0; i < count; i++)
{
Console.WriteLine("Name: {0}, CGPA: {1}", studentNames[i], cgpa[i]);
}
}

public int Count


{
get { return count; }
}
}

class Program
{
static void Main()
{
Student studentClass = new Student(5);

studentClass.AddStudent("Alice", 3.5);
studentClass.AddStudent("Bob", 3.8);
studentClass.AddStudent("Charlie", 3.2);

Console.WriteLine("Total Students: " + studentClass.Count);


studentClass.DisplayStudents();

Console.WriteLine("\nStudent at index 1: " + studentClass[1]);


Console.WriteLine("CGPA of student at index 1: " + studentClass.GetCGPA(1));
}
}

You might also like