1st classes project

Hi,

I'm learning classes, and making a simple calculator to compute area. The program should print the dims of a rectangle's sides, and then print the area.

I don't understand why the area isn't printing.

I'm using MS Visual Studio, and the IDE is new to me.

The problem appears to be in 2 places:

1. main.cpp file, with the line containing

t.area();


2. math.cpp source file,

int Math_function::area(int length, int width)



main.cpp
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
/***** LIBRARIES AND HEADERS *****/
#include <iostream>

//using namespace std;
using std::cout;
using std::cin;
using std::endl;

//#include <iomanip>


#include "math.h"  // class definition for math formulas

int main()
{
    Math_function t;  // creates an object "t" of class 'time'
         
    // print dimensions of area to compute
    cout << "Print dimensions: " << endl;
    t.print_area();
    
    // computer area
    cout << "Area = ";
    t.area();
    
    cin.get(); // keep terminal open until you press a key


    return 0;
}


header 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
#ifndef MATH_H  
#define MATH_H


/*
Create a class which contains the math function(s) to use
The class memember function(s) are placed in 'public'
The class member variables are defined in private.

*/


class Math_function
{
public:

	Math_function();   // constructor
	void area(int, int);  // area formula, set length and width
	void print_area();


private:

	int length;
	int width;

};  // end class Math_function



#endif /* MATH_H */ 


source 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
/*
Class member function definitions for class Math_functions

*/

#include <iostream>

//using namespace std;
using std::cout;
using std::endl;

#include <iomanip>

#include "math.h"  // include definition of class Math_functions from math.h

// Math_function constructor initializes class Math_function's private data members
Math_function::Math_function()
{
	length = 2;
	width = 6;
}  // end of Math_function constructor 


int Math_function::area(int length, int width)
{
	length * width;  // compute area
}


void Math_function::print_area()
{
	cout << "length = " << length << endl;  // print length
	cout << "width = " << width << endl;   // print width
}




please do not name your file math.h --- this is identical to a C header file <cmath> in c++ but <math.h> in C. This isnt likely an issue due to the double quotes, but it is confusing to the reader who is not paying full attention.

I think you missed a print.
print area only pinrts length and width, but not area. It is either badly misnamed, or not complete :)

also lines 23/24 of main look like they want to print area, but do not.
try this instead:
cout << "Area = " << t.area();

IF you fixed print_area to print the area, the 23/24 main line is possibly redundant.

This is nice work. Watch comments going stale as you code: the main line 16 comment is nonsense -- a copy/paste miss or something was edited, but its no good now.
Last edited on
Hello project science,

I agree with jonnin. If you wnat to use "math.h" give the "math" part something more to distinguish it from the C header file "<math.h>".

When I compiled the program with VS 2017 I received several errors the first of which was hard to catch.

In the header file you have:
void area(int, int)

In the cpp file you have:
int Math_function::area(int length, int width)

See the problem?

Also in the ".cpp" file the function promises to return a value, but does not. This will produce an error. Also the line length * width; has no effect and does nothing.

The last part is you are sending the function "length" and "width". These become local variables to the function and you should be using the private variables of the class. Which means there should be no parameters.

Once the errors were fixed the program ran. Then I noticed the problems in "main".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    Math_function t;  // creates an object "t" of class 'time'

    // print dimensions of area to compute
    cout << "Print dimensions: " << endl;

    t.print_area();

    // computer area
    cout << "Area = " << t.area();

    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

For line 11 combining your 2 original line into 1 line the "cout" will pring the return value of the function.

The output I get is:

Print dimensions:
length = 2
width = 6
Area = 12

 Press Enter to continue:



That should help.

Andy
Hi jonnin and Andy. Thanks for your input. (I may change the math.h part to formulas.h or something). Using this, I changed my code slightly and I did get it to work!

Andy, I had errors when I tried to run line 11 in your previous code. For some reason, it gave this error:


no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std:chair_traits<char>> << void


I would have gotten this same result in the first print statement of printing dimensions. I'm using VS 2019.

While I did get the code to work, I want to make sure I understand the purpose of the constructor. I believe the class' constructor allows the class public member functions to use the private variables. Thus, you don't include parameters for the pubic function call as they'd become local variables.

Each class only has 1 constructor, and it takes the name of the class name.

Here's the code I got working:

main.cpp
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
int main()
{
    Math_function t;  // creates an object "t" of class 'time'
         
    // print dimensions
      cout << "Dimensions: " << endl;
      t.dimensions();

    
    /*
     DOES NOT WORK - computer and print area
      cout << "Area = " << t.area();
    */

    //WORKS - computer area
      cout << "\nArea = ";
      t.area();
    

      cout << "\n\n Press 'Enter' twice to close window...";
      cin.get(); // keep terminal open until you press a key


      return 0;
}


header
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
#ifndef MATH_H  
#define MATH_H


/*
Create a class which contains the math function(s) to use
The class memember function(s) are placed in 'public'
The class member variables are defined in private.

*/


class Math_function
{
public:

	Math_function();   // constructor
	void dimensions();
	void area();  // area formula, set length and width
	


private:

	int length;
	int width;

};  // end class Math_function



#endif /* MATH_H */ 



source
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
/*
Class member function definitions for class Math_functions

*/

#include <iostream>

//using namespace std;
using std::cout;
using std::endl;

#include <iomanip>

#include "math.h"  // include definition of class Math_functions from math.h

// Math_function constructor initializes class Math_function's private data members
Math_function::Math_function()
{
	length = 2;
	width = 6;
}  // end of Math_function constructor 


void Math_function::dimensions()
{
	cout << "length = " << length << endl;  // print length
	cout << "width = " << width << endl;   // print width
	//cout << "area = " << length * width << endl;
}

//Doesn't work --> void Math_function::area(int length, int width)
void Math_function::area()
{
	//cout << "area = " << length * width << endl;  // compute area
	cout << length * width;
}
Last edited on
all class methods can access its private members and provide the interface to prevent the user from accessing the member variables directly (you can do direct access, but it is frowned upon).

the purpose of the constructor is to initialize the object. If it has parameters, those are there to be used for initialization, similar to int x = 3 conceptually (set the new thing to a user provided value) (the mechanics of int are different, that isnt the point right now).

so you can do this:
Math_function::Math_function(int L, int W)
{
length = L; width = W;
}
...
Math_function var(11,23);

or you can have a 'setter' that changes the variables:
Math_function::SetWidth(int W) {width = W;}

Last edited on
/*
DOES NOT WORK - computer and print area
cout << "Area = " << t.area();
*/


 
void area();  // area formula, set length and width 


area() is defined as not returning a value - so t.area() displays the area and doesn't return the value and so can't be used with cout. If you want to use t.area() in a cout statement, then have area() return the value and not display it.

Hello project science,

Sorry for the delay. I had some computer problems yesterday and lost track of where I was at.

To start with and what I said eariler:

In the header file you have:
void area(int, int)

In the cpp file you have:
int Math_function::area(int length, int width)


So what I did was change the forward declaration in the header file to match the ".cpp" file and when it used cout << "Area = " << t.area(); it worked because the function call returned a value that "cout" could use.

The error comes from the forward declaration saying that the function is a "void" function, so when the compiler rune it is checking the forward declaration of the class against the function call in the "cout" statement and is thinking that the function returns nothing.

In the error message the last word in the line, "void", is the key to the error.


I believe the class' constructor allows the class public member functions to use the private variables.


Not entirely true as jonnin pointed out. Another way to look at this is that only public member functions of the class can access the private or protected member variables.

The ctor, default or overloaded, have no control over other public member functions. Only the private variables.


Thus, you don't include parameters for the pubic function call as they'd become local variables.


Yes and no. It depends on what you are doing. As jonnin said
1
2
3
4
Math_function::Math_function(int L, int W)
{
    length = L; width = W;
}

Called an overloaded ctor the variables in the () have different names than the private member variables of the class. The "L" and "W" become local variables to the function, but they are used to set the private member variables. And when you reach the closing } these local variables are destroyed.


Each class only has 1 constructor, and it takes the name of the class name.


If not defined each class has 1 default ctor provided by the compiler along with 1 default dtor. Actually a class can have 1 or more overloaded ctors as long as the variables in the () are different. One thing to keep in mind. When you provide an overloaded ctor or dtor the compiler will no longer provide a default. You will have to do the default.

Your revised code works, but the main file is missing the beginning, so I can not see what you have done. So I am guessing that both ".cpp" files should the header file for the class. With out this the code will not compile.

In your function:
1
2
3
4
5
void Math_function::area()
{
	//cout << "area = " << length * width << endl;  // compute area
	cout << length * width;
}

Consider:
1
2
3
4
int Math_function::area()
{
	return length * width;
}

This way you can use the function in a "cout" statement any time you need it. By putting that "cout" in the function it may not print to the screen as you would want without extra work.

I had to add a couple of lines in the ".cpp" files to get the program to work in VS 2017, but it did.

Andy
Topic archived. No new replies allowed.