C++ Problem in task

HELLO GUYS is have I solved the task right so far ?
a) First create the files CParkingLot.h and CParkingLot.cpp and declare the class CParkingLot according to the class diagram shown above. Implement the declared getter and setter methods.
b) Implement the private method void generateId (), which assigns a unique value to the m_id attribute of the CParkingLot objects in the specified value range. Use
a local static variable within this method, which you increment after each assignment.
c) Implement the constructor of the CParkingLot class. Note the assurances and also use the default value if the parameter is not in the value range of m_capacity
lies. To initialize m_id, call the generateId () method in the constructor
b) on.
d) Implement the method bool parkVehicle (), which adds exactly one vehicle to the parking lot. The return value should be true if the vehicle still has a parking space; it returns false if the parking lot is already full.
e) Implement the method int parkVehicles (int numVehicles), which adds several vehicles to the parking lot. The return value should be 0, if there are still parking spaces available for all vehicles; if the parking is already full, the number will be
the vehicles returned for which no more parking space is available.
f) Implement the int getFreeSpots () method, which returns the number of free slots.
g) Implement the float getFreeCapacity () method, which returns the percentage of free spaces.



Header:
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


#ifndef CPARKINGLOT_H_
#define CPARKINGLOT_H_
#include<iostream>
using namespace std;

class CParkingLot{
private:
	int m_id;
	int m_capacity;
	int m_curOccupancy = 0;
	void generateId();
	int m_freeSpots;

public:
CParkingLot(int capacity = 80);

int getId();
int getCapacity();
void setCapacity(int capacity);
int getCurOccupancy();
void setCurOccupancy(int curOccupancy);
bool parkVehicle();
int parkVehicles(int numVehicles);
int getFreeSpots();
void print();
friend ostream& operator<< (ostream& lop,const CParkingLot& rop);


};
ostream& operator<< (ostream& lop,const CParkingLot& rop);


#endif /* CPARKINGLOT_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

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

int m_id;

void generateId()
{
	static int id = 100;

	if (id >= 100 && id <= 1000)
	{
		m_id = id;
		id++;
	}
	else
	{
		throw std::runtime_error("too many ids");
	}
}

CParkingLot::CParkingLot(int capacity ){

	if(m_capacity <= 200 ){

		m_capacity = capacity;
    }
	else{
		int capacity = 80;
	}
	generateId();
}

bool CParkingLot::parkVehicle(){

	if( m_curOccupancy == m_capacity   ){
		return false;
	}
	if ( m_curOccupancy < m_capacity){


			m_curOccupancy++;
			return true;
		}


}

int CParkingLot::parkVehicles(int numVehicles){
	if (m_capacity -m_curOccupancy < m_capacity){

			return 0;
		}

		if( m_curOccupancy == m_capacity   ){
			return numVehicles;
		}

	}

int CParkingLot::getFreeSpots(){

	return m_freeSpots;
}

int CParkingLot::getCapacity(){
	return m_capacity;

	}

int CParkingLot::getCurOccupancy(){
	return m_curOccupancy;

}

void CParkingLot::setCurOccupancy(int curOccupancy){
	m_curOccupancy = curOccupancy;
}

void CParkingLot::setCapacity(int capacity){
	m_capacity = capacity;
}

ostream& CParkingLot::operator<< (ostream& lop,const CParkingLot& rop){
	lop << "ID:" << "" << m_id << "" << "Kapazität:" << "" << m_capacity << "" << "frei:" << m_freeSpots << endl;
	return rop;
}

void CParkingLot::print(){
	cout << " Der Parkplatz hat folgende Eigenschaften :" << endl;
	cout << "ID:" << "" << m_id << "" << "Kapazität:" << "" << m_capacity << "" << "frei:" << m_freeSpots << endl;

}




I have also some errors

https://picload.org/view/dcigwcac/bildschirmfoto2019-01-06um22.3.png.html

https://picload.org/view/dcigwcod/bildschirmfoto2019-01-06um15.3.png.html
Please help me ?
Thank you
Last edited on
1
2
3
4
ostream& CParkingLot::operator<< (ostream& lop,const CParkingLot& rop){
	lop << "ID:" << "" << m_id << "" << "Kapazität:" << "" << m_capacity << "" << "frei:" << m_freeSpots << endl;
	return rop;
}

1. You need to return lop, not rop.
2. You need to use rop to access the members.
I am getting still errors on eclipse :
https://picload.org/view/dcirgdgi/bildschirmfoto2019-01-07um10.0.png.html

Have I solved task d) and e ) right?
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
#include <iostream>
#include <exception>
#include "CParkingLot.h"
using namespace std;

int m_id;

void generateId()
{
	static int id = 100;

	if (id >= 100 && id <= 1000)
	{
		m_id = id;
		id++;
	}
	else
	{
		throw std::runtime_error("too many ids");
	}
}

CParkingLot::CParkingLot(int capacity ){

	if(m_capacity <= 200 ){

		m_capacity = capacity;
    }
	else{
		int capacity = 80;
	}
	generateId();
}

bool CParkingLot::parkVehicle(){

	if( m_curOccupancy == m_capacity   ){
		return false;
	}
	if ( m_curOccupancy < m_capacity){


			m_curOccupancy++;
			return true;
		}


}

int CParkingLot::parkVehicles(int numVehicles){
	if (m_capacity -m_curOccupancy < m_capacity){

			return 0;
		}

		if( m_curOccupancy == m_capacity   ){
			return numVehicles;
		}

	}

int CParkingLot::getFreeSpots(){

	return m_freeSpots;
}

int CParkingLot::getCapacity(){
	return m_capacity;

	}

int CParkingLot::getCurOccupancy(){
	return m_curOccupancy;

}

void CParkingLot::setCurOccupancy(int curOccupancy){
	m_curOccupancy = curOccupancy;
}

void CParkingLot::setCapacity(int capacity){
	m_capacity = capacity;
}

ostream& operator<< (ostream& lop,const CParkingLot& rop){
  lop << "ID:" << "" << rop.m_id << "" << "Kapazität:" << "" << rop.m_capacity << "" << "frei:" << rop.m_freeSpots << endl;

 return lop;
}

void CParkingLot::print(){
	cout << " Der Parkplatz hat folgende Eigenschaften :" << endl;
	cout << "ID:" << "" << m_id << "" << "Kapazität:" << "" << m_capacity << "" << "frei:" << m_freeSpots << endl;

}







Now its working :)
Have I solved task d) and e ) right?
The stream operator<< is a friend function. That means it is not a member of CParkingLot. Hence:
1
2
3
4
ostream& CParkingLot::operator<< (ostream& os,const CParkingLot& parking_lot){
	os << "ID:" << "" << parking_lot.m_id << "" << "Kapazität:" << "" << parking_lot.m_capacity << "" << "frei:" << parking_lot.m_freeSpots << endl;
	return os;
}
Remove line 32 in your header

generateId() on the other hand is a member function and should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int m_id;

void CParkingLot::generateId()
{
	static int id = 100;

	if (id >= 100 && id <= 1000)
	{
		m_id = id;
		id++;
	}
	else
	{
		throw std::runtime_error("too many ids");
	}
}


Have I solved task d) and e ) right?
You fullfill d) with:
1
2
3
4
bool parkVehicle()
{
  return 0 == parkVehicles(1);
}

e)
1
2
3
4
5
6
7
8
9
10
11
12
int CParkingLot::parkVehicles(int numVehicles){
	if ((m_curOccupancy + numVehicles) <= m_capacity){
			m_curOccupancy += numVehicles;
			return 0;
		}
	else {
			int result = (m_curOccupancy + numVehicles) - m_capacity;
			m_curOccupancy = m_capacity;
			return result;
		}

	}
Not tested!
Last edited on
bool parkVehicle()
{
return 0 == parkVehicles(1);
}

Why do you only do this in d)?

What happens with parkVehicles(1); WHy do you take 1 ?
I dont understand

Is my way wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool CParkingLot::parkVehicle(){

	if( m_curOccupancy == m_capacity   ){
		return false;
	}
	if ( m_curOccupancy < m_capacity){


			m_curOccupancy++;
			return true;
		}


}  
Why do you only do this in d)?
Parking one vehicle is a special case of parking multiple vehicles. So you don't need to invent another logic.

What happens with parkVehicles(1); WHy do you take 1 ?
Because of this: "adds exactly one vehicle"

Is my way wrong?
No, it is correct and you can use it if you want. Your parkVehicles(...) was wrong.
Here I have also a problem in understanding ?

1
2
3
4
5
6
7
8
9
10
11
12
13

int CParkingLot::parkVehicles(int numVehicles){
	if ((m_curOccupancy + numVehicles) <= m_capacity){
			m_curOccupancy += numVehicles;
			return 0;
		}
	else {
			int result = (m_curOccupancy + numVehicles) - m_capacity;
			m_curOccupancy = m_capacity;
			return result;
		}

	}


int result = (m_curOccupancy + numVehicles) - m_capacity;
With this line I get the numbers of cars which dont get a parking place .


But why do you do :m_curOccupancy = m_capacity;

?
I dont really understand ?
I would only return result . :)
But why do you do :m_curOccupancy = m_capacity;
My assumption is that the vehicles which can actually do take the free lots (even though it is not mentioned). If not it would not make too much sense to return the number of vehicles that could not find a free parking lot.
But why do you copy m_curOccupancy int0 capacity ?
I understand the logic not really :)

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

void generateId()
{
	static int id = 100;

	if (id >= 100 && id <= 1000)
	{
		m_id = id;   
		id++;
	}
	else
	{
		throw std::runtime_error("too many ids");
	}
}


I get her also an error thatDescription Resource Path Location Type
use of undeclared identifier 'm_id' CParkingLot.cpp /SS18 line 21 C/C++ Problem


m_id cant be resolved .

https://picload.org/view/dcidrcgl/bildschirmfoto2019-01-09um14.0.png.html
Last edited on
I get her also an error thatDescription Resource Path Location Type
use of undeclared identifier 'm_id' CParkingLot.cpp /SS18 line 21 C/C++ Problem
This is because generateId() is a member function of CParkingLot. See my first reply.

But why do you copy m_curOccupancy int0 capacity ?
I don't. I modify m_curOccupancy so that it is equal to the capacity. The difference between m_capacity and m_curOccupancy are the free spots. Change getFreeSpots():
1
2
3
4
int CParkingLot::getFreeSpots(){

	return m_capacity - m_curOccupancy; // Remove the member variable m_freeSpots;
}
Otherwise you would always need to change m_freeSpots each time you change m_curOccupancy.
Last edited on
Implement the method void addParkingLot (int capacity, int occupancy), (7) which adds a parking space to the PMS with the appropriate number of parking spaces and occupied parking spaces. Please note that the list of parking spaces may need to be increased by one parking space, as indicated above. Look for the assurances for m_numLots, and if the assertions are violated, you will get an error message on the console.

I already solved the task but I wanted to ask how I could solve the task with vector ?
Is there an easier solution for it ?

Here is my code:

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
#ifndef CPMS_H_
#define CPMS_H_

#include "CParkingLot.h"
#include <string>
#include <vector>


// [2a] 1 Pkt. fuer das Einkommentieren und Erstellung von CPMS.cpp (je 0,5)
class CPMS
{

private:
	CParkingLot* m_pLots;
	int m_numLots;         // {3 <= m_numLots <= 20}
	int m_nextLotIdx;      // naechster, im Array noch nicht zugewiesener Parkplatz


public:
	// Konstruktor mit Uebergabe-Parameter der max. Anzahl an Stellplaetzen
	CPMS(int numLots = 10);

	// Destruktor
	~CPMS();

	// Copy-Konstruktor
	CPMS(const CPMS& orig);

	// Methode, die einen weiteren Parkplatz in die Liste (das Array) einfuegt
	void addParkingLot(int capacity = 80, int occupancy = 0);

	// Operator, ein CPMS-Objekt zuweist
	CPMS& operator = (CPMS& rop);

	// Operator, der einen weiteren Parkplatz in die Liste (das Array) einfuegt
	CPMS& operator += (CParkingLot& rop);

	// Methode, die den Parkplatz mit den meisten verfuegbaren Stellplaetzen zurueckgibt
	CParkingLot* getMostFreeSpots();

	// Methode, die ein Fahrzeug auf dem Parkplatz mit den meisten verfuegbaren
	// Stellplaetzen parkt
	bool parkAtMostFreeSpots();

	// Methode, die die Belegung aller Parkplaetze zurueckgibt
	void getOccupancy(int &capacity, int &occupancy);

	// Methode, die die PMS-Daten und die Liste der Parkplaetze ausgibt
	void print();

	// Methode, die alle geparkten Fahrzeuge auf moeglichst wenige
	// Parkplaetze verteilt
	void consolidate();



};


#endif /* CPMS_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
79
80
81
82

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

CPMS::CPMS(int numLots ){

	if( m_numLots <=20 && m_numLots >=3 ){

	 m_numLots = numLots;
	}
	else {

		m_numLots = 10;
	}
	m_pLots = new CParkingLot[numLots];
	m_nextLotIdx = 0;
}

CPMS::CPMS(const CPMS &orig)
{
	m_numLots = orig.m_numLots;  // 0,5 Pkt
	m_nextLotIdx = orig.m_nextLotIdx;  // 0,5 Pkt

	m_pLots = new CParkingLot[m_numLots];   // 1 Pkt

	for (int i = 0; i<m_nextLotIdx;i++)  // 1 Pkt
	{
		m_pLots[i].setCapacity(orig.m_pLots[i].getCapacity());  // 0,5 Pkt
		m_pLots[i].setCurOccupancy(orig.m_pLots[i].getCurOccupancy());  // 0,5 Pkt

	}
}



void CPMS::addParkingLot(int capacity, int occupancy)
{
	if (m_nextLotIdx < 20)  // 0,5 Pkt
	{

		if (m_nextLotIdx >= m_numLots)  // 0,5 Pkt
		{
			CParkingLot* tempLots = new CParkingLot[m_numLots + 1]; // 1 Pkt.

			for (int i = 0; i < m_numLots; i++)  // 0,5 Pkt
			{
				tempLots[i] = m_pLots[i];  // 0,5 Pkt
			}
			delete[] m_pLots;  // 0,25 Pkt
			m_pLots = tempLots;  // 0,25 Pkt
			m_numLots++; // 0,25 Pkt.

		}

		if (capacity >= 0 && capacity <= 200) // 1 Pkt.
		{
			m_pLots[m_nextLotIdx].setCapacity(capacity);  // 0,25 Pkt
		}
		else
		{
			m_pLots[m_nextLotIdx].setCapacity(80);  // 0,25 Pkt
		}

		if (occupancy >= 0
				&& occupancy <= m_pLots[m_nextLotIdx].getCapacity()) // 1 Pkt.
		{
			m_pLots[m_nextLotIdx].setCurOccupancy(occupancy);  // 0,25 Pkt
		}
		else
		{
			m_pLots[m_nextLotIdx].setCurOccupancy(0);  // 0,25 Pkt
		}

		m_nextLotIdx++;  // 0,25 Pkt
	}
	else
	{
		cout << "CPMS ist an Kapazit‰tsgrenze, Parklatz nicht hinugef¸gt!";  // 0,5 Pkt
	}

}




Can somebody show me how the task might could be solved with vector?
vector would simplify things. Neither the constructor on line 27 nor the assignment operator on line 33 would be needed anymore.

Do see what's wrong with CPMS::CPMS(int numLots )? Hint: Does line 8 make sense? Using numLots on line 16?

1
2
3
4
5
6
7
8
9
class CPMS
{

private:
	std::vector<CParkingLot> m_pLots;
	// Not necessary since vector contains the number
	int m_numLots;         // {3 <= m_numLots <= 20}
	int m_nextLotIdx;      // naechster, im Array noch nicht zugewiesener Parkplatz
...
1
2
3
4
5
6
7
8
9
10
11
12
CPMS::CPMS(int numLots ){

	if( numLots <=20 && numLots >=3 ){

		m_pLots.resize(numLots);
	}
	else {

		m_pLots.resize(10);
	}
	m_nextLotIdx = 0;
}
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
void CPMS::addParkingLot(int capacity, int occupancy)
{
	if (m_nextLotIdx < 20)  // 0,5 Pkt
	{

		if (m_nextLotIdx >= m_pLots.size())  // 0,5 Pkt
		{
			m_pLots.resize(m_pLots.size() + 1);
		}

		if (capacity >= 0 && capacity <= 200) // 1 Pkt.
		{
			m_pLots[m_nextLotIdx].setCapacity(capacity);  // 0,25 Pkt
		}
		else
		{
			m_pLots[m_nextLotIdx].setCapacity(80);  // 0,25 Pkt
		}

		if (occupancy >= 0
				&& occupancy <= m_pLots[m_nextLotIdx].getCapacity()) // 1 Pkt.
		{
			m_pLots[m_nextLotIdx].setCurOccupancy(occupancy);  // 0,25 Pkt
		}
		else
		{
			m_pLots[m_nextLotIdx].setCurOccupancy(0);  // 0,25 Pkt
		}

		m_nextLotIdx++;  // 0,25 Pkt
	}
	else
	{
		cout << "CPMS ist an Kapazit‰tsgrenze, Parklatz nicht hinugef¸gt!";  // 0,5 Pkt
	}

}
if I solve the method with vector ,can I also let the constructor like it is?

Is the method now fully solved with vector ???

has somebody an idea ?
if I solve the method with vector ,can I also let the constructor like it is?
What constructor?
Since a vector is not constructed like an array the constructors will be different.

Is the method now fully solved with vector ???
What do you mean? What method?
this method:
Implement the method void addParkingLot (int capacity, int occupancy), (7) which adds a parking space to the PMS with the appropriate number of parking spaces and occupied parking spaces. Please note that the list of parking spaces may need to be increased by one parking space, as indicated above. Look for the assurances for m_numLots, and if the assertions are violated, you will get an error message on the console.
I don't see anything missing or obviously wrong. So did you test it? Do you have any doubts?
Topic archived. No new replies allowed.