Hi:)
I need a help with finishing my project using class templates.
The problem is:
I have to write a class Room that represents a room with D=10 numbered tables.
class Room should have:
a) constructor
b) Set<Marriage,N*D> getPairs() const ;
// function that returns information about all pairs sitting in the room
(max number of pairs is N*D=4*10=40, where N=4 is a number of pairs
sitting by one table and D=10 is a number of tables)
c) Pair<Marriage*,int> find(const std::string& surname1, const std::string& surname2) const ;
// function that returns a table number where a pair is sitting (surname is
given)
d) Set< Pair<Marriage*,int>, N*D > findAll(const std::string& surname1, const std::string& surname2) const ;
// function that returns set containing all pairs and table numbers where
they are sitting (surname is given)
Please help me because I really, really need your help...
This is what I've done so far:
a) template class Pair
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
|
#include <ostream>
#ifndef __PAIR_H__
#define __PAIR_H__
template<class T1, class T2>
struct Pair {
Pair();
Pair(const T1& a_first, const T2& a_second);
T1 first;
T2 second;
};
template<class T1, class T2>
Pair<T1,T2>::Pair()
: first(T1()), second(T2())
{}
template<class T1, class T2>
Pair<T1,T2>::Pair(const T1& a_first, const T2& a_second)
: first(a_first), second(a_second)
{}
template<class T1, class T2>
std::ostream& operator<< (
std::ostream& out, const Pair<T1,T2>& a_pair
)
{
out << "(" << a_pair.first << ";" << a_pair.second << ")";
return out;
}
#endif
|
b) class Person
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
|
#include <string>
#include <ostream>
#ifndef __PERSON_H__
#define __PERSON_H__
class Person
{
public:
Person(
const std::string& a_name,
const std::string& a_surname,
) : m_name(a_name),
m_surname(a_surname),
{}
std::string getName() const
{
return m_name;
}
void setName(const std::string& a_name )
{
m_name = a_name;
}
std::string getSurname() const
{
return m_surname;
}
void setSurname(const std::string& a_surname )
{
m_surname = a_surname;
}
private:
std::string m_name;
std::string m_surname;
};
inline
std::ostream& operator<< (
std::ostream& out, const Person& a_person
)
{
out << a_person.getName();
out << a_person.getSurname();
return out;
}
#endif
|
c) template class Array
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
|
#include <stdexcept>
#include <ostream>
#ifndef __ARRAY_H__
#define __ARRAY_H__
template<class T, int CAPACITY>
class Array
{
public:
T& operator[](int index)
{
testIndex(index);
return m_data[index];
}
const T& operator[](int index) const
{
testIndex(index);
return m_data[index];
}
int capacity() const
{
return CAPACITY;
}
private:
void testIndex(int index) const
{
if(index<0 || index>=CAPACITY)
throw std::out_of_range("Array::operator[] - index out of range");
}
T m_data[CAPACITY];
};
#endif
|
d) template class Set
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
|
#include "Array.h"
#ifndef __SET_H__
#define __SET_H__
template<class T, int CAPACITY>
class Set
{
public:
Set() : m_size(0) {}
// ma umozliwiac wstawianie elementu do zbioru na pierwsze wolne miejsce (w przypadku braku miejsca zwraca false)
bool insert(const T& element)
{
this->getElements()[m_size++] = element;
}
// ma zwracac faktyczna ilosc elementow w zbiorze
int size() const
{
return m_size;
}
Array<T,CAPACITY>& getElements()
{
return this->m_elements;
}
// ma zwracac element na podanej pozycji lub NULL, jesli podana pozycja jest nieprawidlowa
T& operator[](int index)
{
if(index<0 || index>=size())
throw std::out_of_range("Size::operator[] - index out of range");
return this->getElements()[index];
}
private:
Array<T,CAPACITY> m_elements;
int m_size;
};
#endif
|
e) class Room beginning
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
|
#include "Set.h"
#ifndef __ROOM_H__
#define __ROOM_H__
class Room
{
public:
const static int N = 10;
const static int D = 4;
typedef Pair<Person,Person> Marriage;
typedef Array<Marriage*,D> Table;
Room()
{
};
Set<Marriage*,N*D> getPairs() const
{
};
Pair<Marriage*,int> find(const std::string& surname1, const std::string& surname2) const
{
};
Set< Pair<Marriage*,int>, N*D > findAll(const std::string& surname1, const std::string& surname2) const
{
};
private:
Array<Table,10> m_tables;
};
#endif
|
f) main class
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
|
#include "Pair.h"
#include "Person.h"
#include "Array.h"
#include "Set.h"
#include "Room.h"
#include <iostream>
typedef Pair<Person,Person> Marriage;
typedef Array<Marriage*,4> Table;
using namespace std;
int main()
{
Person ona1("Ania","Kowalska");
Person on1("Adam","Kowalski");
Person ona2("Maria","Nowak");
Person on2("Wojciech","Nowak");
Marriage m1(ona1,on1);
Marriage m2(ona2,on2);
Table t1;
t1[0] = &m1;
t1[1] = &m2;
cout << m1 << endl;
cout << m2 << endl;
cout << ona4.getPesel();
//system("pause");
return 0;
}
|
Please help me finish class Room because I BADLY need a HELP...