0

The assignment involves using many class files to output volume and areas of figures.

My program works perfectly how it is now, but my instructor wants the class files to use both a .h and .cpp file for each class. At the moment I am only using a .h file for each class.

Would appreciate help on how to split it into two files correctly. Let me know if there are any questions!

Below is one of the class .h files that needs to become a .h and a .cpp file.

#ifndef CYLINDER_H
#define CYLINDER_H

class Cylinder {
    double height, radius;
    double PI=3.1415926535898;

public:
   void set_values (double,double);
   double volume () {return (radius*radius)*(height)*PI;}
   double area() {return ((2.0000*PI)*(radius)*(radius))+((2.0000*PI)*(radius*height));}

 };

void Cylinder::set_values (double x, double y) {
   height = x;
   radius = y;
 }

  #endif    /* CYLINDER_H */
  • Show code samples in your question!! – πάντα ῥεῖ Dec 10 '13 at 22:05
  • Please edit your question to include the code. Posting a link to an image of text is not really the done thing! – enhzflep Dec 10 '13 at 22:06
  • sorry about that! fixed it – user2493923 Dec 10 '13 at 22:18
  • 1
    looks like you already have it split up in your header, just start with moving the Cylinder::set_values() function to the .cpp file and include the header, then you should be fine. you should try it out yourself. when that compiles move the other two functions into the .cpp and leave the prototypes in the same form as set_values() – AndersK Dec 10 '13 at 22:22

1 Answers1

3

divide the code as follows:

in the Cylinder.h

#ifndef CYLINDER_H
#define CYLINDER_H

class Cylinder {
    double height, radius;
    double PI=3.1415926535898;

public:
   void set_values(double,double);
   double volume();
   double area();    
 };
 #endif    /* CYLINDER_H */

in the Cylinder.cpp

#include "Cylinder.h"
void Cylinder::set_values(double x, double y) {
   height = x;
   radius = y;
 }

double Cylinder::volume() {
    return (radius*radius)*(height)*PI;
}
double Cylinder::area() {
    return ((2.0000*PI)*(radius)*(radius))+((2.0000*PI)*(radius*height));
}

The reason this is good practice is that a function can be declared as many times as you like but only defined once. void set_values(double,double); declares the function (notice no curly braces) whereas

void Cylinder::set_values(double x, double y) { 
   height = x;
   radius = y;
}

defines it. By deviding the declarations and the definitions into two different files you are able to include the cylender.h in many other files so that they can use the class without having to worry about it being defined in two places.

It is actually technically more complicated than that because function that are defined inside the body (between the curly braces) of the class are allowed to be defined more than once as long as all the definitions are exactly the same. This is called the One Definition Rule or ODR, it is actually more complicated then it seems at first because macros and the preprocessor can actually change the definition depending on what has been previously defined. That is why it is good advice to divide things into .h and .cpp files in this way until you fully understand the ODR and the preprocessor.

This method also has the advantage that the order in which things are written is not as important. If you declare everything first and then define it you are much less likely to have to shuffle the order around so that everything is known by the compiler before it is used.

I would also suggest reading about operator precetance (for example here: http://en.wikipedia.org/wiki/Operators_in_C_and_C++) since you are making many unneeded parenthesis I think you may benefit from it. For example in volume all the operators are * which is evaluated left to right, which is perfectly fine so radius*radius*height*PI is fine. In area it is a little more complicated, however because operator * has a higher precedence than + 2.0000*PI*radius*radius+2.0000*PI*radius*height should produce the same result.

odinthenerd
  • 4,964
  • 1
  • 22
  • 53