CPP Friend Functions and Classes
Friend Classes and Functions
-
If you want to expose your private member data or member functions to a
function defined outside a class, you must declare that function to be a friend
inside the class:
// InClassName is the class this function should be friend of friend returnType functionName(InClassName & ref , anyType a, anyType b);
-
If you want to expose your private member data or member functions to another
class, you must declare that class to be a friend inside the class.
friend class classFriendName;
-
A friend Function or class method can be defined anywhere inside the
class declaration.
A Full Program Example:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> using namespace std; class WindowPoint { private: char *pName; int xPos; int yPos; WindowPoint *pNext; public: // Make Shape class to be a friend of this class // to give access to the pNext pointer friend class Shape; WindowPoint(char * n, int x, int y):xPos(x),yPos(y) { pName= new char[strlen(n)+1]; strcpy(pName,n); pNext=0; } ~WindowPoint(){ if (pNext!=0) { delete pNext; pNext=0; } delete [] pName; } char * getName() { return pName;} double getVector() const { return (double) sqrt(pow((double)xPos,2)+ pow((double)yPos,2)); } }; class Shape { WindowPoint *pFirst; WindowPoint *pLast; //private: protected: int width, height; public: Shape() { pFirst=0; } ~Shape() { if (pFirst!=0) { delete pFirst; pFirst=0; } } // Make setValues() Function to be a friend of the class friend void setValues(Shape & ,int , int ); /* Add WindowPoints to shape */ void addWindowPoint(char *pName, int x, int y) { WindowPoint *p = new WindowPoint(pName, x,y); p->pNext=0; if (pFirst==0) { pFirst=p; pLast=p; }else { pLast->pNext=p; pLast=p; } } /* Get the vector of for a WindowPoint */ double getWindowVector (char *pName) { bool found=false; WindowPoint *prt=pFirst; while (prt!=0) { if (strcmp(pName,prt->getName())==0) { found=true; break; } prt=prt->pNext; } if (found) { return (prt->getVector()); } return 0; } }; class WindowX: public Shape { public: int windowSize () { return (width * height); } }; void setValues(Shape & s,int a, int b) { s.width=a; s.height=b; } int main () { WindowX *pWindow= new WindowX; int width=800; int height=600; // Using the friend function to set window size setValues (*pWindow,width,height); cout << "With width=" << width << "px and height=" << height << "px the Window contain " << pWindow->windowSize() << " pixles.\n\n"; // Add points to Window pWindow->addWindowPoint("CirclePos",20,40); pWindow->addWindowPoint("RectanglePos",30,60); pWindow->addWindowPoint("DotPos",200,60); pWindow->addWindowPoint("LinePos",300,50); pWindow->addWindowPoint("ButtonPos",400,200); // Printing Vector position of the window point elements cout << "CirclePos WindowPoint vector is " << pWindow->getWindowVector("CirclePos") << " px \n"; cout << "RectanglePos WindowPoint vector is " << pWindow->getWindowVector("RectanglePos") << " px \n"; cout << "DotPos WindowPoint vector is " << pWindow->getWindowVector("DotPos") << " px \n"; cout << "LinePos WindowPoint vector is " << pWindow->getWindowVector("LinePos") << " px \n"; cout << "ButtonPos WindowPoint vector is " << pWindow->getWindowVector("ButtonPos") << " px \n"; delete pWindow; pWindow=0; return 0; }
When we run this application, the result will be:With width=800px and height=600px the Window contain 480000 pixles. CirclePos WindowPoint vector is 44.7214 px RectanglePos WindowPoint vector is 67.082 px DotPos WindowPoint vector is 208.806 px LinePos WindowPoint vector is 304.138 px ButtonPos WindowPoint vector is 447.214 px
You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.