HELP : creating set.h and methods using templates and exceptions

This is a discussion on HELP : creating set.h and methods using templates and exceptions within the C++ Programming forums, part of the General Programming Boards category; hello , can anybody help me write this program? I am expected to create the following classes: • Set<T>: Set.h ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    11

    HELP : creating set.h and methods using templates and exceptions

    hello , can anybody help me write this program?



    I am expected to create the following classes:
    • Set<T>: Set.h


    “Set” is a template class that represents a set in mathematics. A set in this homework can only contain
    items of same type. In other words, if a set is a “set of integers” it cannot contain “char”s, “float”s or
    “double”s etc.


    Methods
    • void add(const T& item)throw(SetException) Adds an item to the set. Throws an instance of “Se-
    tException” class if the “item” already exists. Error code is “ITEM ALREADY EXISTS”, and
    message is “Item already exists”.
    • void remove(const T& item)throw(SetException) Removes an item from the set. Throws an instance
    of “SetException” class if the “item” does not exists. Error code is “ITEM NOT EXISTS”, and
    message is “Item does not exist”.
    • bool exists(const T& item)const Returns true if an item exists in the set, returns false otherwise.
    • int cardinality()const Returns the number of elements in the set.
    • std::vector<T> toSortedVector()const Returns the set as an std::vector with items sorted in in-
    creasing order.


    thannks


    I am only alllowed to use STL algorithms and not allowed to use STL “Set” class!
    Attached Images Attached Images

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    You must be new here - you're supposed to pretend it's not homework, and then we can have fun spotting that it is and telling you to naff off. Otherwise, where's the fun for us?

    We don't do homework for people. Do as much as you can, and come back for specific problems if you run into them. Nobody is going to be bothered doing your assignment for you if you cannot be bothered.

    Now naff off.
    Last edited by JohnGraham; 11-24-2011 at 06:46 AM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    Sir

    I dont want you to do my homework , just asked for helping me do this !!!!!!! I am new here , yes !

    I dont know how to start it , where to write it from ! I am confused ! I can handle it if just be able to write at least add part !

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    can this be true ?
    Code:
    template <class T> class set
    {
    public:
    
    set()
         { length=0; list=0;}
    
    set (int size)
         { length=size; list=new T [size];}
    
    ~set()
         { de;ete [] list; list=0; length=0;}
    
    void add (T x)
         { int k; 
            for (k=0;k<length; k++)
                list[k]=x;
         }
    
    private:
    T listl;
    int length;
    
    };
    Last edited by Salem; 11-24-2011 at 12:35 PM. Reason: code tags

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    >can this be true ?
    Err..what do you mean by 'true' ? ...it doesn't even compile.

    1.Your code does not meet most(if not all) of the expectations in the assignment.
    2.listl should be of type T*..going by your logic .
    3.Did you write the add function with your brain on standby ?...It just replaces every element in the set with the new addition.
    4.The destructor does not need to set the other members to 0;
    Last edited by manasij7479; 11-24-2011 at 11:28 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    By true I just ment "ADD" part of the code ! I know I have not meet the most parts of the assignment ! I dont waste your time with my mind in standby mode ! sorry for being here

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Jeren View Post
    By true I just ment "ADD" part of the code ! I know I have not meet the most parts of the assignment ! I dont waste your time with my mind in standby mode ! sorry for being here
    I meant, that add is supposed to add a new element to the array, not change everything else.
    You have to maintain information about where latest additions took place, check for duplicates by some search method, allocate more memory if necessary...and then copy the 'x' to the now available position.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    how about this? I used vector in this one

    Code:
    
    
    using namespace std;
    #include <iostream>
    #include <vector>
    
    
    
    template <class T> class set
    {
    
    private:
    T *list;
    int length;
    std::vector<T*> v;
    
    
    public:
    
    
    //set()
    //{ length=0; list=0;}
    
    //set (int size)
    //{ length=size; list=new T [size];}
    
    void add (T x)
    { 
    v.push_back(x);
    }
    
    
    void remove (T x)
    { 
    v.pop_back(x);
    }
    
    
    
    void print()
    { 
    std::cout<<v.getName();
    }
    
    
    
    
    
    };
    
    //====================================================
    
    int main()
    {
    
    set<int> s;
    s.add(5);
    s.add(6);
    s.add(7);
    s.print();
    
    return 0;
    
    }
    
    //====================================================
    Last edited by Jeren; 11-24-2011 at 12:51 PM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    errors I get are :

    t.cpp: In member function ‘void set<T>::add(T) [with T = int]’:
    t.cpp:57: instantiated from here
    t.cpp:29: error: invalid conversion from ‘int’ to ‘int*’
    t.cpp:29: error: initializing argument 1 of ‘void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = int*, _Alloc = std::allocator<int*>]’
    t.cpp: In member function ‘void set<T>:rint() [with T = int]’:
    t.cpp:60: instantiated from here
    t.cpp:42: error: ‘class std::vector<int*, std::allocator<int*> >’ has no member named ‘getName’



    how can I solve them????
    Last edited by Jeren; 11-24-2011 at 12:53 PM.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Please... before trying to write code... make sure you understand the problem. Then you'll find that 90% of the work is already done.

    >t.cpp:42: error: ‘class std::vector<int*, std::allocator<int*> >’ has no member named ‘getName'
    It doesn't. I have no idea why you thought it has.

    >t.cpp:29: error: invalid conversion from ‘int’ to ‘int*’
    Well.. your vector stores pointers and you're trying to push integers into it.

    I'd suggest that you try to understand what a set is how to use a set data structure before making one.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    11
    u know what makes me confused? I do not have to include set library and have to implement the methods myself !!!!!

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Jeren View Post
    u know what makes me confused? I do not have to include set library and have to implement the methods myself !!!!!
    But unless you know what you're going to implement, you are NOT going to be able to do so.
    So, take some time to know how a set library should be, then try yourself.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an array with templates
    By Subsonics in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2010, 11:36 AM
  2. Can you inherit Methods into other methods?
    By bobbelPoP in forum C++ Programming
    Replies: 5
    Last Post: 08-02-2008, 08:32 AM
  3. Creating Function Templates
    By marQade in forum C++ Programming
    Replies: 26
    Last Post: 12-03-2007, 03:07 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Exceptions
    By Gelfling in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2002, 03:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21