C++

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.

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
 #include <iostream>

using namespace std;

 

struct X {

       int a;

       int b;

public:

       int getA() { return b; }

       int getB() { return b; }

       void setA(int x) { a=y; }

       void setB(int y) { b=x; }

};

 

int main() {

       X obj_X;

       obj_X.a = 0;

       obj_X.b = 1;

       cout << "Here are a and b: " << obj_X.a << " " << obj_X.b << endl;

}
struct X {
Either replace "struct" by "class" (preferred) or add the next line
private:


int getA() { return b; }
Notice anything?


void setA(int x) { a=y; }
Notice anything?


void setB(int y) { b=x; }
Notice anything?



Then,
1
2
3
       obj_X.a = 0;   // Nooo - private!
       obj_X.b = 1;   // Nooo - private!
       cout << "Here are a and b: " << obj_X.a << " " << obj_X.b << endl;   // Nooo - private! 

I think the idea is to use the (corrected) setters and getters that are defined.



but its giving me error.

Trust me - that's never been a very helpful statement!

Last edited on
Hello icezy,

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.

Andy
Handy
have done that already but it's not working out
here's what I did
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
#include <iostream>

using namespace std;



class X {
private:
    int a;

    int b;

public:

    int getA() { return b; }

    int getB() { return b; }

    void setA(int x) { return y; }

    void setB(int y) { b = x; }

};



int main() {

    X obj_X;

    obj_X.setA(0);

    obj_X.setB(1);

    cout << "Here are a and b: " << obj_X.a << " " << obj_X.b << endl;

}
@icezy,

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.


it's not working out

Not helpful.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace 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';
}

Thanks seeplus
Can I chat you up privately?
We aren't personal tutors (I mean, not for free, at least).
Thanks seeplus
Can I chat you up privately?


No. I'll only provide via the forum.
Topic archived. No new replies allowed.