Answer to Question #338999 in C++ for kkk

Question #338999

Create a class named 'Rectangle' with two data members- length and breadth and a function to calculate the area which is 'length*breadth'. The class has three constructors which are:


1 - having no parameter - values of both length and breadth are assigned zero.

2 - having two numbers as parameters - the two numbers are assigned as length and breadth respectively.

3 - having one number as parameter - both length and breadth are assigned that number.

Now, create objects of the 'Rectangle' class having none, one and two parameters and print their areas.



1
Expert's answer
2022-05-09T14:54:31-0400
#include <iostream>
using namespace std;

class Rectangle {    //define class
public:
    Rectangle() { length = 0; width = 0; }   // default constructor

     //no-default constructor  
    Rectangle(float l, float w) { length = l; width = w; }
    
     //no-default constructor  
    Rectangle(float l) { length = l; width = l; }    

    float area() {return length * width; } //return area


private:
    float length, width;
};

int main() {
    Rectangle r1;
    Rectangle r2(5,7);
    Rectangle r3(5);

cout<<"Area 1: "<<r1.area()     <<endl;
cout<<"Area 2: "<<r2.area()     <<endl;
cout<<"Area 3: "<<r3.area()     <<endl;    
     
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS