I had code:

Program.cs
----------
using System;

class Exercise
{
    static void Main(string[] args)
    {
        People.Person man = new People.Person("Hermine Sandt",
            "Male");
1       HighSchool.Teacher staff =
            new HighSchool.Teacher("Vice Principal");

        Console.WriteLine();

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
}

Persons.cs
----------
using System;

namespace People
{
    public class Person
    {
        private string _name;
        private string _gdr;

3       public Person()
        {
            this._name = "Not Available";
            this._gdr = "Unknown";
        }
        
        public Person(string name, string gender)
        {
            this._name = name;
            this._gdr = gender;
        }
    }
}

StaffMembers.cs
---------------
using System;

namespace HighSchool
{
    public class Teacher : People.Person
    {
        private string _pos;

        public Teacher()
        {
            this._pos = "Staff Member";
        }

2       public Teacher(string pos)
        {
            this._pos = pos;
        }
    }
}

When I reach point marked as 1, program calling procedure which I marked as 2. Next step I don't understand. Why is calling constructor (in place marked as 3)? I assumed that happening because class Teacher derivered from People.Person, but I don't understand why it's happening.

Could somebody explain its to me, please?

Recommended Answers

All 2 Replies

HighSchool.Teacher staff = new HighSchool.Teacher("Vice Principal");

Here u are creating an object of the class named Teacher which is derived from the class Person. So when u create the object 'staff', first the the base class(Person) constructor is called to initialize the data members inherited from the base class(You have marked it as 3) and then the derived class(Teacher) constructor will be invoked(You have marked it as 2). Is this is what you are getting?

Yes, there is a explanation which I seek. It's solve my doubts. Thank's for the reply.

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.