Like Tree4Likes
  • 1 Post By manasij7479
  • 1 Post By manasij7479
  • 2 Post By Elysia

First own class - suggestions and criticism?

This is a discussion on First own class - suggestions and criticism? within the C++ Programming forums, part of the General Programming Boards category; Hi there, I created my first class as an exercise from my textbook and I would appreciate any comment by ...

  1. #1
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197

    First own class - suggestions and criticism?

    Hi there,

    I created my first class as an exercise from my textbook and I would appreciate any comment by you folks.

    The exercise reads as follows:

    Create a class "IntSet" consisting of two files: IntSet.h and IntSet.cpp. The class is supposed to represent a mathematical set for integers. Only the following simple operations should be possible:

    -void add(int el): add element "el" if it is not already in the set, otherwise do nothing

    -void remove(int el): remove element "el" if it is in the set, otherwise do nothing

    -bool isMember(int el): report if element "el" is in the set or not

    -size_t size(): return the number of elements

    -void display(): write all elements to standard output

    -void clear(): remove all elements from the set

    -int getMax() and int getMin(): return the smallest and the biggest element in the set

    To store the elements please us a vector<int> object.

    And here is my solution so far:

    IntSet.h

    Code:
    #ifndef SET_H
    #define SET_H
    #include <vector>
    #include <cstdlib>
    
    class IntSet {  
    public:
        IntSet() {}
        IntSet(int i);
        IntSet(std::vector<int>& v);
        
        void add(int el);
        void remove(int el);
        bool isMember(int el) const;
        size_t size() const;
        void display() const;
        void clear();
        int getMax() const;
        int getMin() const;
        
    private:
        std::vector<int> set;
    };
    
    
    inline IntSet::IntSet(int i) {
        set.push_back(i);
    }
    
    inline bool IntSet::isMember(int el) const {
        for (size_t i = 0; i < set.size(); ++i) {
            if (set.at(i) == el) return true;
            return false;
        }
    }
    
    inline size_t IntSet::size() const {
        return set.size();
    }
    
    inline void IntSet::clear() {
        set.clear();
    }
    
    inline int IntSet::getMax() const {
        if (set.size() == 0) return 0;
        else return set.at(set.size()-1);
    }
    
    inline int IntSet::getMin() const {
        if (set.size() == 0) return 0;
        else return set.at(0);
    }
    
    #endif

    IntSet.cpp

    Code:
    #include "IntSet.h"
    #include <algorithm>
    #include <iostream>
    
    IntSet::IntSet(std::vector<int>& v) {
        std::sort(v.begin(), v.end());
        for (size_t i = 1; i < v.size(); ++i) {
            if (!(v.at(i) == v.at(i-1))) set.push_back(v.at(i));
        }
    }
    
    void IntSet::add(int el) {
        for (size_t i = 0; i < set.size(); ++i) {
            if (set.at(i) == el) return;
        }
        set.push_back(el);
        std::sort(set.begin(), set.end());
    }
    
    void IntSet::remove(int el) {
        for (size_t i = 0; i < set.size(); ++i) {
            if (set.at(i) == el) {
                set.erase(set.begin() + i);
            }
        }
    }
    
    void IntSet::display() const {
        if (set.size() == 0) {
            std::cout << "\n( )";
        } else {
            std::cout << "\n(";
            for (size_t i = 0; i < set.size(); ++i) {
                std::cout << set.at(i);
                if ((set.size() > 1) && (i < set.size()-1)) {
                    std::cout << ", ";
                }
            }
            std::cout << ")";
        }
    }

    I performed some test operations and the class seems to work as expected. Still I would appreciated any remarks.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,507
    1. Why is the max or min of an empty set 0 ? Throw an exception because the user should have checked the empty function (or size()==0 ..in this case) beforehand.
    2. Why are you changing the argument of IntSet(std::vector<int>& v) within the function. copy and then sort if you want. Make it const ref to be certain you are not violating that.
    3. Is the Single element constructor really necessary?


    Other than that, using std::vector for a set is a really bad idea, but you can't change that, it seems.
    Last edited by manasij7479; 11-02-2013 at 07:06 PM.
    dennis.cpp likes this.
    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 !



  3. #3
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Thanks for the remarks, manasij7479.

    1. I am not happy with that solution too. But I couldn't think of any other idea. I have not learned about exceptions so far, and I wanted the getMin() and getMax() functions to still work correctly if called on an empty set. Is there any other solution without exceptions?
    2. Does it any harm in this example or is it just a "bad practice" think? However, I will revise my idea here and work out an alternative according to your hint.
    3. To be honest, I'm not sure about this either. I thought it somehow more convenient if it was there.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,507
    1. returning 0 is dangerous because it'll result in hard to find bugs.
    Well, other possible solutions are more cumbersome.
    Code:
    std::pair<int,bool> max()
    {
        if(<empty>)return {0,false};
        value =<actually find max>;
        return {value,true};
    }
    //use
    auto x=max();
    if(x.second)<use x.first as max>;
    else <error>;
    Or you could have either one of those as reference arguments instead of return values.
    Code:
    bool max(int& value)
    {
        if(<empty>)return false;
        value=<find max>;
        return true;
    }
    //use
    int x;
    if(max(x))
       <use x>;
    else <error>;
    2. Could cause problems. Suppose you have a vector of ints that has to be stored in some arbitrary order...say marks of 100 students.
    Now, if you construct a set from this vector..the the original list would be changed and everyone would start naming children with names starting with 'Z'
    Last edited by manasij7479; 11-02-2013 at 07:26 PM.
    dennis.cpp likes this.
    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 !



  5. #5
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    I did not really understand your first code snippet, since again it contains things I have not learned about yet, but the second one looks good, though it's not following the exercize exactly.

    As for the second problem, I have thought about it for a while and also got some opinions from a german CPP forum I also follow. I will move the sorting into the display()-function and also sort a local copy of the set. Seems reasonable not to change the original, especially in the constructor.

    Again, thanks for your remarks!
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Suggestions (not necessarily within your reach now, but something you can strive towards):
    - Use exceptions to enforce preconditions on get*-functions (ie, don't allow calling the get*-functions if the set is empty).
    - Fix isMember (it's broken!).
    - Ensure the vector is always sorted after the set is constructed (currently you do that, but it's important for future efficiency, so keep it that way).
    - To find an element, use std::binary_search.
    - To insert elements, for the appropriate position using binary_search, then insert it instead of adding it to the end and sorting it.
    - Use std::find_if and std::copy_if instead of loops. Works perfectly with C++11's new lambdas.
    - Add an overloaded constructor that takes an std::initializer_list (C++11 feature).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewrote my cMap class. Looking for more criticism
    By Raigne in forum C++ Programming
    Replies: 18
    Last Post: 11-07-2008, 01:58 PM
  2. Problem with vector class (and suggestions)
    By cs_student in forum C++ Programming
    Replies: 9
    Last Post: 09-09-2008, 03:21 AM
  3. Looking for criticism on my GameState class
    By Raigne in forum Game Programming
    Replies: 1
    Last Post: 03-21-2008, 10:45 AM
  4. Constructive criticism/suggestions
    By LineOFire in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 10:32 AM
  5. Constructive criticism, suggestions etc
    By BobS0327 in forum C Programming
    Replies: 3
    Last Post: 01-08-2006, 09:35 AM

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