Passing arguments to a function 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 cls //'cls' is name of Class.
{
public:
void fun() //this is function.
{
cout<<"This is Simple Function"<<endl;
}
void fun2(int a, int b, char c) //passing arguments here to parameters.
{ //and printing.
cout<<"1st Value: "<<a<<endl<<"2nd Value: "<<b<<endl<<"3rd Value: "<<c;
}
};
void main()
{
cls obj; //'obj' is object of Class 'cls'.
obj.fun(); //calling 'fun()' function.
obj.fun2(123, 415, 'A'); //calling and passing arguments to parameters.
getch();
}
#include<iostream.h>
#include<conio.h>
class cls //'cls' is name of Class.
{
public:
void fun(char *name, int roll, char sec) //this is function and passing here
{ // Arguments to parameters, and printing.
cout<<"Name is: "<<name<<endl<<"Roll Number is: "<<roll<<endl<<"Section is: "<<sec<<endl;
}
};
void main()
{
cls obj; //'obj' is object of Class 'cls'.
obj.fun("HiLaLs", 120122067, 'B'); //passing arguments to parameters.
obj.fun("Haq Nawaz", 1201220, 'A'); //passing again arguments to parameters.
getch();
}
#include<iostream.h>
#include<conio.h>
class cls //'cls' is name of Class.
{
public:
char *name; //'name' is for pointer value.
int roll;
char sec;
void fun() //this is function.
{
cout<<"Name is: "<<name<<endl;
cout<<"Roll No is: "<<roll<<endl;
cout<<"Section is: "<<sec<<endl;
}
};
void main()
{
cls obj; //'obj' is object of Class 'cls'.
obj.name = "HiLaLs"; //with the help of object 'obj'
obj.roll = 120122067; //assigning values to 'name', 'roll'
obj.sec = 'B'; //and 'sec'.
obj.fun(); //and calling 'fun()' function.
getch();
}
#include<iostream.h>
#include<conio.h>
class cls //'cls' is name of Class.
{ //this is function which Return type is Integer and
public:
int fun(int a, int b) //passing here arguments.
{
int c = a + b; //adding 'a' and 'b' values and assigning to 'c'.
return c; //Returning 'c' value, U also write like 'return (c);'.
}
};
void main()
{
cls obj; //'obj' is object of Class 'cls'.
//calling and passing arguments to parameters and printing
cout<<"Return Value is: "<<obj.fun(6, 5); //Return values.
getch();
}