Create A Class CalledExampleSeparateImplementationVeryImportant
Create A Class CalledExampleSeparateImplementationVeryImportant
#include <iostream>
#include <iomanip>
class Rectangle
public:
private:
double Rectangle::getWidth()
return width;
double Rectangle::getLength()
{
return length;
double Rectangle::perimeter()
double Rectangle::area()
int main()
{
Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 );
// output Rectangle a
cout << "a: length = " << a.getLength() << "; width = "
// output Rectangle b
cout << "b: length = " << b.getLength() << "; width = "
cout << "c: length = " << c.getLength() << "; width = "
<< c.getWidth() << "; perimeter = " << c.perimeter()
// getch();
return 0;
} // end main
For better software engineering, it is recommended that the class declaration and implementation be kept in 2 separate files:
declaration is a header file ".h"; while implementation in a ".cpp".
This is known as separating the public interface (header declaration) and the implementation.
Interface is defined by the designer, implementation can be supplied by others.
While the interface is fixed, different vendors can provide different implementations.
Furthermore, only the header files are exposed to the users, the implementation can be provided in an object file ".o" (or in
a library). The source code needs not be given to the users.
Rectangle
private
area
perimeter
setLength
setWidth
getWidth
getLength
5. What does the prototype look like for the area member function?
double area(void);
double getLength(void);
double Rectangle::area(void)
10. Write the member function for setLength to enforce the data constraints.
void Rectangle::setLength(double L)
length=L;
else
length=1.0;
Rectangle::Rectangle(double l, double w)
setWidth(w);
setLength(l);
13. What does the header file look like for this project??
//
#ifndef crect
#define crect
class Rectangle {
public:
double perimeter(void);
double area(void);
double getWidth(void);
double getLength(void);
private:
double length;
double width;
};
#endif
//
//
#include
#include "crect.h"
// Constructor
Rectangle::Rectangle(double L, double W)
{
setWidth(W);
setLength(L);
double Rectangle::area(void)
double Rectangle::perimeter(void)
return 2*(length+width);
void Rectangle::setWidth(double W)
{
else width=1.0;
void Rectangle::setLength(double L)
double Rectangle::getWidth(void)
return width;
}
double Rectangle::getLength(void)
return length;
//
// Driver
//
#include <iostream>
#include <iomanip>
#include "crect.h"
Rectangle a;
Rectangle b(4.0,5.0);
Rectangle c(67.0,888.0);
// Rectangle a
cout << "a : area =" << a.area() << endl << endl;
// Rectangle b
cout << "b : area =" << b.area() << endl << endl;
// Rectangle c
cout << "c : area =" << c.area() << endl << endl;
system("pause");
return 0;
}
When using CodeBlocks (Some of you might be using MS Visual Studio, the idea is the same)
1. Create a Project
2.
3. Add the files using File Menu or
Create a Project
Open CodeBlocks
To create a project, click on the File pull-down menu, open New and then Project.
This will bring up the New from template window. Opening (clicking on) Console Application will then allow you to write a
program on the console. The other applications are for developing more advanced types of applications. After selecting
Console application, click on the Go button to begin using the Console Application Wizard.
Press Next to go to the next step.
The next window allows you to choose the language that you will use. Select the language as C++, then press Finish.
Start by filling in the Project Title. You will notice that the Project Filename automatically becomes the same name. If you
wish, you can change the filename, but for simplicity leave it as is. To specify the location of the folder to contain the
project, click on the “...” button (selected in the picture above) and browse to a folder on your drive to store the project.
Generally, you can save it in My Documents.
Press Ok after selecting My Documents
Click finish.
Copy and paste the class definition from the previous example into the file between the green text and save.
Go to the File menu again, choose New then File.
Choose C/C++ source as shown below.
Click GO.
Click NEXT.
Enter file name as indicated above and then click FINISH.
Copy and paste the code for the function definitions. Remember to include: #include “Rectangle.h” in order to link the class
definition to this file.
Click Save.
Go to file new again as shown below:
Click File
As in the previous step ,choose C/C++ source file and then click Go.
Click NEXT
Click NEXT
Enter file details as shown above:
Click FINISH
Copy and paste the code for the main function/driver program. Remember to include: #include “Rectange.h” in order to
connect the driver program to the other two files.Now you have all three files connected or linked together.
Save and then Compile the project and run it!
When finished,do the following from the File menu:
Rectangle.h
class Rectangle
public:
Rectangle.cpp
//#include <iostream>
#include "Rectangle.h"
double Rectangle::getWidth()
return width;
double Rectangle::getLength()
return length;
} // end fucntion getLength
double Rectangle::perimeter()
double Rectangle::area()
TestImp
//
// Driver
//
#include <iostream>
#include <iomanip>
#include "Rectangle.h"
int main()
// output Rectangle a
cout << "a: length = " << a.getLength() << "; width = "
// output Rectangle b
cout << "b: length = " << b.getLength() << "; width = "
cout << "c: length = " << c.getLength() << "; width = "
// getch();
return 0;
} // end main
Close project
Quit CodeBlocks