Reputation: 57
Here is my assignment for C++ course. Certainly I will finish it by myself. But there is a place I really have no ideas about. Hope someone can explain that to me. Thanks in advance.
The requirement of this assignment is like this:
We need to use template to create a customized class named Set to realize the functionality of union (using operator +
) and intersection (using operator *
). For example,{4,2,3}+{9,4,8,2}
will be {8,3,9,2,4} and {3,2,4}*{8,4,9,2}
will be {4,2}
Here is the skeleton of the xxxx.h file:
Here is the code, but it can't display the red rectangle as the pic does. But it is more readable: )
#ifndef A2P2_H
#define A2P2_H
#include <exception>
#include <iostream>
using namespace std;
//========part a-Comments here:
//========part b-author's details
void info(){/* missing code */}
//========part c-exception classes:
class RemoveFromEmpty : exception{
public:
RemoveFromEmpty(){/* missing code */}
const char* what() const noexcept {/* missing code */}
private:
string mMessage;
};
class NonExistingElem:exception{
/* to be thrown when the element to be removed is not found in
the set --------code missing */
};
//========part d-Set class template
template <typename EType>
class Set{
public:
//constructors
Set( );
Set( const Set & rhs );
Set( Set && rhs );
//destructor
~Set( );
//operators overloaded
Set & operator=( const Set & rhs );
Set & operator=( Set && rhs );
Set operator+( const Set & rhs ) const; //set union
Set operator*( const Set & rhs ) const; //set intersection
//methods
bool isElement( const EType & x ) const;
bool isEmpty( ) const;
int getSize( ) const;
//display on out all elements in the set between {..}
void print( ostream & out = cout ) const;
void setToEmptySet( );
//methods to work with individual elements of a set
void insert( const EType & x );
void remove( const EType & x );
private:
struct Node{// type of the elements of the set
EType mData;
Node *mNext;
Node( const EType & d = EType( ), Node *n = nullptr )
: mData( d ), mNext( n ) { }
};
Node *mFirst;
int mSize; // to have an efficient getSize().
};
//Write the definitions of all Set function members here:
//========part e-the output operator:
template <typename EType>
ostream & operator<< /* code missing here */
#endif
See the picture above and pay more attention to the highlighted part. At the lower part, there is a red rectangle and the content is const EType &d = EType()
. I know EType is the type that I have declared in the template. But what is EType()
? It's a function?
There is no other things related with EType()
(i.e. some implementations for that function maybe) in the rest part of the requirement. Actually, the left part is xxxx.cpp file and a result when executing that code. I also post the them here if it would be helpful.
#include <iostream>
#include "a2p2.h"
using namespace std;
//It works only for sets of integers
template <typename T = int>
void testCopyCtr(Set<T> st){
cout<< func <<": ";
st.insert(23); //because of this statement
st.print();
cout<<endl;
}
int main( ){
info(); //authors details
try{
Set<int> s1, s2;
s1.insert( 8 );
s1.insert( 3 );
s1.insert( 1 );
s1.insert( 4 );
s1.insert( 1 );
s1.remove( 8 );
s1.insert( 2 );
s2.insert( 4 );
s2.insert( 2 );
s2.insert( 6 );
cout << "S1: " << s1 << ", size= " << s1.getSize( ) << endl;
cout << "S2: " << s2 << ", size= " << s2.getSize( ) << endl;
Set<int> s3 = s1+ s2; //union
Set<int> s4 = s1* s2; //intersection
cout << "s1 + s2: " << s3 << endl;
cout << "s1 * s2: " << s4 << endl;
Set<int> s5 = s4 = s3 = s2 = s1 = s1;
cout << "S1: " << s1 << ", size= " << s1.getSize( ) << endl;
cout << "S2: " << s2 << ", size= " << s2.getSize( ) << endl;
cout << "S3: " << s3 << ", size= " << s3.getSize( ) << endl;
cout << "S4: " << s4 << ", size= " << s4.getSize( ) << endl;
cout << "S5: " << s5 << ", size= " << s5.getSize( ) << endl;
cout << "S4 again : " << s4 << endl;
testCopyCtr(s4);
Set<float> sf;
sf.remove(3);
}
catch(RemoveFromEmpty ex) {
cout<<endl<<ex.what()<<endl;
cout<<"Nothing to be done\n";
}
catch(NonExistingElem ex) {
cout<<endl<<ex.what()<<endl;
cout<<"Nothing to be done\n";
}
cout<<"All is well when it ends well!\n";
return 0;
}
Sorry for the inconvenience that result can only display in the picture.
Upvotes: 0
Views: 132
Reputation: 2806
It is an invocation of a constructor. You are assigning a default-constructed EType
value to variable d
as d
's default value.
Upvotes: 1