C# Program to add two numbers using hierarchical inheritance


In this program, You will learn how to add two numbers using hierarchical inheritance in C#.


public class A { 
    //statement
}

public class B : A { 
    //statement
}

public class C : A { 
    //statement
}

Example: How to add two numbers using hierarchical inheritance in C#.

using System;
public class A
{
	public static int x, y, z;
}

public class B : A
{
	public void input()
	{
		Console.Write("Enter two numbers:");
		x = Convert.ToInt32(Console.ReadLine());
		y = Convert.ToInt32(Console.ReadLine());
	}
}

public class C : A
{

	public void add()
	{
		z = x + y;
	}

	public void display()
	{
		Console.WriteLine("sum is:" + z);
	}

	public static void Main(string[] args)
	{
		B t1 = new B();
		t1.input();

		C t2 = new C();
		t2.add();
		t2.display();
	}
}

Output:

Enter two numbers:23
3
sum is:26