COM Lab # 1: Re-Using Class Via. Static Library
COM Lab # 1: Re-Using Class Via. Static Library
3. Invoke New Project dialog by selected File->New menu-item. This displays the New Project dialog.
4. On the Project types pane, under Visual C++, select Win32. On the Templates pane, select
Win32 Console Application. Choose a name for the project, such as MathLib, and enter it in the
Name field.
7. Press Finish to create the project. Press Finish. This would create a Win32 Static Library project
with no source files in it.
3. Repeat the steps as above and add a new C++ File file to the project and name it as Circle.cpp.
4. Now the project has two files added to it namely Circle.h and Circle.cpp. These are empty at
this point. In the Solution explorer, expand Header Files tree. You would see Circle.h listed
under this tree.
5. Add the following code to Circle.h file. This code essentially declares a class named Circle.
This Circle class has one double member variable that denotes radius of the circle. And, it has a
constructor that initializes the radius to zero. This Circle class has two methods. First method
SetRadius sets the radius of the circle using the specified value. The other method, GetArea
computes the area of the circle and returns the area. Save the file after entering this code.
class Circle
{
private:
double m_dRadius;
public:
Circle();
7. Add the following code to Circle.cpp file. This code essentially implements the constructor and
two methods SetRadius and GetArea of Circle class. Save the file after entering this code.
#include "Circle.h"
(ii) Immediately after the previous line, add the following code that declares a constant
for denoting PI value. This value is used for calculating the area of the circle.
(iii) Write the code the constructor. It sets the radius to zero.
Circle::Circle()
{
this->m_dRadius = 0.0;
}
(iv) Write the code for SetRadius method. This method takes in a double value and
sets the value to radius of the circle.
(v) Write the code for GetArea method. Using the PI value declared earlier, area of the
circle is computed (as PI * radius). Yes, I know that this formula is wrong. We will
correct it later.
double Circle::GetArea()
{
double dArea = MATH_PI * m_dRadius; // incorrect formula
return (dArea);
}
2. Select Projects tab. As we are creating a Console Application, select Win32 Console
Application project type. Specify the name of the project as MathLibClient in Project name
textbox. Specify the location of the project as C:\COMLabs\ in Location textbox. With these
inputs, the dialog looks like the following.
3. Press OK button.
4. On the Overview page of the Win32 Application Wizard dialog box, press Next. On the
Application Settings page of the Win32 Application Wizard, under Application type, select
Console Application. On the Application Settings page of the Win32 Application Wizard,
under Additional options clear the Precompiled header check box.
6. In the Solution explorer, expand Source Files tree. You would see MathLibClient.cpp listed
under this tree.
7. Add the following code to this file. This code essentially implements the creates an instance of
Circle class and sets the radius to 10.0. And, prints the area of the Circle to the console. Save
the file after entering this code.
(i) Include the iostream.h and Circle.h in the beginning of the file.
#include <iostream>
using namespace std;
#include "Circle.h"
(ii) Replace the generated main method with the below main method.
(iii) Write the code in the main method to create an instance of Circle class and print the
area of the circle after setting the radius to 10.0.
Circle circle;
circle.SetRadius(10.0);
9. Expand treeview and goto Linker. Change the Category to Input. Add
C:\COMLabs\MathLib\Debug\MathLib.lib in Additional dependencies. We are hard coding
11. Compile the project by selecting Build->Rebuild All menu-item. This command compiles the
project and creates the output MathLibClient.exe application. Go to
C:\COMLabs\MathLibClient\Debug folder and verify that MathLibClient.exe is created
successfully.
12. Run the MathLibClient.exe and verify that area is printed in the console window is 31.415. You
can also print the value using ctl+F5 from the project. Yes, this value is incorrect. It should be
314.15 instead. We will correct this in the next section.
2. Go to the function GetArea in this file. Correct the formula as shown below in BOLD. Save the
file after entering this code.
return (dArea);
}
3. Compile the project by selecting Build->Rebuild All menu-item. This command compiles the
project and recreates the output MathLib.lib static library.
4. Now that we have corrected the formula for calculating the area of the circle, let us go back and
run the MathLibClient.exe. When we run the MathLibClient.exe, what did you observe? Well,
it still prints the incorrect value for area.
6. Run the new MathLibClient.exe. What did you see now? It prints the correct value 314.15 for
the area.