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 !

ba0708
  • 10,180
  • 13
  • 67
  • 99
satyres
  • 51
  • 1
  • 8
  • Take a look at [this question](http://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp). – ba0708 Oct 20 '12 at 16:01
  • 2
    The constructor for `Personne` _was_ called. Just not the constructor you were expecting. – anton.burger Oct 20 '12 at 17:02
  • Just a note: you *very* rarely want to define a destructor in C#. http://stackoverflow.com/questions/4898733/when-should-i-create-a-destructor – Tim Sparkles Sep 19 '16 at 23:46

4 Answers4

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;
        }



    };
PrimeNerd
  • 194
  • 4
  • Thanks so much !! normally the parent constructor should be called directly ! like C++ !! i don't know why they have changed this behaviour ! – satyres Oct 20 '12 at 15:56
  • 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. – LightStriker Oct 20 '12 at 15:57
  • You don't say... But I don't regret it and I would never go back to C++. :D – LightStriker Oct 20 '12 at 16:00
  • Is it obligatory in C# to make a default Constructor without parameters ? – satyres Oct 20 '12 at 17:35
  • @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. – PrimeNerd Oct 20 '12 at 17:42
  • When you want to make inheritance ,you have to create a constructor without parameters !! try to remove the constructor(without param) and it will not work – satyres Oct 20 '12 at 18:25
  • @satyres Yes, it will. If the base class has a parameterless constructor (including an implicit one), and the derived class doesn't explicitly call a base constructor, then the compiler adds an implicit call to the parameterless base constructor. If the base class has a parameterised constructor but no parameterless one, it still works - but derived classes have to *explicitly* call a base constructor, because the compiler can't guess what values to pass to the base. IIRC, this is *exactly* how it works in C++. – anton.burger Oct 23 '12 at 08:15
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.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • 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 – anton.burger Oct 20 '12 at 17:03
1
public Humain(string _Name, int _Age,string _Langue) : base(_Name, _Age)
{
    Lange = _Langue;
}
aknuds1
  • 65,625
  • 67
  • 195
  • 317
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;
    }
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93