Arithmetic Operations Using An Interface Concept

Introduction to interface

  • ”Interface” is the keyword .
  • Interfaces define properties and methods, which are the members of the interface.
  • Interfaces contains only the declaration of the members in the programming language.

Why use an interface

  • Interface is used instead of multiple inheritance.
  • Methods are to be declared by the base class and the deriving class implements the functionalities. 
Use of an interface
  • It is the responsibility of the deriving class to define the members.
  • It provides a standard structure. 
Description
  • In this blog, we learn about the interface concept in C# programming language
  • To perform arithmetic operations of addition, subtraction ,Multiplication and division, we use interface concept in C# programming language. 
REQUIREMENTS
  • Visual Studio 2010.
  • Windows operating system Windows 7,8,10.

The code of a program is given below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace arithmeticinterface {  
  6.     public interface interex {  
  7.         void add(int a, int b);  
  8.         void sub(int a, int b);  
  9.         void mul(int a, int b);  
  10.         void divi(double a, double b);  
  11.         void display();  
  12.     }  
  13.     class inter1: interex {  
  14.         int x, y, z;  
  15.         double d;  
  16.         public void add(int a, int b) {  
  17.             int m, n;  
  18.             m = a;  
  19.             n = b;  
  20.             x = m + n;  
  21.         }  
  22.         public void sub(int a, int b) {  
  23.             int m, n;  
  24.             m = a;  
  25.             n = b;  
  26.             y = a - b;  
  27.         }  
  28.         public void mul(int a, int b) {  
  29.             int m, n;  
  30.             m = a;  
  31.             n = b;  
  32.             z = a * b;  
  33.         }  
  34.         public void divi(double a, double b) {  
  35.             double m, n;  
  36.             m = a;  
  37.             n = b;  
  38.             d = a / b;  
  39.         }  
  40.         public void display() {  
  41.             Console.WriteLine("Addition value is:" + x);  
  42.             Console.WriteLine("Subtraction value is:" + y);  
  43.             Console.WriteLine("Multiplication value is:" + z);  
  44.             Console.WriteLine("Divition value is:" + d);  
  45.         }  
  46.     }  
  47.     class Program {  
  48.         static void Main(string[] args) {  
  49.             inter1 obj = new inter1();  
  50.             int g, h;  
  51.             Console.WriteLine("Enter the first Number to perform Arithmetic operations:");  
  52.             g = Convert.ToInt16(Console.ReadLine());  
  53.             Console.WriteLine("Enter the second Number to perform Arithmetic operations:");  
  54.             h = Convert.ToInt16(Console.ReadLine());  
  55.             obj.add(g, h);  
  56.             obj.sub(g, h);  
  57.             obj.mul(g, h);  
  58.             obj.divi(g, h);  
  59.             obj.display();  
  60.             Console.ReadKey();  
  61.         }  
  62.     }  
  63. }  
The output is given below.

 

Conclusion
 
I assured that after reading this blog, you all know about an interface in C# programming language.
Ebook Download
View all
Learn
View all