0% found this document useful (0 votes)
4 views4 pages

Practical(4 6)

The document contains three C# programs demonstrating object-oriented programming concepts. The first program defines a 'Distance' class to calculate and display the sum of two distances. The second program defines a 'salary' class to calculate and display an employee's salary with default values, and the third program creates a 'TrafficSignal' class with delegate methods to represent traffic signals and invoke them using an array of delegates.

Uploaded by

Nitin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Practical(4 6)

The document contains three C# programs demonstrating object-oriented programming concepts. The first program defines a 'Distance' class to calculate and display the sum of two distances. The second program defines a 'salary' class to calculate and display an employee's salary with default values, and the third program creates a 'TrafficSignal' class with delegate methods to represent traffic signals and invoke them using an array of delegates.

Uploaded by

Nitin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

4. Create a program to declare class ,,Distance" have data members dist l ,dist2 ,dist3.

Initialize the
two data members using constructor and store their addition in third data member using function
and display addition.

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PRACTICAL4
{
class Distance
{
private int dis1;
private int dis2;
private int dis3;

public Distance(int a, int b)


{
dis1 = a;
dis2 = b;
}
public void calc()
{
dis3 = dis1 + dis2;
}

public void display()


{
Console.WriteLine("the sum is {0}", dis3);
}
}

class Program
{
static void Main(string[] args)
{
Distance d = new Distance(10, 20);
d.calc();
d.display();
Console.ReadKey();

}
}
}
5. Define a class ,,salary" which will contain member variable Basic, TA, DA, HRA. Write a program
using Constructor with default values for D and HRA and calculate the salary of employee.

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace practical5
{
class salary
{
private int ta, da, hra, sal;
public salary(int t, int d = 1500, int h = 6000)
{
ta = t;
da = d;
hra = h;
}

public void calculate()


{
sal = 35000 + ta + da + hra;
}
public void display()
{
Console.WriteLine("total salary {0}", sal);
}
}

class Program
{
static void Main(string[] args)
{
salary s = new salary(1000);
s.calculate();
s.display();
Console.ReadKey();

}
}
}
6. Create a program to create a delegate called TrafficDel and a class called TrafficSignal with the
following delegate methods. Public static void Yellow() { Console.WriteLine("Yellow Light
Signal To Get Ready");} Public static void Green() { Console.WriteLine("Green Light Signal To
Go"); } Public static void Red() { Console.WriteLine("Red Light Signal To Stop"); } Also
include a method IdentifySignal() to initialize an array of delegate with the above methods and a
method show() to invoke members of the above array.

using System;

// Delegate definition
public delegate void TrafficDel();

// TrafficSignal class
public class TrafficSignal
{
// Delegate methods
public static void Yellow()
{
Console.WriteLine("Yellow Light Signal To Get Ready");
}

public static void Green()


{
Console.WriteLine("Green Light Signal To Go");
}

public static void Red()


{
Console.WriteLine("Red Light Signal To Stop");
}

// Method to initialize an array of delegate with the above methods


public TrafficDel[] IdentifySignal()
{
TrafficDel[] signals = { Yellow, Green, Red };
return signals;
}

// Method to invoke members of the delegate array


public void Show(TrafficDel[] signals)
{
foreach (var signal in signals)
{
signal.Invoke();
}
}
}
// Main class
class Program
{
static void Main()
{
// Create an instance of TrafficSignal
TrafficSignal trafficSignal = new TrafficSignal();

// Initialize an array of delegate with the above methods


TrafficDel[] signals = trafficSignal.IdentifySignal();

// Invoke members of the delegate array


trafficSignal.Show(signals);
}
}

Output:

Yellow Light Signal To Get Ready


Green Light Signal To Go
Red Light Signal To Stop

You might also like