Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Confused with vc++

Posted on 1998-10-22
10
289 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
ID: 1175761
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
ID: 1175762
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
ID: 1175763
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:psdavis
ID: 1175764
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
ID: 1175765
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
 

Author Comment

by:stove102198
ID: 1175766
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
ID: 1175767
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
ID: 1175768
That's fantastic psdavis, how should I accept the answer so that you get the points.
0
 
LVL 10

Expert Comment

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

0
 
LVL 7

Expert Comment

by:psdavis
ID: 1175770
Thanks ronslow.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 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