×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to find the largest number between three numbers using class

Given three numbers, we have to find the largest number among them class and object approach.
Submitted by Shubh Pachori, on August 04, 2022

Problem statement

Given three numbers, we have to find the largest number among them class and object approach.

Example:

Input: 
Enter 1st Number:33
Enter 2nd Number:44
Enter 3rd Number:11
Output: 
44 is greater than 33 and 11

C++ Code to find the largest number among three numbers using class and object approach

#include <iostream> using namespace std; // create a class class LargestNumber { // private data members private: int number_1, number_2, number_3; // public function with three int type parameters public: void largest(int n_1, int n_2, int n_3) { // copying values of parameters in the data members number_1 = n_1; number_2 = n_2; number_3 = n_3; // if condition to check if the first number is the greatest if ((number_1 > number_2) && (number_1 > number_3)) { cout << number_1 << " is greater than " << number_2 << " and " << number_3 << endl; } // else if condition to check if the second number is the greatest else if ((number_2 > number_1) && (number_2 > number_3)) { cout << number_2 << " is greater than " << number_1 << " and " << number_3 << endl; } // else condition for the third number else { cout << number_3 << " is greater than " << number_1 << " and " << number_2 << endl; } } }; int main() { // create a object LargestNumber L; // declare three int type variables for storing numbers int n_1, n_2, n_3; cout << "Enter 1st Number: "; cin >> n_1; cout << "Enter 2nd Number: "; cin >> n_2; cout << "Enter 3rd Number: "; cin >> n_3; // calling function using object L.largest(n_1, n_2, n_3); return 0; }

Output

RUN 1:
Enter Number: 5
Sum of first 5 Natural Number is 15

RUN 2:
Enter Number: 25
Sum of first 25 Natural Number is 325

RUN 3:
Enter Number: 3
Sum of first 3 Natural Number is 6

Explanation

In the above code, we have created a class LargestNumber, three int type data members number_1, number_2, and number_3 to store the numbers, and a public member function largest() to find out the largest among the given integer numbers.

In the main() function, we are creating an object L of class LargestNumber, reading three integer numbers by the user, and finally calling the largest() member function to find out the largest among the given integer numbers. The largest() function contains the logic to check the largest of the given numbers and printing the largest number.

C++ Class and Object Programs (Set 2) »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.