1
setwidth()
是類Box
的成員函數,它也是Sbox
類的朋友函數,它設置兩個類的成員width
的值。作爲另一個類的成員函數的朋友函數設置值
Sbox
類width
的值未正確設置/顯示。
#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;
}
它的工作,謝謝 – falco97