Thread: Class Inheritance

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Class Inheritance

    Code:
    using System;
    
    namespace myProgram
    {
    
        class Student
        {
            public static int NumberOfStudents;
            private int Id;
            private string Name;
            private string Type;
    
            public Student(int id, string name, string type)
            {
                this.Id = id;
                this.Name = name;
                this.Type = type;
                NumberOfStudents++;
            }
    
            ~ Student()
            {
                Console.WriteLine("Student Terminated.");
                NumberOfStudents--;
            }
        }
    
        class SchoolStudent : Student
        {
    
            public SchoolStudent(int id, string name) : base(id, name, "School")
            {
                
            }
        }
    
        class myProgram
        {
            static void Main()
            {
                SchoolStudent [] students = new SchoolStudent[15];
                students[0] = new SchoolStudent(1, "Jack");
                students[1] = new SchoolStudent(2, "Jack");
                students[2] = new SchoolStudent(3, "Jack");
                students[3] = new SchoolStudent(4, "Jack");
                Console.WriteLine("There are currently "+Student.NumberOfStudents+" students.");
                
            }
        }
    }
    Well, this is what i came up with after reading lessons about C# class inheritance.
    Maybe you veterans could help me making this code better...

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    The student should only contain the details for that student. Student count would only ever be 1 or zero, which is useless

    So remove the counter.

    The "School" and it's constructor part isn't used, but I would refrain from using a string for something named 'Type' as it sounds like it should be using an enum.

    I would use a List for the array of students as well, then you could easily use the array name followed by '.Count' to get the number of students.

    Others may post more.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    public class Student
    {
        public int ID { get; private set; }
        public string Name { get; private set; }
                
        public Student(int studentID, string studentName)
        {
            ID = studentID;
            Name = studentName;
        }
    }
    • Don't use Type since it is a specific object in C#. IE: Type classType = Assembly.GetType("SomeTypeName");
    • There is no need for the number of students to have anything to do with a single student, thus no need for NumberOfStudents
    • ID is a bit vague - what is this?
    • Remove the destructor as it does nothing and this could easily be added to a RemoveStudent or SuspendStudent inside of a School class. Be careful about destructors in C# and make sure you understand exactly what they do and when they are called. They are much different than C++ destructors. A good article for this: http://www.fredmastro.com/post/Net-e...rformance.aspx


    It may seem at first that the entire class is public and it is...except for the private setters. The compiler will handle the actual storage of your properties and you are allowing clients to access your data but you are not allowing them to set your data. But it does allow for easy access to your data.
    Last edited by VirtualAce; 10-09-2010 at 01:30 PM.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Thank you guys for your answers.
    I don't seem to understand the following statement:

    Code:
        public int ID { get; private set; }
    I've learned about getters and setters but not the way you're using. And besides, arent getters and setters only for private members since public members can already be defined/acessed without a setter/getter?

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The setter/getter I demonstrated is actually just a C# property. Instead of accessing internal variables of the class I'm using a property to allow the user to get....but not set the property value. In recent C# and in .NET 3.5 you can define a property by using the form I've shown. The 'backing store' as it used to be called for the actual variable is handled the compiler and is transparent to the programmer. Also when you simply want get to return the value and set to set the value you no longer have to explicitly code this. Most people quickly realized that get and set were nearly identical across all properties. In fact this is a good way to judge if you should use a property or not. If the get and set logic is extremely simple then it is a good candidate for a property. If more needs to be done you can still make the variable a property but this does not mean it is the wisest choice or the best design. It is up to the programmer to figure out when a property actually needs to be a member variable accomponied by some getter/setter methods.

    So here is essentially what my code does in one line:

    public int GetID() { return m_ID; }
    private int SetID(int ID) { m_ID = ID; }

    As I said it appears at first that the entire class is public but it is not entirely. The getters for each property are public but the setters are not meaning that the only object that can set these values is the object itself.

    I do not think this would work:

    private int ID { public get; private set; }

    The public get has a less restrictive visibility than the private property. I believe this will fail to compile.
    Last edited by VirtualAce; 10-10-2010 at 01:34 AM.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Thank you all for your answers.

    I forgot to ask one more thing: Why does the code mentioned in the first post outputs Student terminated 4 times? That message should only be output in case the destructor was to be called, and it wasnt, cos i never set none of these objects to null.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Destructors are always run on objects as the garbage collector tries to collect them unless you specifically tell the garbage collector to ignore them. You really shouldn't use destructors in C# until you understand how much they change an object's lifetime.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by dhuan View Post
    Thank you guys for your answers.
    I don't seem to understand the following statement:

    Code:
        public int ID { get; private set; }
    I've learned about getters and setters but not the way you're using. And besides, arent getters and setters only for private members since public members can already be defined/acessed without a setter/getter?
    This establishes the generally used getters and setters, i.e.:

    Code:
    public int ID { get; private set; }
    is equal to:

    Code:
    public int ID
    {
        get
        {
            return ID;
        }
        private set
        {
            ID = value;
        }
    }
    The first format is the shorthand for the second. Hope that helps.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Closed. Please don't bump old threads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Inheritance, istream, class datamember
    By guda in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2004, 12:25 PM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 03:44 PM