The exercise is as follows:
Given the height and base of a rectangle, find its area and perimeter.
I started doing it the way they explained to me in class and this is my main code (I have the rectangle.h where I declare the class and rectangle.cpp where I define the methods).
main.cpp
This way yes it compiles me
#include "rectangulo.h"
#include <iostream>
using namespace std;
void IOParametros(Rectangulo& rect);
void IOArea(Rectangulo rect);
void IOPerimetro(Rectangulo rect);
int main()
{
Rectangulo rectangulo;
IOParametros(rectangulo);
IOArea(rectangulo);
IOPerimetro(rectangulo);
cin.get();
}
void IOParametros(Rectangulo& rect)
{
int x, y;
cout << "Introduce la altura y la base del rectángulo" << endl;
cin >> x >> y;
rect.setX(x);
rect.setY(y);
}
void IOArea(Rectangulo rect)
{
int x, y, ar;
x = rect.getX();
y = rect.getY();
ar = x * y;
cout << "El área del rectángulo es " << ar << endl;
}
void IOPerimetro(Rectangulo rect)
{
int x, y, per;
x = rect.getX();
y = rect.getY();
per = (2*x)+(2*y);
cout << "El perímetro del rectángulo es " << per << endl;
}
But if I structure it like this to make it a little clearer:
main.cpp
This way I don't compile
#include "rectangulo.h"
#include <iostream>
using namespace std;
int main()
{
Rectangulo rectangulo;
IOParametros(rectangulo);
IOArea(rectangulo);
IOPerimetro(rectangulo);
cin.get();
}
io_rectangle.cpp
#include "rectangulo.h"
#include <iostream>
using namespace std;
void IOParametros(Rectangulo& rect);
void IOArea(Rectangulo rect);
void IOPerimetro(Rectangulo rect);
void IOParametros(Rectangulo& rect)
{
int x, y;
cout << "Introduce la altura y la base del rectángulo" << endl;
cin >> x >> y;
rect.setX(x);
rect.setY(y);
}
void IOArea(Rectangulo rect)
{
int x, y, ar;
x = rect.getX();
y = rect.getY();
ar = x * y;
cout << "El área del rectángulo es " << ar << endl;
}
void IOPerimetro(Rectangulo rect)
{
int x, y, per;
x = rect.getX();
y = rect.getY();
per = (2*x)+(2*y);
cout << "El perímetro del rectángulo es " << per << endl;
}
How do I get it to compile by having the main.cpp without the I/O functions, and an IO_rectangle.cpp with the I/O function definitions?
PS: rectangle.h to add more information:
#pragma once
class Rectangulo
{
private:
int _x, _y;
public:
Rectangulo();
int getX();
void setX(int x);
int getY();
void setY(int y);
int perimetro(int x, int y);
int area(int x, int y);
};
The problem is simple, and the compiler itself is telling you clearly. Let me translate it in case you have difficulties with English:
In the version that doesn't compile for you, the Translation Unit
main
is using three functions calledIOParametros
,IOArea
, andIOPerimetro
of which the compiler is not aware. That's why when adding them the program compiles.This is a trick question, and the answer is just as tricky: You will never be able to compile it the way you describe.
Header include statements (
#include
) are like copy-pasting the included file into the file the include statement is in. You could create a pair of files containingIOParametros
,IOArea
, andIOPerimetro
:IOFunciones.hpp
IOFunciones.cpp
Then you include the corresponding header in
main
: