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 :: why ostream cannot be constant 
C :: trie tableau c 
C :: c colour text 
C :: C bold output 
C :: how to create random integers from a specific range in c language 
C :: terminal size in c 
C :: plt hide axis ticks 
C :: c remove last character from a string 
C :: pygame draw transparent rectangle 
C :: space after format specifiers in c 
C :: golden cobblestone modpack 
C :: how to find all the missing dates and increment the series in r 
C :: c how to get an integer from user input 
C :: prime numbers c 
C :: Successeur récurssive 
C :: armstrong number using function in c 
C :: dart in android studio 
C :: reverse list in C 
C :: how to login to another user in powershell 
C :: c programing strtok use 
C :: how to modulo in c without % 
C :: limit axis in one direction plt 
C :: c in array 
C :: try and catch in rust 
C :: loading builder in flutter 
C :: typedef vs #define 
C :: c bubble sort 
C :: actionbar content color in android 
C :: Complete the function in the editor. It has one parameter: an array, . It must iterate through the array performing one of the following actions on each element: 
C :: sizeof file c 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =