65.9K
CodeProject is changing. Read more.
Home

Write a C++ class

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Apr 12, 2000

viewsIcon

60700

A Macro which writes an empty C++ class.

This macro writes a an empty C++ class with:

  • a default constructor,
  • an unimplemented copy constructor,
  • a non virtual destructor,
  • and an unimplemented assignment operator.

When you invoke this macro, a user prompt is shown to get the class name and the author's name. Author's name is taken for the documentation header. The class is defined in a classname.h and implemented in classname.cpp. These files are created in the current directory of the DevStudio. The files are not added to the current project intentionally

The .h and the .cpp files contain appropriate preprocessor directives to prevent multiple inclusions of the .h file.

In the header file,

#ifndef _MYCLASS_H_INCLUDED_
#define _MYCLASS_H_INCLUDED_

// class definition

#endif // _MYCLASS_H_INCLUDED_

And in the .cpp file,

#include "StdAfx.h"

#ifndef _MYCLASS_H_INCLUDED_
	
	#include "MyClass.h"

#endif // _MYCLASS_H_INCLUDED_

Also, it defines pointer types for the defined class. i.e.

typedef CMyClass * LPCMYCLASS;
typedef const MyClass * LPCCMYCLASS;

Below this line is the entire code of the macro.

'------------------------------------------------------------------------------
'FILE DESCRIPTION: Dhandy's Macro File
'------------------------------------------------------------------------------
Function emitDocHeader(aDocFile_inout, bHFile_in, strClassName_in, 
strAuthorName_in) aDocFile_inout.Selection.StartOfDocument strHeader = Application.ActiveDocument.Selection If bHFile_in = true Then defnOrDecl = "definition" extension = ".h" Else defnOrDecl = "implementation" extension = ".cpp" End If strHeader = "/*-----------------------------------------------" +_
"-------------------------------" + vbLf strHeader = strHeader + Chr(09) + "File name : " +_
strClassName_in + extension + vbLf + vbLf strHeader = strHeader + Chr(09) + "Author : " +_
strAuthorName_in + vbLf + vbLf strHeader = strHeader + Chr(09) +_
"Description : This file cotains the " + defnOrDecl +_
" of the class " + strClassName_in + vbLf strHeader = strHeader+_
"--------------------------------------------------" +_
"----------------------------*/" + vbLf + vbLf + vbLf + vbLf aDocFile_inout.Selection = strHeader End Function Function emitHFileIfDef(aDocFile_inout, strClassName_in) 'DESCRIPTION: Generates #ifdef _MYCLASS_H_INCLUDED_ ... #endif definitions strIfdefMacro = "_" + Ucase(strClassName_in) + "_H_INCLUDED_" aDocFile_inout.Selection = "#ifndef " + strIfdefMacro + vbLf +_
"#define " + strIfdefMacro + vbLf aDocFile_inout.Selection.EndOfDocument aDocFile_inout.Selection = vbLf + vbLf + vbLf + vbLf +_
"#endif // " + strIfdefMacro + vbLf emitHFileIfDef = strIfdefMacro End Function Function includeHeaderFileInCPP(aDocFile_inout, strIfdefMacro_in,
strHeaderFileName_in) 'DESCRIPTION: Inserts an #include "CMyClass.h" aDocFile_inout.Selection = vbCrLf + "#include " + Chr(34) +_
"StdAfx.h" + Chr(34) + vbLf + vbLf + vbLf aDocFile_inout.Selection = "#ifndef " + strIfdefMacro_in + vbLf +_
vbLf aDocFile_inout.Selection = Chr(09) + "#include " + Chr(34) +_
strHeaderFileName_in + Chr(34) + vbLf + vbLf aDocFile_inout.Selection = "#endif // " + strIfdefMacro_in +_
vbLf + vbLf + vbLf + vbLf End Function Function defineClass(aDocFile_inout, strClassName_in) ' DESCRIPTION: Emits a class definition with the given name in the given file. aDocFile_inout.Selection.EndOfDocument aDocFile_inout.Selection.LineUp dsMove, 3 classDefinition = "class " + strClassName_in + vbLf classDefinition = classDefinition + "{" + vbLf + vbLf classDefinition = classDefinition + "public: // methods." + vbLf +_
vbLf classDefinition = classDefinition + Chr(09) + strClassName_in +_
"(); // Default constructor." + vbLf + vbLf classDefinition = classDefinition + Chr(09) + strClassName_in +_
"(const " + strClassName_in +_
" & rhs_in); // unimplemented copy constructor." + vbLf + vbLf classDefinition = classDefinition + Chr(09) + "~" + strClassName_in +_
"(); // Destructor." + vbLf + vbLf classDefinition = classDefinition + Chr(09) + "const " +_
strClassName_in +_
" & operator = (const " + strClassName_in +_
" & rhs_in); // unimplemented assignment operator." +_
vbLf + vbLf + vbLf classDefinition = classDefinition + "public: // data." + vbLf +_
vbLf + vbLf + vbLf classDefinition = classDefinition + "protected: // methods." +_
vbLf + vbLf + vbLf classDefinition = classDefinition + "protected: // data." +_
vbLf + vbLf + vbLf + vbLf classDefinition = classDefinition + "private: // methods." +_
vbLf + vbLf + vbLf classDefinition = classDefinition + "private: // data." + vbLf +_
vbLf + vbLf classDefinition = classDefinition + "};" + vbLf + vbLf + vbLf + vbLf classDefinition = classDefinition + "typedef " + strClassName_in +_
" * LP" +_
Ucase(strClassName_in) + ";" + vbLf + vbLf classDefinition = classDefinition + "typedef const " +_
strClassName_in + " * LPC" + Ucase(strClassName_in) +_
";" + vbLf + vbLf aDocFile_inout.Selection = classDefinition End Function Function implementClass(aDocFile_inout, strClassName_in) ' DESCRIPTION: Emits the implementation of the given class in the given file. aDocFile_inout.Selection.EndOfDocument classImpl = strClassName_in + "::" + strClassName_in + "()" + vbLf classImpl = classImpl + "{" + vbLf classImpl = classImpl + Chr(09) +_
"// TODO: write your INITIALIZATION code here" + vbLf classImpl = classImpl + "}" + vbLf + vbLf + vbLf + vbLf + vbLf + vbLf classImpl = classImpl + strClassName_in + "::~" +_
strClassName_in + "()" + vbLf classImpl = classImpl + "{" + vbLf classImpl = classImpl + Chr(09) +_
"// TODO: write your CLEANUP code here" + vbLf classImpl = classImpl + "}" + vbLf + vbLf + vbLf + vbLf +_
vbLf + vbLf aDocFile_inout.Selection = classImpl End Function Sub writeClass() 'DESCRIPTION: DHNADY will write a class for you. Give him the class name
'and he'll ceate .h and .cpp files for it.
Dim headerFileDoc, cppFileDoc strUserInput = InputBox("Dhandy will write an empty class for you " +_
" in the given classname.h and classname.cpp file in the " +_
"current directory" + vbLf + vbLf +_
"Enter the class' name and the author's name. Put a ^ between " +_
"class name and author's name. Don't worry about the spaces " +_
"around the ^." + vbLf + vbLf + "e.g. CString ^ Shri " +_
"Chullumal Popatmal Khatri, Jhumritaliya, Bharat." + vbLf +_
vbLf + "The class will be 'CString' and Author will be " +_
"'Shri Chullumal Popatmal Khatri, Jhumritaliya, Bharat.'" ,
"Dhandy writes a class for you!") strUserInput = Trim(strUserInput) If strUserInput = "" Then Exit Sub End If iCaretPos = Instr(strUserInput, "^") If(iCaretPos = 0) Then strClassName = strUserInput strAuthorName = "_AUTHOR_NAME_" else strClassName = Mid(strUserInput, 1, iCaretPos - 1) strAuthorName = Mid(strUserInput, iCaretPos + 1) End If strClassName = Trim(strClassName) strAuthorName = Trim(strAuthorName) strHeaderFileName = strClassName + ".h" Set headerFileDoc = Application.Documents.Add("Text") headerFileDoc.Save(strHeaderFileName) emitDocHeader headerFileDoc, true, strClassName, strAuthorName strIfdefMacro = emitHFileIfDef(headerFileDoc, strClassName) defineClass headerFileDoc, strClassName strCppFileName = strClassName +".cpp" Set cppFileDoc = Application.Documents.Add("Text") cppFileDoc.Save(strCppFileName) emitDocHeader cppFileDoc, false, strClassName, strAuthorName includeHeaderFileInCPP cppFileDoc, strIfdefMacro, strHeaderFileName implementClass cppFileDoc, strClassName headerFileDoc.Save cppFileDoc.Save headerFileDoc.Active = true End Sub