1
#include <iostream>

using namespace std;

class Circle
{
    private:
        double radius;
    public:
        Circle() //Constructor 
        {   radius = 0; }
        
        Circle(double); //Parameterized Constructor 
        
        void setRadius(double);
        
        double getRadius() const
        { return radius; }
        
        double getArea() const
        { return 3.14 * radius * radius; }
};

Circle::Circle(double r)
{
    radius = r;
}

void Circle::setRadius(double r) 
{
    radius = r;
}

int main()
{
    int size = 3;  //Set size of array of objects 
    Circle arr[size] = { Circle(1.2), Circle(2.2), Circle(5.5) }; //Array of Objects 
    
    for (int index = 0; index < size; index++)
    {
        cout << arr[index].Circle();  //Error: Invalid use of Circle::Circle
    }
    
    Circle b(2.8);                  //Instance of class
    cout << b.getRadius();          //Prints out the radius of object
    
    Circle c;                       //Another instance 
    cout << endl << c.getRadius(); //Another radius
    
    double r;                     //Variable to store the radius based on user input 
    cout << "\nEnter a radius: "; 
    cin >> r;                       
    c.setRadius(r);             //Pass user input into radius member function 
    cout << c.getRadius();      //Print new radius of object c
    cout << endl << c.getArea(); //Prints area of object c.
    

    return 0;
    
}

I'm trying to figure out how to print out the values of the above array of objects using the class parmeterized constructor (it's supposed to print out the radius of each Circle object). When run the program, the error I get says invalid use of Circle::Circle.

3
  • You have to either implement a getter function, or a print operator. Commented Feb 1, 2021 at 9:05
  • 1
    In your for loop change cout << arr[index].Circle(); to cout << arr[index].getRadius() << endl; Commented Feb 1, 2021 at 9:08
  • Thanks, guys for the quick response! Commented Feb 1, 2021 at 9:18

1 Answer 1

1

You need to overload the <<operator for the Circle class as you need it (here you need to display the radius of the Circle object) As a parameter, you give it a Circle object.

In the class, I added the line :

friend std::ostream& operator<< (std::ostream &out, const Circle &matrix);

And outside the class its definition like this:

std::ostream& operator<< (std::ostream& out, const Circle& matrix)
{
    out << "Circle radius " << matrix.radius<<std::endl;
}

Then in the main function, your loop shall be modified to only give a Circle object like this:

for (int index = 0; index < size; index++)
{
      //cout << arr[index].Circle();  //Error: Invalid use of Circle::Circle
      cout << arr[index]; // no error as you defined operator <<for your Circle class
}

Here is you complete code modified

#include <iostream>

using namespace std;

class Circle
{
    private:
        double radius;
    public:
        Circle() //Constructor
        {   radius = 0; }

        Circle(double); //Parameterized Constructor

        void setRadius(double);

        double getRadius() const
        { return radius; }

        double getArea() const
        { return 3.14 * radius * radius; }

        friend std::ostream& operator<< (std::ostream &out, const Circle &matrix);

};

std::ostream& operator<< (std::ostream& out, const Circle& matrix)
{
    out << "Circle radius " << matrix.radius<<std::endl;
}

Circle::Circle(double r)
{
    radius = r;
}

void Circle::setRadius(double r)
{
    radius = r;
}

int main()
{
    int size = 3;  //Set size of array of objects
    Circle arr[size] = { Circle(1.2), Circle(2.2), Circle(5.5) }; //Array of Objects

    for (int index = 0; index < size; index++)
    {
      //cout << arr[index].Circle();  //Error: Invalid use of Circle::Circle
      cout << arr[index]; // no error as you defined operator <<for your Circle class    
   }

    Circle b(2.8);                  //Instance of class
    cout << b.getRadius();          //Prints out the radius of object

    Circle c;                       //Another instance
    cout << endl << c.getRadius(); //Another radius

    double r;                     //Variable to store the radius based on user input
    cout << "\nEnter a radius: ";
    cin >> r;
    c.setRadius(r);             //Pass user input into radius member function
    cout << c.getRadius();      //Print new radius of object c
    cout << endl << c.getArea(); //Prints area of object c.


    return 0;

}

The output is:

Circle radius 1.2
Circle radius 2.2
Circle radius 5.5
2.8
0
Enter a radius: 5
5
78.5
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.