This is my code. I’m trying to do some basic inheritance in it but the display method doesn’t seem to work. I think it has something to do with the constructor so I already put a “base(a,b,c);” there. 🙂
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace School
{
public class CollegeCourse
{
public CollegeCourse(String a, int b, double c)
{
dep = a;
kors = b;
cre = c;
}
public String dep;
public int kors;
public double cre;
public double fee;
public String getDep()
{ return dep; }
public int getKors()
{ return kors; }
public double getCre()
{ return cre; }
public virtual void display()
{
fee = cre * 120;
Console.WriteLine("Dep is : {0}\n"+
" Course is : {1}\n Credit is : {2}\nFee is : {3}",
getDep(), getKors(), getCre(), fee);
}
}
public class LabCourse : CollegeCourse
{
public LabCourse(String a, int b, double c)
: base(a, b, c)
{
dep = a;
kors = b;
cre = c;
}
public override void display()
{
fee = cre * 120;
Console.WriteLine(@"Dep is : {0}\n "+
"Course is : {1}\n Credit is : {2}\nFee is : {3}",
dep, kors, cre, fee + 50);
}
}
public class UseCourse
{
public static void teste()
{
String a;
int b;
double c;
Console.WriteLine("Input Department:");
a = Console.ReadLine();
Console.WriteLine("Input Course:");
b = Int16.Parse(Console.ReadLine());
Console.WriteLine("Input Credits:");
c = Double.Parse(Console.ReadLine());
CollegeCourse aw = new CollegeCourse(a, b, c);
LabCourse oy = new LabCourse(a, b, c);
aw.display();
oy.display();
Console.ReadLine();
}
}
}
In your constructors, you are creating new local variables (within the constructor) and not setting the properties in the class.
If you remove the type definitions in the CollegeCourse constructor, this should resolve your problem:
And in the LabCourse, you do not need to set any properties since you are calling the inherited constructor with the parameters that were passed to the LabCourse constructor: