Question

C++

Create a Rectangle class that has 2 constructors. The class should have private or protected fields: length and width and a p

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

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new c++ program with name "main.cpp" is created, which contains following code.

main.cpp :

//c++ program to calculate area of Rectangle
//header files
#include <iostream>
using namespace std;
//class Rectangle
class Rectangle{
private :
double length;
double width;
public:
Rectangle(); //default constructor
Rectangle(double l,double w); //parameterized constructor
double area(); //method to calculate area
void display(); //method to display area
};
//default constructor assigning length and width to zero
Rectangle::Rectangle()
{
length=0;
width=0;
}
//parameterized constructor
Rectangle::Rectangle(double l,double w)
{
length=l;
width=w;
}
//method returning area of Rectangle
double Rectangle::area()
{
return length*width; //return area
}
//method display area of Rectangle
void Rectangle::display()
{   
//calling area from this method
cout<<"Area of Rectangle with length : "<<length<<" and width: "<<width<<" is :"<<area()<<endl;
}
//main method
int main()
{
double length, width;
//asking user to enter length
cout<<"Enter Length of Rectangle : "<<endl;
//reading length
cin>>length;
//asking user to enter width
cout<<"Enter Width of Rectangle : "<<endl;
//reading width
cin>>width;
//creating object of Rectangle and passing length and width
Rectangle myRectangle(length,width);
//calling method to display area
myRectangle.display();
return 0;
}

======================================================

Output : Compile and Run above program to get the screens as shown below

Screen 1 :Screen asking length and width

Enter Length of Rectangle: 10 Enter Width of Rectangle 12

Screen 2 :Screen showing area rectangle

Enter Length of Rectangle: 10 Enter Width of Rectangle : 12 Area of Rectangle with length 10 and width: 12 is :120 . Program

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
C++ Create a Rectangle class that has 2 constructors. The class should have private or protected...
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
  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • please use c++ Write a program that contains a class Rectangle with two private double precision...

    please use c++ Write a program that contains a class Rectangle with two private double precision members iLength and iWidth, public set and get member functions for these two members, a two argument constructor that sets the length and width to any two user specified values and a void function area() that computes the area and then prints this value by inserting it into the cout output stream. Write a main function that ereates a Rectangle with a length of...

  • Create the class Rectangle. The class will have two int properties, length and width. Create the...

    Create the class Rectangle. The class will have two int properties, length and width. Create the method printFigure. The method will print a character representation of the rectangle, using an asterisk, *, for each length/width position of the rectangle. Think of the length of the rectangle as the number of rows and the width as the number of columns. Use nested loops to create your solution. If the length was 5 and the width 8, then the printed figure would...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • C++ code For each of the following classes create default constructor and parameterized constructors(calling parent(s) parameterized...

    C++ code For each of the following classes create default constructor and parameterized constructors(calling parent(s) parameterized constructors where it applies Add accessor and mutator function for the attribute inherent to the class Create a Base class called Shape2d with the protected floating point attribute area operator overload the + & - and operations to return the float respective to the area Derive from the Base class from called Shape2d called Rectangle with the additional floating-point attributes length & width Derive...

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • 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...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

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