Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 #include <iostream>
using namespace std;
 class  area{
 
     int arr1;
     int arr2;
     public:
      void rectangle(
          int lenght ,int breadth){
              cout<<"the value of length is"<<endl;
              cin>>lenght;
              cout<<"the value of breadth is "<<endl;
              cin>>breadth;
              arr1=lenght*breadth;
              cout<<"the area of rectangle is"<<arr1<<endl;
          }
        void circle(int r){
           int pi;
            pi=3.14;
            cout<<"the value of radius is"<<endl;
            cin>>r;
            arr2=pi*r*r;
            cout<<"the area of circle is"<<arr2<<endl;
        }
 };
int main(){
    rectangle();
    circle();
    
    return 0;
}

What I have tried:

<pre>
 
 class  area{
 
     int arr1;
     int arr2;
     public:
      void rectangle(
          int lenght ,int breadth){
              cout<<"the value of length is"<<endl;
              cin>>lenght;
              cout<<"the value of breadth is "<<endl;
              cin>>breadth;
              arr1=lenght*breadth;
              cout<<"the area of rectangle is"<<arr1<<endl;
          }
        void circle(int r){
           int pi;
            pi=3.14;
            cout<<"the value of radius is"<<endl;
            cin>>r;
            arr2=pi*r*r;
            cout<<"the area of circle is"<<arr2<<endl;
        }
 };
int main(){
    rectangle();
    circle();
    
    return 0;
}
Posted
Updated 28-Oct-21 6:42am

The most obvious errors:

You are not using the classes you have created. Once you have defined area class, you have to create an instance of area class and store it in a variable to access its methods easily.
C++
int main()
{
   area myArea;
   area.rectangle(/* missing parameters here */);
   area.circle(/* and here as well */);
}


As said in code comments, the second issue is that you are trying to call rectangle() and circle() functions without providing them with their parameters. The function call must match function signature, otherwise compiler will issue an error.

Maybe a quick refresh on c++ 101, and its usage of classes, could suit you?
C++ Classes and Objects[^]

You will also meet an issue with arr2, since you will store in an integer the result of a double operation; your results for the circle will be truncated values (values without their decimal parts). You should double type for arr2; and probably for arr1 as well, as a matter of consistency.

Last, you should try to give meaningful name to your variables:
C++
// Compare
arr2 = pi*r*r;
// to
circleArea = pi*r*r;

This makes things a lot easier for you and for anyone who wants to read the code.
 
Share this answer
 
v2
I did notice that your rectangle and circle functions' definitions indicate that they require input parameter values, but you ignore them and take inputs from the console. So your declarations should be changed to:
C++
void rectangle(){

// and for circle 

void circle(){


Also you have put these methods into a class called area but you are trying to call its members directly, which is not possible.

Change the code in main to the following:
C++
area myarea; // instantiate an area object
myarea.rectangle(); // call the object's methods
myarea.circle();
 
Share this answer
 

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