Home > Software engineering >  Method overloading in C#
Method overloading in C#

Time:05-13

I Need a help in C# Related In method overloading

The question is one shop have 3 phone's

  1. Samsung Rs.1000 something
  2. Micromax Rs. something
  3. lava Rs. ''''

well if i click anything in the number it show's

your selecting (for ex 1) Samsung

after that show how munch of months you have to pay this phone in EMI it show's

  1. 3 Months 2%

  2. 6 Months 4%

  3. 11 Months 6% Interest

after that I select anything For example 2

it show's per month amount and 3 month's amount adding interest

This operation is process in method overloading in c#

CodePudding user response:

In your case, I prefer to use method overriding instead of method overloading since you have the same type of operations.

You can refer to this : https://www.geeksforgeeks.org/c-sharp-method-overriding/

CodePudding user response:

class baseClass {

double amount = 0;
  
public virtual void show()
{
    
    Console.WriteLine("Please enter your model\n ");
    Console.WriteLine("1.Samsung Rs.5000\n 2. Lava Rs.2000\n 3. Micromax Rs.3000 ");
    int modelNo = Convert.ToInt32(Console.ReadLine());
    switch (modelNo)
    {
        case 1:
            Console.WriteLine("Your Selected Samsung Rs.5000");
            amount = 5000;
            break;
        case 2:
            Console.WriteLine("Your selected Lava Rs.2000");
            amount = 2000;
            break;
        case 3:
            Console.WriteLine("Your selected Micromax Rs.3000");
            amount = 3000;
            break;
        default:
            Console.WriteLine("Your Selected Wrong Choice Please try again");
            break;
 }

} class derived : baseClass {

//'show()' is 'override' here
public override void show()
{
    Console.WriteLine("Please enter your EMI option");
    Console.WriteLine("1. 2 month 1%\n 2. 4 month 3%\n 3. 6 months 5% ");
    int intrest=0;
    int month=0;
    int EMI = Convert.ToInt32(Console.ReadLine());
 switch(EMI)
 {
     case 1:
            intrest = 1;
            month = 2;
            break;
    case 2:
            month = 4;
            intrest = 3;
            break;
    case 3:
            month = 6;
            intrest = 5;
            break;
 }

 

CodePudding user response:

class GFG {

// Main Method
public static void Main()
{

    baseClass obj; 
    obj = new baseClass();   
    obj.show();
    obj = new derived();       
    obj.show();

}

}

  • Related