Overload (+) operator

This is a discussion on Overload (+) operator within the C++ Programming forums, part of the General Programming Boards category; Can anyone help! Problem: write a program to initialize, add to, remove from, and print out a list of records. ...

  1. #1
    kit
    kit is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Red face Overload (+) operator

    Can anyone help!

    Problem: write a program to initialize, add to, remove from, and print out a list of records. The database and the records should be separate C++ classes. In addition, overload the + operator to create the combination of two databases.

    Question: how to overload the + operator to combine two different database? Assuming this is given in main:

    Database db;
    db.insertRecord("Doe, John", 2542698, 2.5, 5);

    Database db2;
    db.insertRecord("Lewis, Rick", 4562457, 3.8, 2);

    Database db3 = db + db2;

    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    // guessing:

    Database db;
    db.insertRecord("Doe, John", 2542698, 2.5, 5);

    Database db2;
    db2.insertRecord("Lewis, Rick", 4562457, 3.8, 2);

    Database db3 = db + db2;

    // db3 should now contain all records from db and db2.

    how to overload '+' to do the job?

    What kind of data structures are you using in your design?

    If it were linked lists i would simply implement '+' to take all records from db2 and db3 and simply append them to db1.

    hope this helps.
    Last edited by greenRoom; 10-18-2001 at 12:40 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    class Database
    {
    //Hope you have a good & fast copy constructor...:
    const Database operator+(const Database& rSecond) const
    {
    Database newDbs;

    //Some pseudo code:
    for (all records in current databese - this)
    newDbs.InsertRecord(currentRecord);
    for (all records in rSecond)
    newDbs.InsertRecord(currentRecord);
    return newDbs;
    }
    }

  4. #4
    kit
    kit is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    6
    Since I need at least two classes: one for the Database class and one for the student record class. I can see that you basically use the Database class to initialize (constructor), insertRecord, possibly deleteRecord, and printDatabase as well. So, what can I do with the other class - studentRecord class?????


    Thanks very much for the replies. They are very helpful.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    I think overloading + in this case is not applicable for students we don't add students to each other in real life. If you were modeling some type of project data, and we wanted to know the cost of projectA and projectB combined then projectAplusB = projectB + projectA would be useful. That is to say projectAplusB would represent the the costs and possibly the resource required, or whatever makes up our projects.

    ...just my two cents.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    Originally posted by kit
    Since I need at least two classes: one for the Database class and one for the student record class. I can see that you basically use the Database class to initialize (constructor), insertRecord, possibly deleteRecord, and printDatabase as well. So, what can I do with the other class - studentRecord class?????


    Thanks very much for the replies. They are very helpful.

    I would like make the record class store student info. For example you have this statement:

    db.insertRecord("Doe, John", 2542698, 2.5, 5);

    so then the records class can store the name and these 3 numbers here:

    class records { // class thus far
    private:
    string, unsigned, double, int
    }

    and of course you can come up with some member functions to set these fields, print the fields, change the fields, etc... you'll probably come up with more as you design your db class.

    the db class, would need some kind data structure. Are you famaliar with lists or vectors? you could use a templatized list from the stl and have it store your class records.

    class db {
    private:
    list<records> database;
    }

    keep in mind this is just a very basic outline. at this point you might start thinking about member functions for the db class" '+', insert, delete, print (maybe 'search' for xtra credit?)

  7. #7
    kit
    kit is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Delete a record

    How could delete a record in the database using Class. There are two class: record class and databse class. A record would consist to student name, ID#, etc.... So, in the record class, there are set and get functions. The database class is for inserting and deleting record.

    So, the delete function would look like this:

    void DB::delete(string name)
    {
    for(int i =0; i<size; i++){
    // compare the name, delete it if equal the one in the
    // database
    if(strcmp(name, Rec[i].GetName() )== 0){
    Rec[i+1].SetName(name);

    size--; // size - 1 for each record deleted
    } // end if
    } // end for

    } // end delete

    Please help, thanks.

  8. #8
    Unregistered
    Guest
    Let's say you have a class holding information about students. You are storing a group of instances of that class in a second class that implements as singly linked list that you are calling Database. You are using these tools to represent a university that offers to courses, each of which has it's own set of students. You now want to define the universe of students at this university by combining the two lists into one and removing all duplicate names so the resultant Database has only unique students in it. You are trying to do this with the + operator.

    class Student
    private:
    char name[40];
    long studentNumber;
    public:
    default constructor
    copy constructor
    get and set functions for all of the private variables.

    struct Node
    Student student;
    Node * next;

    class DataBase//really a list but who cares what you call it
    public:
    Node * head;
    void add();
    void delete();
    void display();
    default constructor
    copy constructor
    destructor
    DataBase operator+(DataBase rhs);
    void operator= (DataBase rhs);

    int main()
    DataBase course1, course2, college;
    enter data for course1 and course2 here;
    college = course1 + course2;
    college.display();
    return 0;

    Now the problem is how to implement the + and = operators to allow the combining of course1 and course2 into college leaving only unique Students in college, leaving course1

    If this setup correlates with what you are attempting to do, then

  9. #9
    Unregistered
    Guest
    Sorry, hit combination of tab, enter before I meant too.

    If the scenario as I posted is similar to what you are trying to do, so indicate, and someone (me or whoever) will help you out.

    Do you mean to keep course1 and course2 intact as you combine them into college, or can you destroy course1 and course2 along the way?

    BTW, it will probably help to have the copy constructor, too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Template overload of operator ++/--
    By Elysia in forum C++ Programming
    Replies: 26
    Last Post: 10-23-2007, 08:45 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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