我写了这段代码,现在我想从用户那里获取增量输入,例如要增加多少用户数量。我该怎么办?
#include <iostream>
using namespace std;
class myClass
{
friend void increment(myClass *, int);
private:
int topSecret;
public:
void display() {
cout << "\n The value of the topSecret is "
<<topSecret;
}
myClass();
};
// constructor of the class
myClass::myClass()
{
topSecret = 100;
}
// Friend function definition
void increment(myClass *a, int i)
{
a->topSecret += i; // Modify private data
}
// showing the use of the friend function
main()
{
myClass x;
x.display();
increment(&x, 20);
x.display();
}
当前结果是:
The value of the topSecret is 100
The value of the topSecret is 120
答案 0 :(得分:1)
#include <iostream>
using namespace std;
class myClass
{
private:
int topSecret;
public:
void increment(int);
void display() { cout << "The value of the topSecret is: " << topSecret << "\n"; }
myClass();
};
// constructor of the class
myClass::myClass()
{
topSecret = 100;
}
// Friend function definition
void myClass::increment(int i)
{
topSecret += i; // Modify private data
}
// showing the use of the friend function
int main()
{
myClass x;
x.display();
int number;
std::cout << "input number: ";
std::cin >> number;
x.increment(number);
x.display();
}