0% found this document useful (0 votes)
40 views

COM Lab # 1: Re-Using Class Via. Static Library

This document describes a lab to demonstrate creating and using a static library in C++. It involves: 1) Creating a static library project called "MathLib" containing a Circle class with methods to set the radius and get the area. 2) Adding and implementing the Circle class in the MathLib project. 3) Creating a "MathLibClient" application project that includes the Circle header, creates a circle, sets the radius to 10, and prints the area. 4) Linking the MathLibClient project to the MathLib static library and correcting an error in the area calculation formula.

Uploaded by

Dr Engineer
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

COM Lab # 1: Re-Using Class Via. Static Library

This document describes a lab to demonstrate creating and using a static library in C++. It involves: 1) Creating a static library project called "MathLib" containing a Circle class with methods to set the radius and get the area. 2) Adding and implementing the Circle class in the MathLib project. 3) Creating a "MathLibClient" application project that includes the Circle header, creates a circle, sets the radius to 10, and prints the area. 4) Linking the MathLibClient project to the MathLib static library and correcting an error in the area calculation formula.

Uploaded by

Dr Engineer
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

COM Lab # 1

Re-using Class via. Static Library

Created by: Hari Krishna Bhattu


Creation Date: 21st Nov 2007
Objective
This lab shows how to reuse the functionality of a class using a Static Library. This library contains a
simple class that represents a Circle. For this lab, Circle is denoted by just its radius. The compiled
Static Library is then used in a Test Console application. Test Console application creates an instance of
Circle class and prints its Area.

1 Steps (for creating empty Static Library)


1. All the projects created in this lab are placed under a folder C:\COMLabs. Create this folder.

2. Open Visual C++ IDE.

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.

5. Press OK to start the Win32 application wizard.

COM Training Lab # 1 Page 2 of 10


6. 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
Static library. On the Application Settings page of the Win32 Application Wizard, under
Additional options clear the Precompiled header check box.

7. Press Finish to create the project. Press Finish. This would create a Win32 Static Library project
with no source files in it.

2 Steps (for adding Circle class to Static Library)


1. Right click on MathLib project in solution explorer. Click on Add->New Item. Select Header File
(.h) in the Add New Item dialog. Specify the name of it as Circle.h in the textbox. Make sure
that the location in Location textbox is C:\ComLabs\MathLib. With these inputs, the dialog
looks like the following.

COM Training Lab # 1 Page 3 of 10


2. Press Add button.

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();

void SetRadius(double dRadius);


double GetArea();
};

COM Training Lab # 1 Page 4 of 10


6. In the Solution explorer, expand Source Files tree. You would see Circle.cpp listed under this
tree.

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.

(i) Include the Circle.h in the beginning of the file.

#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.

static const double MATH_PI = 3.1415;

(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.

void Circle::SetRadius(double dRadius)


{
this->m_dRadius = dRadius;
}

(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);
}

8. Compile the project by selecting Build->Rebuild Solution menu-item. This command


compiles the project and creates the output MathLib.lib static library. Go to
C:\COMLabs\MathLib\Debug folder and verify that MathLib.lib is created successfully. We will
link to this library when we are creating the Test Client application.

COM Training Lab # 1 Page 5 of 10


3 Steps (for creating Math Lib Client application)
1. We will create a Math Lib Client application to test MathLib static library. The Math Lib Client
application will be a simple Win32 Console Application. Open VC++ IDE. Invoke New Project
dialog by selected File->New menu-item. This displays the New Project dialog.

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.

COM Training Lab # 1 Page 6 of 10


5. Press Finish. This would create a Win32 Console Application project with one source file
named MathLibClient.cpp.

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.

COM Training Lab # 1 Page 7 of 10


void main(void)
{
}

(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);

std::cout << "Area of the circle:: " << circle.GetArea() <<


std::endl;

8. Open Project properties dialog by selecting Project->MathLibClient Properties menu-item. In


MathLibClient Property Pages, navigate the treeview to Configuration Properties->C/C++-
>General. Specify C:\COMLabs\MathLib in Additional include directories textbox. This is
the location of Circle.h. We are hard coding these paths for demonstration purposes. In real
projects, we never hardcode the paths like this. With this change, the Project Settings dialog
looks like the following.

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

COM Training Lab # 1 Page 8 of 10


these paths for demonstration purposes. In real projects, we never hardcode the paths like this.
With this change, the Project Settings dialog looks like the following.

10. Press OK.

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.

4 Steps (for correcting the formula for Area calculation)


1. Open MathLib project in VC++ IDE. In the Solution explorer, expand Source Files tree. You
would see Circle.cpp listed under this tree. Double click the item Circle.cpp. This would open
Circle.cpp file for editing.

2. Go to the function GetArea in this file. Correct the formula as shown below in BOLD. Save the
file after entering this code.

COM Training Lab # 1 Page 9 of 10


double Circle::GetArea()
{
double dArea = MATH_PI * m_dRadius * m_dRadius;

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.

5. Re-compile the MathLibClient project by selecting Build->Rebuild All menu-item. This


command compiles the project and creates the output MathLibClient.exe application again.

6. Run the new MathLibClient.exe. What did you see now? It prints the correct value 314.15 for
the area.

COM Training Lab # 1 Page 10 of 10

You might also like