Q:

C++ Class output incorrect

    #include <iostream>

using namespace std;

class Pyramid{

    public:
        Pyramid();
        void set_surfaceArea(double, double, double);
        void set_volume(double, double);
        double get_surfaceArea();
        double get_volume();
        void print_sufraceArea();
        void print_volume();

    private:
        double height,
                     sideLength,
                     perimeter,
                     baseArea,
                     surfaceArea,
                     volume;
};

Pyramid::Pyramid(){
    height = 0;
    baseArea = 0;
    perimeter = 0;
    sideLength = 0;
}

void Pyramid::set_surfaceArea(double baseArea, double sideLength, double perimeter){
    baseArea = baseArea;
    sideLength = sideLength;
    perimeter = perimeter;
}

void Pyramid::set_volume(double baseArea, double height){
    baseArea = baseArea;
    height = height;
}

double Pyramid::get_surfaceArea(){
    return (1/2) * baseArea * perimeter * sideLength;  

}

double Pyramid::get_volume(){
    x = (1/3) * baseArea * height;
    return x;
}

int main(){
    double height,
                 test,
                 sideLength,
                 perimeter,
                 baseArea;
  cout << "Enter height: ";
  cin >> height;
  cout << "Enter side length: ";
  cin >> sideLength;
  cout << "Enter perimeter: ";
  cin >> perimeter;
  cout <<"Enter base area: ";
  cin >> baseArea;

  Pyramid x;

    x.set_surfaceArea(baseArea, sideLength, perimeter);
    x.set_volume(baseArea, height);

    test = x.get_volume();
    cout << "test: " << test << endl;
}

When I run this code (and enter numbers for input), I get the output of "test: 0". This code is my first try at making a class from scratch. I'm sure someone can easily point out why my output is giving me '0' regardless of what I put in.
Thanks!

A:

The problem here is actually very clear. Your code is suffering from having 2 variables with the exact same name, where the scope is important in order to know which variable is actually used.
An example: You have defined 'height' as a class member, though also as a method parameter for set_volume. So if you write height = height; you actually are doing a self-assignment. For this specific example, my compiler (clang) is warning for this with the error message explicitly assigning value of variable of type 'double' to itself (I expect other compilers to generate something similar)
To fix this problem, you'll have to rename one of the variables. I would suggest renaming the class members to either _height, m_height or mHeight in order to clearly indicate that this is a member. (Examples are based upon existing style guides)
So, as soon as you've fixed this problem, the members will get a different value from 0, however the result is still the same as you have written: (1/3)
This code will always evaluate to 0, as you are doing an integer division. This is defined as dividing an integer by another integer, which results in the floored value of the result. So, in this case 0.333 gets floored to 0. Replacing this code by (1./3.) fixes this issue.

