Function Arguments in OOP
Objects of a class can be passed as Function Arguments.Objects of a class can be passed to the member functions, non-member functions and friend functions.
Note: Member and Friend Functions can access private and protected members of the passed objects. Whereas, Non-Member Functions can access only the public members of the passed object.
Following program shows how the objects are passed as arguments and executed :
Example:
//C++ program to demonstrate objects as arguments
#include <iostream.h>
using namespace std;
class dist
{
float m, km;
public :
void in_m()
{
cout << "Enter distance in meter : " ;
cin >> m;
}
void in_km(dist obj) //function definition with object as argument
{
obj.km=obj.m/1000;
cout << "\n \t The distance in kilometer : " << obj.km;
}
};
int main ()
{
dist obj1; //object declaration
obj1.in_m();
obj1.in_km(obj1); //passing the object obj1
cout << "\n";
return 0;
}
OUTPUT:
Static Data and member Function
Static data member has the following properties:
1. It is a variable which is declared inside class using static keyword.
2. It is initialized by zero when first object of class is created.
3. Only one copy of static data member is created for the entire class even if there are many class objects.
4. All the objects share the static data member.
5. Its scope is within class but its lifetime is entire program.
6. They are used to store the values that are common for all the objects.
Syntax:
class class_name
{
static data_type variable_name; //declaration of static data
};
data_type class_name::variable_name=value; //defining static data
//main function
Example:
//C++ program to demonstrate static data
#include<iostream.h>
using namespace std;
class cube
{
public:
static int vol ; //declaring static data member
};
int cube::vol = 900 ; //defining the declared member
int main()
{
cout<<"\nVolume of the cube is: "<<cube::vol<< " cubic unit";
return 0;
}
OUTPUT:
Static member Function
It has following properties:
1. A static function can access only the static data members (variables) and the static functions declared inside the class.
2. A static function is independent of the objects for its calling.
3. It can be accessed directly using the class name by using scope resolution operator :: .
Syntax:
class_name:: function_name();
Example:
// C++ program to demonstrate static member function
#include<iostream.h>
using namespace std;
class tech
{
public:
static void set_msg() //static member function
{
cout<<"Welcome to Coding Smash!";
}
};
int main() // main function
{
tech::set_msg(); //calling the static function
}
OUTPUT:
Now, let's consider an program where static data member is accessed by the static function member.
Example:
//C++ program to demonstrate static data and member function
#include <iostream.h>
using namespace std;
class operation
{
private:
static int a; //static data members
static int b;
public:
static int sum;
static void write() //static member function
{
cout<<"Value of a: "<<a<<endl;
cout <<"Value of b: "<<b<<endl;
sum=a+b;
cout <<"Sum: " <<sum<< endl;;
}
};
//static data members initializations
int operation :: a = 20;
int operation :: b =30;
int operation::sum;
int main()
{
operation::write(); //calling the static function through class name
return 0;
}
OUTPUT:
Note: If you don't know what Array and function is, we suggest that you read our Array and function in OOPs tutorial.
0 Comments