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

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

PREVIOUS NEXT
Code Example
Cpp :: how to read rotary encoder c++ 
Cpp :: curl upload folder and subfolders 
Cpp :: parking charge system project c++ 
Cpp :: c++ round number to 2 decimal places 
Cpp :: c++ graphics online compiler 
Cpp :: ejemplo 
Cpp :: binary to int c++ bitset 
Cpp :: #include <iostream #include <stdio.h using namespace std; int main() { int a[5]; a[0]=12; a[1]=13; a[2]=14; a[3]=15; 
Cpp :: c++ vector move element 
Cpp :: pointer in cpp details 
Cpp :: c++ execute thread and forget 
Cpp :: find min and max in array c++ 
Cpp :: strong number in c++ 
Cpp :: transpose function example in c++ 
Cpp :: txt to pdf CPP 
Cpp :: c++ check if cin got the wrong type 
Cpp :: sinh nhi phan c++ 
Cpp :: bnchch 
Cpp :: a suprise... c++ 
Cpp :: c++ schleife abbrechen 
Cpp :: how to calculate 2^7 in cpp code 
Cpp :: gcc compile multi thread 
Cpp :: hpp files 
Cpp :: avl tree c++ 
Cpp :: linked 
Cpp :: xor in c++ 
Cpp :: generate consecutive numbers at compile time 
Cpp :: how to convert malloc function to cpp 
Cpp :: initialize multiple variables to 0 c++ 
Cpp :: vector of vector definaion in c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =