Hello, i've been learning java for a few months now, but i woulld also like to try c++. I tried rewriting one of my exercises in c++, but i cant seem to get objects working.
These lines all prodduce errors.
Cylinder outer(5,5);
Cylinder inner(4,4);
cout << "\n" << outer.volume() << inner.volume() << "\n";
main.cpp:19: undefined reference to `Cylinder::Cylinder(double, double)'
main.cpp:20: undefined reference to `Cylinder::Cylinder(double, double)'
main.cpp:21: undefined reference to `Cylinder::volume()'
main.cpp:21: undefined reference to `Cylinder::volume()'
Main.cpp
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include "Cylinder.h"
using namespace std;
int main(int argc, char** argv) {
Cylinder outer(5,5);
Cylinder inner(4,4);
cout << "\n" << outer.volume() << inner.volume() << "\n";
return (EXIT_SUCCESS);
}
Cylinder.cpp
#include <iostream>
#include <math.h>
using namespace std;
class Cylinder{
private:
double radius;
double height;
public:
Cylinder()
{
radius = 0;
height = 0;
}
Cylinder(double aradius, double aheight)
{
radius = aradius;
height = aheight;
}
void set_radius(double aradius)
{radius = aradius;}
double get_radius()
{return radius;}
void set_height(double aheight)
{height = aheight;}
double get_height()
{return height;}
void set_both(double aradius, double aheight)
{
radius = aradius;
height = aheight;
}
void print()
{cout << "The radius is " << radius << "\nThe height is "
<< height << ".\n";}
double volume()
{
const double PI = 3.141592;
return (PI*pow(radius,2)*height);
}
double surface()
{
const double PI = 3.141592;
return ((2*PI*radius*height)+(2*PI*pow(radius,2)));
}
//true if the cylinder in this object is greater.
//1 greater, 0 equal, -1 smaller
short compare(double volume2)
{
if(this->volume()>volume2)
return 1;
if(this->volume()==volume2)
return 0;
else
return -1;
}
Cylinder duplicate()
{
return *this;
}
virtual ~Cylinder() {}
};
Cylinder.h
#ifndef CYLINDER_H
#define CYLINDER_H
class Cylinder {
private:
double radius;
double height;
public:
Cylinder();
Cylinder(double aradius, double aheight);
void set_radius(double aradius);
double get_radius();
void set_height(double aheight);
double get_height();
void set_both(double aradius, double aheight);
void print();
double volume();
double surface();
short compare(double volume2);
Cylinder duplicate();
virtual ~Cylinder(){};
};
#endif /* _CYLINDER_H */