C++ Area of a Rectangle

1 and 2: Write a function RectArea that takes in lengths (double) of a rectangle and returns the area (double) + overload RectArea to take ints in place of doubles

3. ) Write a driver (main) that uses the file from question 1 & 2 (prototype with extern). The program should
take in the values from command line (./main one two) and prints the area in the driver program (File=main1.cpp).

Hello, I am trying to teach myself C++ and I came across these problems on the Internet. I was able to 1 and 2, but I am not quite sure about how to do 3.

My guess is that you have to make a new program, and in this new program, you put #include <iostream> and #include rectArea.cpp (That's the file of question 1 and 2) and using namespace std;

Am I right?????

Also, the problem said something about extern. What should I do with extern prototypes????

If this is the case, what do you do after using namespace std;?



Here's what I have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
using namespace std;

double rectArea(double, double);
int rectArea(int, int);

int main ()
{
	double length;
	double width;
	
	cout << "What is the length of the rectangle? " << endl;
	cin >> length;

	cout << "What is the width of the retangle? " << endl;
	cin >> width;

	rectArea(length, width);

	return 0;
}

double rectArea(double l, double w)
{
	double area;
	area = l * w; 
	return area;
}

int rectArea(int l, int w)
{
	int area;
	area = l * w;
	return area;
}
Last edited on
I assume you write a file that contains just the rectArea functions, and then another file that contains just a main function. However, in the other file, you declare the other functions, using function prototypes (like in lines 4-5 of your program). I don't know what it means by extern, extern is only used for global variables...
So I need to write a file that has the double rectArea and int rectArea function. Then, in the other file, you would call these two functions by #include rectArea.cpp and have function prototypes + main function. Am I right?

Also, how can I print the area? I thought by returning the area, it would automatically return the value
No, do not include rectArea.cpp. You just have your compiler both files, and the linker will do the work. So, for example (with GCC), you would compile like this:

g++ -Wall -Wextra -pedantic-errors -c -o rectArea.o rectArea.cpp
g++ -Wall -Wextra -pedantic-errors -c -o main1.o main1.cpp
g++ -o main main1.o rectArea.o
Topic archived. No new replies allowed.