Good evening ladies and gentlemen. i have a serious problem. Despite my best efforts to follow the principles of object orienting design, i noticed that my child classes are not getting any variables derived from their respective parent class stored with their values from user input to be used in each phase of the program. if you look at this code and run it in your compiler you will see clearly what i mean:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Student_application{

class student {
protected string first_name,last_name;
protected int age,Year_of_Birth;

public student(){

string first_name;
string last_name;
int age;
int Year_of_Birth;

}

// getter and setter methods
public  string Student_getsetfname{
  get{return first_name;}
  set{first_name=value;}    
}

public string Student_getsetlname{
  get{return last_name;}
  set{last_name=value;}    
}

public int Student_getsetage{
  get{return age;}
  set{age=value;}    
}

public int Student_getsetyear{
  get{return Year_of_Birth;}
  set{Year_of_Birth=value;}    
}

// public method in student class to get student information
public void getstudentinfo()
{
 Console.WriteLine("What is your first name,last name,age,year of birth?");
 first_name=Console.ReadLine();
 last_name=Console.ReadLine();
 age=Convert.ToInt32(Console.ReadLine());
 Year_of_Birth=Convert.ToInt32(Console.ReadLine());

}

  //end of student class  
}

// Demonstration of class inheritance by course being a derived class of student
// public method to input courses
class Course:student{

protected int Number_of_Courses;

public Course(){
     int Number_of_Courses;
    }

public  int Course_Modify{
  get{return Number_of_Courses;}
  set{Number_of_Courses=value;}    
}

public void Course_selection(){

Console.WriteLine(" Welcome{0} {1}",first_name,last_name);
Console.WriteLine("Input the number of courses you wish to pursue for this semester.");
Number_of_Courses=Convert.ToInt32(Console.ReadLine());

}

}

class fee_details:Course{
protected  double course_cost=500.34;
protected  double exam_fee=230.34;
protected  double library_fee=110.12;
protected double Tuition;

//fee_details constructor
public fee_details(){

  Display();
}

public void Display(){

  Tuition=Number_of_Courses*course_cost+exam_fee+library_fee;
  Console.WriteLine("Student Name:{0}",first_name,last_name);
 Console.WriteLine("Number of Courses:{0}",Number_of_Courses);
Console.WriteLine("Exam fee:{0}",exam_fee); 
Console.WriteLine("Library fee:{0}",library_fee); 
Console.WriteLine("Course course per course:{0}",course_cost);
Console.WriteLine("Your Tuition cost for this semester:{0}",Tuition);  
}

 static void Main(string[] args){

          student student1= new student();
          Course course1=new Course();
          fee_details display1= new fee_details();
          student1.getstudentinfo();
          course1.Course_selection();
          display1.Display();

        }

//end of fee_details class
}

// End of Namespace
}

I need to understand what I am doing wrong here any help or suggestions are highly appreciated. Thank you.

Recommended Answers

All 2 Replies

Regardless of any language or syntax problems, your class hierarchy is wrong.

Inheritance is a "is-a-kind-of" relationship, as in:
Mammal is a kind of Animal
Cat is a kind of Mammal
so Cat is a subclass of Mammal, Mammal is a subclass of Animal.

Is a Course a kind of Student? Definitely not. Same for fee details.

Those classes are related by a "has-a" relationship - composition, not inheritance
a Student has (one or more) courses.
You implement that by Student having a variable that can contain one or more Courses, not by inheritance.

(Strictly speaking that is association rather than composition, but the distinction may be a bit subtle when just starting to learn OO)

I see. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.