#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class Shape
{
private:
int length;
int width;
public:
Shape() {
this->length=0;
this->width=0;
}
Shape(int length,int width) {
this->length=length;
this->width=width;
}
void setLength() {
int length=0;
cout<<"Enter the length for box:";
cin>>length;
this->length=length;
}
void setWidth() {
int width=0;
cout<<"Enter the width for box:";
cin>>width;
this->width=width;
}
int getLength()
{
return this->length;
}
int getWidth()
{
return this->width;
}
int area() {
return this->length*this->width;
}
int operator +(Shape &S2) {
return this->area()+S2.area();
}
};
int main()
{
Shape S1();
Shape S2();
S1.setLength();
S1.setWidth();
S2.setWidth();
S2.setLength();
cout<<"Area of shape box1:"<<S1.area()<<endl;
cout<<"Area of shape box2:"<<S2.area()<<endl;
cout<<"Addition of areas of box1 + box2:"<<S1+S2<<endl;
return 0;
}
TERMINAL IS GIVING THIS ERROR:
<pre>PS C:\Users\kk\Documents\C++ course\23k-7702> cd "c:\Users\kk\Documents\C++ course\23k-7702\" ; if ($?) { g++ question3.cpp -o question3 } ; if ($?) { .\question3 }
question3.cpp: In function 'int main()':
question3.cpp:65:5: error: request for member 'setLength' in 'S1', which is of non-class type 'Shape()'
S1.setLength();
^~~~~~~~~
question3.cpp:66:5: error: request for member 'setWidth' in 'S1', which is of non-class type 'Shape()'
S1.setWidth();
^~~~~~~~
question3.cpp:67:5: error: request for member 'setWidth' in 'S2', which is of non-class type 'Shape()'
S2.setWidth();
^~~~~~~~
question3.cpp:68:5: error: request for member 'setLength' in 'S2', which is of non-class type 'Shape()'
S2.setLength();
^~~~~~~~~
question3.cpp:69:34: error: request for member 'area' in 'S1', which is of non-class type 'Shape()'
cout<<"Area of shape box1:"<<S1.area()<<endl;
^~~~
question3.cpp:70:34: error: request for member 'area' in 'S2', which is of non-class type 'Shape()'
cout<<"Area of shape box2:"<<S2.area()<<endl;
^~~~
question3.cpp:71:47: error: invalid operands of types 'Shape()' and 'Shape()' to binary 'operator+'
cout<<"Addition of areas of box1 + box2:"<<S1+S2<<endl;
~~^~~
PS C:\Users\kk\Documents\C++ course\23k-7702>
What I have tried:
I am creating shape class objects with parametermized constructor my code works fine but when I create objects with default constructor compiler gives me error.