none
Beginner question: Child objects RRS feed

  • Question

  • Back again with another newbie question. My C# book has instructed me to create a child object and run the code. However, I have no idea how to create a child object in main. Here's what the code looks like:

                                                         

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ClassDemo1
    {



        class Member
            {
                protected string annualFee;
                private string name;
                private int memberID;
                private int memberSince;

                public override string ToString()
                {
                    return "\nName: " + name + "\nMember ID: " + memberID + "\nMember since: " + memberSince + "\nTotal annual fee: " + annualFee;
                }

                public Member ()
                {
                    Console.WriteLine("Parent constructor with no parameter.");
                }

                public Member(string pName, int pMemberID, int pMemberSince)
                {
                    Console.WriteLine("Parent constructor with 3 parameters");

                    name = pName;
                    memberID = pMemberID;
                    memberSince = pMemberSince;
                }

               class NormalMember: Member
                {
                    public NormalMember(string remarks, string name, int memberID, int memberSince): base(name, memberID, memberSince)
                    {
                        Console.WriteLine("Child constructor with 4 parameters.");
                        Console.WriteLine("Remarks = {0}.", remarks);
                    }
                }


            }





        class Program
        {
            static void Main(string[] args)
            {


                NormalMember myChildMember = new NormalMember("Special Rate", "James", 1, 2015);

                Console.Read();
            }
        }
    }

    "NormalMember" is underlined red as an error in main. Any idea what I'm doing wrong? Thanks in advance.

    Friday, October 13, 2017 5:33 AM

Answers

  • I think there is some confusion going on with what your book calls child classes (generally I've always thought of these as inherited or descendent classes) and inner classes.

    A child class inherits all the properties, fields etc from the parent class. The typical example you see is that of an Animal class. And from the Animal class you get child/descendent classes called Dog, Cat and so on.

    public class Animal
    {
    }
    
    public class Dog : Animal
    {
    }
    
    public class Cat : Animal
    {
    }

    Note that the Dog and Cat classes are defined as separate classes outside of Animal (i.e. not within the Animal curly brackets).

    Your book has a parent/ancestor class called Member and a child/descendent class called NormalMember:

    public class Member
    {
      protected int annualFee;
      // etc
    }
    
    public class NormalMember: Member
    {
    }
    

    The bit after the class name and colon (:) which I've highlighted in bold is saying what class your child class is inheriting from. That is, that NormalMember inherits all the field, properties and methods of Member.


    • Edited by RJP1973 Friday, October 13, 2017 4:19 PM
    • Marked as answer by Clydey2Times Friday, October 13, 2017 4:55 PM
    Friday, October 13, 2017 4:19 PM

All replies

  • Add this thing to your head of code

    using static ClassDemo1.Member;

    Friday, October 13, 2017 6:44 AM
  • The NormalMember class is private to the Member class isn't it?
    You could make it public so it is visible and then use new Member::NormalMember(...) but that defeats the purpose of declaring "child" classes within a containing parent. The containing parent class would normally have a public interface and the "child" class would be used internally by the parent. Alternatively the "child" class might implement a public interface and the "parent" class would act as a factory for it. If the desired outcome is the "child" class specializes or extends the "parent" class - inheritance as it it is known, then the "child" class should be declared outside of the "parent" class and then it would be available to use in the way your Main is using it.

    class Parent
    {
    }

    class Child:Parent
    {
    }

    void Main()
    {
    var x = new Child();

    }

    Friday, October 13, 2017 7:32 AM
  • Thanks for the responses. I'm not entirely sure I understand the explanations, though. If you don't mind, I'll include the pages of my C# book and perhaps you can tell me if I've missed the answer?

    I'm struggling with some sections with this book, as I feel it's omitting key details.

    Friday, October 13, 2017 4:10 PM
  • I think there is some confusion going on with what your book calls child classes (generally I've always thought of these as inherited or descendent classes) and inner classes.

    A child class inherits all the properties, fields etc from the parent class. The typical example you see is that of an Animal class. And from the Animal class you get child/descendent classes called Dog, Cat and so on.

    public class Animal
    {
    }
    
    public class Dog : Animal
    {
    }
    
    public class Cat : Animal
    {
    }

    Note that the Dog and Cat classes are defined as separate classes outside of Animal (i.e. not within the Animal curly brackets).

    Your book has a parent/ancestor class called Member and a child/descendent class called NormalMember:

    public class Member
    {
      protected int annualFee;
      // etc
    }
    
    public class NormalMember: Member
    {
    }
    

    The bit after the class name and colon (:) which I've highlighted in bold is saying what class your child class is inheriting from. That is, that NormalMember inherits all the field, properties and methods of Member.


    • Edited by RJP1973 Friday, October 13, 2017 4:19 PM
    • Marked as answer by Clydey2Times Friday, October 13, 2017 4:55 PM
    Friday, October 13, 2017 4:19 PM
  • Oh wow, so that's what I missed? I just needed to create the class outside of the parent class, rather than within it? I just want to make sure I'm getting this right. I created an inner class (the book hasn't covered this yet), as opposed to a child class? Thank you so much. I was struggling to get past this part. 
    Friday, October 13, 2017 4:24 PM