Solved

Confused with vc++

Posted on 1998-10-22
10
282 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:stove102198
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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....
0
 
LVL 1

Expert Comment

by:Darren_Simons
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:szetoa
Comment Utility
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.
0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
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( )
   {
   }

protected:

   int m_iFirst;
   int m_iSecond;

public:
 
   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.

Phillip
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
Comment Utility
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.

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:stove102198
Comment Utility
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?

0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
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.

Clear?

Phillip
0
 

Author Comment

by:stove102198
Comment Utility
That's fantastic psdavis, how should I accept the answer so that you get the points.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
If you prefer psdavis answer, then you will need to reject mine (the currently proposed answer).

0
 
LVL 7

Expert Comment

by:psdavis
Comment Utility
Thanks ronslow.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now