C#.Net find output programs (Inheritance) | set 3

Find the output of C#.Net programs | Inheritance | Set 3: Enhance the knowledge of C#.Net Inheritance concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 08, 2021

Question 1:

using System;

namespace Demo
{
    class ABC
    {
        protected int A;
        protected int B;

        public ABC()
        {
            A = 10;
            B = 20;
        }

        public void Print()
        {
            Console.WriteLine("ABC: "+A + " " + B);
        }
    }

    class PQR : ABC
    { 
        public void Print()
        {
            base.Print();
            Console.WriteLine("PQR: "+A + " " + B);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            ABC X = new PQR();
            X.Print();        
        }
    }
}

Output:

ABC: 10 20
Press any key to continue . . .

Explanation:

In the above program, we created three classes ABC, PQR, and Program. The ABC class contains two protected members A and B that are initialized in the constructor with 10 and 20 respectively. Here, we inherited class ABC into class PQR. Here, Print() method is implemented in both classes ABC and PQR that will print values of A and B on the console screen. Here, we called the Print() method of base class in the child class PQR.

Now look to the Main() method of Program class, which is the entry point of the Program.

ABC X = new PQR();
X.Print(); 

Here, object X will call the Print() method of class ABC.

Question 2:

using System;

namespace Demo
{
    class ABC
    {
        public int A;
        public int B;

        public ABC()
        {
            A = 10;
            B = 20;
        }

        public void Print()
        {
            Console.WriteLine("ABC: "+A + " " + B);
        }
    }

    class PQR extends ABC
    { 
        public void Print()
        {
            base.Print();
            Console.WriteLine("PQR: "+A + " " + B);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            ABC X = new PQR();
            X.Print();        
        }
    }
}

Output:

main.cs(22,14): error CS1525: Unexpected symbol `extends'
main.cs(24,15): error CS1525: Unexpected symbol `void'
main.cs(26,12): error CS1525: Unexpected symbol `base'
main.cs(26,22): error CS1525: Unexpected symbol `('
main.cs(27,29): error CS1525: Unexpected symbol `('
main.cs(28,9): error CS1514: Unexpected symbol `}', expecting `.' or `{'
main.cs(40,0): error CS1525: Unexpected symbol `}'

Explanation:

The above program will generate syntax errors because we used extends to inherit ABC class into the PQR class, which is not correct. In C# we need to use a colon ":" operator for inheritance.

Question 3:

using System;

namespace Demo
{
    class ABC
    {
        protected int A;
        protected int B;

        public ABC()
        {
            A = 10;
            B = 20;
        }

        public void Print()
        {
            Console.WriteLine("ABC: "+A + " " + B);
        }
    }

    class PQR : ABC
    { 
        public void Print()
        {
            Console.WriteLine("PQR: "+A + " " + B);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            new PQR().Print(); 
        }
    }
}

Output:

PQR: 10 20
Press any key to continue . . .

Explanation:

In the above program, we created three classes ABC, PQR, and Program. The ABC class contains two protected members A and B that are initialized in the constructor with 10 and 20 respectively. Here, we inherited class ABC into class PQR. Here, Print() method is implemented in both classes ABC and PQR that will print values of A and B on the console screen. Here, we called the Print() method of base class in the child class PQR.

Now look to the Main() method of Program class, which is the entry point of the Program,

new PQR().Print(); 

Here, Print() method of class PQR gets called.

Question 4:

using System;

namespace Demo
{
    class Student
    {
        protected int S_ID;
        protected string S_NAME;

        public Student(int id, string NAME)
        {
            S_ID    = id;
            S_NAME  = NAME;
        }
        public void Print()
        {
            Console.WriteLine("Student ID  : " + S_ID);
            Console.WriteLine("Student NAME: " + S_NAME);
        }
    }

    class Sample:Student
    {
        public Sample()
        {
            Console.WriteLine("Sample: Constructor called");
            Print();
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

        }
    }
}

Output:

main.cs(24,16): error CS1729: The type `Demo.Student' does not contain a constructor that takes `0' arguments
main.cs(10,16): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error because we did not define a zero-argument constructor in the Student class. Because we defined zero-argument constructor in the Sample class. To resolve we have to define a parameterized constructor in the Sample class that will call the constructor of the Student class.

Question 5:

using System;

namespace Demo
{
    class Student
    {
        protected int S_ID;
        protected string S_NAME;

        public Student(int id, string NAME)
        {
            S_ID    = id;
            S_NAME  = NAME;
        }
        public void Print()
        {
            Console.WriteLine("Student ID  : " + S_ID);
            Console.WriteLine("Student NAME: " + S_NAME);
        }
    }

    class Sample:Student
    {
        public Sample(int id, string name): base(id, name)
        {
            Console.WriteLine("Sample: Constructor called");
            Print();
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample(101,"Rahul Dravid");

        }
    }
}

Output:

Sample: Constructor called
Student ID  : 101
Student NAME: Rahul Dravid
Press any key to continue . . .

Explanation:

In the above program, we created three classes Sample, Student, and Program class. The Student class contains two data members S_ID and S_NAME, and parameterized constructor and Print() method. 

The constructor of the Student class is used to initialize data members, and the Print() method is used to print the values of data members. Then we inherited the Student class into Sample class. The Sample class also contains a parameterized constructor that will call the constructor of the base class and call the Print() method of the base class.

Now look to the Main() method of class Program, here we created object S and initialized with student id 101 and name "Rahul Dravid" that will call the constructor of Sample class and Sample class constructor will call the constructor of Student class.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.