-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriendfunctions.cpp
62 lines (43 loc) · 1.92 KB
/
friendfunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
//Friend Functions
//These functions has acess tp private and protected members of a class because of the friendship relationship
class equalateralTriangle{
//these members are private, bec members of a class are private by default
float a; //length of the side
float circumference;
float area;
public:
void setA(float length){ // takes the parameter length
a = length;
circumference = a*3; // we can include these here because they are all calculated from a, whihc is the input
//parameter here aka length
area = (1.73 * a * a)/4;
}
//friend void Printresults(equalateralTriangle); // you dont need to specify the name of the paramter, just the type here
//a class can have multiple freind functions
//you can also have a friend class
friend class homework;
};
//gloabal function
//void Printresults(equalateralTriangle et){ //takes a traingle or object of the equalateralTriangle class which has attributes of circumference and area
//cout << "circumference=" << et.circumference<<endl;
//cout << "area=" << et.area<<endl;
//}
class homework {
public:
void Printresults(equalateralTriangle et){ //takes a traingle or object of the equalateralTriangle class which has attributes of circumference and area
cout << "circumference=" << et.circumference<<endl;
cout << "area=" << et.area<<endl;
}
};
int main(){
equalateralTriangle et;
et.setA(3);
//Printresults(et); //you need to pass the et/object of the class as the paramter
//the function is not directly in the class so you dont bneed to do et.printresults to acess/invoke
homework math;
math.Printresults(et);
//some tips: frindship is not mutual so we have acess to private members from the triangle class in homework but we wouldnt have acess to private members of homework in traingle
//nor are freidns inherited
}