Calling functions of a class in C++ programming using object of that class. It is user-defined data type which combines data and methods for manipulating that data into one package.
#include<iostream.h>
#include<conio.h>
class x
{
private: //using 'a, b and c' as a Private.
int a, b, c;
public: //using Functions as a Public.
void get() //this is Function.
{
cout<<"Enter Values for a, b and c"<<endl;
cin>>a>>b>>c;
}
void put() //this is function.
{
cout<<"a is: "<<a<<endl;
cout<<"b is: "<<b<<endl;
cout<<"c is: "<<c;
}
};
void main()
{
x obj; //'obj' is object of class 'x'.
obj.get(); //calling 'get()' function.
obj.put(); //calling 'put()' function.
getch();
}
#include<iostream.h>
#include<conio.h>
class x
{
private:
int a, b;
public:
friend fun(); //this is Friend Function.
};
fun()
{
x obj; //'obj' is object of class 'x'.
obj.a = 5; //assigning value to 'a'.
obj.b = 6; //assigning value to 'b'.
cout<<"Addition of two values is: "<<obj.a + obj.b; //adding and printing.
}
void main()
{
fun(); //calling 'fun()' function.
getch();
}
//with the help of 'friend' Function we Declare, define any function
//outside the Class, 'friend' must write before function.