Question

Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

Creating a Class in C++

Summary

In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter.

Instructions

Ensure the class file named Rectangle.cpp is open in your editor.

In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double.

Write public set methods to set the values for lengthand width.

Write public get methods to retrieve the values for length and width.

Write a public calculateArea()method and a public calculatePerimeter()method to calculate and return the area of the rectangle and the perimeter of the rectangle.

Open the file named MyRectangleClassProgram.cpp.

In the MyRectangleClassProgram, create two Rectangle objects named rectangle1 and rectangle2 using the default constructor as you saw in MyEmployeeClassProgram.cpp.

Set the length of rectangle1to 10.0 and the width to 5.0. Set the length of rectangle2to 7.0 and the width to 3.0.

Print the value of rectangle1’s perimeter and area, and then print the value of rectangle2’s perimeter and area.

Execute the program by clicking the Run button at the bottom of the screen

// This program uses the programmer-defined Rectangle class.
#include "Rectangle.cpp"
#include <iostream>
using namespace std;
int main()
{

Rectangle rectangle1;
Rectangle rectangle2;

rectangle1.setLength(10.0);
rectangle1.setWidth(5.0);
rectangle2.setLength(7.0);
rectangle2.setWidth(3.0);


cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;
cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;
cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;
cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Rectangle.cpp

class Rectangle
{
private:
  
double Length,Width; //PRIVATE ATTRIBUTES LENGTH AND WIDTH
double Area,Perimeter; //PRIVATE ATTRIBUTES AREA AND PERIMETER

public:
  
double setLength(double a)
{
//PUBLIC METHOD TO SET VALUE OF LENGTH
Length=a;
return 0;
}
double setWidth(double b)
{
//PUBLIC METHOD TO SET VALUE OF WIDTH
Width=b;
return 0;
}
double getLength()
{
//PUBLIC METHOD TO GET THE VALUE OF LENGTH
return Length;
}
double getWidth()
{
//PUBLIC METHOD TO GET THE VALUE OF WIDTH
return Width;  
}
double calculatePerimeter()
{
//PUBLIC METHOD TO CALCULATE AND DISPLAY THE PERIMETER OF THE RECTANGLE
Perimeter=2*(Length+Width);
return Perimeter;
}
double calculateArea()
{
//PUBLIC METHOD TO CALCULATE AND DISPLAY THE AREA OF THE RECTANGLE
Area=Length*Width;
return Area;
}
};

Note: Copy the above code and save as Rectangle.cpp

MyRectangleClassProgram.cpp

#include "Rectangle.cpp" //IMPORTS THE CLASS FILE Rectangle.cpp
#include <iostream>

using namespace std;

int main()
{
Rectangle rectangle1; //rectangle1 is an object of Class Rectangle
Rectangle rectangle2; //rectangle2 is an object of Class Rectangle

rectangle1.setLength(10.0); //This code will set the value to the attribute Length of Object rectangle1
rectangle1.setWidth(5.0); //This code will set the value to the attribute Width of Object rectangle1
rectangle2.setLength(7.0); //This code will set the value to the attribute Length of Object rectangle2
rectangle2.setWidth(3.0); //This code will set the value to the attribute Width of Object rectangle2

//Following statement will calculate and display the Perimeter of Object rectangle1
cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;
  
//Following statement will calculate and display the Area of Object rectangle1
cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;
  
//Following statement will calculate and display the Perimeter of Object rectangle2
cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;
  
//Following statement will calculate and display the Area of Object rectangle2
cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;

return 0;
}

Note: Copy the code and save as MyRectangleClassProgram.cpp

Output Perimeter of rectanglel is 30 Area of rectanglel is 50 Perimeter of rectangle2 is 20 Area of rectangle2 is 21

Add a comment
Know the answer?
Add Answer to:
Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...

    Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • I need help with the C++ for the following problem: Write a program uses objected oriented...

    I need help with the C++ for the following problem: Write a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(),...

  • Use C++ to implement a program uses objected oriented programming to calculate the area of a...

    Use C++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the...

  • Creating a class Rectangle with attributes length and width.

    Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle's perimeter and area. It hasset and get methods for both lengthand width. Theset methodsshould verify that lengthand width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program totest class Rectangle.Program should be written in Java .I need some with this problem so urgently----test is due tomorrow and will be similar tothe above question. I...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT