Having trouble understanding how header files work.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 1
Reputation: ThunderSnow is an unknown quantity at this point 
Solved Threads: 0
ThunderSnow ThunderSnow is offline Offline
Newbie Poster

Having trouble understanding how header files work.

 
0
  #1
May 3rd, 2008
I am working on a project for school. I read the book but I'm still having trouble getting my program to function. We are working with multisets and are using 3 files. We have a multiset.h header file that is only supposed to contain the class definition. This was given to us to use and we aren't supposed to change anything in the public part of the file. We then have a multiset.cpp file that should contain out implementation of the member functions in the multiset class. Our final file is a project2.cpp file that is supposed to contain the main program and code to test each operation in the multiset class. I'm working on the first two class definitions at the moment. This is the code I have so far...

This is the multiset.h file.

  1. #include<vector>
  2. #include<iostream>
  3. #ifndef MULTISET_H
  4. #define MULTISET_H
  5. using namespace std;
  6. class multiset
  7. {
  8. public:
  9. multiset( ); // constructs an empty multiset
  10. multiset( int ); // constructs a multiset with one item
  11. multiset _union( const multiset& ) const; // union operation (notice underscore _)
  12. multiset intersect( const multiset& ) const; // intersects two multisets
  13. multiset difference( const multiset& ) const; // difference of two multisets
  14. multiset join( const multiset& ) const; // joins two multisets
  15. multiset unique( ) const; // returns unique items
  16. int length( ) const; // total number of items
  17. int occurrence( int ) const; // occurrence operation
  18. void display( ) const; // display the contents of the multiset
  19. // as { item, item, item ... }
  20. void print( ) const; // print the contents of the multiset
  21. // as { (item,count), (item,count) ... }
  22. // overloaded operators performing the intersection, difference, union and join operations
  23. multiset operator* ( const multiset& ) const; // intersection operation (A * B)
  24. multiset operator- ( const multiset& ) const; // difference operation (A - B)
  25. multiset operator|| ( const multiset& ) const; // union operation (A || B)
  26. friend multiset operator+ ( const multiset&, const multiset& ); // join operation (A + B)
  27. private:
  28. vector<int> multiSet;
  29. vector<int>multiSet1;
  30. };
  31. #endif

This is the multiset.cpp file

  1. #include "multiset.h"
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. multiset::multiset( ){
  8. this->multiSet = multiSet;
  9. }//end multiset
  10.  
  11. multiset::multiset(int){
  12. multiSet[0] = 1;
  13. this->multiSet = multiSet;
  14. cout<<multiSet[0];
  15. }//end multiset

And this is my project2.cpp file

  1. #include <iomanip>
  2. #include <iostream>
  3. #include <cmath>
  4. #include "multiset.h"
  5.  
  6. void main(){
  7. multiset::multiSet{
  8. cout<<multiSet[0];
  9. }
  10. }//end main

Now I know that the last file is wrong but I believe that the other two are correct. My question is how can I get test cases to work in the main area? For the first case I just want to be able to show that the multiset is empty but I'm not sure how to go about it. The same with the second case where I want to show that it has a multiset with 1 item. Any help would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Having trouble understanding how header files work.

 
0
  #2
May 3rd, 2008
multiset::multiset( );

The above is a default constructor. It should initialize any member variables to a default value, if they need one. Vectors don't need to be set to a default value and they are the only member variables I see, so none are needed that I can see. Therefore the definition of the default contstructor could just be:

multiset::multiset( ){}

This:

multiset::multiset(int);

is a nondefault constructor. According to the h file it is supposed to create a multiset with 1 item, but the instructions don't say what value that item should have. Therefore, I'd interpret it like this:

multiset::multiset(int input)
{
vector<int> multiSet(1);
vector<int>multiSet1(1);
}

which creates two vectors each with capacity for 1 int each though, though the value of the int isn't specified. I've never actually written a constructor like that before since it seems quite pointless, but that's the way educational exercises frequently are.

An alternative interpretation of the nondefault constructor is that the int passed in should be entered into the vector variables like this:

multiset::multiset(int input)
{
multiSet.push_back(input);
multiSet1.push_back(input);
}

This interpretation makes more sense but it isn't how I'd interpret the instructions. You can go whichever way you want I guess.

main() should never have return type of void, even if your compiler permits it. It should always have a return type of int.

To use the constructors in main() you could use these lines:

multiset m; //uses the default constructor
multiset mM(1234); //uses the non default constructor.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC