Question

Like any other C# method, constructors can be overloaded. Show how to create two public constructors-one...

Like any other C# method, constructors can be overloaded. Show how to create two public constructors-one with no parameters, and one with a parameter called sal-that both set the value for the property Salary within the following class:
class Employee
{
public double Salary;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Employee
{
private double salary;

//default constructor with no args
public Employee()
{
//initialize salary with 0
salary = 0;
}
//Overload Constructor
/**
* Taking one arg double type to set salary
*/
public Employee(double sal)
{
salary = sal;
}

//properties
public double Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}
}


//Main program to test class

using System;

class Program
{
static void Main(string[] args)
{
//default constructor
Employee employee1 = new Employee();
//Overload constructor
Employee employee2 = new Employee(200.56);
//print info using property
Console.WriteLine("The salary: $" + employee1.Salary);
Console.WriteLine("The salary: $" + employee2.Salary);
//to hold output screen
Console.ReadLine();
}
}

//output

//if you need any help regarding this solution ............ please leave a comment ........... thanks

Add a comment
Know the answer?
Add Answer to:
Like any other C# method, constructors can be overloaded. Show how to create two public constructors-one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Like any other C# method, constructors can be overloaded. Show how to create two public constructors-one...

    Like any other C# method, constructors can be overloaded. Show how to create two public constructors-one with no parameters, and one with a parameter called sal-that both set the value for the property Salary within the following class: class Employee { public double Salary; }

  • C+ Create an application named CarDemo that declares at least two Car objects and demonstrates how...

    C+ Create an application named CarDemo that declares at least two Car objects and demonstrates how they can be incremented using an overloaded ++ operator. Create a Car class that contains the following properties: Model - The car model (as a string) Mpg The car's miles per gallon (as a double) Include two overloaded constructors. One accepts parameters for the model and miles per gallon; the other accepts a model and sets the miles per gallon to 20. Overload a...

  • Problem 3: (15 pts) Modify the Patient class with two overloaded constructors: A new default constructor...

    Problem 3: (15 pts) Modify the Patient class with two overloaded constructors: A new default constructor which initializes the data items for a patient object using code within the constructor (use any data). A second constructor with parameters to pass all the data items into the object at the time of instantiation. Create a test main class which instantiates two objects. Instantiate the first object using the default constructor and the second object using the constructor with the parameters.

  • C++ Create an application named CarDemo that declares at least two Car objects and demonstrates how...

    C++ Create an application named CarDemo that declares at least two Car objects and demonstrates how they can be incremented using an overloaded ++ operator. Create a Car class that contains the following properties: Model - The car model (as a string) Mpg The car's miles per gallon (as a double) Include two overloaded constructors. One accepts parameters for the model and miles per gallon; the other accepts a model and sets the miles per gallon to 20. Overload a...

  • Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...

    Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors • Be able to create accessor and mutator methods • Be able to create toString methods • Be able to refer to both global and local variables with the same name Introduction Until now, you have just simply been able to refer to any variable by its name. This works for our purposes, but what happens when there are multiple variables with the same name?...

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • Create another Java class named EmployeeMain within the same project, which includes the main method. a....

    Create another Java class named EmployeeMain within the same project, which includes the main method. a. Include another class named Employee in the same Java file. This class has the following instance variables and instance methods. Define all the instance/static variables with private access modifier and constructors, instance/static methods with public access modifier. Instance Variables empID: int employeeName: String basicSalary: double Constructor Set the value for empID. Instance Methods Get and set methods for all instance variables. displayEmployee: Display the...

  • Design a blueprint class that records the payment and purchase made in a CashRegister. (Write the...

    Design a blueprint class that records the payment and purchase made in a CashRegister. (Write the code for CashRegister - JAVA) The requirements are as follows: A) The class/object model will keep track of payments and purchases, both of which can be of type double. To do this, have the two instance variables called payment and purchase. B) The class/object model will provide the values of the payments and purchases to the client program. To do this, have proper getters...

  • Write four overloaded methods called randomize. Each method will return a random number based on the...

    Write four overloaded methods called randomize. Each method will return a random number based on the parameters that it receives: randomize() - Returns a random int between min and max inclusive. Must have two int parameters. randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter. randomize() - Returns a random double between min and max. Must have two double parameters. randomize() - Returns a random double between 0 and max. Must have one...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT