0% found this document useful (0 votes)
18 views62 pages

Create A Class CalledExampleSeparateImplementationVeryImportant

1. The document describes a Rectangle class with private length and width data members that default to 1. 2. It includes public member functions to calculate the perimeter and area, and set/get functions for length and width. 3. The set functions verify length and width are between 0-20, otherwise they default to 1.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views62 pages

Create A Class CalledExampleSeparateImplementationVeryImportant

1. The document describes a Rectangle class with private length and width data members that default to 1. 2. It includes public member functions to calculate the perimeter and area, and set/get functions for length and width. 3. The set functions verify length and width are between 0-20, otherwise they default to 1.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 62

Create a class called “Rectangle” with attributes length and width, each of which defaults to 1.

They are defined as private.


Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for
the length and width attributes. The set functions should verify that length and width are each floating-point numbers
larger than 0.0 and less than 20.0. They are defined as public.

#include <iostream>

#include <iomanip>

using namespace std;

class Rectangle

public:

Rectangle( double = 1.0, double = 1.0 ); // default constructor

void setWidth( double w ); // set width

void setLength( double l ); // set length


double getWidth(); // get width

double getLength(); // get length

double perimeter(); // perimeter

double area(); // area

private:

double length; // 1.0 < length < 20.0

double width; // 1.0 < width < 20.0

}; // end class Rectang

// Member-function definitions for class Rectangle.

Rectangle::Rectangle( double w, double l )

setWidth(w); // invokes function setWidth


setLength(l); // invokes function setLength

} // end Rectangle constructor

void Rectangle::setWidth( double w )

width = w > 0 && w < 20.0 ? w : 1.0; // sets width

} // end function setWidth

void Rectangle::setLength( double l )

length = l > 0 && l < 20.0 ? l : 1.0; // sets length

} // end function setLength

double Rectangle::getWidth()

return width;

} // end function getWidth

double Rectangle::getLength()
{

return length;

} // end fucntion getLength

double Rectangle::perimeter()

return 2 * ( width + length ); // returns perimeter

} // end function perimeter

double Rectangle::area()

return width * length; // returns area

} // end function area

int main()

{
Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 );

cout << fixed;

cout << setprecision( 1 );

// output Rectangle a

cout << "a: length = " << a.getLength() << "; width = "

<< a.getWidth() << "; perimeter = " << a.perimeter()

<< "; area = " << a.area() << '\n';

// output Rectangle b

cout << "b: length = " << b.getLength() << "; width = "

<< b.getWidth() << "; perimeter = " << b.perimeter()

<< "; area = " << b.area() << '\n';

// output Rectangle c; bad values attempted

cout << "c: length = " << c.getLength() << "; width = "
<< c.getWidth() << "; perimeter = " << c.perimeter()

<< "; area = " << c.area()<< endl;

// getch();

return 0;

} // end main

Separating Header and Implementation

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.

I shall illustrate with the following examples.


Instead of putting all the codes in a single file. We shall "separate the interface and implementation" by placing the codes in
3 files.

1. Rectangle.h: defines the public interface of the Rectangle class.


2. Rectange.cpp: provides the implementation of the Rectangle class.
3. TestRectangle.cpp: A test driver program for the Rectangle class.

1. What is the class called.?

Rectangle

2. What data members are in the class?

length and width both doubles

3. What access modifier should the data member have?

private

4. What member functions need to be defined?

area

perimeter

setLength
setWidth

getWidth

getLength

5. What does the prototype look like for the area member function?

double area(void);

6. What does a get for length prototype look like?

double getLength(void);

7. What does a set for length prototype look like?

void setLength(double l);

8. Write the function for area.

double Rectangle::area(void)

return length * width;

9. How would the area function be invoked?


Rectangle a;

cout << "a : area=" << a.area() << endl;

10. Write the member function for setLength to enforce the data constraints.

void Rectangle::setLength(double L)

if ( L > 0 && L < 20.0)

length=L;

else

length=1.0;

11. Do we need multiple (overloaded) functions for the contruction?

It should be able to handle the following instances.


Rectangle a;

Rectangle b(20.0); // Set the length

Rectangle c(10.0,10.0) // set the length and width

No we can handle this with deafult arguments:

12. Write the constructor

Rectangle(double=1.0, double=1.0); // Constructor

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:

Rectangle(double=1.0, double=1.0); // Constructor

double perimeter(void);

double area(void);

void setWidth(double w);

void setLength(double l);

double getWidth(void);

double getLength(void);

private:

double length;
double width;

};

#endif

14. Write the member functions

//

// Member Functions for Rectangle

//

#include

#include "crect.h"

using namespace std;

// Constructor

Rectangle::Rectangle(double L, double W)

{
setWidth(W);

setLength(L);

double Rectangle::area(void)

return length * width;

double Rectangle::perimeter(void)

return 2*(length+width);

void Rectangle::setWidth(double W)
{

if ( W> 0 && W < 20.0) width=W;

else width=1.0;

void Rectangle::setLength(double L)

length= L > 0 && L < 20.0 ? L : 1.0;

double Rectangle::getWidth(void)

return width;

}
double Rectangle::getLength(void)

return length;

15. Write the main file.

//

// Driver

//

#include <iostream>

#include <iomanip>

#include "crect.h"

using namespace std;


int main()

Rectangle a;

Rectangle b(4.0,5.0);

Rectangle c(67.0,888.0);

cout << setiosflags(ios::fixed | ios::showpoint);

cout << setprecision(1);

// Rectangle a

cout << "a : length =" << a.getLength() << endl;

cout << "a : width =" << a.getWidth() << endl;

cout << "a : perimeter =" << a.perimeter() << endl;

cout << "a : area =" << a.area() << endl << endl;
// Rectangle b

cout << "b : length =" << b.getLength() << endl;

cout << "b : width =" << b.getWidth() << endl;

cout << "b : perimeter =" << b.perimeter() << endl;

cout << "b : area =" << b.area() << endl << endl;

// Rectangle c

cout << "c : length =" << c.getLength() << endl;

cout << "c : width =" << c.getWidth() << endl;

cout << "c : perimeter =" << c.perimeter() << endl;

cout << "c : area =" << c.area() << endl << endl;

system("pause");

return 0;
}

16. Compile the project and run it!

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 on the Next Button when done.


The next window to pop up will be the Compiler screen. This specifies where the Debug and Release compiled versions of
your program will be placed. Leave this setting alone and press Next.
The system will then return to the [myclassExampleS] window and you are ready to write your program. It should be noted
that the Build target is Debug, which will allow you to use the debugger to find errors.
In the Management area of the screen (Shift-F2 toggles the Management display), you will see the files that are part of the
project in the Projects tab. To see the source files, click on the plus [+]’s to expand the Workspace and its subdirectories.
Adding Files To Your Project
If you have a project with additional existing files, go to the Project menu and select “Add files.” This will bring in the files
associated with your program. You also have the option to Remove files,performing Build options and to Set programs’
arguments….
Choose the file you want to create (In the current program of file separation, we have a header file and 2 cpp files)
Choose the header file
Click GO
Click next
Enter file details as indicated in the diagram above.

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:

1. Close all files ( File the close all files)


2. Close project (Click File then Close project)

3. Quit CodeBlocks (File menu then Quit)

The code of the rectangle as a single unit in given below

Rectangle.h

class Rectangle

public:

Rectangle( double = 1.0, double = 1.0 ); // default constructor

void setWidth( double w ); // set width

void setLength( double l ); // set length

double getWidth(); // get width

double getLength(); // get length

double perimeter(); // perimeter

double area(); // area


private:

double length; // 1.0 < length < 20.0

double width; // 1.0 < width < 20.0

}; // end class Rectang

Rectangle.cpp

// Member-function definitions for class Rectangle.

//#include <iostream>

#include "Rectangle.h"

Rectangle::Rectangle( double w, double l )

setWidth(w); // invokes function setWidth

setLength(l); // invokes function setLength

} // end Rectangle constructor


void Rectangle::setWidth( double w )

width = w > 0 && w < 20.0 ? w : 1.0; // sets width

} // end function setWidth

void Rectangle::setLength( double l )

length = l > 0 && l < 20.0 ? l : 1.0; // sets length

} // end function setLength

double Rectangle::getWidth()

return width;

} // end function getWidth

double Rectangle::getLength()

return length;
} // end fucntion getLength

double Rectangle::perimeter()

return 2 * ( width + length ); // returns perimeter

} // end function perimeter

double Rectangle::area()

return width * length; // returns area

} // end function area

TestImp

//

// Driver

//
#include <iostream>

#include <iomanip>

#include "Rectangle.h"

using namespace std;

int main()

Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 );

cout << fixed;

cout << setprecision( 1 );

// output Rectangle a

cout << "a: length = " << a.getLength() << "; width = "

<< a.getWidth() << "; perimeter = " << a.perimeter()


<< "; area = " << a.area() << '\n';

// output Rectangle b

cout << "b: length = " << b.getLength() << "; width = "

<< b.getWidth() << "; perimeter = " << b.perimeter()

<< "; area = " << b.area() << '\n';

// output Rectangle c; bad values attempted

cout << "c: length = " << c.getLength() << "; width = "

<< c.getWidth() << "; perimeter = " << c.perimeter()

<< "; area = " << c.area()<< endl;

// getch();

return 0;

} // end main

Close all files

Close project
Quit CodeBlocks

You might also like