I am to revise the program and correct the errors of the program and make varaibles a and b not accessible by ".".
so that the output is: Here are a and b: 0 1
and I should also keep the meanings of the main function.
but its giving me error.
I think what you may not be understanding is the a struct is public by default. As lastchance said yo could change the struct to a class. The class is private by default, so the layout between the {}s would work for a class.
Or you could reverse the 2 sections and change public to private to keep the struct.
Changing the struct to a class I believe, with out testing, that it should work.
Then refer to what lastchance said about the other problems.
I think the idea is that you take on board what others have written.
Only one of the following is correct:
1 2 3 4 5 6 7
int getA() { return b; } // Asks for a ... returns b ???
int getB() { return b; }
void setA(int x) { return y; } // Argument is x ... so decides to use y??? ... and return it from a void function???
void setB(int y) { b = x; } // Argument is y ... so decides to use x???
And this line does precisely what your coursework tells you not to do: cout << "Here are a and b: " << obj_X.a << " " << obj_X.b << endl;
Use your getters for obj_X.a and obj_X.b. These variables have (correctly) been declared private.
#include <iostream>
usingnamespace std;
class X {
private:
int a {}, b {};
public:
int getA() const { return a; }
int getB() const { return b; }
void setA(int x) { a = x; }
void setB(int y) { b = y; }
};
int main()
{
X obj_X;
obj_X.setA(0);
obj_X.setB(1);
cout << "Here are a and b: " << obj_X.getA() << " " << obj_X.getB() << '\n';
}