Lab Deliverable 2 Implementing OOP concepts in C#
Part II
1. Write a program to display the name and age of a person. Use a default constructor to assign
values to the name and age variables. Use a parameterized constructor to pass the values of
name and age. Use a single method to display the values from both the constructors.
Solution:
using System;
class Employee
{
private string name;
private int age;
// Default Constructor
public Employee()
{
name="Mark";
age=25;
}
// Parameterized Constructor
public Employee (string varName, int varAge)
{
name = varName;
age = varAge;
}
public void ShowData()
{
[Link]("Name = " + name);
[Link]("Age = " + age);
}
static void Main()
{
Employee objEmpOne = new Employee ();
Employee objEmpTwo = new Employee ("Allen",30);
[Link]();
[Link]();
[Link]();
}
}
The output of the program is as shown in Figure 2.1.
1
Figure 2.1: Output of [Link]
2. Write a program that calculates the square of an integer, say 3 and a double, say 4.2. Use
method overloading to calculate the square of the integer and double values.
Solution:
using System;
class Maths
{
public void DoOverLoad()
{
int intX = 3;
double dblY = 4.2;
[Link]("Square of int value is : "+Square(intX) +
"\n" + "Square of double value is : " + Square(dblY));
}
public int Square(int intY)
{
return intY*intY;
}
public double Square(double dblY)
{
return dblY*dblY;
}
}
public class OverLoad
{
public static void Main()
{
Maths objMaths = new Maths();
[Link]();
}
}
The output of the program is as shown in the Figure 2.2.
Figure 2.2: Output of [Link]
3. Write a program that displays the values of two integers; intX and intY. Assign the values
10 and 20 to intX and intY respectively using a parameterized constructor. Include a
default constructor that does nothing. Instantiate an object of this constructor and print the
values of the variables intX and intY. Overload the operator – to change the sign of the
values (i.e. negative values). Display the values of intX and intY.
Solution:
using System;
class Complex
{
private int intX;
private int intY;
public Complex()
{
}
public Complex(int intI, int intJ)
{
intX = intI;
intY = intJ;
}
public void showXY()
{
[Link]("X = "+intX+" ; Y = "+intY);
}
public static Complex operator -(Complex comC)
{
Complex objCom = new Complex();
[Link] = -[Link];
[Link] = -[Link];
return objCom;
}
}
class MyClient
{
public static void Main()
3
{
Complex objComOne = new Complex(10,20);
[Link]();
Complex objComTwo = new Complex();
[Link]();
objComTwo = -objComOne;
[Link]();
}
}
The output of the program is as shown in the Figure 2.3.
Figure 2.3: Output of [Link]
Do It Yourself
1. Write a program to display the value of two integers using parameterized constructor and a
method. Create two objects of this and pass the sets of values (10, 20) and (30, 40)
respectively. Overload the ‘+’ operator to add the two x values and two y values of these
objects to form a new object (x, y). The answer should be (10+30, 20+40) = (40, 60).
Solution:
using System;
class OperatorOvrldDemo
{
private int intX;
private int intY;
public OperatorOvrldDemo(int intI, int intJ)
{
intX = intI;
intY = intJ;
}
public void ShowXY()
{
[Link](intX+" "+intY);
}
public static OperatorOvrldDemo operator +(OperatorOvrldDemo
objCom1,OperatorOvrldDemo objCom2)
{
OperatorOvrldDemo objCom = new OperatorOvrldDemo();
[Link] = [Link]+[Link];
[Link] = [Link]+[Link];
return objCom;
}
}
class Addition
{
public static void Main()
{
OperatorOvrldDemo objCom1 = new OperatorOvrldDemo(10,20);
[Link]();
OperatorOvrldDemo objCom2 = new OperatorOvrldDemo(30,40);
[Link]();
OperatorOvrldDemo objCom3 = new OperatorOvrldDemo();
objCom3 = objCom1 + objCom2;
[Link]();
[Link]();
}
}
The output of the program is as shown in the Figure 2.6.
Figure 2.6: Output of [Link]