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 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

friend Class in C++

class ClassB;

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

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

C++ friend keyword

// Add members of two different classes using friend functions

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {
    
    public:
        // constructor to initialize numA to 12
        ClassA() : numA(12) {}
        
    private:
        int numA;
        
         // friend function declaration
         friend int add(ClassA, ClassB);
};

class ClassB {

    public:
        // constructor to initialize numB to 1
        ClassB() : numB(1) {}
    
    private:
        int numB;
 
        // friend function declaration
        friend int add(ClassA, ClassB);
};

// access members of both classes
int add(ClassA objectA, ClassB objectB) {
    return (objectA.numA + objectB.numB);
}

int main() {
    ClassA objectA;
    ClassB objectB;
    cout << "Sum: " << add(objectA, objectB);
    return 0;
}
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 :: c++ structure 
Cpp :: string substr c++ 
Cpp :: joins in mysql use sequelize 
Cpp :: check if whole string is uppercase 
Cpp :: vector length c++ 
Cpp :: c++ triple 
Cpp :: lambda c++ 
Cpp :: create copy constructor c++ 
Cpp :: c++ power 
Cpp :: map declaration c++ 
Cpp :: even and odd sum in c++ 
Cpp :: c++ thread incide class 
Cpp :: how to sort vector of struct in c++ 
Cpp :: vector find 
Cpp :: iterate through map c++ 
Cpp :: std vector random shuffle 
Cpp :: classes and objects in c++ 
Cpp :: ++i and i++ 
Cpp :: setw c++ 
Cpp :: C++, for-loop over an array array 
Cpp :: Inner Section Sticky Scroll in elementor 
Cpp :: how to delete an element in vector pair in cpp 
Cpp :: all permutations with repetition C++ 
Cpp :: how to make window resizable in sdl 
Cpp :: c++ get pointer from unique_ptr 
Cpp :: how to convert string to int in c++ 
Cpp :: c++ shell 
Cpp :: how to grab each character from a string 
Cpp :: trie code cpp 
Cpp :: c++ remove all elements equal to 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =