Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Problems:

1. The Engineer Class


The following class implements an Engineer and methods to handle billing for that Engineer.
using System;
class Engineer
{
// constructor
public Engineer(string name, float billingRate)
{
this.name = name;
this.billingRate = billingRate;
}
// figure out the charge based on engineer's rate
public float CalculateCharge(float hours)
{

return(hours * billingRate);
}
// return the name of this type
public string TypeName()
{
return("Engineer");
}
private string name;
protected float billingRate;
}
class Test
{
public static void Main()
{
Engineer engineer = new Engineer("Hank", 21.20F);
Console.WriteLine("Name is: {0}", engineer.TypeName());
}
}

Error:-

wants a main function.

2.what is heap memory?

3.how new keyword is working in this example?


A CivilEngineer is a type of engineer, and therefore can be derived from the Engineer class:
using System;
class Engineer
{
public Engineer(string name, float billingRate)
{
this.name = name;
this.billingRate = billingRate;
}
public float CalculateCharge(float hours)
{
return(hours * billingRate);
}
public string TypeName()
{
return("Engineer");

}
private string name;
protected float billingRate;
}
class CivilEngineer: Engineer
{
public CivilEngineer(string name, float billingRate) :
base(name, billingRate)
{
}
// new function, because it's different than the
// base version
public new float CalculateCharge(float hours)
{
if (hours < 1.0F)
hours = 1.0F; // minimum charge.
return(hours * billingRate);
}
// new function, because it's different than the
// base version
public new string TypeName()
{
return("Civil Engineer");
}
}
class Test
{
public static void Main()
{
Engineer e = new Engineer("George", 15.50F);
CivilEngineer c = new CivilEngineer("Sir John", 40F);
Console.WriteLine("{0} charge = {1}",
e.TypeName(),
e.CalculateCharge(2F));
Console.WriteLine("{0} charge = {1}",
c.TypeName(),
c.CalculateCharge(0.75F));
}
}
Because the CivilEngineer class derives from Engineer, it inherits all the data members of the
class (though the name member can’t be accessed, because it’s private), and it also inherits the
CalculateCharge() member function.
Constructors can’t be inherited, so a separate one is written for CivilEngineer. The constructor
doesn’t have anything special to do, so it calls the constructor for Engineer, using the base syntax. If

the call to the base class constructor was omitted, the compiler would call the base class constructor
with no parameters.
CivilEngineer has a different way to calculate charges; the minimum charge is for 1 hour of time, so
there’s a new version of CalculateCharge().
The example, when run, yields the following output:
Engineer Charge = 31
Civil Engineer Charge = 40

dataset and datareader:=

client callbacks

Sort expression in bound field of grid view

You might also like