C++ Objectives: 1. Classes and Data Abstraction 2. User-defined classes Question
ID: 3785743 • Letter: C
Question
C++
Objectives:
1. Classes and Data Abstraction
2. User-defined classes
Question:
In this assignment, you are asked to write the definition for a rectangle class and test it with the test program provided. The description of the rectangle class and the driver program are given on the attached file rectangle.cpp. Download the rectangle.cpp and without removing or changing any of the member functions or member variables of this class, complete 10 member functions bodies such that your program produces exact output as provided. You should keep the class definition and the main function in the same source file, and you are not allowed to use global variables. Your program should be well commented and in particular, begin with a series of comments specifying your name and lab number. (You should not change class definition or main function. Just complete the rest of the work.)
rectangle.cpp below:
#include <iostream>
using namespace std;
class rectangle
{
public:
rectangle(int len, int wid); //Parameterized Constructor
rectangle(); //Default Constructor; Default lenght is 20 and default width is 10
void setWidth(int newWid); //Mutator function
void setLength(int newLen);//Mutator function
int getWidth() const;//Accessor function
int getLength() const;//Accessor function
void printRectangle(); //Print function
int getArea();//Find Area; hint: Area is width * length
int getPrimeter();//Find Perimeter; hint: Perimeter is (width + length)*2
bool equals(const rectangle& otherRectangle) const; //Comparison function
private:
int length; //variable to store length
int width; //variable to store width
};
//Start your class functions codes from here!
// Driver program for the rectangle class
int main()
{
rectangle s1;
rectangle s2(5, 20);
rectangle s3;
//Display rectangles
cout << "Rectangle s1 ";
s1.printRectangle();
cout << "Rectangle s2 ";
s2.printRectangle();
cout << "Rectangle s3 ";
s3.printRectangle();
// Test Equality
if (s1.equals(s2))
cout << "s1 and s2 are equal rectangle!" << endl;
else
cout << "s1 and s2 are unequal rectangle!" << endl;
if (s1.equals(s3))
cout << "s1 and s3 are equal rectangle!" << endl;
else
cout << "s1 and s3 are unequal rectangle!" << endl;
//Display area and perimeter
cout << "Area of s1=" << s1.getArea() << endl;
cout << "Area of s2=" << s2.getArea() << endl;
cout << "Perimeter of s1=" << s1.getPrimeter() << endl;
cout << "Perimeter of s2=" << s2.getPrimeter() << endl;
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
class rectangle
{
private:
int length; //variable to store length
int width; //variable to store width
public:
//Parameterized Constructor
rectangle(int len, int wid)
{
length=len;
width=wid;
}
//Default Constructor; Default lenght is 20 and default width is 10
rectangle()
{
length=20;
width=10;
}
void setWidth(int newWid)
{
width=newWid;
}
//Mutator function
void setLength(int newLen)//Mutator function
{
length=newLen;
}
int getWidth()//Accessor function
{
return width;
}
int getLength()
{
return length;
}
void printRectangle()
{
//Print function
cout<<"length is"<<length<<"width is "<<width;
}
int getArea()//Find Area; hint: Area is width * length
{
cout<<" the area is"<<width*length<<endl;
}
int getPrimeter()//Find Perimeter; hint: Perimeter is (width + length)*2
{
cout<<"the perimeter is"<<((width+length)/2)<<endl;
}
};
//Start your class functions codes from here!
// Driver program for the rectangle class
int main()
{
rectangle s1;
rectangle s2(5, 20);
rectangle s3;
//Display rectangles
cout << "Rectangle s1 ";
s1.printRectangle();
cout << "Rectangle s2 ";
s2.printRectangle();
cout << "Rectangle s3 ";
s3.printRectangle();
//Display area and perimeter
cout << "Area of s1=" << s1.getArea() << endl;
cout << "Area of s2=" << s2.getArea() << endl;
cout << "Perimeter of s1=" << s1.getPrimeter() << endl;
cout << "Perimeter of s2=" << s2.getPrimeter() << endl;
return 0;
}
output
Rectangle s1 length is20width is 10Rectangle s2 length is5width is 20Rectangle s
3 length is20width is 10 the area is200
Area of s1=4745728
the area is100
Area of s2=4745728
the perimeter is15
Perimeter of s1=4745728
the perimeter is12
Perimeter of s2=4745728
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.