Class Templates - BADLY NEED A HELP

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...
I can't imagine how templates fit into the solution of this problem. Why the emphasis on templates?
Dude, the fact that your teacher named a class they created the same as an STL Container, except with a capital letter, in a project that (as kbw pointed out) does not demonstrate a real world use for templates made me half scream at my monitor; and I'm at work. This is one of the reasons I question if I could ever go back to school.

First things first. You use a function template to declare that a function is going to except as arguments and\or return non-specific data. So I would get rid of the static integers you have in your class.

From what I've read of your code, you should give this a glance: http://www.cplusplus.com/doc/tutorial/templates/

I'll check back here a little later to see if you made any progress.
Now now, except for the fact that using the capacity as a template argument is a bit hard to chew, you can't really expect schools to have real world problems in computer "science" classes. First of all, many of the teachers in schools don't even have real world experience in the first place, and even if they did - do you really think the students would understand real world problems? Really? From what I remember from high school, none of my classes had any particularily high niveau. IMHO all the way up to high school it's more about learning how to learn rather than the actual subjects. The average high school student doesn't even nearly fulfill the conditions that are required for serious work.
@ hanst99: I'm almost 10 years out of high school, and you forget that stuff pretty quick. In the business world I've seen so many people destroyed who don't really deserve it, that it's hard to remember the types of people who are teaching.

@ OP: The "Room" constructor should probably be what initializes your array.

Also, all you're writting is the Room class right? I take it the rest of this code was provided to you?
Last edited on
@crisis025: your teacher need you to use template to create a class "Room" with several functions (methods) in it such as "Set, Pair...".

Why you created classes for each of those methods?

Do you have a compiler? Try to make some coding and then compile it by youself.

My suggestion is: start from easier things, firstly make a normal "Room" class with their methods (Set, Pair...) in, then make it compiling successfully, and then add template in.
Last edited on
Topic archived. No new replies allowed.