Difference between revisions of "CPP/Classes/FriendClass"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==FriendClass.cpp== <source lang="cpp"> #include <iostream> //forward declaration class B; class A { private: friend class B; int a; public: }; class B…') |
(→FriendClass.cpp) |
||
Line 13: | Line 13: | ||
public: | public: | ||
+ | A(int aA) : a(aA) { }; | ||
}; | }; | ||
Line 21: | Line 22: | ||
public: | public: | ||
− | + | int getAa(const A& aObj) { return aObj.a; }; | |
}; | }; | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
− | + | A objA(42); | |
+ | B objB; | ||
+ | std::cout << "Value of A.a is " << objB.getAa(objA) << std::endl; | ||
return 0; | return 0; | ||
} | } | ||
</source> | </source> |
Latest revision as of 23:05, 30 November 2010
FriendClass.cpp
#include <iostream> //forward declaration class B; class A { private: friend class B; int a; public: A(int aA) : a(aA) { }; }; class B { private: public: int getAa(const A& aObj) { return aObj.a; }; }; int main(int argc, char *argv[]) { A objA(42); B objB; std::cout << "Value of A.a is " << objB.getAa(objA) << std::endl; return 0; }