0

I am writing a programme that deals with classes and objects and the like. I must create a Rectangle class and do the functions within that class, one of which includes returning a string with all the information about my Rectangle (display_info()). The problem is, when I try to use display_info() in my source, nothing comes on the screen. It is blank. What am I doing wrong with this function? I will post all my code so that you can see if there is some error somewhere else. Thank you.

Header:

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iomanip>
#include <string>
#include <iostream>
using namespace std; 

class Rectangle
{
public:
    Rectangle();
    Rectangle(double l, double w);
    void set_length(double l);
    void set_width(double w); 
    double get_perimeter();
    double get_area();
    string display_info(); 

private:
    double length;
    double width; 
};

#endif

Rectangle.cpp:

#include "Rectangle.h"

Rectangle::Rectangle()
{
    length = 0;
    width = 0;
}

Rectangle::Rectangle(double l, double w)
{
    length = l;
    width = w;
}
void Rectangle::set_length(double l)
{
    length = l;
    return; 
} 
void Rectangle::set_width(double w)
{
    width = w;
    return;
}
double Rectangle::get_perimeter()
{
    double perimeter = 2 * (length * width);
    return perimeter; 
}
double Rectangle::get_area()
{
    double area = length * width;
    return area; 
}
string Rectangle::display_info()
{
    double perimeter = Rectangle::get_perimeter();
    double area = Rectangle::get_area();
    string s = "The length is " + to_string(length) + "\nThe width is " + to_string(width) + "\nThe perimeter is " + to_string(perimeter)
    + "\nThe area is " + to_string(area);  
    return s; 
}

Source:

#include "Rectangle.h"

int main()
{
    Rectangle r1;
    r1.set_length(5);
    r1.set_width(4);
    cout << "r1's info:" << endl;
    r1.display_info();

    cout << endl << endl; 

    Rectangle r2(10, 5);
    cout << "r2's info:" << endl;
    r2.display_info();

system("pause");
return 0; 
}
Joshua
  • 1
  • 2
  • You've done nothing to output the string returned by *display_info()*. Returning a string does not automatically print it. You apparently are familiar with `cout`, because you use it on the line immediately above the call to `r2.display_info()`. – Ken White Apr 09 '16 at 01:01
  • `using namespace std;` at global scope in a header, can easily yield unintended name collisions. One workaround is to define your own namespace. – Cheers and hth. - Alf Apr 09 '16 at 01:17

2 Answers2

3

You meant to write:

std::cout << r1.display_info();

Also, putting "using namespace std;" in a header file is asking for trouble. Don't do it.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

Your method display_info returns a string containing the information. It does not print the information itself.

As the method is named "display info" , I assume you want it to display the actual info, so I would suggest changing it to return void. And replace "return s;" with "std::cout << s;"

Kake_Fisk
  • 1,107
  • 11
  • 22