DEV Community

Matthew Cullen
Matthew Cullen

Posted on

C# Constructor Overloading Example

using System;

namespace LESSON2_ConstructorHandsOn
{
    class Program
    {
        static void Main(string[] args)
        {
            Owner momo = new Owner("Momo");
            Cat ownedCat = new Cat("Lennny", momo);
            Cat unownedCat = new Cat("Buddy");
        }
    }

    class Cat
    {
        public string Name { get; set; }
        public Owner Owner { get; set; }

        public Cat(string name)
        {
            Name = name;
            Console.WriteLine($"ADDED CAT: {Name}");
        }

        public Cat(string name, Owner owner)
        {
            Name = name;
            Owner = owner;
            Console.WriteLine($"ADDED CAT: {Name}, OWNER: {Owner.Name}");
        }
    }

    class Owner
    {
        public string Name { get; set; }

        public Owner(string name)
        {
            Name = name;
            Console.WriteLine($"ADDED OWNER: {Name}");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
//// OUTPUT
//ADDED OWNER: Momo
//ADDED CAT: Lenny, OWNER: Momo
//ADDED CAT: Buddy
Enter fullscreen mode Exit fullscreen mode

Top comments (0)