Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++
Article

Write a C++ class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Apr 2000 60.3K   14   3
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.

VB.NET
'------------------------------------------------------------------------------
'FILE DESCRIPTION: Dhandy's Macro File
'------------------------------------------------------------------------------
Function emitDocHeader(aDocFile_inout, bHFile_in, strClassName_in, <BR>                       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 = "/*-----------------------------------------------" +_<BR>                    "-------------------------------" + vbLf
	strHeader = strHeader + Chr(09) + "File name    : " +_<BR>                           strClassName_in + extension + vbLf + vbLf
	strHeader = strHeader + Chr(09) + "Author       : " +_<BR>                            strAuthorName_in + vbLf + vbLf
	strHeader = strHeader + Chr(09) +_<BR>                       "Description  : This file cotains the " + defnOrDecl +_<BR>                                " of the class " + strClassName_in + vbLf
	strHeader = strHeader+_<BR>                    "--------------------------------------------------" +_<BR>                    "----------------------------*/" + 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 +_<BR>                            "#define " + strIfdefMacro + vbLf

	aDocFile_inout.Selection.EndOfDocument

	aDocFile_inout.Selection = vbLf + vbLf  + vbLf + vbLf +_<BR>                            "#endif // " + strIfdefMacro + vbLf

	emitHFileIfDef = strIfdefMacro

End Function


Function includeHeaderFileInCPP(aDocFile_inout, strIfdefMacro_in,<BR>                     strHeaderFileName_in)
'DESCRIPTION: Inserts an #include "CMyClass.h"

	aDocFile_inout.Selection = vbCrLf + "#include " + Chr(34) +_<BR>                       "StdAfx.h" + Chr(34) + vbLf + vbLf + vbLf

	aDocFile_inout.Selection = "#ifndef " + strIfdefMacro_in + vbLf +_<BR>                                          vbLf

	aDocFile_inout.Selection = Chr(09) + "#include " + Chr(34) +_<BR>                          strHeaderFileName_in + Chr(34) + vbLf + vbLf

	aDocFile_inout.Selection = "#endif // " + strIfdefMacro_in +_<BR>                               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 +_<BR>                                                          vbLf

	classDefinition = classDefinition + Chr(09) + strClassName_in +_<BR>                            "(); // Default constructor." + vbLf + vbLf
	
	classDefinition = classDefinition + Chr(09) + strClassName_in +_<BR>                 "(const " + strClassName_in +_<BR>              " & rhs_in); // unimplemented copy constructor." + vbLf + vbLf
	
	classDefinition = classDefinition + Chr(09) + "~" + strClassName_in +_<BR>                                 "(); // Destructor." + vbLf + vbLf
	
	classDefinition = classDefinition + Chr(09) + "const " +_<BR>                            strClassName_in +_<BR>                  " & operator = (const " + strClassName_in +_<BR>                  " & rhs_in); // unimplemented assignment operator." +_<BR>                  vbLf + vbLf + vbLf

	classDefinition = classDefinition + "public: // data." + vbLf +_<BR>                                            vbLf + vbLf + vbLf

	classDefinition = classDefinition + "protected: // methods." +_<BR>                                      vbLf + vbLf + vbLf
	classDefinition = classDefinition + "protected: // data." +_<BR>                                      vbLf + vbLf + vbLf + vbLf

	classDefinition = classDefinition + "private: // methods." +_<BR>                                      vbLf + vbLf + vbLf
	classDefinition = classDefinition + "private: // data." + vbLf +_<BR>                                        vbLf + vbLf

	classDefinition = classDefinition + "};" + vbLf + vbLf + vbLf + vbLf

	classDefinition = classDefinition + "typedef " + strClassName_in +_<BR>                              " * LP" +_<BR>                              Ucase(strClassName_in) + ";" + vbLf + vbLf

	classDefinition = classDefinition + "typedef const " +_<BR>                     strClassName_in + " * LPC" + Ucase(strClassName_in) +_<BR>                     ";" + 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) +_<BR>                     "// TODO: write your INITIALIZATION code here" + vbLf

	classImpl = classImpl + "}" + vbLf + vbLf + vbLf + vbLf + vbLf + vbLf


	classImpl = classImpl + strClassName_in + "::~" +_<BR>                              strClassName_in + "()" + vbLf
	classImpl = classImpl + "{" + vbLf

	classImpl = classImpl + Chr(09) +_<BR>                          "// TODO: write your CLEANUP code here" + vbLf

	classImpl = classImpl + "}" + vbLf + vbLf + vbLf + vbLf +_<BR>                                vbLf + vbLf

	aDocFile_inout.Selection = classImpl

End Function


Sub writeClass()
'DESCRIPTION: DHNADY will write a class for you. Give him the class name <BR>'and he'll ceate .h and .cpp files for it.

	Dim headerFileDoc, cppFileDoc

	strUserInput = InputBox("Dhandy will write an empty class for you " +_<BR>              " in the given classname.h and classname.cpp file in the " +_<BR>              "current directory" + vbLf + vbLf +_<BR>              "Enter the class' name and the author's name. Put a ^ between " +_<BR>              "class name and author's name. Don't worry about the spaces " +_<BR>              "around the ^." + vbLf + vbLf + "e.g. CString ^ Shri " +_<BR>              "Chullumal Popatmal Khatri, Jhumritaliya, Bharat." + vbLf +_<BR>              vbLf + "The class will be 'CString' and Author will be " +_<BR>              "'Shri Chullumal Popatmal Khatri, Jhumritaliya, Bharat.'" ,<BR>              "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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Home Depot
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAdd to current project Pin
shahzado17-Jun-04 22:35
shahzado17-Jun-04 22:35 
Generalbeginer Pin
dnqhung12-Jun-04 20:31
dnqhung12-Jun-04 20:31 
GeneralNice Job! Very Useful Pin
Rudy Schalk20-Sep-00 12:35
Rudy Schalk20-Sep-00 12:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.