Help for calling from main to a class

How can i call the value of setLength and setWidth from the main() to the class Rectangle from another .cpp file?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  //MyRectangleClassProgram.cpp
  // This program uses the programmer-defined Rectangle class. 
#include "Rectangle.cpp"
#include <iostream>
using namespace std; 
int main()
{
   
   Rectangle rectangle1;
   Rectangle rectangle2;

   rectangle1.setLength(10.0);
   rectangle1.setWidth(5.0);
   rectangle2.setLength(7.0);
   rectangle2.setWidth(3.0);

   
   cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;
   cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;
   cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;
   cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;
   
   return 0;
}


// Rectangle.cpp
#include <iostream>
using namespace std;
class Rectangle
{
   public: 
      // Declare public methods here
    void setLength(double len)
	{
	   length = len;

	   return;	
	}
	
	void setWidth(double wid)
	{
	   // write setWidth here
	   width = wid;
	   
	   return;
	}
	
	double getLength()
	{
	   // write getLength here
	   return length;
	}
	
	double getWidth()
	{
	   // write getWidth here
	   return width;
	}	
	
	
	double calculatePerimeter()
	{
	   // write calculatePerimeter here
           //how to call the value here???///
	}
	double calculateArea()
	{
	   // write calculate Area here
           //how to call the value here???///
	}
	
   private:	
      // Create length and width here
      double length, width, perimeter, area;
}; 
Hello OlaveraLuffy,

At first I thought you are including a ".cpp" file in a ".cpp" file until I say the class file and realized you have the wrong extension. For the class file it should ".h" or ".hpp" not ".cpp".

Sometimes the order of include files does not make any difference, but in this case it does. "iostream" should be followed by "rectangle.h" or ".hpp", so that the include files of "main" will cover anything that is needed in the header file.

For the header file do not include files like "iostream". These are better used in the ".cpp" files. And never use using namespace std; in a header file. Read this: http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

For the functions:
1
2
3
4
5
6
7
8
9
10
double calculatePerimeter()
{
	// write calculatePerimeter here
	//how to call the value here???///
}
double calculateArea()
{
	// write calculate Area here
	//how to call the value here???///
}

Just write your calculation and return the answer. Being member functions of the class they have access to the private variables of the class, so just use the variables. There is no special way of call anything to access them.

Once you do the math for "perimeter" and "area" these variables will have a value and you could write a "get" function to retrieve their value, but it is not necessary for this program.

Hope that helps,

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.