Create a class Employee with attributes cnic, name and salary. Make all fields private and expose them via proper getter and setter methods. Choose appropriate data type for each property yourself. In Employee class, make following constructors: a) Constructor that takes CNIC and name only. It shall intialize both fields. b) Fully parameterized constructor, that means, that takes all parameters and initialize associated instance attributes. From this constructor, call the 2nd construction to intialize CNIC and name instance attributes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Q178745
{
class Program
{
class Employee {
private int cnic;
private string name;
private double salary;
public int Cnic
{
get { return cnic; }
set { cnic = value; }
}
//a) Constructor that takes CNIC and name only. It shall intialize both fields.
public Employee(int cnic, string name) {
this.cnic = cnic;
this.name = name;
}
//b) Fully parameterized constructor, that means, that takes all parameters and initialize associated instance attributes.
//From this constructor, call the 2nd construction to intialize CNIC and name instance attributes
public Employee(int cnic, string name, double salary): this(cnic, name)
{
this.salary = salary;
}
public string Name
{
get { return name; }
set { name = value; }
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
}
static void Main(string[] args)
{
Employee employee1=new Employee(4545,"Mike");
Console.WriteLine("Cnic: {0}", employee1.Cnic);
Console.WriteLine("Name: {0}", employee1.Name);
Console.WriteLine("Salary: {0}", employee1.Salary);
Employee employee2 = new Employee(454213, "Mary",5652);
Console.WriteLine("\nCnic: {0}", employee2.Cnic);
Console.WriteLine("Name: {0}", employee2.Name);
Console.WriteLine("Salary: {0}", employee2.Salary);
Console.ReadLine();
}
}
}
Comments
Leave a comment