Runtime Error

So I have to write a .cpp file and header file that will compile and pass all of the tests of a test program.

The problem is that the program compiles and passes part of the tests, but not all of the tests.

Here are the instructions:

Create a class called square.
Square have one private data member called side_ (what type should side be?).

Square should have two constructors. The default constructor should initialize side_ to 1.

Square should have an accessor function getSide and a mutator function setSide.

Square should have a member function to compute and return the area. This function should be called Area.

Square should have a member function to compute and return the perimiter. This function should be called Perim.

Your class should be seperated into a class and header file. Your program should compile with my testing program


Here is part of the test program:
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
void testSquare(Tester & t)
{
	square s1;
	square s2(4);
    
    //Testing constructors
    //
    t.test(s1.getSide()==1, "Testing default constructor");
    t.test(s2.getSide()==4, "Testing single-parameter constructor");
    
    //testing member functions
    t.test(s1.getSide()==1, "Testing getSize function");
    s2.setSide(5);
    t.test(s2.getSide()==5, "Testing setSize function");
    t.test(s1.Perim()==4, "Testing Perim function 1x1 square");
    t.test(s2.Perim()==20, "Testing Perim function 5x5 square");
    t.test(s1.Area()==1, "Testing Area function 1x1 square");
    t.test(s2.Area()==25, "Testing Area function 5x5 square");
    
}

int main()
{
    Tester t;     //our tester
    
    testSquare(t);
    
    cout << endl;
    if (t.allPassed())
    {
        cout << "All tests successful"
        << endl;
    }
    else
    {
        cout << "Tests ********** UNSUCCESSFUL **********"
        << endl;
    }
    cout << endl;
    
    cout << "Press ENTER to quit ";
    while (cin.get() != '\n') ;
    return 0;
}


Here is the header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef square_Header_h
#define square_Header_h

class square
{
    double side_;
public:
    square(double x)
    {
        side_ =1;
    }
    square()
    {
    }
    double getSide();
    void setSide(double x);
    double Area();
    double Perim();
};


#endif 


Here is the generic .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
#include <iostream>
#include "square.h"

using namespace std;



void square::setSide(double x)
{
    side_= x;
}
double square::getSide()
{
    side_=1;
    return side_;
}
double square::Area()
{
    return side_*side_;
}
double square::Perim()
{
    return 4*side_;
}


When I run the program, it spits out this in the console area:

" Test: Testing default constructor - passed
Test: Testing single-parameter constructor - ********** FAILED **********
Test: Testing getSize function - passed
Test: Testing setSize function - ********** FAILED **********
Test: Testing Perim function 1x1 square - passed
Test: Testing Perim function 5x5 square - ********** FAILED **********
Test: Testing Area function 1x1 square - passed
Test: Testing Area function 5x5 square - ********** FAILED **********

Tests ********** UNSUCCESSFUL **********

Press ENTER to quit"


So my question is.. how come the program is working for only part of the tests, and not all of them?
Last edited on
how come the program is working for only part of the tests, and not all of them?
possibly because some of your code is not correct, I guess.

In the header file, constructor code, what happens to the parameter x, and what value is assigned to side_ ?
1
2
3
4
    square(double x)
    {
        side_ =1;
    }


Below, what value is assigned to side_ ?
1
2
3
    square()
    {
    }
Last edited on
Well what I want to happen is that side_ will automatically default to 1 when not assigned anything, thus enabling the program to pass the tests. However, I do not believe that it is actually defaulting to side_ = 1.

So I assigned side_ = 1, but I know I did it incorrectly so to say.
The code is quite clear, when a value is supplied, it is ignored, and instead the value of 1 is substituted - that's an error. Also, when no value is supplied and the default value of 1 is supposed to be used, nothing is done at all, side_ remains uninitialised. That's another error.

I didn't study the rest of the code in detail, there may be further errors. It's just a case of matching up the written specification which you were given, with the code which you are writing, and make sure they agree. In the two cases I mentioned, the code doesn't match the specification.

Alrighty! I figured it out thanks!

So what I did is in the header file, I made side_ = x; and then added in side_=1 in the square().

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
 
#ifndef square_Header_h
#define square_Header_h

class square
{
    double side_;
    
public:
    square(double x)
    {
        side_ = x;
    }
    square()
    {
        side_ = 1;
    }
    double getSide();
    void setSide(double x);
    double Area();
    double Perim();
};


#endif


Everything compiled and it said all tests passed. :) Thank you!
Topic archived. No new replies allowed.