complete the code plz

can anyone plz complete this code, its for ma exams, thank u

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

using namespace std;

class midq1 {
	float x;
	float y;
public:
	midq1() { x = 0.0; y = 0.0; }
	midq1(float x, float y) { this->x = x; this->y = y; }
	float getsum() {
		return x + y;
	}
	float getx() { return x; }
	float gety() { return y; }
	friend bool compare(midq1);
};
bool compare(midq1 A, midq1 B) {
	if (A.getx() == B.getx() && A.gety() ==B.gety()) {
		return true;
	}
	else { return false; }
}

class midq2 {
	float* fp;
	float v=0;
public:
		midq2() {fp = new float[1];}
		midq2(float val) {
			fp = new float[1];
			if (fp == NULL) {
				cerr << "Memory could not be allocated" << endl;
				exit(-1);
			}
			*fp = val;
		}
		void show() {
			cout << *fp << endl;
		}
		float getv() { return v; }
		void f1() {
			getv()
		}
};


int main() {
	midq2 v1;
	v1.show(); cout << endl; // should print 0

	midq2 v2(99.9);
	v2.show(); cout << endl;	// should print 99.9 

	v1.increment();
	v1.show(); cout << endl;	// should print 1

	v2.f1() = 33.3;
	v2.show(); cout << endl;  // should print 33.3

	//v2.f2()->show(); cout << endl;	// should print 33.3
}
can you explain what you want, fix the syntax errors so we have a clean starting place, and help us help you? Its largely nonsense code, not a single comment as to what it is supposed to do, and almost written as if intentionally trying to be convoluted or wordy... take the compare function, why not just return the result of the boolean logic, why wrap that in a conditional block?
Based upon expected output and incorrect given code, perhaps:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

class midq1 {
	float x {};
	float y {};

public:
	midq1() {}
	midq1(float x1, float y1) :x(x1), y(y1) {}

	float getsum() const { return x + y; }
	float getx() const { return x; }
	float gety() const { return y; }

	friend bool compare(midq1, midq1);
};

bool compare(midq1 A, midq1 B) {
	return A.getx() == B.getx() && A.gety() == B.gety();
}

class midq2 {
	float *fp {};
	float v {};

public:
	midq2() : fp(new float {}) {}
	midq2(float val) : fp(new float(val)) {}
	~midq2() { delete fp; }

	void increment() { ++* fp; }
	void show() const { std::cout << *fp; }
	float getv() const { return v; }
	float& f1() { return *fp; }
	const float& f1() const { return *fp; }
	const midq2* f2() const { return this; }
};

int main() {
	midq2 v1;

	v1.show();
	std::cout << '\n'; // should print 0

	midq2 v2(99.9);

	v2.show();
	std::cout << '\n';	// should print 99.9

	v1.increment();
	v1.show();
	std::cout << '\n';	// should print 1

	v2.f1() = 33.3;
	v2.show();
	std::cout << '\n';  // should print 33.3

	v2.f2()->show();
	std::cout << '\n';	// should print 33.3
}



0
99.9
1
33.3
33.3

Topic archived. No new replies allowed.