Friday, April 19, 2019

How to Use Member Function C++ (For And Max Value)

What Is Member Function C++? 

A class in C++ 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.


Program of Member Function C++ (For And Max Value)


 #include <iostream>
using namespace std;
class Number{
int x,y,z;
public:
void get_data(void);
void maximum(void);
void minumum(void);
};
void Number::minumum(void){
int min;
min = x;
if (min>y)
min = y;
if (min>z)
min = z;
cout<<"\n Minmum value is= "<<min<<endl;
}
// Member Function
void Number::get_data(void){
cout<<"Enter value of 1st num :- ";
cin>>x;
cout<<"Enter value of 2nd num :- ";
cin>>y;
cout<<"Enter value of 3rd num :- ";
cin>>z;
}

// Member Function 2nd
void Number::maximum(void){
int max;
max = x;
if(max<y)
max = y;
if (max < z)
max = z;
cout<<"\n Maximum value is= "<<max<<endl;
}

int main()
{
Number num;
num.get_data();
num.minumum();
num.maximum();
return 0;
}


Output

Enter value of 1st num :- 6
Enter value of 2nd num :- 3
Enter value of 3rd num :- 6
 Minmum value is= 3
 Maximum value is= 6

No comments:

Post a Comment