Sanjay has written the following code. The purpose is to create a Bird class and implement function overloading. The code has some errors. Debug the code.
private class Bird
{
public string Name;
public double Maxheight;
public Bird() //Default Constructor
{
this.Name = Mountain Eagle;
this.Maxheight = “500”; // TODO: Add constructor logic here
}
public Bird(string birdname, double max_ht) //Overloaded Constructor
{
this.Name = “Another Bird”;
this.Maxheight = null;
}
public void fly()
{
Console.WriteLine(“this.Name is flying at altitude this.Maxheight”);
}
public void fly(string AtHeight)
{
if(AtHeight <= this.Maxheight)
Console.WriteLine(this.Name + " flying at " + AtHeight.ToString());
elseif
Console.WriteLine(this.Name cannot fly at this height);
}
}
The code in the Main function is as follows:
Bird b = new Bird(“Eagle” , double.Parse(“200”));
b.fly();
b.fly(double.Parse(“300”))
using System;
namespace Test
{
class Bird
{
private string Name;
private double Maxheight;
public Bird() //Default Constructor
{
this.Name = "Mountain Eagle";
this.Maxheight = 500;
}
public Bird(string birdname, double max_ht) //Overloaded Constructor
{
this.Name = birdname;
this.Maxheight = max_ht;
}
public void fly()
{
Console.WriteLine(this.Name + " is flying at altitude "+ this.Maxheight);
}
public void fly(double AtHeight)
{
if(AtHeight <= this.Maxheight)
Console.WriteLine(this.Name + " flying at " + AtHeight.ToString());
else
Console.WriteLine(this.Name + " cannot fly at this height");
}
}
class BirdTest
{
static void Main()
{
Bird b = new Bird("Eagle" , 200);
b.fly();
b.fly(300);
}
}
}
Comments
Leave a comment