2 . cpp files and 1 header file

am making this rectangle program...and my teacher would like us to make the project have 2 .cpp files and header file...i have the program sorta running with only .cpp file and the .h file...could someone help me make the other .cpp file?

Heres the .h 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
77
78
79
80
81
82
83
//Thomas Diaque
//C++ Assignment 1

#ifndef CLASS_H
#define CLASS_H

using namespace std;

class rectangle
{
private:
  double length; //attributes of length and width
  double width;
  
public: 
	rectangle();
	 rectangle(double length);
	rectangle(double length, double width);

	~rectangle();
//	rectangle::~rectangle()
//	{	
//		cout << "Out of scope" << endl;
//	}
 
  double calcPerimeter()
  {
    double Peri;
    Peri = length * 2 + width * 2;
    return Peri;
  };
  double calcArea()
  {
    double Area;
    Area = length * width;
    return Area;
  };
  void setLength(double newLength)
  {
    if (length < 0.0 || length >= 20.0)
      cout << "The length does not fall between the correct range (0.0 and 20.0)" << endl;
	else
		length = newLength;
  };
  void setWidth(double newWidth)
  {
    if (width < 0.0 || width >= 20.0)  
      cout << "The width does not fall between the correct range (0.0 and 20.0)" << endl;
	else
		width = newWidth;
   
  };
    
	double getLength()
    {
		return length;
    };
    
	double getWidth()
    {
		return width;
    };


	bool isSquare()
	{
		if ((length - width) < .0001 )
		{
			return true;
		}
		else
		{
			return false;
		}
	}


 

	
};
#endif

Heres the .cpp file 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//Thomas Diaque
//C++ Assignment 1

using namespace std;
#include <iostream>
#include "class.h"



//constructor
rectangle::rectangle()
{
  length = 1.0;
  width = 1.0;
}
rectangle::rectangle(double length)
{
  setLength(length);
  width = 1.0;
}
rectangle::rectangle(double length, double width)
{
  setWidth(width);
  setLength(length);
}


rectangle::~rectangle() //destructor
	{	
		cout << "Out of scope" << endl;
	}

int main()
{
	rectangle R1;
		R1.setLength(1);
		R1.setWidth(1);

//	rectangle R2(7.1,3.2);
	rectangle R2;
		R2.setLength(7.1);
		R2.setWidth(3.2);
	
//	rectangle R3(6.3);
	rectangle R3;
		R3.setLength(6.3);
		R3.setWidth(1);
	
//	rectangle R4(-30,50);
	rectangle R4;
		R4.setLength(-30); 
		R4.setWidth(50);

//	rectangle R5(R2.getLength(),R2.getWidth());
	rectangle R5;
		R5.setWidth(R2.getWidth());
		R5.setLength(R2.getLength());

	cout << "length: " << R1.getLength() << ", width: " << R1.getWidth() << ", perimeter: " << R1.calcPerimeter() << ", area: " << R1.calcArea() << " is square? ";
	if (R1.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R2.getLength() << ", width: " << R2.getWidth() << ", perimeter: " << R2.calcPerimeter() << ", area: " << R2.calcArea() << " is square? ";
	if (R2.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R3.getLength() << ", width: " << R3.getWidth() << ", perimeter: " << R3.calcPerimeter() << ", area: " << R3.calcArea() << " is square? ";
	if (R3.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R4.getLength() << ", width: " << R4.getWidth() << ", perimeter: " << R4.calcPerimeter() << ", area: " << R4.calcArea() << " is square? ";
	if (R4.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R5.getLength() << ", width: " << R5.getWidth() << ", perimeter: " << R5.calcPerimeter() << ", area: " << R5.calcArea() << " is square? ";
	if (R5.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	
		
		cout <<"*************************************"<< endl;

	//rectangle R1(5.4,10.5);
	R1.setLength(5.4); 
	R1.setWidth(10.5);
	
	//rectangle R4(15.6);
	R4.setLength(15.6);
	R4.setWidth(15.6);



	cout << "length: " << R1.getLength() << ", width: " << R1.getWidth() << ", perimeter: " << R1.calcPerimeter() << ", area: " << R1.calcArea() << " is square? ";
	if (R1.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R2.getLength() << ", width: " << R2.getWidth() << ", perimeter: " << R2.calcPerimeter() << ", area: " << R2.calcArea() << " is square? ";
	if (R2.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R3.getLength() << ", width: " << R3.getWidth() << ", perimeter: " << R3.calcPerimeter() << ", area: " << R3.calcArea() << " is square? ";
	if (R3.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R4.getLength() << ", width: " << R4.getWidth() << ", perimeter: " << R4.calcPerimeter() << ", area: " << R4.calcArea() << " is square? ";
	if (R4.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;

	cout << "length: " << R5.getLength() << ", width: " << R5.getWidth() << ", perimeter: " << R5.calcPerimeter() << ", area: " << R5.calcArea() << " is square? ";
	if (R5.isSquare() ) cout << "yes." << endl;
		else cout << "no." << endl;



	
		
	
	



//	R1.calcPerimeter();
//	R1.calcArea();
//	R2.calcPerimeter();
//	R2.calcArea();
//	R3.calcPerimeter();
//	R3.calcArea();
//
//
//cout <<"the area is "<< R1.calcArea() <<"!" << endl;
//cout<< "the perimeter is "<< R1.calcPerimeter() <<"!" << endl;
//
//cout <<"the area is "<< R2.calcArea() <<"!" << endl;
//cout<< "the perimeter is "<< R2.calcPerimeter() <<"!" << endl;
//
//cout <<"the area is "<< R3.calcArea() <<"!" << endl;
//cout<< "the perimeter is "<< R3.calcPerimeter() <<"!" << endl;
//
cin.clear();   
cin.ignore(255, '\n');   
cin.get();  
return 0;
}


also if you see any errors in the code, or any suggestions to make it bette, feel free to help out! thanks...
A suggestion, above every rectangle instantiation you have a comment that is something like this.

//rectangle R1(5.4,10.5);

You already have a constructor defined to handle this so why not use it.

Also rectangle r5. I would use a copy constructor instead of the two setheight and width statements.

rectangle r5(r2);

The default copy constructor should work for this.

Having the isSquare function return a string yes or no would eliminate the use of the if statments

Last edited on
Generally the way to seperate/organize your files is to have a class occupy one cpp/h file pair... in which you don't have anything else. This is probably what your teacher wants you to do.

To fullfill this part of your assignment, keep your header the way it is, but move all of the rectangle member functions from your cpp file to a seperate cpp file (typically named the same as your header -- so something like 'rectangle.h' and 'rectangle.cpp'). And leave main() in the other cpp file.
Topic archived. No new replies allowed.