In C Write an abstract class called StaffMember that has two
In C#
Write an abstract class called StaffMember that has two auto-implemented string properties called Name and Phone, a two-argument constructor that sets those two property values, a ToString() method that returns a string with those two property values preceded by \"Name: \" in front of the Name value, and \"Phone: \" in front of the Phone value, on separate lines. And create an abstract method called Pay() that returns a decimal. Below the first class, write another class called Volunteer which inherits from StaffMember and has no additional fields. It has a two-argument constructor that sets the Name and Phone properties, a Pay() method that just returns zero, and a ToString() method that returns the word \"Volunteer: \" followed by the ToString() of its base class. And, lastly, write a class called Employee which inherits from StaffMember and has an additional protected decimal variable called salary and decimal property called Salary. The Salary property is not auto-implemented and its value cannot be negative. Employee has a three-argument constructor that sets the Name, Phone and Salary properties. It also has a Pay() method that returns the value of the Salary property, and a ToString() method that returns the word \"Employee: \" followed by the ToString() of its base class, followed on a separate line by the word \"Pay: \", followed by the value of the Salary property formatted as a currency.
Solution
using System;
public class Test
{
public static void Main()
{
Volunteer v = new Volunteer(\"Joseph Black\",\"7686-567-56\");
Console.WriteLine(v.ToString());
Console.WriteLine(\"Pay :$\"+v.Pay());
Employee e = new Employee(\"John Smith\",\"3543-575-77\",6755);
Console.WriteLine(e.ToString());
}
}
abstract class StaffMember
{
public string Phone { get; set; } //auto implemented properties
public string Name { get; set; }
public StaffMember(String name,String phone)
{
Phone = phone;
Name = name;
}
public override string ToString()
{
return \"Name: \" + Name + \"\ \" + \"Phone : \" +Phone;
}
public abstract decimal Pay(); //abstract method with no definition
}
class Volunteer : StaffMember
{
public Volunteer(string name,string phone):base(name,phone) //calling base class constructor
{
}
public override decimal Pay()
{
return 0;
}
public override string ToString()
{
return \"Volunteer \" + base.ToString(); //calling base class ToString() method
}
}
class Employee : StaffMember
{
protected decimal salary;
public decimal Salary
{
get
{
return salary;
}
set
{
if (value < 0)
Console.WriteLine(\"Salary must be positive value.\");
salary = value;
}
}
public Employee(string name,string phone ,decimal salary):base(name,phone)
{
Salary = salary;
}
public override decimal Pay()
{
return Salary;
}
public override string ToString()
{
return \"Employee \" + base.ToString() +\"\ Pay : $\"+ Salary;
}
}
output:

