C++
Important Links
Types of Member Functions.
Following are different types of Member functions,
Simple Member functions :-
return_type functionName(parameter_list)
{
function body;
}
Example :-
void ageshow()
{
cout<<age;
}
Static Member functions :-
Static is a keyword which can be used with data members as well as the member functions.
A function is made static by using static keyword with function name. These functions work for the class as whole rather than for a particular object of a class.
It can be called using the object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.
We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.
A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.
A static member function can only access static data member, other static member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to the this pointer of the class.
A static member function is a special member function, which is used to access only static data members, any other normal data member cannot be accessed through static member function. Just like static data member, static member function is also a class function; it is not associated with any class object.
We can access a static member function with class name,
Example :-
class X
{
public:
static void f(){};
};
int main()
{
X::f(); // calling member function directly with class name
}
These functions cannot access ordinary data members and member functions, but only static data members and static member functions.
Example :-
#include <iostream>
using namespace std;
class Demo
{
private:
//static data members
static int X;
static int Y;
public:
//static member function
static void Print()
{
cout <<"Value of X: " << X << endl;
cout <<"Value of Y: " << Y << endl;
}
};
//static data members initializations
int Demo :: X =10;
int Demo :: Y =20;
int main()
{
Demo OB;
//accessing class name with object name
cout<<"Printing through object name:"<
OB.Print();
//accessing class name with class name
cout<<"Printing through class name:"<
Demo::Print();
return 0;
}
Output :-
Value of X: 10
Value of Y: 20
Printing through class name:
Value of X: 10
Value of Y: 20
Note :-
if the static member variables are private? :-
class Something
{
private:
static int s_value;
};
int Something::s_value = 1;
// initializer, this is okay even though s_value is private since it's a definition
int main()
{
// how do we access Something::s_value since it is private?
}
In this case, we can’t access Something::s_value directly from main(), because it is private. Normally we access private members through public member functions. While we could create a normal public member function to access s_value, we’d then need to instantiate an object of the class type to use the function!
Example :-
:-
C++ Inline Functions :-
All the member functions defined inside the class definition are by default declared as Inline. but you can also make any non-class function inline by using keyword inline with them.
Example :-
inline void fun(int a)
{
return a++;
}
Some Important points about Inline Functions :-
Example :-
#include <iostream>
using namespace std;
// Program to create functions using inline...
inline double square(int n)
{
return n*n;
}
inline float average(int n1,int n2)
{
return ( (float)(n1+n2)/2 );
}
int main()
{
cout<<"\n SQUARE IS = "<< square(12);
cout<<"\n AVERAGE IS = "<< average(10,21)<<"\n";
return 0;
}
Output :-
AVERAGE IS = 15.5
Example :-
Inline functions with in the class
A member function can also be declared as inline function, use the inline keyword before function declaration and definition, if function definition is written outside of class.
#include <iostream>
using namespace std;
class Number
{
private:
int num;
public:
void getNumber(void);
inline double square(void);
inline double cube(void);
};
// defining these functions
void Number::getNumber (void)
{
cout<<"Enter an integer number :";
cin>>num;
}
inline double Number::square (void)
{
return num*num;
}
inline double Number::cube(void)
{
return num*num*num;
}
int main()
{
Number objN;
objN.getNumber();
cout<<"\n SQUARE IS ="<< objN.square ();
cout<<"\n CUBE IS ="<< objN.cube ();
cout<< endl;
return 0;
}
Output :-
SQUARE IS =121
CUBE IS =1331
Friend functions :-
If a function is defined as a friend function of a class, then that function can access all the private and protected data.
To make a function as a friend of a class, it is declared inside the class either in private or in public section with keyword friend before its declaration
Data hiding is one of the basic features of C++. the private data cannot b access any non member funtion and directly by the object. the private member of the class are accessed only from member function of that class.
friend function has access permission to the private member of the class. friend is the keyword.
The declaration of friend function is done inside the class in private or public part and a function can be declared as friend function in any no of classes. these function use objects as arguments.
Example :-
class WithFriend
{
int i;
public:
friend void fun(); // Global function as friend
};
void fun()
{
WithFriend wf;
wf.i=10; // Access to private data member
cout << wf.i;
}
int main()
{
fun(); //Can be called directly
}
Hence, friend functions can access private data members by creating object of the class.
Example :-
we can also make function of other class as friend, or we can also make an entire class as friend class.
class Other
{
void fun();
};
class WithFriend
{
private:
int i;
public:
void getdata(); // Member function of class WithFriend
friend void Other::fun(); // making function of class Other as friend here
friend class Other; // making the complete class as friend
};
Note :-
When we make a class as friend, all its member functions automatically become friend functions.
Friend Functions is a reason, why C++ is not called as a pure Object Oriented language. Because it violates the concept of Encapsulation.
Example :-
class Temperature
{
int celsius;
public:
Temperature()
{
celsius = 0;
}
friend int temp( Temperature ) //declaring friend function
};
Here temp is a friend function of the class Temperature. So, it can access all the private and protected members of the class.
Example :-
#include <iostream>
using namespace std;
class Temperature
{
int celsius;
public:
Temperature()
{
celsius = 0;
}
friend int temp( Temperature ); // declaring friend function
};
int temp( Temperature t ) // friend function definition
{
t.celsius = 40;
return t.celsius;
}
int main()
{
Temperature tm;
cout << "Temperature in celsius : " << temp( tm ) << endl;
return 0;
}
Here we declared a function 'temp' as the friend function of the class 'Temperature'. In the friend function, we directly accessed the private member celsius of the class 'Temperature'.
When the first statement of the main function created an object 'tm' of the class 'Temperature' thus calling its constructor and assigning a value 0 to its data member celsius.
The second statement of the main function called the function 'temp' which assigned a value 40 to celsius.
Example :-
#include <iostream>
using namespace std;
class B; //declaration of class B
class A
{
int value;
public:
A()
{
value = 5;
}
friend int sum(A, B); // declaring friend function
};
class B
{
int value;
public:
B()
{
value = 3;
}
friend int sum(A, B); // declaring friend function
};
int sum( A v1, B v2 ) // friend function definition
{
return (v1.value + v2.value);
}
int main()
{
A a;
B b;
cout << "Sum : " << sum( a, b ) << endl;
return 0;
}
we declared sum() as a friend function of the classes A and B. So, this function can now access the private and protected members of both these classes. The objects of both the classes are passed into as an argument to the function.
First, we created an object for both the classes, thus calling their respective constructors and initializing the values of their respective data member value to 5 and 3. The function 'sum' then returned the sum of the data members of the two classes by calling the data members of the two classes by their respective objects.
Friend Class :-
We can also make a class a friend of another class. In that case, all the member function of the class declared as friend become the friend functions of the other class.
#include <iostream>
using namespace std;
class Square
{
friend class Rectangle; // declaring Rectangle as friend class
int side;
public:
Square ( int s )
{
side = s;
}
};
class Rectangle
{
int length;
int breadth;
public:
int getArea()
{
return length * breadth;
}
void shape( Square a )
{
length = a.side;
breadth = a.side;
}
};
int main()
{
Square square(5);
Rectangle rectangle;
rectangle.shape(square);
cout << rectangle.getArea() << endl;
return 0;
}
Example :-
Example :-
#include <iostream>
using namespace std;
class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
//friend function
friend int addFive(Distance);
};
// friend function definition
int addFive(Distance d)
{
//accessing private data from non-member function
d.meter += 5;
return d.meter;
}
int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}