I am having trouble understanding classes, I'm new at this and have not had any problems with c++ until now. This is what I have so far. I was supplied the driver and cannot make any changes to it. Any help would be appreciated greatly. Thanks.

#include <iostream>

using namespace std;

const pi = 3.1415926;//global constant Pi

/*
   A cylindrical can. 
*/


class SodaCan
{
private:
int height, radius;
public:
    void get_volume (double);
    void get_surface_area (double);

};


void SodaCan::get_volume (int height, int radius)//can volume = piR^2H
{
volume=pi* ?????
}

void SodaCan::get_surface_area (int height, int radius);//can surface area = 2piRH+2piR^2
{
surface=2*pi  ??????
}


// a driver:
int main()
{
   SodaCan can(10, 5); //height is 10, and radius is 5
   cout << can.get_volume() << "\n";
   cout << can.get_surface_area() << "\n";
   return 0;
}

Dani AI

Generated

Two problems caused the weird outputs: the use of ^ (bitwise XOR) instead of squaring, and an untyped/truncated Pi constant. With radius = 5, 5 ^ 2 evaluates to 7 (binary 101 XOR 010 = 111), and if Pi is effectively treated as 3 the volume becomes 3 * 7 * 10 = 210 — which exactly matches the numbers shown. That explains the results.

Fixes and best practices:

  • Give Pi a proper floating type. Prefer constexpr double PI = 3.141592653589793; (or const double pi = 3.1415926; on older compilers).
  • Square integers with radius * radius (or std::pow(radius, 2) if you need the <cmath> semantics), not ^.
  • Initialize members with a constructor initialization list (SodaCan(int h, int r) : height(h), radius(r) {}) and make accessor methods return double and be const (e.g., double volume() const;). Don’t keep surface/volume as public stored values unless you need caching — compute them on demand.

Output and types:

  • If exact decimals matter, use <iomanip> and std::fixed / std::setprecision(3) when printing.
  • Consider making height and radius double if fractional sizes are possible.

Quick checklist (to match and ):

  • Add a constructor (prefer init-list).
  • Make Pi a double/constexpr.
  • Replace ^ with multiplication or pow.
  • Have getters return double and mark them const.

Applying those changes will produce the expected 785.398... and 471.239... outputs.

Recommended Answers

All 7 Replies

Your SodaCan class needs a constructor to assign something to the private height and radius data members. eg, the line

SodaCan can(10,5);

requires a constructor which looks like

SodaCan::SodaCan(int, int)

Much in the same way that, for an library type, such as std::string you can say

std::string s("Hello");

which will initialise s with the value "Hello" - Of course, the constructor for std::string is well hidden inside the standard library, but there's a constructor somewhere, which may have a signature like

string::string(const char*)

Check out this link for more info :)
C++ FAQ - Constructors

Member Avatar for Member #46692

I like to think of them as getter and setter methods...

Take a look at this example I did...

#include <iostream>
#include <string>
    
class Student
{
  public:
    void set(int,std::string);
    void get_student_number();
    void get_student_name();
  
  private:
    int student_number;
    std::string student_name;
            
};
    //setter methods
     void Student::set(int a, std::string b)
    {
         student_number = a;
         student_name = b;
    }
    //getter methods
    void Student::get_student_number()
    {
        std::cout << student_number;
    }  
    void Student::get_student_name()
    {
        std::cout << student_name;
    }     
  
    
int main()
{
    Student kid;
    kid.set(1232,"thwee");
    kid.get_student_number();
    std::cout << "\n";
    kid.get_student_name();
    
    std::cin.get();
}

[IMG][/IMG]
Piworld ™
[Tis simple as Pie]

I think I've gotten a little bit further, but still have yet to grasp classes, this is what I've got now.

#include <iostream>

using namespace std;

const pi = 3.1415926;//global constant Pi

/*
   A cylindrical can. 
*/


class SodaCan
{
private:
int height, radius;
public:
	 double surface;
	 double volume;
         void get_volume ();
	 void get_surface_area ();
	 SodaCan (int, int);

};

SodaCan::SodaCan (int a, int b)
{
height = a;
radius = b;
}

void SodaCan::get_volume ()//can volume = piR^2H
{
volume=((pi*(radius^2))*height);
}

void SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2
{
surface=((2*pi*radius*height)+(2*pi*(radius^2)));
}


// the driver:
int main()
{
   SodaCan can(10, 5); //height is 10, and radius is 5
   cout << can.get_volume() << "\n";
   cout << can.get_surface_area() << "\n";
   return 0;
}

I think I've gotten a little bit further, but still have yet to grasp classes, this is what I've got now.

What exactly are you having trouble understanding? Your class looks fine for the most part

void SodaCan::get_volume ()//can volume = piR^2H
{
volume=((pi*(radius^2))*height);
}

void SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2
{
surface=((2*pi*radius*height)+(2*pi*(radius^2)));
}


// the driver:
int main()
{
   SodaCan can(10, 5); //height is 10, and radius is 5
   cout << can.get_volume() << "\n";
   cout << can.get_surface_area() << "\n";
   return 0;
}

Your problem here is that get_surface_area() and get_volume() functions both have return type void - so passing them to cout won't do much :) Remember that those 2 functions run a calculation and assign them to a variable, they don't output anything.

If you edit those functions so that they return the result of the calculations instead (a double), then you won't need the two values inside your SodaCan class surface and volume.

I got it to work only that I'm not getting the right values, and I can't figure out why. The program should display 785.398 & 471.239, instead I get 210 & 342. Thanks for all the help.

#include <iostream>

using namespace std;

const pi = 3.1415926;//global constant Pi

/*
   A cylindrical can. 
*/


class SodaCan
{
private:
int height, radius;
public:
	 double surface;
	 double volume;
  	 SodaCan (int, int);
	 double get_volume();
	 double get_surface_area();

};

SodaCan::SodaCan (int a, int b)
{
height = a;
radius = b;
}

double SodaCan::get_volume ()//can volume = piR^2H
{
volume=(pi*(radius^2)*height);
return volume;
}

double SodaCan::get_surface_area ()//can surface area = 2piRH+2piR^2
{
surface=((2*pi*radius*height)+(2*pi*(radius^2)));
return surface;
}


// the driver:
int main()
{
   SodaCan can(10, 5); //height is 10, and radius is 5
   cout << can.get_volume() << "\n";
   cout << can.get_surface_area() << "\n";
   return 0;
}
Member Avatar for Member #46692

The problem is with your power function.

You're using '^' which is the xor operator, I think.

Try swapping radius^2 with radius*radius

and const pi = 3.1415926;//global constant Pi

with double pi = 3.1415926;//global constant Pi

[IMG][/IMG]
Piworld ™
[Tis simple as Pie]

Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.