Blog Moved ....

ScottCate.com

community

frenz

my book(s)

my products

This will help you understand C# Constructor Overloads and Inheritance

The below example is a pretty easy example to walk, that shows you how C# contructors work with both overloads and inheritance. This is a question that comes up pretty often, so I thought I'de throw together a quick console application, that uses a tiny bit of inheritance, and ctor (ctor is a common shortcut for constructor) overloading.

There is also a pretty easy to find lesson with the use of the keywrod virtual., which simply means that the method is as defined for the entire chain of object, unless it is OVERRIDEn in the concreate class.

The sample has a base class of Type Person. Then we have a HistoricalFigure class that inherits from Person, as all Historical people are in fact persons to begin with. So we know that every HistoricalFigure will for example, have a BirthDate, which is a property of Person.

3 classes total

  • TestConsole (for visual output)
  • Person
  • HistoryFigure

TestConsole.cs

    1 using System;
    2 
    3 namespace TestConsole
    4 {
    5     /// <summary>
    6     /// Summary description for main.
    7     /// </summary>
    8     public class TestConsole
    9     {
   10         private TestConsole() {}
   11         public static void Main(string[] args)
   12         {
   13             Person p = new Person("Cameron Cate");
   14             Console.WriteLine(p.HowOld());
   15 
   16             p = new Person("Scott Cate", new DateTime(1973,6,24));
   17             Console.WriteLine(p.HowOld());
   18 
   19             HistoryFigure h = new HistoryFigure("Thomas Jefferson", new DateTime(1743,4,13), new DateTime(1826,7,4));
   20             Console.WriteLine(h.HowOld());
   21 
   22             h = new HistoryFigure("President Bill Clinton", new DateTime(1946,8,19));
   23             Console.WriteLine(h.HowOld());
   24 
   25             Console.Read();
   26         }
   27     }
   28 }


 Person.cs

    1 using System;
    2 
    3 namespace TestConsole
    4 {
    5     /// <summary>
    6     /// Summary description for Person.
    7     /// </summary>
    8     public class Person
    9     {
   10         public Person(string Name) : this(Name, DateTime.Now) {}
   11 
   12         public Person(string Name, DateTime Birthdate)
   13         {
   14             _Name = Name;
   15             _Birthdate = Birthdate;
   16         }
   17 
   18         private string _Name;
   19         public virtual string Name
   20         {
   21             get {    return _Name; }
   22             set { _Name = value; }
   23         }
   24 
   25         private DateTime _Birthdate;
   26         public DateTime Birthdate
   27         {
   28             get {    return _Birthdate; }
   29             set { _Birthdate = value; }
   30         }
   31 
   32         private DateTime _DeathDate;
   33         public DateTime DeathDate
   34         {
   35             get {    return _DeathDate; }
   36             set { _DeathDate = value; }
   37         }
   38 
   39         /// <summary>
   40         /// Calculate Years Old. I'm not sure if this is the best
   41         /// calculation method, but it works.
   42         /// </summary>
   43         /// <summary>
   44         /// Calculate Life Span
   45         /// </summary>
   46         public int YearsOld
   47         {
   48             get
   49             {
   50                 TimeSpan alive;
   51                 if(DeathDate == DateTime.MinValue)
   52                     alive = new TimeSpan(DateTime.Now.Ticks - Birthdate.Ticks);
   53                 else
   54                     alive = new TimeSpan(DeathDate.Ticks - Birthdate.Ticks);
   55                 return (int)alive.TotalDays/365;
   56             }
   57         }
   58 
   59         /// <summary>
   60         /// Helper method to write out the person's age, virtual means that
   61         /// this method stands fro all objects inheriting from person, UNLESS
   62         /// it's OVERRIDEn in the concreate class (See HistoryFigure)
   63         /// </summary>
   64         /// <returns></returns>
   65         public virtual string HowOld()
   66         {
   67             return string.Format("{0} is {1} years old.",Name,YearsOld);
   68         }
   69     }
   70 }


HistoryFigure.cs

    1 using System;
    2 
    3 namespace TestConsole
    4 {
    5     /// <summary>
    6     /// Summary description for HistoryFigure.
    7     /// </summary>
    8     public class HistoryFigure : Person
    9     {
   10         //Notice there is no (string Name) ctor;
   11 
   12         public HistoryFigure(string Name, DateTime Birthdate) : base(Name, Birthdate) {}
   13 
   14         public HistoryFigure(string Name, DateTime Birthdate, DateTime DeathDate) : base(Name, Birthdate) 
   15         {
   16             //Name & Birthdate will be set in base ctor
   17             base.DeathDate = DeathDate;
   18         }
   19 
   20         public override string Name
   21         {
   22             get { return string.Format("Historical Figure \"{0}\"", base.Name); }
   23             set { base.Name = value; }
   24         }
   25 
   26 
   27         /// <summary>
   28         /// Overrides base Age
   29         /// </summary>
   30         /// <returns></returns>
   31         public override string HowOld()
   32         {
   33             if(DeathDate == DateTime.MinValue)
   34                 return base.HowOld();
   35             else
   36                 return string.Format("{0} died {1}, and was {2} years old.",Name,DeathDate.ToShortDateString(),YearsOld);
   37         }
   38 
   39     }
   40 }

Comments

jayson knight said:

Great concise example! I don't see enough of this on many of the projects I've worked on...well, not enough done well at least.

I probably would have overridden ToString() and either chained it to HowOld(), or would have used it in place of HowOld() :-).

Good job!
# November 24, 2005 3:35 PM

Scott Cate - Knowledge Base Software said:

Yep, overriding Tostring() would work. I was trying to show how the virtual keyword was used, but this maybe wasn't the best example.
# November 25, 2005 1:51 AM