iut-cse/OOKata

Parent or Child?

XLR8-07 opened this issue · 0 comments

Overview

Ori is learning a new language but she got confused about the terms parent and child. She is trying to understand the output of codes without running them but is stuck with the code below. Can you help her understand this one?

Ori's Code

// Language: C#
using System;
using System.Collections.Generic;
using System.Text;

namespace QuizTask
{
    class Program
    {
        static void Show(Child obj)
        {
            obj.Print();
        }
        static void Main(string[] args)
        {
            Child x = new Child();
            Child y = new Parent(15);
            Parent z = new Parent(10);

            Show(z);
            Show(y);
            Show(x);
        }
    }

     public class Child
    {
        public Child()
        {
            Console.WriteLine("Child");
        }
        public Child(int i)
        {
            Console.WriteLine("Dhaka "+ i);
        }
        public Child(string y, int k):this(k)
        {
            Console.WriteLine("Borisal "+ k);
        }
        public virtual void Print()
        {
            Console.WriteLine("In Child Class");
        }
    }

    public class Parent:Child
    {
        public Parent(int i):this("CS")
        {
            Console.WriteLine("Sylhet " + i);
        }

        public Parent(int i, string y) : base(y,20)
        {
            Console.WriteLine("Sylhet " + i);
        }

        public Parent(string y) :this(20,y)
        {
            Console.WriteLine("Bogra " + y);
        }

        public override void Print()
        {
            Console.WriteLine("In Parent Class");
        }
    }
}

Task

  • Predict the output of the code above.
  • This is RUN IN HEAD problem. Please do not run the code in an IDE.
  • Write your solution in the comment.

Reminders

  • React to the problem if you find it interesting and helpful. This will help others to easily identify good problems to solve.
  • Feel free to comment about the problem. Is the description unclear? Do you think it is too easy or too difficult than what is mentioned? Comment about it.
  • Discussion about the solution is OK. But do not paste a solution here. Give a link to the solution instead.
  • Do you have an interesting problem? Post it.