PrimeOfKnights
PrimeOfKnights

Reputation: 423

How to use friend function or friend class?

#include <iostream>
using namespace std;

class CBase
{   
    public:
    int a;
    int b;
    private:
    int c;
    int d;
    protected:
    int e;
    int f;
    //friend int cout1();
 };

class CDerived : public CBase
{
    public:
    friend class CBase;
    int cout1()
    {
        cout << "a" << endl;
        cin >> a;
        cout << "b" << endl;
        cin >> b;
        cout << "c" << endl;
        cin >> c;
        cout << "d" << endl;
        cin >> d;
        cout << "e" << endl;
        cin >> e;
        cout << "f" << endl;
        cin >> f;
        cout << a << "" << b << "" << c << "" << d << "" << e << "" << f << "" << endl;
    }
};

int main()
{
    CDerived chi;
    chi.cout1();
}

How to use friend class and friend function? Please help me out. I have many similar errors:

c6.cpp: In member function int CDerived::cout1():
c6.cpp:10: error: int CBase::c is private
c6.cpp:30: error: within this context
  c6.cpp:11: error: int CBase::d is private
c6.cpp:32: error: within this context
  c6.cpp:10: error: int CBase::c is private
c6.cpp:37: error: within this context
  c6.cpp:11: error: int CBase::d is private
c6.cpp:37: error: within this context

Upvotes: 1

Views: 547

Answers (5)

matekm
matekm

Reputation: 6030

When You're writing

friend class CBase;

it means that CBase can access private methods of CDerived. What You want to do here is to write

friend class CDerived

in CBase, so CDerived could use private methods of CBase

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

The best solution for you is to make the fields you access from the base class protected, not private. Still if you want to use friend, you have to make CDerived a friend class to the class CBase.

Upvotes: 1

RobbieE
RobbieE

Reputation: 4350

CDerived cannot access the private members of CBase. It doesn't matter if you make it a friend or not. If you want such access to be allowed, you have to make CBase declare the friendship relationship.

#include <iostream>
  using namespace std;

  class CDerived; // Forward declaration of CDerived so that CBase knows that CDerived exists

  class CBase
  {   
  public:
      int a;
      int b;
  private:
      int c;
      int d;
  protected:
      int e;
      int f;

      friend class CDerived; // This is where CBase gives permission to CDerived to access all it's members
 };

 class CDerived : public CBase
 {

 public:
 void cout1()
 {
        cout<<"a"<<endl;
        cin>>a;
        cout<<"b"<<endl;
        cin>>b;
        cout<<"c"<<endl;
        cin>>c;
        cout<<"d"<<endl;
        cin>>d;
        cout<<"e"<<endl;
        cin>>e;
        cout<<"f"<<endl;
        cin>>f;
        cout<<a<<""<<b<<""<<c<<""<<d<<""<<e<<""<<f<<""<<endl;
  } 
};

int main()
{
    CDerived chi;
    chi.cout1();
}

Upvotes: 4

rectummelancolique
rectummelancolique

Reputation: 2237

If you want class B to bypass the encapsulation of class A, then A must declare B as a friend, not the other way around.

Upvotes: 0

Karthik T
Karthik T

Reputation: 31952

When you say

class CDerived : public CBase
 {

    public:
    friend class CBase;

That means that CBasehas access to CDerived's private members, not the other way around. Depending on your design, perhaps it is better to make those members protected. Else you need to declare CDerived as a friend of CBase.

Upvotes: 1

Related Questions