what is the output?

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


#include <iostream>

using namespace std;

class Funny {
private:
	double ax;	double bx;	double cx;
public:
	void setData (double a, double b, double c) {ax = a; bx = b; cx = c;}
	double getValue (int choice);
	Funny(double a = 0, double b = 0, double c= 0) {setData( a,b,c);}
	~Funny() {cout <<"destructor happens!\n";}
};

double Funny::getValue (int choice{
	if (choice ==1) return bx;
	else if (choice == 3) return ax;
	else if (choice == 2) return cx;
	else cout << "invalide choice!\n";
}

	int main(void)
	{
		Funny joke, cartoon(3,9), spoof(4.5,6);
		cout<<spoof.getValue(2)<<endl;
		cout << joke.getValue(3)<<endl;
		cout<<cartoon.getValue(1)<<endl;
	}


ignore the careless mistakes. in the book it said the output is
6
0
9
destructor
destrcutor
destructor.
why? how come?
i want to know why joke.getvalue(3) will have an output, becuase techically it's getting the forth column which is not even there in the program.


And one more question.
let's say i initialize the obejcts as follow(ax,bx,cx)
a)first object, 1 and 2
b)second object. -49
c)third object 11,11,11
d)4th object 0,0,0.
 
Funny objs[4] {funny(1,2), -49, Funny(11,11,11)}//i don't need to decalre the fourth object since it's 0 0 and 0 it's default anywayz. 

write a code to output the third value in the fourth object of the arrey after updating the secound value in the third object by 23. What value would be displayed?
Last edited on
joke is an object of type funny.

joke.getValue(3) looks like it returns ax:

else if (choice == 3) return ax;

So, what is ax in the object joke? Clearly, it is zero. What's this fourth column you're talking about? There are no columns or arrays.

The output is
0
0
9
destructor
destructor
destructor
joke.getvalue(3) is saying pass the argument '3' to the member function "getValue", getValue then assigns the argument to 'choice' and runs it through it's body. When it comes to else if (choice == 3) return ax; on line 19 it returns ax like it's supposed to. If you changed the code so that the function was called "HamSandwich" it would do the same thing, and that is what the book is trying to show you.
the output is
6//how u get 0? it said spoof.getvalue(2) which is 6 from spoof(4,5,6)
0
9
Because in your code you didn't write spoof(4,5,6), you wrote spoof(4.5, 6)
i edit my post with a new question can you asnwer it for me?i have the solution but the solution doesn't make sense (it somehow say 11, 11+23,11, which i thought is changing 11 to 23 in second value)so i want some expert to show me.
Last edited on
Topic archived. No new replies allowed.