Friend functions in C++ have the right to access the public, protected and private members of the class.
Example
[c]
#include <iostream>
using namespace std;
class Pyramid
{
double height;
public:
friend void printHeight(Pyramid pyr );
void setHeight( double pyr);
};
// Member function definition
void Pyramid::setHeight( double hei )
{
height = hei;
}
// Note: printHeight() is not a member function of any class.
void printHeight( Pyramid pyr)
{
/* Because printWidth() is a friend of Pyramid, it can
directly access any member of this class */
cout << "Height of Pyramid : " << pyr.height <<endl;
}
int main( )
{
Pyramid pyr;
// set Pyramid height without member function
pyr.setHeight(48.0);
// Use friend function to print the heigth.
printHeight( pyr );
return 0;
}[/c]
Output[c]
Height of Pyramid : 48
[/c]
this pointer
Description
The member functions of C++ classes will have the this pointer to point to the address of class objects. Friend functions are not eligible for using this pointer as they are not the member functions of class.
Example
[c]
#include <iostream>
using namespace std;
class Square
{
public:
// Constructor definition
Square(double s1=2.0, double s2=2.0)
{
cout <<"Constructor is called." << endl; side_1= s1; side_2= s2; } double Area() { return side_1 * side_2; } int compare(Square sqr) { return this->Area() > sqr.Area();
}
private:
double side_1;
double side_2;
};
int main(void)
{
Square Square1(5.0, 5.5); // Declare box1
Square Square2(5.0, 6.0); // Declare box2
if(Square1.compare(Square2))
{
cout << "Square2 is smaller than Square1" <<endl;
}
else
{
cout << "Square2 is equal to or larger than Square1" <<endl;
}
return 0;
}[/c]
Output[c]
Constructor is called.
Constructor is called.
Square2 is equal to or larger than Square1[/c]
Static
Description
Static represents the class members which are having only single copy of static member irrespective of number of objects created and that copy is shared by all the objects members.
The keyword used is staticand scope resolution operator:: is used to access static members.
Static member functions are independent of objects of the class and can be called without objects also. Even though they are member functions this pointers cannot be used by static.The static member functions can access functions outside the class.
Example
[c]
#include <iostream>
using namespace std;
class Triangle
{
public:
static int objectCount;
// Constructor definition
Triangle(double b=2.0, double h=2.0)
{
cout <<"Constructor is called." << endl;
base = b;
height = h;
// Incrementing object
objectCount++;
}
double Area()
{
return (base*height)/2;
}
static int getCount()
{
return objectCount;
}
private:
double base;
double height;
};
// Initialize static member
int Triangle::objectCount = 0;
int main(void)
{
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Triangle::getCount() << endl;
Triangle tri1(5.8, 2.2);
Triangle tri2(6.0, 4.5);
// Print total number of objects after creating object.
cout << "Final Stage Count: " << Triangle::getCount() << endl;
return 0;
}[/c]
Output[c]
Inital Stage Count: 0
Constructor is called.
Constructor is called.
Final Stage Count: 2
[/c]