Trouble with classes (for experts only)

Design a class point that has x and y coordinates as data members:
Include the following member functions:
· Constructor with arguments
· Constructor without arguments (Default constructor)
· Scale function: void scale(double a)

This function is supposed to scale the coordinates of the point by a, i.e., multiply each by the
real number a.
· Add function: void add(point q)

If p and q are of type point, the function p.add (q) should modify p in such a way that:
p.x is assigned p.x + q.x, and p.y is assigned p.y + q.y.
2.2 Based on the class point, design a class rectangle that defines a rectangle as follows:
In this problem, by a rectangle we mean a rectangle whose edges are either vertical or horizontal
(i.e., we do not deal with rotated rectangles). Thus to specify a rectangle we only need to specify
its lower left corner point and its upper right corner point.
Include the member functions:
· Constructor with arguments which allows the user to initialize a rectangle by specifying its 2 defining corner points.
· Constructor without arguments (Default constructor)
· Area member function : double area()
· Scale member function: void scale(double a)

This function is supposed to scale each of the corner points of the rectangle by a.
· Translate member function: void translate(point q)

This function is supposed to translate the rectangle by the vector q, i.e., add q to both corner points.
· containsPoint member function: bool containsPoint(point p)

This function is supposed to check if a given point p is inside the rectangle.
· The pointSetIntersect function:

void pointSetIntersect(point A[], int n, point B[], int &m)
This function is supposed to store in the array B all the points in A[0 . . . n − 1] which are
inside the rectangle. It is supposed also to set m to the number of points in A[0 . . . n − 1]
which are inside the rectangle.
Write a program to test your classes and functions.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <cmath>
using namespace std;

class point
{ 
	friend class rectangle;
	private:
		double x,y;

	public:
		point()
		{x=y=0;}

		point(double a, double b)
		{
			x=a;
			y=b;
		}

		void scale(double a)
		{
			x=x*a;
			y=y*a;
		}
	
		void add(point q)
		{
			x= x+ q.x;
			y= y+ q.y;
		}

		double getx()
		{
			return x;
		}

		double gety()
		{
			return y;
		}
};

class rectangle
{
private:
	
	point p1,p2;

public:

	rectangle (point c1, point c2)
	{
		p1=c1;
		p2=c2;

	}

	rectangle ()
	{
		point p;
		p1=p;
		p2=p;
	}

	double area ()
	{
		double H=fabs(p1.y-p2.y);
		double W=fabs(p1.x-p2.x);
		return (H*W);
	}

	void scale (double a)
	{
		p1.scale(a);
		p2.scale(a);
	}

	void translate (point q)
	{
		p1.add(q);
		p2.add(q);
	}

	bool containsPoint(point p)
	{
		if( (p.x > p1.x && p.x < p2.x) && (p.y < p1.y && p.y > p2.y))
			return true;
		else
			return false;
	}

	void pointSetIntersect (point * A, int n, point * B)
	{
	    int K=0;
		int m=0;
		for(int i=0; i<n; i++)
		{
			if(containsPoint (A[i]))
			{ B[K]=A[i];
			K++;
			m++;
			}
		}
	}
}; //end of class rectangle.

void main()
{
	point p(5,6);


	p.scale(5);
	cout<<"P is now: "<<p.getx<<" "<<p.gety<<endl;

	p.add(p);
	cout<<"P is now: "<<p.getx<<" "<<p.gety<<endl;

    rectangle r;
	cout<<"Area is is: "<<r.area ()<<endl;
	r.scale(6);
	r.translate(p);
	r.containsPoint (p);
	point A,B;
	r.pointSetIntersect(&A,7,&B);
}


The error that i get are :
1>------ Build started: Project: assignment, Configuration: Debug Win32 ------
1> assignment.cpp
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(114): error C3867: 'point::getx': function call missing argument list; use '&point::getx' to create a pointer to member
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(114): error C3867: 'point::getx': function call missing argument list; use '&point::getx' to create a pointer to member
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(114): error C3867: 'point::gety': function call missing argument list; use '&point::gety' to create a pointer to member
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(114): error C3867: 'point::gety': function call missing argument list; use '&point::gety' to create a pointer to member
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(117): error C3867: 'point::getx': function call missing argument list; use '&point::getx' to create a pointer to member
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(117): error C3867: 'point::getx': function call missing argument list; use '&point::getx' to create a pointer to member
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(117): error C3867: 'point::gety': function call missing argument list; use '&point::gety' to create a pointer to member
1>c:\users\fares\documents\visual studio 2010\projects\assignment\assignment\assignment.cpp(117): error C3867: 'point::gety': function call missing argument list; use '&point::gety' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
When you call a function you need to put parentheses after the function name.
Thanks peter, it is error free now, however when i debug the area is always 0
Let's walk through the code starting at line 119.

* You declare a rectangle r using the default constructor.
* The default constructor sets p1 and p2 to p, which uses the default Point constructor.
* The default Point constructor sets x and y both to 0, so p1 and p2 in the rectangle declared on line 119 are both points with x and y set to 0.
* Line 120 calls the area function.
* H and W in the area function are using the x and y values from the two points. Those are all 0, so H and W will be 0.
* Return H*W, so 0*0 is 0.

My guess is line 119 needs to use the constructor that passes in points. So you will need to create 2 points that have non-zero x and y values, then pass them into the rectangle.

Good luck!
Topic archived. No new replies allowed.