Answer to Question #2550 in C# for ch azan
Question #2550
Define a console application with a class ‘Employee’ with variables as emp_id (int), name (String) and Grade (double).Define five different constructors for this class to initialize the variable values. Define Finalize method to display the Grade value. Define a for loop in Main for 4 iterations and inside for loop create Employee object. After the for loop call Garbage Collector.
Expert's answer
using System;
namespace Employee
{
class Employee
{
& // Member variables of the class
& private int emp_id;
& private string name;
& private double grade;
& // The constructors
& // The default constructor
& public Employee()
& {
// Some default values
emp_id = 1;
grade = 1;
name = "Some name";
& }
& // Three different constructors depending on the amount of parameters
& public Employee(int pEmpId)
& {
emp_id = pEmpId;
& }
& public Employee(int pEmpId, string pName)
: this(pEmpId)
& {
name = pName;
& }
& public Employee(int pEmpId, string pName, double pGrade)
: this(pEmpId, pName)
& {
grade = pGrade;
& }
& // Finalize method (destructor)
& ~Employee()
& {
Console.WriteLine("Finalizator: grade = {0}", grade);
& }
}
}
namespace Employee
{
class Employee
{
& // Member variables of the class
& private int emp_id;
& private string name;
& private double grade;
& // The constructors
& // The default constructor
& public Employee()
& {
// Some default values
emp_id = 1;
grade = 1;
name = "Some name";
& }
& // Three different constructors depending on the amount of parameters
& public Employee(int pEmpId)
& {
emp_id = pEmpId;
& }
& public Employee(int pEmpId, string pName)
: this(pEmpId)
& {
name = pName;
& }
& public Employee(int pEmpId, string pName, double pGrade)
: this(pEmpId, pName)
& {
grade = pGrade;
& }
& // Finalize method (destructor)
& ~Employee()
& {
Console.WriteLine("Finalizator: grade = {0}", grade);
& }
}
}
Need a fast expert's response?
Submit orderand get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Comments
Leave a comment