3

i'm a C++ Programmer,and i'm new in C# i have written a little program to test inheritance so here the source code :

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

namespace Lesson3_Class_inherit_
{
   public class Personne
    {
        public string Name;
        public int Age;
        public Personne() { }
        public Personne(string _Name, int _Age) 
        {
            Name = _Name;
            Age = _Age;
            Console.WriteLine("Constrcut Personne Called\n");

        }
        ~Personne() 
        {
            Console.WriteLine("Destruct Personne Called\n");
        }


    };
    class Humain :  Personne 
    {
        public string Langue;
        public Humain(string _Name, int _Age,string _Langue)
        {
        Console.WriteLine("Constrcut Humain Called\n");
         Name = _Name;
         Age = _Age;
         Langue =_Langue;
        }



    };

    class Program
    {
        static void Main(string[] args)
        {
            Humain H1 = new Humain("majdi", 28, "Deutsch");

            Console.ReadLine();
        }
    }
}

The output : Construct Humain Called\ and the construct for the class Personne was not called why !!! In C++ the parent class constructor is called first !! Please help !

3
  • Take a look at this question. Commented Oct 20, 2012 at 16:01
  • 2
    The constructor for Personne was called. Just not the constructor you were expecting. Commented Oct 20, 2012 at 17:02
  • Just a note: you very rarely want to define a destructor in C#. stackoverflow.com/questions/4898733/… Commented Sep 19, 2016 at 23:46

4 Answers 4

6

In C# you must explicitly call a parent constructor by using the base keyword. so Humain would look like

class Humain :  Personne 
    {
        public string Langue;
        public Humain(string _Name, int _Age,string _Langue) : base(_Name, _Age)
        {
         Console.WriteLine("Constrcut Humain Called\n");
         Name = _Name;
         Age = _Age;
         Langue =_Langue;
        }



    };
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks so much !! normally the parent constructor should be called directly ! like C++ !! i don't know why they have changed this behaviour !
It allows you to control if it is called or not. It's a good advantage. It also allows you to target a specific constructor.
You don't say... But I don't regret it and I would never go back to C++. :D
Is it obligatory in C# to make a default Constructor without parameters ?
@satyres It isn't required, usually. If you define a parametrized constructor you won't get an implicit parameter-less constructor. I know WPF controls require a parameter-less constructor if you want to use it in XAML and I think WinForm controls require it too.
|
1

Because it calls the default constructor. To call the other constructor you need to write:

base(_Name, _Age);

at the beginning of Humain's constructor.

1 Comment

This is the right answer. (1) You don't have to explicitly call a base constructor, just as you don't have to in C++, and (2) if you don't, that doesn't mean that no base constructor is called. The behaviour is in fact pretty similar to C++, with some minor syntactic differences :P
1
public Humain(string _Name, int _Age,string _Langue) : base(_Name, _Age)
{
    Lange = _Langue;
}

Comments

0

Try to call the base class constructor this way:

class Humain : Personne
{
    public string Langue;
    public Humain(string _Name, int _Age, string _Langue) : base (_Name, _Age)
    {
        Console.WriteLine("Constrcut Humain Called\n");
        Name = _Name;
        Age = _Age;
        Langue = _Langue;
    }
}

As per your requirement, you can even call the default constructor instead of parametrized constructor as well.

    public Humain(string _Name, int _Age, string _Langue) : base ()
    {
        Console.WriteLine("Constrcut Humain Called\n");
        Name = _Name;
        Age = _Age;
        Langue = _Langue;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.