Write a C++ program using the concept of OOP to find the greater number between three numbers


Write a C++ program using the concept of OOP to find the greater number between three numbers 

Algorithm:

  1. Create a GreaterNumber class with private variables num1, num2, num3 to store the three numbers.
  2. Define a public method findGreater() in the class to find the greater number between the three stored numbers.
  3. In the main() function, prompt the user to enter three numbers.
  4. Create a GreaterNumber object with the three numbers entered by the user.
  5. Call the findGreater() method of the GreaterNumber object to find the greater number.
  6. Print the greater number.
  7. End the program.

C++ Program:

#include <iostream>
 
class GreaterNumber {
  private:
    int num1, num2, num3;
 
  public:
    GreaterNumber(int n1, int n2, int n3) {
      num1 = n1;
      num2 = n2;
      num3 = n3;
    }
 
    int findGreater() {
      int greater = num1;
      if (num2 > greater) greater = num2;
      if (num3 > greater) greater = num3;
      return greater;
    }
};
 
int main() {
  int n1, n2, n3;
 
  std::cout << "Enter first number: ";
  std::cin >> n1;
  std::cout << "Enter second number: ";
  std::cin >> n2;
  std::cout << "Enter third number: ";
  std::cin >> n3;
 
  GreaterNumber g(n1, n2, n3);
 
  std::cout << "Greater Number: " << g.findGreater() << std::endl;
 
  return 0;
}

#OOP
#HappyProgramming
#HappyCoding
 

No comments:

Post a Comment