In: Computer Science
Lab 7 - Rectangle class- (Lec. 7)
1.) Create a new project and
name it: Rectangle
|
2.) Create the following 3 files:
3.) Write a program that produces
this output:
Rectangle.h
4.)In the Rectangle.h file, write a class specification for a Rectangle class.
2 private data members: _width (int) and _length (int)
7 public member functions:
Default constructor - Assigns ‘safe’ values to the data members.
Descructor
setWidth – An int value, passed from main(), is assigned to _width.
setLength - An int value, passed from main(), is assigned to _length.
getWidth – Returns the object’s width.
getLength – Returns the object’s length.
getArea – Calculates the area of the Rectangle object and returns the value.
Rectangle.cpp
5.) In the Rectangle.cpp file, write a function implementation for each function
prototype listed in the Rectangle class specification.
Source.cpp
6.)In main(), declare an object of Rectangletype, and name it: tennisCourt
7.) In main(), declare three variables of double data type, and
and name them: width, length and area
6.) Prompt the user to enter a width and length (see output).
7.) Read in the user’s inputs and assign them to the data members of tennisCourt,
by using the ‘set’ member functions.
8.)Then assign the tennis court’s width, length and area to variable in main() by
using the ‘get’ member functions.
9.)Display the width, length, and area. (see output)
//Source.cpp
#include<iostream>
#include"Rectangle.h"
int main()
{
Rectangle tennisCourt;
double width = 0, length = 0, area = 0;
std::cout<<"Enter the width of tennis court:
";
std::cin>>width;
tennisCourt.setWidth(width);
std::cout<<"\nEnter the length of tennis court: ";
std::cin>>length;
tennisCourt.setLength(length);
std::cout<<"\nThe width of Tennis court is
"<<tennisCourt.getWidth()<<" feet.";
std::cout<<"\nThe length of Tennis court is
"<<tennisCourt.getLenght()<<" feet.";
area = tennisCourt.getArea();
std::cout<<"\nThe Area of Tennis court is
"<<area<<" square feet.";
}
//Rectangle.h
class Rectangle{
private:
int _length;
int _width;
public:
Rectangle();
void setWidth(int width);
void setLength(int length);
int getWidth();
int getLenght();
int getArea();
~Rectangle();
};
//Rectangle.cpp
#include"Rectangle.h"
Rectangle::Rectangle(){
this->_length = 0;
this->_width = 0;
}
void Rectangle::setLength(int length){
this->_length = length;
}
void Rectangle::setWidth(int width){
this->_width = width;
}
int Rectangle::getLenght(){
return this->_length;
}
int Rectangle::getWidth(){
return this->_width;
}
int Rectangle::getArea(){
return this->_length * this->_width;
}
Rectangle ::~Rectangle(){
}