MOST SIMPLE PROGRAM FOR FRIEND CLASS IMPLEMENTATION IN C++
Friend class in CPP:
"Base class contains basicSalary which is a private data member we want to access it from outside the class so we declared child class as friend of base class now we can access the private data members of base class from outside of that class this is called as friend class in c++".
PROGRAM:
#include<iostream>
using namespace std;
class base
{
int basicSalary = 1000;
friend class child;
};
class child : public base
{
public :
void print()
{
cout<<"Basic Salary from Base class: "<<basicSalary<<endl;
}
};
int main()
{
child c;
c.print();
return 0;
}
OUTPUT:
Basic Salary from Base class: 1000
#COFPROG
Sign up here with your email
2 comments
Write commentsThank you for sharing nice information about class implementation. it's very helpful for me.
Replyjava classes in pune
This is most informative and also this post most user friendly and super navigation to all posts. java courses in pune
ReplyConversionConversion EmoticonEmoticon