25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Template text for a C++ class with declarations and definitions for constructor, destructor, and common operators

This template text is a skeleton class declaration and definition to use when creating new C++ classes. I wrote it when I realized that much of the text in my new C++ classes was always the same.   

  1. Paste the anyclass.cpp text into your program where you want the class to be.
  2. Do a case-sensitive mass-replace of all "CL" with the name of your class
  3. Lots of typing avoided.
/*	anyclass.cpp               4-27-00
	A skeleton C++ class where you fill in the blanks.
	Paste the text into your program, do a case-sensitive mass-replace of 
	all "CL" with the name of your class,
	and voila, instant class with lots of typing saved.

	Text similar to this is also in an AutoText entry in the CPROGRAM.DOT MS Word template.

*/
//////////////////////////////////////////////////////////////////////////////
//
class CL : public TArrayAsVector<complex>
{
public:
	CL();
	CL(const CL& other) { *this = other; }
	virtual ~CL();

	CL& operator = (const CL& other);
	BOOL operator == (const CL& other) const;
	BOOL operator != (const CL& other) const { return(!(*this == other)); }
	BOOL operator < (const CL& other) const;
	operator int() { return(GetItemsInContainer()); }

	friend ostream& operator << (ostream& os, const CL& s);	// write
	friend istream& operator >> (istream& is, CL& s);		// read

	// functions

	// variables

protected:

};
//----------------------------------------------------------------------------
// 							end class CL
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// class CL code
//----------------------------------------------------------------------------
// constructor
CL::CL()
{

}                       	//constructor
//----------------------------------------------------------------------------
// destructor
CL::~CL()
{

}                       	//destructor
//----------------------------------------------------------------------------
// assignment operator	(always define this, and let copy constructor call it)
CL& CL::operator = (const CL& other)
{
if(this == &other)
	return(*this);

return(*this);
}                    		//assignment operator
//----------------------------------------------------------------------------
// equality
BOOL CL::operator == (const CL& other) const
{
	return(&other == this);   					// not always the test to use
}                        	//operator ==
//----------------------------------------------------------------------------
// less than
BOOL CL::operator < (const CL& other) const
{
	return(X < other.X);
}                     		//operator <
//----------------------------------------------------------------------------
// output to a stream
ostream& operator << (ostream& os, CL& s)
{
// you usually let the caller add the endl
os << "";

return(os);
}                 			//operator <<
//----------------------------------------------------------------------------
// read from a stream
// be sure to set ALL members, even those not read from the stream.
// this example is complicated, for easy reference, but uses >> ws unusually, particularly:
// do not use a final >> ws (or any input) after you have all the input you need for the object,
// because if you got what you needed, it shouldn't return as failed until the *next* call.
istream& operator >> (istream& is, CL& s)
{
char buf[80];
is >> ws;							// ensure first getline has something to read
while(is.getline(buf,sizeof(buf)))
{
	istrstream str(buf);
	str >> x >> ws;                 // do force a failure if there's nothing left on the line
	if(str)                  		// if there is anything left, it is Y
	{
		str >> y;
		s.Add(x,y);
	}
	else
		s.Add(x);            		// this x is really the y variable
	is >> ws;                       // skip blank lines before the next getline()
}
return(is);
}                  			//operator >>
//----------------------------------------------------------------------------
// 						end class CL
//////////////////////////////////////////////////////////////////////////////

 

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.