Using Microsoft Visual C++ 6



Using Microsoft Visual C++ 6.0 – Quick Overview

What follows is a quick overview of the steps to take when working with a C++ class based program in Microsoft Visual C++ 6.0. This document is not sensitive to any particular version (i.e.: Student, Professional, etc…) of the compiler. The examples used in this document are based on the FRACTION class. However, the steps outlined here are valid for any class based C++ program. The full source of the FRACTION class and a sample client program is provided at the end. In the interest of document and file size, the comments have been stripped from the source files. Your instructor may have the fully commented source, or links to where you can download the fully commented source.

Starting a class-based project from scratch

← Start Microsoft Visual C++ 6.0

← Choose FILE | NEW

← Make sure the PROJECTS tab is selected.

← Select WIN32 CONSOLE APPLICATION

+ This ensures that the program is DOS based, and not a Windows application.

← Enter a project name. No spaces. No punctuations.

+ For our example, we name the project: Fraction

← Enter the location for the project to be saved.

← CREATE NEW WORKSPACE should be checked.

+ Visual C++ divides every work area into workspaces, which in turn may contain several projects. Each project contains CPP and H files for all the classes belonging to that project. While it is possible to have more than one project in a workspace, this isn’t usually the case. This document assumes that you limit your workspace to one project.

← WIN 32 PLATFORM should be checked.

← Click OK.

← Choose EMPTY PROJECT.

+ While “Hello World” and some other options might seem interesting, they will add extra code to your program which you might not want or need.

← Click FINISH.

← Click OK.

← Visual Studio will place additional files into your project folder. Some of the ones which might seem strange to you are:

+ DSW: Workspace file. Used to organize projects.

+ DSP: Project file. Used to organize files in the project.

+ CLW: Classwizard file. Used by the IDE.

+ ODL: Object description language. Used by the IDE.

+ NCB: No compile browser. Used to provide auto-hints.

+ STDAFX.*: Precompiled headers. Used to speed up builds.

← We now have an empty project. Time to build the FRACTION class.

← Choose FILE | NEW

← Make sure the FILES tab is selected.

← Choose C/C++ Header file.

← Make sure ADD TO PROJECT is checked.

← Put in the file name. The default extension is ‘H’.

+ For our example, we simply enter Fraction.

← Click OK.

← Choose FILE | NEW

← Make sure the FILES tab is selected.

← Choose C++ Source file.

← Make sure ADD TO PROJECT is checked.

← Put in the file name. The default extension is ‘CPP’.

+ For our example, we enter Fraction to match the H file.

← We now have both the interface and the implementation file for our class.

+ The panel on the left side of the screen holds a directory view of our program. This panel contains the SOURCE folder, the HEADERS folder and the RESOURCE folder. The SOURCE folder now holds our Fraction.cpp, and the HEADERS folder now holds our Fraction.h. We will not be using the RESOURCE folder in this example.

← Enter the code for Fraction.h.

← Enter the code for Fraction.cpp.

← We now have a working class.

+ Now that you have a class as part of the project, you can switch from FILE VIEW to CLASS VIEW in the panel on the left. The CLASS VIEW will list your project in terms of the classes it contains. Clicking on a class name will, in turn, list all of its methods and data members. You can also see if the method or member is in the private or public access domain. Double-clicking on the class name will take you to the corresponding ‘H’ file. Double-clicking on a method will take you to its implementation in the ‘CPP’ or the ‘H’ file.

← Choose BUILD | COMPILE to compile this class.

← Fix any errors, and address any warnings.

← We now need to build a client to test our newly created class.

← Choose FILE | NEW

← Make sure the FILES tab is selected.

← Choose C++ Source file.

← Make sure ADD TO PROJECT is checked.

← Put in the file name. The default extension is ‘CPP’.

+ For our example, we enter FractionTest.

← Enter the code for FractionTest.cpp.

← Choose BUILD | COMPILE to compile this file.

← Choose BUILD | BUILD to make an EXE file.

← We are done!

Opening existing source code

← To open an existing project:

← Remove any existing projects by closing the workspace.

← Choose FILE | OPEN WORKSPACE.

← Find and open the DSW file of the existing project.

← To open an existing CPP or H file:

← Choose FILE | OPEN

← Find and open the CPP or H file.

← To open a project which doesn’t have a workspace:

← Choose FILE | OPEN

← Find and open the DSP file of the existing project.

Adding existing files to a project

← Choose PROJECT | ADD TO PROJECT | FILES.

← Find and choose the CPP and/or H files.

Removing a class from the project

← Switch to FILE VIEW and remove the H and CPP files of the class.

← Remove any references to the deleted H file from other files in the project.

← This does not delete the files from the hard drive. It only removes them from the current project.

///////////////////////////////////////////////////////////////////////

// Fraction.h

// Fraction Class Interface

//

class Fraction

{

public:

Fraction ();

Fraction (int InNum, int InDenom);

~Fraction ();

inline bool SetNumerator (int InNewNumer)

{numer = InNewNumer; return true;}

inline int GetNumerator () const {return numer;}

bool SetDenominator (int InNewDenom);

inline int GetDenominator () const {return denom;}

void AddFractionTo (Fraction InFraction);

void MultiplyBy (Fraction InFraction);

void MultiplyBy (int InInt);

void Print () const;

void ReduceToLowestTerms ();

private:

int numer, denom;

int ComputeGCD (int x, int y) const;

};

///////////////////////////////////////////////////////////////////////

// Fraction.cpp

// Fraction Class Implementation

#include

#include "Fraction.h"

Fraction::Fraction ()

{

numer = 1;

denom = 1;

}

Fraction::Fraction (int InNumer, int InDenom)

{

numer = InNumer;

bool success;

success = SetDenominator (InDenom);

if (!success)

InDenom = 1;

}

Fraction::~Fraction ()

{}

bool Fraction::SetDenominator (int InNewDenom)

{

if (InNewDenom == 0)

return false;

else

{

denom = InNewDenom;

return true;

}

}

void Fraction::AddFractionTo (Fraction InFraction)

{

if (denom == InFraction.denom)

{

numer = numer + InFraction.numer;

}

else

{

int oldDenom = denom;

bool success = SetDenominator(denom * InFraction.denom);

if (success)

{

numer = InFraction.denom * numer + oldDenom * InFraction.numer;

}

}

}

void Fraction::MultiplyBy (int InInt)

{

SetNumerator (InInt * numer);

}

void Fraction::MultiplyBy (Fraction InFraction)

{

SetNumerator (numer * InFraction.numer);

SetDenominator (denom * InFraction.denom);

}

void Fraction::Print () const

{

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download