这是我的C ++课程作业。当然,我会自己完成它。但是有一个地方我真的没有想法。希望有人可以向我解释。提前谢谢。
这项任务的要求是这样的:
我们需要使用模板来创建名为 Set 的自定义类,以实现union(使用运算符+
)和交集(使用运算符*
)的功能。例如,{4,2,3}+{9,4,8,2}
将为{8,3,9,2,4},{3,2,4}*{8,4,9,2}
将为{4,2}
这是xxxx.h文件的框架:
这是代码,但它不能像pic一样显示红色矩形。但它更具可读性:)
#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
请参见上图并更加关注突出显示的部分。在下半部分,有一个红色矩形,内容为const EType &d = EType()
。我知道EType是我在模板中声明的类型。但是什么是EType()
?这是一个功能吗?
在要求的其余部分中没有与EType()
相关的其他内容(即可能是该功能的某些实现)。实际上,左边部分是xxxx.cpp文件,并且是执行该代码时的结果。如果它有用,我也会在这里张贴它们。
#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;
}
很抱歉结果只能在图片中显示给您带来的不便。
答案 0 :(得分:1)
它是构造函数的调用。您将默认构造的EType
值分配给变量d
作为d
的默认值。