Finding Smallest of Two Numbers using Class in C++
This is a simple tutorial for computing smallest of two numbers using the classes and objects in C++. In this tutorial, I have used Dev C++ v5.11 software for compiling the C++ program.
Classes and Objects in C++
In C++ language, Object Oriented Programming (OOP) is a concept introducted to classify or organize data into objects. A class is a user defined type or data structure declared with keyword class
that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private
, protected
or public
(by default access to members of a class is private
). The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of a class data type are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.
Source Code
// Smallest of two numbers using Class.
#include <iostream>
#include <conio.h>
using namespace std;
class comparison {
public:
int a, b, min;
int smallest (int a, int b) {
min = a < b ? a : b;
return min;
}
};
int main() {
int x, y;
system("cls");
cout << "Smallest of two numbers:-\n";
cout << "\nEnter the first number: ";
cin >> x;
cout << "Enter the second number: ";
cin >> y;
comparison c;
cout << "\nSmallest number is " << c.smallest(x, y);
getch();
}
Comments
Post a Comment