setwidth()
is a member function of class Box
and it is also a friend function of class Sbox
which is setting the values of members width
of both classes.
The value of width
of the class Sbox
is not setting/coming out properly.
#include<iostream>
using namespace std;
class Sbox;
class Box{
private:
double width;
public:
friend void printwidth(Box box);
void setwidth(Sbox sbox, double wid);
};
class Sbox {
private:
double width;
public:
friend void Box::setwidth(Sbox sbox, double wid);
void printwidth() {
cout << "the width of small box: " << width; //this value is coming wrong
}
};
void Box::setwidth(Sbox sbox, double wid) {
width = wid;
sbox.width = wid;
}
void printwidth(Box box) {
cout << "width of the box: " << box.width << endl;
}
int main() {
Box box;
Sbox sbox;
box.setwidth(sbox, 10.77);
printwidth(box);
sbox.printwidth();
return 0;
}