Confused with vc++

I'm new to vc++, but I have a reasonable understanding of most of the basics. But I there is a major gap in my understanding which I would like someone to help with please.

I'm using MFC, and I'd just like to make a simple class that contains a few variables and a few functions.
The functions should do simple arithmetic like adding two integers. So no windows stuff is involved.
So far I have used CLass Wizard to try and make a class. But I'm keep getting confused. What is the best way to achieve my goal?

If anyone can just sum up what I should be doing I would be eternally grateful.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

RONSLOWConnect With a Mentor Commented:
MFC has little to do with you problems ... its just a library of classes that sit over windows (and do other stuff).

Your problem is more with the VC Developer Studio IDE.  It provides a class wizard etc to help provide a skeleton for an MFC (usually CWnd) derived class.

However, there is not such help for non-MFC classes.

The reason is most likely that there is less need for help .. you don't need to set up message maps, do data exchange, override MFC virtual functions etc.

Just add a new source and header file to your program and type your class declaration and definitions.

The developer studio will still show the class in the Class View and let you add members and functions etc.  You can also still use the class browser to cross-reference definitions and uses etc.

However, you still need to start the class off manually yourself.

I'm not sure about VC5 (its been a while since I used it) .. But VC6 lets you right-click in the call vie and create a new class.  You can specify its name (VC will create the source and header files) and say what class it is derived from (if any).  VC6 will then create the skeleton class for you.

At first: If you don't need the windows stuff at all, keep off MFC - it's main use is to encapsulate the Win32 API in C++. Simply try coding in plain C++, without using any of the tools DevStudio offers. If you need collections of any kind use STL instead of MFC - using a single MFC CList will mean that you'll have to use all MFC runtime DLLs....
Alternatively, assuming you are using MFC but this particular class does not use it, simply create your class in a .H and .cpp file using the editor. When you try to compile the .cpp file you will be prompted to add this file to the project.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Another alternative:

To create a simple program without the Windows stuff, you can use still use the VC++'s Microsoft Developer Studio.  Then go to "File" --> "New" --> "Project Workspace" --> "Console Application" and then type in the project name (usually your target executable name).  This creates a directory for your project and all the necessary files.  You can then use your favorite editor or still using the VC++'s Microsoft Developer Studio ("File" --> "New" --> "Text File") to create the .h and .cpp files, just as Darren_Simons said.  Then you have to do "Insert" --> "Files Into Project" to include the .h and .cpp files in your project.  Press the compile button and it's done.  To test run the program, press F10 to step through each line of your code (in debugging mode).

I strongly recommend you to read through the manuals or the online tutorial.  It helps a lot.

Good luck.
Sigh...  The kind of answers I expected asking a MFC question in the C++ question area :)

The basis of almost all MFC classes is the CObject class.  If this is one of your first classes, you can use ClassWizard to derive it from CObject, not a CWnd ok?

Second, just try something super easy like this in a header file (you won't really need a .cpp file for something this easy)

class CSimple : public CObject
   CSimple( int iFirst, int iSecond )
      m_iFirst = iFirst;
      m_iSecond = iSecond;

  ~CSimple( )


   int m_iFirst;
   int m_iSecond;

   int Add( )  
      return m_iFirst + m_iSecond;

   int Subtract( )
      return m_iFirst - m_iSecond;

Now when you want to use this class, (in a .cpp file), you can simply call it like this...

CMyWinApp::Test( )
   CSimple Test( 15, 12 );
   int iAdd = Test.Add( );
   int iSubtract = Test.Subtract( );
Get the idea?  I just made this up so there might be errors (yes, I really did! Ain't that amazin?)

Definitely go with tutorials.    

For now, ignore the asserts and the dump stuff involved with a CObject class.  Just always use them when you're in MFC and things will be easier for you.

stove102198Author Commented:
psdavis, that's the sort of answer I was after, thanks ( I should've asked it in the mfc area). The only problem is, I used class wizard and went to add a new class, but the list of base classes did not include CObject. What am I doing wrong?

Actually, you're doing nothing wrong.  Class Wizard is based on windows and will let you create complex window overrides, but not on plain objects.  I was wrong to tell you otherwise.

Since the CObject is extremely simple in usage requirements, there was no need for ClassWizard to enhance your classes functionality. All you have to do is obtain an example from a standard C++ book and derive from CObject.  

The reason ClassWizard was added around version 2 or so was to help with the difficult window messages and the overrides available to each class, not with generic C++ programming.


stove102198Author Commented:
That's fantastic psdavis, how should I accept the answer so that you get the points.
If you prefer psdavis answer, then you will need to reject mine (the currently proposed answer).

Thanks ronslow.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.