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.

#include<vector>
#include<iostream>
#ifndef MULTISET_H
#define MULTISET_H
using namespace std;
class multiset
{
public:
	multiset( ); // constructs an empty multiset
	multiset( int ); // constructs a multiset with one item
	multiset _union( const multiset& ) const; // union operation (notice underscore _)
	multiset intersect( const multiset& ) const; // intersects two multisets
	multiset difference( const multiset& ) const; // difference of two multisets
	multiset join( const multiset& ) const; // joins two multisets
	multiset unique( ) const; // returns unique items
	int length( ) const; // total number of items
	int occurrence( int ) const; // occurrence operation
	void display( ) const; // display the contents of the multiset
	// as { item, item, item ... }
	void print( ) const; // print the contents of the multiset
	// as { (item,count), (item,count) ... }
	// overloaded operators performing the intersection, difference, union and join operations
	multiset operator* ( const multiset& ) const; // intersection operation (A * B)
	multiset operator- ( const multiset& ) const; // difference operation (A - B)
	multiset operator|| ( const multiset& ) const; // union operation (A || B)
	friend multiset operator+ ( const multiset&, const multiset& ); // join operation (A + B)
private:
	vector<int> multiSet;
	vector<int>multiSet1;
};
#endif

This is the multiset.cpp file

#include "multiset.h"
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

multiset::multiset( ){
	this->multiSet = multiSet;
}//end multiset

multiset::multiset(int){
	multiSet[0] = 1;
	this->multiSet = multiSet;
	cout<<multiSet[0];
}//end multiset

And this is my project2.cpp file

#include <iomanip>
#include <iostream>
#include <cmath>
#include "multiset.h"

void main(){
	multiset::multiSet{
		cout<<multiSet[0];
	}
}//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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.