Need turotial/help class array/vector

This is a discussion on Need turotial/help class array/vector within the C++ Programming forums, part of the General Programming Boards category; It has become apparent to me that I do not understand the rleation ship of a class array and a ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    Thumbs up Need turotial/help class array/vector

    It has become apparent to me that I do not understand the rleation ship of a class array and a vector.

    Assume I have the following and I want to change it to vector format
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class School
    { 
    public:
        School ();
        School (string,double);
        string name;
        float tuition;
    };
    School::School()
    {
        name="";
        tuition=0.;
    }
    School::School(string n,double t)
    {
         name=n;
         tuition= t;
    }
    
    int main ()
    {
         School mySchool [5] = {School ("John", 10.),
                                              School ("Jim", 11.),
                                              School ("Greg", 12.),
                                               School ("Spencer", 13.),
                                              School ("Joan", 14.)};
    int i=0;
    while (i < 5)
    {
       cout << mySchool[i].name << "  " << mySchool[i].tuition << endl;
       ++i;
    }
    
    
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Give me some options. Assume I am a coplete idiot.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class School
    { 
    public:
        School ();
        School (string,double);
        string name;
        float tuition;
    };
    School::School()
    {
        name="";
        tuition=0.;
    }
    School::School(string n,double t)
    {
         name=n;
         tuition= t;
    }
    
    int main ()
    {
         vector<School> mySchool;
         mySchool.push_back(School ("John", 10.));
         mySchool.push_back(School ("Jim", 11.)),
         mySchool.push_back(School ("Greg", 12.));
         mySchool.push_back(School ("Spencer", 13.));
         mySchool.push_back(School ("Joan", 14.));
       
         int i=0;
         while (i < mySchool.size()) {
            cout << mySchool[i].name << "  " << mySchool[i].tuition << endl;
            ++i;
         }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    thanks.

    Thanks, I will work on and try to get a tougher question.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    getting sort to work

    I am using bloodshed C

    I got the reverse to work in the following sample, but the sort blows all to h* in the compile. Where did I go wrong
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    class School
    { 
    public:
        School ();
        School (string,double);
        string name;
        float tuition;
    };
    School::School()
    {
        name="";
        tuition=0.;
    }
    School::School(string n,double t)
    {
         name=n;
         tuition= t;
    }
    
    int main ()
    {
           vector<School> mySchool;
         mySchool.push_back(School ("John", 10.));
         mySchool.push_back(School ("Jim", 11.)),
         mySchool.push_back(School ("Greg", 12.));
         mySchool.push_back(School ("Spencer", 13.));
         mySchool.push_back(School ("Joan", 14.));
      
        int i=0;
         while (i < mySchool.size()) {
            cout << mySchool[i].name << "  " << mySchool[i].tuition << endl;
            ++i;
         }
          cout << endl;
       reverse(mySchool.begin(),mySchool.end());
         i=0;
         while (i < mySchool.size()) {
            cout << mySchool[i].name << "  " << mySchool[i].tuition << endl;
            ++i;
         }
         cout <<endl;
        sort(mySchool.begin(),mySchool.end());
         i=0;
         while (i < mySchool.size()) {
            cout << mySchool[i].name << "  " << mySchool[i].tuition << endl;
            ++i;
         }
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    That is because sort has no idea how to sort a School only basic types. Here is a better School class and I also implemented a sorting function for you it is commented so you should be able to figure out what is going on.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    class School
    {
        public:
            School();
            School(const std::string &toName, const double &setTuition);
            const std::string &Get_Name() const;
            const double &Get_Tuition() const;
            void Set_Name(const std::string &toName);
            void Set_Tuition(const double &setTuition);
        private:
            std::string schoolName;
            double tuition;
    };
    
    //Default construtor just sets everything to "nothing"
    School::School()
    {
        schoolName = "";
        tuition = 0.0;
    }
    //Copy Constructor that takes the name and double passed to it and
    //sets them as the School's name and tuition.
    School::School(const std::string &toName, const double &setTuition)
    {
        schoolName = toName;
        tuition = setTuition;
    }
    
    //Returns the current name of our school
    const std::string &School::Get_Name() const
    {
        return schoolName;
    }
    
    //Returns the current tuition
    const double &School::Get_Tuition() const
    {
        return tuition;
    }
    
    //Sets the name of our school
    void School::Set_Name(const std::string &toName)
    {
        schoolName = toName;
    }
    
    //Takes the double passed to it and sets the tuition to
    //it
    void School::Set_Tuition(const double &setTuition)
    {
        tuition = setTuition;
    }
    
    //Sort the class based on who has the least tuition
    bool TuitionSort(const School &s1, const School &s2)
    {
        return (s1.Get_Tuition() < s2.Get_Tuition());
    }
    
    int main()
    {
        std::vector<School> mainSchool;
        
        //Fill the vector
        mainSchool.push_back(School("Billy",600.25));
        mainSchool.push_back(School("Joe",800.70));
        mainSchool.push_back(School("Jon",200.50));
        mainSchool.push_back(School("Bob",300.25));
        mainSchool.push_back(School("Dave",1000.75));
        
        std::cout<<"Not Sorted"<<std::endl;
        
        for(int i = 0; i < mainSchool.size(); i++)
        {
            //Print out the unsorted elements
            std::cout<<mainSchool[i].Get_Name()<<std::endl;
            std::cout<<mainSchool[i].Get_Tuition()<<std::endl;
        }
        
        std::cout<<std::endl;
        //Do the sort thing by calling our sort function to do the
        //sorting
        std::sort(mainSchool.begin(),mainSchool.end(),TuitionSort);
        std::cout<<"Sorted"<<std::endl;
        
        for(int i = 0; i < mainSchool.size(); i++)
        {
            //Print out the sorted entries
            std::cout<<mainSchool[i].Get_Name()<<std::endl;
            std::cout<<mainSchool[i].Get_Tuition()<<std::endl;
        }
        
        std::cin.get();
        return 0;
    }
    Woop?

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    quick query

    I assume "Tuit ionSort" is tuitionSort?

    and to sort by namde I change Get_Tuition to Get_Name? I will try of course, but just verifiying?

    Best to have a function for each type of sort, or can I generalize the sort field?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Yes it should be TuitionSort (it's a bug in the forum).

    It will work if you change Get_Tuition to Get_Name in that code because Get_Name returns a string and strings already have a less than operator that will compare them.

    It is best to have a function for each type of sort (remember that was my advice in your last thread) and just call sort with the appropriate function based on which you want to use.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    thanks, it is starting to make sense

    thanks, I think I am starting to understand. I have created sort by name and sort by tuition and it works.

    sorry about missing the comment on generalizing the sort. .

    Where is there documentation on using std:sort with 3 arguements? everything I have found only has 2 arguements

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Don't worry about missing any comments... there are a lot to go through.

    What documentation are you using? The third argument is optional, and defaults to using less which defaults to using the operator< for whatever object is held in the container. There are several ways to create the predicate to be used for the third argument. The simplest is the standalone function that you are using now. To get more details on these things a book is really the best idea. The C++ Standard Library by Josuttis for reference and Effective STL by Meyers for tips are two good options. Otherwise you use the tools you got and learn from practice.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    vector sort info

    I have been using the book by Deitel plus doing google searches. Most tutorials only talk about the 2 arguements. devshed, about etc. Also look at Linux documentation. I will check into the other 2 books you mentions, Thanks


  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    You can either overload the less-than operator for your class and use the 2-argument version of the sort function, or if that operator is not provided, you can provide a predicate as part of the 3-argument version of the function.

    The Josuttis book is excellent... carry it with me wherever I go. Don't personally know about the Meyers book though.

    Quote Originally Posted by prog-bman
    Code:
    //Copy Constructor  that takes the name and double passed to it and
    //sets them as the School's name and tuition.
    School::School(const std::string &toName, const double &setTuition)
    {
        schoolName = toName;
        tuition = setTuition;
    }
    Copy constructor?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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