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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.