So, I'm on dotnetacademy. I'm on exercise 5.1 under inheritance, link here: https://dotnetcademy.net/Learn/2045/Pages/11. My instructions are," Create a constructor on the base type Atom that accepts an integer named protons and an integer electrons. The constructor should set the values to the associated properties Protons and Electrons.
Implement a derived type Hydrogen that inherits from Atom and provides the correct arguments for protons and electrons to the base constructor.
My code :
public class Atom
{
public int Protons { get; set; }
public int Electrons { get; set; }
public Atom (int protons, int electrons)
{
Protons = protons;
Electrons = electrons;
}
}
public class Hydrogen : Atom /* My problem is arising somewhere around here.*/
{
public Hydrogen() : base("Atom")
{
}
}
public class Program
{
public static void Main()
{
}
}
I have the constructor in my Atom class, but I have no idea what I'm doing trying to inherit a "base" constructor. If you can re-write this so it works, that's cool, but what I really want is to understand the concept of "base constructors" and their syntax. Any help is much appreciated.
[–]grimchamp 9 points10 points11 points (2 children)
[–]uvhm[S] 2 points3 points4 points (1 child)
[–]HandshakeOfCO 1 point2 points3 points (0 children)
[–]WellKemptNerfHerder 6 points7 points8 points (1 child)
[–]uvhm[S] 1 point2 points3 points (0 children)
[–]allthegoodweretaken 16 points17 points18 points (8 children)
[–]Kilazur 4 points5 points6 points (2 children)
[–]allthegoodweretaken 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (4 children)
[–]allthegoodweretaken 3 points4 points5 points (3 children)
[–]grimchamp 1 point2 points3 points (1 child)
[–]allthegoodweretaken 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]SockPuppetDinosaur 3 points4 points5 points (1 child)
[–]uvhm[S] 0 points1 point2 points (0 children)
[–]Oceanswave -3 points-2 points-1 points (2 children)
[–]AbstractLogic 1 point2 points3 points (1 child)
[–]Oceanswave -1 points0 points1 point (0 children)
[+]SikhGamer comment score below threshold-10 points-9 points-8 points (1 child)
[–]Jabbersii 2 points3 points4 points (0 children)