Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent or Child? #18

Open
XLR8-07 opened this issue Mar 24, 2020 · 0 comments
Open

Parent or Child? #18

XLR8-07 opened this issue Mar 24, 2020 · 0 comments
Labels
Fundamentals Topic - OOP Fundamentals like Polymorphism, Inheritance, Encapsulation Abstraction etc Run in Head Type of problems that people should try running in their head, not in IDE Very Easy Difficulty Level - Very Easy

Comments

@XLR8-07
Copy link

XLR8-07 commented Mar 24, 2020

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.
@XLR8-07 XLR8-07 added Fundamentals Topic - OOP Fundamentals like Polymorphism, Inheritance, Encapsulation Abstraction etc Very Easy Difficulty Level - Very Easy labels Mar 24, 2020
@mohayemin mohayemin changed the title Parent?or Child??! Parent or Child? Mar 24, 2020
@mohayemin mohayemin added the Run in Head Type of problems that people should try running in their head, not in IDE label Mar 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fundamentals Topic - OOP Fundamentals like Polymorphism, Inheritance, Encapsulation Abstraction etc Run in Head Type of problems that people should try running in their head, not in IDE Very Easy Difficulty Level - Very Easy
Projects
None yet
Development

No branches or pull requests

2 participants