Operator <<c++

Pages: 12
Hi can somebody help me to get rid of the errors?
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


#ifndef ASCIISCREENSOLUTION_MYCODE_CRECTANGLE_H_
#define ASCIISCREENSOLUTION_MYCODE_CRECTANGLE_H_

#include <ostream>
using namespace std;

#include "CPoint.h"
#include "CScreen.h"

/**
 * Diese Klasse beschreibt ein Rechteck in einem
 * Ganzzahl-Koordinatensystem �ber die Eigenschaften
 * "untere linke Ecke" und "obere rechte Ecke".
 *
 * Als zus�tzliche Eigenschaft hat die Klasse ein Zeichen
 * (char) das bei der graphischen Darstellung als
 * F�llzeichen verwendet wird.
 */
class CRectangle
{
private:
	/** Die linke untere Ecke. */
	CPoint m_bottomLeft;
	/** Die rechte obere Ecke. */
	CPoint m_topRight;
	/** Das F�llzeichen f�r die graphische Darstellung. */
	char m_fillChar;

public:
	/**
	 * Erzeugt ein neues Rechteck mit linker unterer und
	 * rechter oberer Ecke bei (0,0) und dem angegebenen
	 * F�llzeichen.
	 */
	CRectangle(char fillChar = '#');

	/**
	 * Erzeugt ein neues Rechteck mit der angegebenen linken
	 * unteren und rechten oberen Ecken sowie dem angegebenen
	 * F�llzeichen.
	 *
	 * Beim Erzeugen wird die Zusicherung �berpr�ft, dass die
	 * rechte obere Ecke rechts von und oberhalt der linken
	 * unteren Ecke liegen muss! Falls die x-Koordinate
	 * der rechten oberen Ecke nicht gr��er als die x-Koordinate
	 * der linken unteren Ecke ist, wird sie auf den Wert
	 * der x-Koordinate der linken unteren Ecke gesetzt. Falls
	 * die y-Koordinate der rechten oberen Ecke nicht gr��er als
	 * die y-Koordinate der linken unteren Ecke ist, wird sie auf
	 * dem Wert der y-Koordinate der linken unteren Ecke gesetzt.
	 */
	CRectangle(CPoint bottomLeft, CPoint topRight, char fillChar = '#');

	/**
	 * Weist den Eigenschaften "linke untere Ecke" und "rechte obere
	 * Ecke" neue Werte zu.
	 *
	 * Vor der Zuweisung wird die Zusicherung �berpr�ft, dass die
	 * rechte obere Ecke rechts von und oberhalt der linken
	 * unteren Ecke liegen muss! Ist das nicht der Fall wird keines
	 * der Attribute ver�ndert.
	 */
	void setCorners (CPoint bottomLeft, CPoint topRight);

	/**
	 * Liefert die linke untere Ecke des Rechtecks zur�ck.
	 */
	CPoint getBottomLeftCorner() const;

	/**
	 * Liefert die rechte obere Ecke des Rechtecks zur�ck.
	 */
	CPoint getTopRightCorner() const;

	/**
	 * Weist dem F�llzeichen den angegebene Wert zu.
	 */
	void setFillChar(char fillChar = '#');

	/**
	 * Liefert den Wert des F�llzeichens.
	 */
	char getFillChar() const;

	/**
	 * Pr�ft, ob die beiden Rechtecke in allen Eigenschaften �bereinstimmen.
	 */
	bool operator== (const CRectangle& other) const;

	/**
	 * Zeichnet das Rechteck in das �bergebene Bildschirmobjekt. Das heisst,
	 * es werden mit CScreen::setPoint die Werte aller "Punkte", die im 
	 * Bereich des Rechtecks liegen, auf das F�llzeichen des Rechtecks gesetzt.
	 */
	void draw(CScreen& screen) const;

	/**
	 * Gibt eine textuelle Darstellung des Rechtecks auf dem �bergebenen
	 * Ausgabestrom aus. Das Format ist in der Aufgabenstellung beschrieben.
	 */
	friend ostream& operator<< (ostream& lhs, const CRectangle& rhs);
};

#endif /* ASCIISCREENSOLUTION_MYCODE_CRECTANGLE_H_ */ 




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
#include "CRectangle.h"
#include "CPoint.h"

CRectangle::CRectangle(char fillChar)
{
	m_fillChar = fillChar;
}

CRectangle::CRectangle(CPoint bottomLeft, CPoint topRight, char fillChar)
{

	m_bottomLeft = bottomLeft;
	m_topRight = topRight;
	m_fillChar = fillChar;
	if(topRight.getX() < bottomLeft.getX()){

		topRight.setX(bottomLeft.getX());




	}

	if(topRight.getY() < bottomLeft.getY()){

			topRight.setY(bottomLeft.getY());




		}

}

void CRectangle::setCorners(CPoint bottomLeft, CPoint topRight)
{
	if (bottomLeft.getX()<= topRight.getX() && bottomLeft.getY() <= topRight.getY()) {
			m_bottomLeft = bottomLeft;
			m_topRight = topRight;
		}
}

CPoint CRectangle::getBottomLeftCorner() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_bottomLeft;
}

CPoint CRectangle::getTopRightCorner() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_topRight;
}

void CRectangle::setFillChar(char fillChar)
{
	m_fillChar = fillChar;
}

char CRectangle::getFillChar() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_fillChar;
}

bool CRectangle::operator ==(const CRectangle& other) const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return false;
}

void CRectangle::draw(CScreen& screen) const
{
	// Bitte implementieren
}
ostream& operator<< (ostream& lhs, const CRectangle& rhs){

	lhs << "CRectangle(" << bottomLeft.getX() << "," << bottomLeft.getY() << "),(" << topRight.getX() <<","<< topRight.getY()<< fillChar  << endl;}


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


class CPoint
{
private:
	/** Die x-Koordinate (Spalte) */
	int m_x;
	/** Die y-Koordinate (Zeile) */
	int m_y;

public:
	/**
	 * Erzeugt ein neues Objekt mit den angegeben
	 * Werten f�r die x- und y-Koordinate.
	 */
	CPoint(int x = 0, int y = 0);

	/**
	 * Setzt die x-Koordinate auf den angegebenen Wert.
	 */
	void setX(int x);

	/**
	 * Setzt die y-Koordinate auf den angegebenen Wert.
	 */
	void setY(int y);

	/**
	 * Liefert den Wert der x-Koordinate.
	 */
	int getX() const;

	/**
	 * Liefert den Wert der y-Koordinate.
	 */
	int getY() const;

	/**
	 * Pr�ft, ob die x- und y-Koordinaten der Punkte �bereinstimmen.
	 */
	bool operator== (const CPoint& other) const;

	/**
	 * Pr�ft, ob sich die x- und y-Koordinaten der Punkte unterscheiden.
	 */
	bool operator!= (const CPoint& other) const;
};

#endif /* ASCIISCREENSOLUTION_MYCODE_CPOINT_H_ */





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



#include "CPoint.h"

CPoint::CPoint(int x, int y)
{
	m_x = x;
	m_y = y;
}

void CPoint::setX(int x)
{
	m_x = x;
}

void CPoint::setY(int y)
{
	m_y = y;
}

int CPoint::getX() const
{
	return m_x;
}

int CPoint::getY() const
{
	return m_y;
}

bool CPoint::operator ==(const CPoint& other) const
{
	return m_x == other.m_x && m_y == other.m_y;
}

bool CPoint::operator !=(const CPoint& other) const
{
	return m_x != other.m_x || m_y != other.m_y;
}





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


#include "CPoint.h"

/**
 * Dies Klasse repr�sentiert einen Bereich von 80x24 "Punkten",
 * die als ein beliebiges Zeichen dargestellt werden k�nnen.
 */
class CScreen
{
private:
	/** Der Speicher f�r die Darstellung der Punkte. */
	char m_content[24*80];

public:
	/**
	 * Erzeugt eine neue Darstellung, bei der alle Punkte
	 * auf den Wert '.' gesetzt sind.
	 */
	CScreen();

	/**
	 * Setzt den angegebenen Punkt auf das angegebene Zeichen.
	 */
	void setPoint(CPoint point, char content);

	/**
	 * Gibt die Darstellung aus.
	 */
	void print() const;

	/**
	 * L�scht die Darstellung. Alle Zeichen werden wieder auf
	 * '.' gesetzt.
	 */
	void clear();
};

#endif /* ASCIISCREENSOLUTION_MYCODE_CSCREEN_H_ */





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

#include <iostream>
using namespace std;

#include "CScreen.h"

CScreen::CScreen()
{
	clear();
}

void CScreen::setPoint(CPoint point, char content)
{
	if (point.getX() < 0 || point.getY() < 0
			|| point.getY() * 80 + point.getX() >= 24*80) {
		return;
	}
	m_content[point.getY() * 80 + point.getX()] = content;
}

void CScreen::print() const
{
	cout << "    ";
	for (int col = 0; col < 80; col++) {
		cout << col / 10;
	}
	cout << endl;
	cout << "    ";
	for (int col = 0; col < 80; col++) {
		cout << col % 10;
	}
	cout << endl;
	for (int row = 23; row >= 0; row--) {
		if (row < 10) {
			cout << ' ';
		}
		cout << row << ": ";
		for (int col = 0; col < 80; col++) {
			cout << m_content[row * 80 + col];
		}
		cout << endl;
	}
	cout << endl;
}

void CScreen::clear() {
	for (int i = 0; i < 24*80; i++) {
		m_content[i] = '.';
	}
}







The other codes were given and I have an error in CRectangle.cpp

How can I fix the operator<<?
The task is

Implement the operator <<. The output should be in the format CRectangle [(<blX>, <blY>), (<trX>, <trY>), '<fillChar>']
(without a final line break). Where "<blX>" stands for "bottom left X (- coordinate)" ("<trY>") corresponding to "top right Y (coordinate)"). For a rectangle with left lower corner at (1,2) and upper right corner at (3,4) and pad '#' would result in:
CRectangle [(1,2), (3,4), '#']
How can I fix the operator<<?
When posting a question, you should post the least amount of code necessary to demonstrate the problem. Dumping large amounts of code like that into the forum is just going to ensure fewer people will even look at it. You also don't describe what your code is doing. You just say "how can I fix it?" Is it broken? Does it not compile? Does it not produce correct output? If you have an error message, why not post it? Am I supposed to guess what it is?

Here is a quick example of how to overload the ostream operator<<, which is more or less what you already have so I'm not sure what's wrong without more information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

struct Point {
   int x;
   int y;
};

std::ostream& operator<<(std::ostream& out, const Point& p) {
   return out << "(" << p.x << ", " << p.y << ")";
}

int main() {
   Point p{10, 20};
   std::cout << p << std::endl;
   return 0;
}


Actually, on second glance it appears you're missing a return statement. The return statement is necessary from the operator<< because the expression must evaluate to the same ostream& so it can be chained together. If it didn't return, you couldn't do std::cout << myRectangle << "something else"; because as this is evaluated from left to right, the expression std::cout << myRectangle would have a type of void and you cannot apply operators to void. For that reason, ensure that your operator<< functions return the ostream&.
Description Resource Path Location Type
Invalid overload of 'endl' CRectangle.cpp /Rectangle line 85 Semantic Error
Method 'getX' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
Method 'getX' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
Method 'getY' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
Method 'getY' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
non-const lvalue reference to type 'std::__1::ostream' (aka 'basic_ostream<char>') cannot bind to a value of unrelated type 'const CRectangle' CRectangle.cpp /Rectangle line 86 C/C++ Problem
Symbol 'bottomLeft' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
Symbol 'bottomLeft' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
Symbol 'fillChar' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
Symbol 'topRight' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
Symbol 'topRight' could not be resolved CRectangle.cpp /Rectangle line 85 Semantic Error
use of undeclared identifier 'bottomLeft' CRectangle.cpp /Rectangle line 85 C/C++ Problem
use of undeclared identifier 'fillChar' CRectangle.cpp /Rectangle line 85 C/C++ Problem
use of undeclared identifier 'topRight' CRectangle.cpp /Rectangle line 85 C/C++ Problem
make: *** [CRectangle.o] Error 1 Rectangle C/C++ Problem



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

#include "CRectangle.h"
#include "CPoint.h"

CRectangle::CRectangle(char fillChar)
{
	m_fillChar = fillChar;
}

CRectangle::CRectangle(CPoint bottomLeft, CPoint topRight, char fillChar)
{

	m_bottomLeft = bottomLeft;
	m_topRight = topRight;
	m_fillChar = fillChar;
	if(topRight.getX() < bottomLeft.getX()){

		topRight.setX(bottomLeft.getX());




	}

	if(topRight.getY() < bottomLeft.getY()){

			topRight.setY(bottomLeft.getY());




		}

}

void CRectangle::setCorners(CPoint bottomLeft, CPoint topRight)
{
	if (bottomLeft.getX()<= topRight.getX() && bottomLeft.getY() <= topRight.getY()) {
			m_bottomLeft = bottomLeft;
			m_topRight = topRight;
		}
}

CPoint CRectangle::getBottomLeftCorner() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_bottomLeft;
}

CPoint CRectangle::getTopRightCorner() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_topRight;
}

void CRectangle::setFillChar(char fillChar)
{
	m_fillChar = fillChar;
}

char CRectangle::getFillChar() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_fillChar;
}

bool CRectangle::operator ==(const CRectangle& other) const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return false;
}

void CRectangle::draw(CScreen& screen) const
{
	// Bitte implementieren
}
ostream& operator<< (ostream& lhs, const CRectangle& rhs){

	lhs << "CRectangle(" << bottomLeft.getX() << "," << bottomLeft.getY() << "),(" << topRight.getX() <<","<< topRight.getY()<< fillChar  << endl;
	return rhs;
}



Still the error exist?
Last edited on
OK, so first off you're not returning the ostream&, you're returning the CRectangle&. You need to return lhs, not rhs.

You also seem to be a bit confused as to where operator<< lives. This is not a member function of CRectangle, you cannot directly access bottomLeft or topRight from this function. It looks like you'll need something more like rhs.getBottomLeft().getX() in those instances.
Shouldnt I write rhs.getBottomLeftCorner().getX() ?

Shouldnt I write rhs.getBottomLeftCorner().getX() ?


Yes, which goes back to why you shouldn't post so much code when asking a question. Write a new program that demonstrates the problem, something that fits in a single file on a single screen. I didn't want to scroll back up and find the name and I mis-remembered it.
Now I wanted to test it .

Task:
In main.cpp, create a rectangle1 rectangle with the lower left corner (5,5) and the upper right corner (25,15) and the filler character '#'. Output the rectangle using operator << on the console.


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>		// Header f�r die Standard-IO-Objekte (z.B. cout, cin)
#include <stdlib.h>
// TODO: F�gen Sie hier weitere ben�tigte Header-Dateien der
// Standard-Bibliothek ein z.B.
// #include <string>

using namespace std;	// Erspart den scope vor Objekte der
						// C++-Standard-Bibliothek zu schreiben
						// z.B. statt "std::cout" kann man "cout" schreiben

// Inkludieren Sie hier die Header-Files Ihrer Klassen, z.B.
// #include "CFraction.h"
#include "CPoint.h"
#include "CRectangle.h"
#include "CScreen.h"


// Hauptprogramm
// Dient als Testrahmen, von hier aus werden die Klassen aufgerufen
int main (void)
{
    // TODO: Tragen Sie hier Ihren Namen, die Matrikelnummer und die Rechnernummer ein
	cout << "Name: Michael Lipp, Matrikelnummer: <Matrikelnummer>" << endl << endl;
	CRectangle rectangle1(5,5,25,15,'#');

	return 0;
}




Showing error
Description Resource Path Location Type
no matching constructor for initialization of 'CRectangle' main.cpp /Rectangle line 29 C/C++ Problem

Why?
CRectangle rectangle1(5,5,25,15,'#');

You have no constructor that takes in (int, int, int, int, char).
perhaps you want to use CRectangle::CRectangle(CPoint bottomLeft, CPoint topRight, char fillChar)
and give your constructor 2x CPoints instead of 4x ints.
Last edited on
You don't have a constructor that takes 5 arguments, you only have one that takes 1 and 3 arguments. Your declaration should look more like this: CRectangle r{{5,5},{25,15},'#'};. Note two things: the use of the newer initializer syntax with braces, and inside that are more initializers to create points. The more verbose equivalent of this would be CRectangle r(CPoint(5,5), CPoint(15,25), '#');.
* Check if the two rectangles match in all properties.
* /
bool operator == (const CRectangle & other) const;

Which values should I compare ? if(bottomLeftCornergetX() == topRightCornergetx()


Or which value should I compare?
You should overload operator== on CPoint to compare both coordinates, and then say something like return bottomLeftCorner == other.bottomLeftCorner && topRightCorner == other.topRightCorner). I'm not really sure why you would compare bottomLeft to topRight.
Sorry for the late reply.
I am getting errors now
Description Resource Path Location Type
Field 'BottomLeftCorner' could not be resolved CRectangle.cpp /Rectangle line 75 Semantic Error
Field 'TopRightCorner' could not be resolved CRectangle.cpp /Rectangle line 75 Semantic Error
no member named 'BottomLeftCorner' in 'CRectangle' CRectangle.cpp /Rectangle line 75 C/C++ Problem
no member named 'TopRightCorner' in 'CRectangle' CRectangle.cpp /Rectangle line 75 C/C++ Problem
no viable conversion from returned value of type 'const CPoint' to function return type 'bool' CRectangle.cpp /Rectangle line 76 C/C++ Problem
no viable conversion from returned value of type 'const CPoint' to function return type 'bool' CRectangle.cpp /Rectangle line 77 C/C++ Problem
Symbol 'BottomLeftCorner' could not be resolved CRectangle.cpp /Rectangle line 75 Semantic Error
Symbol 'TopRightCorner' could not be resolved CRectangle.cpp /Rectangle line 75 Semantic Error
use of undeclared identifier 'BottomLeftCorner' CRectangle.cpp /Rectangle line 75 C/C++ Problem
use of undeclared identifier 'TopRightCorner' CRectangle.cpp /Rectangle line 75 C/C++ Problem
make: *** [CRectangle.o] Error 1 Rectangle C/C++ Problem
No return, in function returning non-void CRectangle.cpp /Rectangle line 73 Code Analysis Problem


1
2
3
4
5
6
7
8
9
10
11

bool CRectangle::operator ==(const CRectangle& other) const
{
	if(BottomLeftCorner == other.BottomLeftCorner && TopRightCorner == other.TopRightCorner){
		return m_bottomLeft;
		return m_topRight;


	}

}


Have I returned the right values?
That function makes no sense. You should return a bool, in accordance with your return type. Why are you trying to return two things, m_bottomLeft and m_topRight?

I get the feeling you're just trying things, hoping that they will eventually work, without understanding why they should work or not. For example, earlier you asked about comparing the top left corner and bottom right corner... why would you ever do this?

None of the code you're posted so far shows that you have a member variable called "BottomLeftCorner" or "TopRightCorner". You never defined them. You do, however, have two functions called "getBottomLeftCorner" and "getTopRightCorner". I assume you mean to call those. If you don't understand this, or how to call functions, I suggest going back to the basics before you try to tackle this.

The comparison itself is a boolean value.
Basic format:
1
2
3
4
if (x == y)
    return true;
else
    return false;

Is equivalent to:
 
return x == y;


Return the comparison, using your properly called functions mentioned before. Don't return m_bottomLeft or some other random variable. Use that hint to try to figure out what you should be doing inside your function. You're close.
Last edited on
Check the correct handling of the assertion in the constructor by creating a rectangle in main.cpp with the lower left corner (10,20) and the upper right corner (15,15) (default value for filler characters). Output the rectangle using operator << on the console. Use CRectangle :: operator == to check whether the new rectangle has the expected properties and, if successful or failing, issue a corresponding message.

How do I test operator == now in main?
I never did that? In class they dont really show us how to programme ,so that I have not so much knowledge.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool CRectangle::operator ==(const CRectangle& other) const
{
	if(m_bottomLeft == other.m_bottomLeft&& m_topRight== other.m_topRight){
		return true;


	}
	else{

		return false;
	}

}



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>		// Header f�r die Standard-IO-Objekte (z.B. cout, cin)
#include <stdlib.h>
// TODO: F�gen Sie hier weitere ben�tigte Header-Dateien der
// Standard-Bibliothek ein z.B.
// #include <string>

using namespace std;	// Erspart den scope vor Objekte der
						// C++-Standard-Bibliothek zu schreiben
						// z.B. statt "std::cout" kann man "cout" schreiben

// Inkludieren Sie hier die Header-Files Ihrer Klassen, z.B.
// #include "CFraction.h"
#include "CPoint.h"
#include "CRectangle.h"
#include "CScreen.h"


// Hauptprogramm
// Dient als Testrahmen, von hier aus werden die Klassen aufgerufen
int main (void)
{
    // TODO: Tragen Sie hier Ihren Namen, die Matrikelnummer und die Rechnernummer ein
	cout << "Name: Michael Lipp, Matrikelnummer: <Matrikelnummer>" << endl << endl;
	CRectangle r(CPoint(5,5), CPoint(15,25), '#');
	CRectangle r2(CPoint(5,5), CPoint(25,15), '#');

	cout << r << endl;
	cout << r2 << endl;
}


I dont know how I can test operator ==?
I have gone a little further in the task but I dont really know ,if my constructor is right?

Implement the constructor, taking into account the given initial values ​​and validity ranges. In the event that a validity range is violated, the value should be set to the default value. Allocate the memory for the CRectangle objects.



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
#ifndef CDRAWING_H_
#define CDRAWING_H_
#include "CPoint.h"
#include "CScreen.h"
#include "CRectangle.h"

class CDrawing{
private:
	CRectangle* m_rectangles;
	int m_nextFree = 0;
	int m_arraySize;

public:
	CDrawing(int initialSize =10);
	 ~CDrawing();
	void add(const CRectangle& rectangle);
	void print();
	void drawFiltered( CScreen& screen, char filter = 0);



};




#endif /* CDRAWING_H_ */



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
#include<iostream>
using namespace std;
#include"CDrawing.h"

CDrawing::CDrawing(int initialSize ){


if(initialSize >10 || initialSize < 0){
	initialSize = 10;


}

	m_rectangles = new CRectangle [m_arraySize];




}

CDrawing::~CDrawing(){

	delete [] m_rectangles;
}




Is the constructor right?

Here the class Diagramm:
https://www.pic-upload.de/view-35665355/Bildschirmfoto2018-07-21um17.51.57.png.html
Close, but it doesn't seem to be exactly correct. Hint: What is the value of m_arraySize on line 14 of your last code snippet? That's the value that actually matters, changing initialSize doesn't do anything because that variable only exists within your constructor.

To test your == operator, you need to use it between two rectangle objects.

1
2
3
4
	CRectangle r(CPoint(5,5), CPoint(15,25), '#');
	CRectangle r2(CPoint(5,5), CPoint(25,15), '#');
	if (r == r2) { std::cout << "equal\n"; }
	else { std::cout << "not equal\n"; }
Last edited on
hmm the value of arraySize is not given ?

nextFree is the counting variable of the next free Position.

Should I also initialize array Size in the Constructor?
This would be my idea

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;
#include"CDrawing.h"

CDrawing::CDrawing(int initialSize, int arraySize ){

m_arraySize = arraySize;
if(m_arraySize <0){

    initialSize = 10;
	m_arraySize = initialSize;


}
else{

	m_rectangles = new CRectangle [m_arraySize];
}


}
Last edited on
I think I have it now?

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

#include<iostream>
using namespace std;
#include"CDrawing.h"

CDrawing::CDrawing(int initialSize){

	m_arraySize = initialSize;


if(m_arraySize < 1){

    m_arraySize = 10;


}


	m_rectangles = new CRectangle [m_arraySize];



}

CDrawing::~CDrawing(){

	delete [] m_rectangles;
}
Pages: 12