FRIEND CLASS IMPLEMENTATION IN CPP EASIEST PROGRAM - COFPROG

FRIEND CLASS IMPLEMENTATION IN CPP EASIEST PROGRAM

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
Previous
Next Post »

2 comments

Write comments
Unknown
AUTHOR
13 June 2017 at 21:54 delete

Thank you for sharing nice information about class implementation. it's very helpful for me.
java classes in pune

Reply
avatar
Technogeekscs
AUTHOR
24 September 2020 at 23:55 delete

This is most informative and also this post most user friendly and super navigation to all posts. java courses in pune

Reply
avatar