Answer to Question #178745 in C# for muhammad arfan

Question #178745

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


1
Expert's answer
2021-04-06T12:51:10-0400
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();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS