code rectangel

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter13: Overloading And Templates
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

code rectangel

#include <iostream>

using namespace std;

class Rectangle{

private:

double width;

double length;

static int numberOfObjects;

public:

Rectangle();

Rectangle(double , double);

~Rectangle(){cout<<"Destructor called "<<endl;}

static void printNumberOfObjects(){

cout<<"number of objects: "<<endl;}

void setDimention(double, double);

double getWidth();

double getLength();

double getArea();

void printRectangle() const ;

double getPerimeter();

};

int Rectangle::numberOfObjects=0;

Rectangle::Rectangle(){

/*width = 0;

length = 0;*/

cout<<"Default constructor called"<<endl;

setDimention(0,0);

numberOfObjects++;

}

Rectangle::Rectangle(double w, double l){

/* if(w>=0)// validation on the input

width= w;

else

width=0;

if(l>=0)

length = l;

else

length= 0;*/

cout<<"constructor with parameters called"<<endl;

setDimention(w,l);

numberOfObjects++;

}

void Rectangle::setDimention(double W, double L){

if(W>=0)// validation on the input

width= W;

else

width=0;

if(L>=0)

length = L;

else

length= 0;

// printRectangle(); we can call the other func directly

}

double Rectangle::getWidth(){

return width;

}

double Rectangle::getLength(){

return length;

}

double Rectangle::getArea(){

return width*length;

}

double Rectangle::getPerimeter(){

return 2*(width+length);

}

void Rectangle::printRectangle()const {

// width = 5; error

cout<<"The rectangle width is :"<<width<<" and the length is : "<<length<<endl;

}

 

void print(const Rectangle& r){

cout<<"printing from a func:";

r.printRectangle();

}

int main()

{

/*

Rectangle rect1;

// rect1.setDimention(5,10);

rect1.printRectangle();

cout<<"width = "<<rect1.getWidth()<<" length ="<<rect1.getLength()<<endl;

//set the values from the user:

double w,l;

cout<<"Please enter w: ";

cin>>w;

cout<<"Please enter l: ";

cin>>l;

rect1.setDimention(w,l);

rect1.printRectangle();

//set values from other object

Rectangle rect2;

rect2 = rect1;

cout<<"rectangle 2 info:"<<endl;

rect2.printRectangle();

double area = rect1.getArea();

cout<<"the area of the rectangle 1 is:"<<area<<endl;

double perimeter = rect1.getPerimeter();

cout<<"the perimeter of the rectangle 1 is:"<<perimeter<<endl;

print(rect2);

Rectangle rect3(15,16);

rect3.printRectangle();*/

Rectangle arrayRect[5];

double w, l;

for (int i = 0;i< 5;i++){

cout<<"Please enter a value for Rectangle"<<i<<" width & length"<<endl;

cin>>w; cin>>l;

arrayRect[i].setDimention(w,l);

}

for(int i = 0;i<5;i++){

cout<<"Rectangle "<<i<<" info: "<<endl;

arrayRect[i].printRectangle();

}

Rectangle::printNumberOfObjects();

arrayRect[0].printNumberOfObjects();

return 0;

}

Assignment Details:
|(1) Rewrite the Rectangle Class previously defined in lectures, to overload all
necessary operators (+ , - ,++,<<, >> etc...)
(2) write a main function to define object of the classes and use the override
operators.
Transcribed Image Text:Assignment Details: |(1) Rewrite the Rectangle Class previously defined in lectures, to overload all necessary operators (+ , - ,++,<<, >> etc...) (2) write a main function to define object of the classes and use the override operators.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT