Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ friend class

class A {
  	private:
      	int key;

 	// give class B access to class A
    friend class B;
};

class B {
	// new object from class A
  	A new_a;
  
  	void fn(){
		// access private value from class B
		new_a.key;
    }
}
Comment

friend function in c++

class className{
  // Other Declarations
  friend returnType functionName(arg list);
};
Comment

friend Class in C++

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}
Comment

friend function in c++

// Program to illustrate friend function

#include<iostream>

using namespace std;

class integer
{
  int a, b;
  public:
    void set_value()
    {
    a=50;
    b=30;
    }
  friend int mean(integer s);  //declaration of friend function
};

int mean(integer s)
{
  return int(s.a+s.b)/2.0; //friend function definition
}
int main()
{
  integer c;
  c.set_value();
  cout<< "Mean value:" <<mean(c);
  return 0;
}
Comment

friend Class in C++

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}
Comment

friend Class in C++

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}
Comment

friend Class in C++

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}
Comment

friend Class in C++

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}
Comment

C++ friend keyword -3 (friend class)

// C++ program to demonstrate the working of friend class

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {
    private:
        int numA;

        // friend class declaration
        friend class ClassB;

    public:
        // constructor to initialize numA to 12
        ClassA() : numA(12) {}
};

class ClassB {
    private:
        int numB;

    public:
        // constructor to initialize numB to 1
        ClassB() : numB(1) {}
    
    // member function to add numA
    // from ClassA and numB from ClassB
    int add() {
        ClassA objectA;
        return objectA.numA + numB;
    }
};

int main() {
    ClassB objectB;
    cout << "Sum: " << objectB.add();
    return 0;
}
Comment

friend Class in C++

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}
Comment

friend Class in C++

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: run program until ctrl-d c++ 
Cpp :: how to change the default camera speed values opengl 
Cpp :: operator overloading prefix postfix c++ 
Cpp :: c++ string to const char* 
Cpp :: c ++ loop 
Cpp :: online compiler c++ with big O calculatorhttps://www.codegrepper.com/code-examples/cpp/how+to+convert+string+to+wchar_t+in+c%2B%2B 
Cpp :: linked 
Cpp :: print the elements of the array without using the [] notation in c++ 
Cpp :: c++ program to convert kelvin to celsius 
Cpp :: esp8266 wifi.localip() to string 
Cpp :: can you use rand to read in from an external file inc++ 
Cpp :: cpp reference array 
Cpp :: Character convert c++ 
Cpp :: cpp-variadics/problem? 
Cpp :: c++ linker input and output 
Cpp :: ue4 foreach loop c++ 
Cpp :: compile c++ program 
Cpp :: button creation in C++ GUI 
Cpp :: 12 to december in c++ code 
Cpp :: multilevel inheritance in c++ private method 
Cpp :: how can I convert each and every element of string push into set in c++? 
Cpp :: gcd of two number in c++ stl 
Cpp :: c++ cin accept only numbers 
Cpp :: online compiler cpp 
Cpp :: c++ multi-dimensional arrays 
Cpp :: return function in cpp 
Cpp :: sum function in c++ 
Cpp :: set elements to 42 back 
C :: remix icon cdn 
C :: pygame detect click 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =