what's 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?
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.


There are no columns. joke.getvalue(3) is calling the getvalue function of the object called joke, and passing in the parameter value '3'. There are no columns.

Funny objs[4] {funny(1,2), -49, Funny(11,11,11)}

That won't work. Even ignoring that there is no such class as funny, only Funny, the second object you are trying to pass in is not an object of type Funny, just the value -49. I assume you meant something like this:

Funny objs[4] {Funny(1,2), Funny(-49), Funny(11,11,11)}

You can get the third value in the fourth object like this:

objs[3].getValue(2);

The value displayed would be zero, as it was zero to begin with and you never changed it.
Last edited on
Topic archived. No new replies allowed.