Click here to Skip to main content
15,882,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class Shape
{
 private:
 int length;
 int width;
 public:
 Shape() //DEFAULT CONSTRUCTOR
 {
  this->length=0;
  this->width=0;
 }
 Shape(int length,int width) //PARAMETERIZED CONSTRUCTOR
 {
  this->length=length;
  this->width=width;
 } 
 void setLength() //SETTER FUNCTION FOR SETTING LENGTH
 { 
   int length=0;   
   cout<<"Enter the length for box:";
   cin>>length;
   this->length=length;
 }
 void setWidth() //SETTER FUNCTION FOR SETTING WIDTH
 {
  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() //AREA FUNCTION THAT RETURNS THE AREA OF SHAPE OBJECT
 {
  return this->length*this->width;
 }
 int operator +(Shape &S2) // + OPERATOR OVERLOADING FOR ADDING AREAS OF TEO 
                           // SHAPE OBJECTS
 {
  return this->area()+S2.area();
 }
};
int main()
{
 Shape S1();
 Shape S2();
 //Shape S1(3,2);
 //Shape S2(3,4);
 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.
Posted
Updated 15-Oct-23 8:11am
v2

1 solution

Try writing it as
C++
int main()
{
    Shape s1;
    Shape s2;
    ...
}
With no arguments the parenthesis are unnecessary.
 
Share this answer
 
Comments
Zari Mariam 15-Oct-23 20:40pm    
Thank you @Rick York. Now my code is working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900