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;
}

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 iamthwee

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]http://img476.imageshack.us/img476/5171/cut20ln.png[/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 iamthwee

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]http://img476.imageshack.us/img476/5171/cut20ln.png[/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.