0

I'm working on a template class called Collection that has only and array as a private member of the template class. Also there are other classes that I need to implement, but for now I'm working on one of them which is called Hotel.

I'm supposed to create a template like template class Type,int p_, so when I do call Collection Hotel,5 h; in main funcion, it should create an array of type hotel right? But when I try to do that it just creates an object of type hotel and call its deafult constuctor.

I could use some advice how to set the values for the objects. The hotel class has char* HotelName;int numRooms,openedYear,catagoryHotel.

template <class Type,int p>
class Collection
{
private:
    Type* niz;
    int n;//added this just in case
public:
    Collection();
    ~Collection();
    bool Find(Type t);//checks if the t is in the array with operator!=
    void Arrange();//arranges the array
    void Reverse();//swithces the first memmber of array to the last and the second to p-2
    void Set(int i,Type t);//should set t to the niz[i]
    void SaveElement(int i, char* Dat);//this is for saving in a file
    void ReedElement(char*Dat);//this is for printing on the screan from a file
    //bool operator!=(Type& t,Type& a);//visual studio is telling me 2 many arguments,dont know why

};
class Hotel
{
private:
    char*HotelName;
    int numRooms, openedYear,catagoryHotel;
public:
    Hotel();
    ~Hotel();
    void SetHotel();
    //bool operator <=(Hotel& h1, Hotel& h2);this for some reason doesnt work also
};
//In my constructor for Collection i typed
template<class Type, int p>
inline Collection<Type, p>::Collection()
{
    n = p;
    niz = new Type[n];
}
//in the constructor for Hotel() i typed
Hotel::Hotel()
{
    HotelName = "Undenfined";
    catagoryHotel = 0;//how much stars it has
    openedyear = 0;
    numRooms = 0;
}

This is all I typed for hotel, I didn't want to go any further untill I clear this up.

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
  • 1
    Please take care to indent your code in a way that makes it as readable as possible. Flat indentation (that is, none at all) makes it hard to know exactly where a block begins and where it ends. – François Andrieux Jan 16 '18 at 16:51
  • 1
    *Im suppouse to create a template like "template class Type,int p" so when i do call Collection Hotel,5 h; in main funcion,it should create an array of type hotel right?* No, that is not correct. Please read [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to have a solid foundation. – R Sahu Jan 16 '18 at 16:52
  • im still new to this site,sorry – Vladan Mladenov Jan 16 '18 at 16:52
  • It's not clear to me what exactly you are asking, or what the problem you are encountering is. Do you have a compilation error? Do you have a runtime error? Do you not get the expected result? – François Andrieux Jan 16 '18 at 17:05
  • Does not compile - several typos. openedyear vs openedYear, "char* HotelName" should probably be "std::string HotelName" probably should use Hotel initialization list instead. – 2785528 Jan 16 '18 at 18:06
  • i did include the ,i just put out the code for the classes,i didnt get the result i wanted,i wanted to make an array of any type but when i type Collection h; it just creates and object and puts it into niz.It compiles and doesnt put out any error messeges. – Vladan Mladenov Jan 16 '18 at 18:45
  • @VladanMladenov It's still not clear to me exactly how your expected behavior differs from your observed behavior. – François Andrieux Jan 16 '18 at 19:03
  • Please describe what you observe. *"The program prints XYZ"* is a good direct observation. OTOH *"it just creates an object of type hotel and call its deafult constuctor"* doesn't look like an observation. How do you know this is what has happened? Providing a [mcve] is the best way to get your question answered. – n. m. could be an AI Jan 16 '18 at 20:36

1 Answers1

0

After looking at your code and from the point of view of a design process when looking at your code; you should separate your Collection class template, your Hotel class, and other classes into separate modules - files.

Within your Hotel object You have a default constructor and it has private members, but you have no way to set these values. You could either define a constructor that sets its members or have a function that does this which it appears that you do SetHotel() however it does not take any parameters.

In your class template instead of using a raw T pointer you have several choices.

  • You can have a container of type T {vector, list, etc.} and just contain objects directly if your collection is relatively small for example: numTypes <= 100000. Otherwise you can use the heap: see below.
  • Or you can have a vector of smart pointer(s) shared_ptr or unique_ptr in this case a shared_ptr would be more suitable. This makes memory management much cleaner, easier to read, less chance of having memory leaks, dangling pointers, invalid references, etc.

Examples of your above classes:


Hotel.h

#ifndef HOTEL_H
#define HOTEL_H

#include <string>

class Hotel {
private:
    // char* HotelName; // replace with std::string
    std::string hotelName_;
    unsigned numRooms_; // changed to unsigned since you would not have negative rooms. 
    unsigned openingYear_; // same as above for unsigned as well as made the variable name make more sense.
    unsigned hotelRating_; // same as above for unsigned, also changed name to rating

public:
    Hotel(); // default constructor okay
    Hotel( const std::string& hotelName, unsigned numRooms, unsigned rating );
    ~Hotel() = default; // default destructor okay: set to default - not managing memory via new & delete

    void setHotel( const std::string& hotelName, unsigned numRooms, unsigned rating );
};

#endif // !HOTEL_H

Hotel.cpp

#include "Hotel.h"

// Hotel() - Default Constructor - Enables you to instantiate an "empty" object
Hotel::Hotel() : 
hotelName_( "Undefined" ),
numRooms_( 0 ),
rating_( 0 ) {
}

// Hotel( ... ) - User Defined - Enables you to instantiate an object that requires known data
Hotel::Hotel( const std::string& hotelName, unsigned numRooms, unsigned rating ) :
hotelName_( hotelName ),
numRooms_( numRooms ),
raiting_( rating ) {
}

// ~Hotel() - Default Destructor - Defaulted in header file

void Hotel::setHotel( const std::string& hotelName, unsigned numRooms, unsigned rating ) {
    hotelName_ = hotelName;
    numRooms_  = numRooms;
    rating_    = rating;
}

Collection.h

#ifndef COLLECTION_H
#define COLLECTION_H

#include <vector>
#include <memory>

template<class Type, int p>
class Collection {
private:
    std::vector<std::shared_ptr<Type>> collection_;
    int n;//added this just in case
public:
    Collection();
    ~Collection();

    bool find( Type t ); 
    void arrange(); // without any parameters how do you plan to arrange your container?
    void reverse(); 
    // void set( int i, Type t ); // since I decided to use std::vector<...> changed this to addElement( Type t );
    void addElement( Type t );
    void saveElement( int i, const std::string& dat );
    void readElement( const std::string& dat ); 
    //bool operator!=(Type& t,Type& a); //visual studio is telling me 2 many arguments,dont know why    
};

template<class Type, int p>
inline Collection<Type, p>::Collection() {
    // don't need to do anything since you have
    // an empty std::vector<std::shared_ptr<Type>> 
}

template<class Type, int p>
inline Collection<Type, p>::addElement( Type t ) {
    collection_.emplace_back( new Type( t ) ); // Psudeo
}

#endif // !COLLECTION_H

This is just from a design perspective and as for the issues with your overloaded operators you should ask about them in a separate question.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59