What’s wrong with this set of code? I have errors and I can’t find out what’s wrong with it. Below is the logic for what I am trying to do. Can someone help me solve this?
CustNum says that it’s hiding an inherited member and it’s saying an error for the Convert.ToInt32 line and the new Customer() method does not work.
using System;
public class DebugEight01
{
public static void main()
{
Customer aRegularCustomer = new Customer();
FrequentCustomer aFrequentCustomer = new Customer(); // I have an error here.
aRegularCustomer.CustNum = 2514;
aRegularCustomer.CustBalance = 765.00;
aFrequentCustomer.CustNum = 5719;
aFrequentCustomer.CustBalance = 2500.00;
aFrequentCustomer.DiscountRate = 0.15; //15 %
Console.WriteLine("\naRegularCustomer #{0} owes {1}",
aRegularCustomer.CustNum,
aRegularCustomer.CustBalance = Convert.ToInt32; // I have an error here
Console.WriteLine("\naFrequentCustomer #{0} would owe {1} without the discount",
aFrequentCustomer.CustNum,
aFrequentCustomer.CustBalance.ToString("C2"));
double newBal = (1 - aFrequentCustomer.DiscountRate) *
aFrequentCustomer.CustBalance;
Console.WriteLine("...with {0} discount, customer owes {1}",
aFrequentCustomer.DiscountRate.ToString("P"), newBal.ToString("C"));
}
}
public class Customer
{
private int custNum;
private double custBalance;
public int CustNum
{
get
{
return custNum;
}
set
{
custNum = value;
}
}
public double CustBalance
{
get
{
return custBalance;
}
set
{
CustBalance = value;
}
}
}
class FrequentCustomer : Customer
{
private double discountRate;
public double DiscountRate
{
get
{
return discountRate;
}
set
{
discountRate = value;
}
}
public int CustNum // I have an error here, it's hiding inherited member?
{
get
{
return base.CustNum;
}
set
{
CustNum = value;
}
}
}
When you are working with classes and inheritance, there are some rules that are applied when you instantiate variables.
If your inheritance tree is something like this
class Object
+ class Customer
+ class FrequentCustomer
If you declare a variable of type
Objecton the right side of the declaration it can take any type that is further down the inheritance tree. This means, it your variable is of typeFrequentCustomerit has to be assigned an instance ofFrequentCustomeronly. If it’s of typeCustomerthen it can take bothCustomerandFrequentCustomerand so on. All classes inherit the typeobject, which is why I added it to the inheritance tree.The following are all valid declarations.
This is where why your first compile error occurs.
Second error is because you are using the
Convert.ToInt32()method incorrectly. The correct syntax isYour third error is not an error, but a warning only. The class
FrequentCustomeralready has the properties fromCustomerinherited, even though you haven’t specified them explicitly. This means, that you don’t need to specify the propertCustNumin theFrequentCustomerclass, it already had it inherited. However, if for some reason you need to add the property (because for example it has different implementation that his parent class), then you need to add the keywordnewon the property like this:If you don’t do this, this will be done automatically (hiding inherited member), but you will get a warning about it.