Hi I have to learn c++ for uni and so here I am. I had been programming in c before so its not as hard but I'm trying to make sure I do things the right way for c++ rather than for c. This is my first attempt and I wanted to check that I haven't done anything in a c way or badly.
By the way I had a seperate function to add pizza's because I'm planning on having a linked list of them. Is having a function seperate to the class the right way of doing this? Also should I put the next and prev pointers in the class? I'm asking because I don't want to start bad habits.
Thanks, David
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
const double PIE = 3.14;
// Define the cPizza class
class cPizza
{
/* add_pizza(int diameter, double cost);*/
double radius, area;
double cost;
public:
double value(void);
cPizza(int diameter, float temp_cost);
};
// The contructor for cPizza
cPizza::cPizza(int diameter, float temp_cost)
{
radius = diameter / 2;
cost = temp_cost;
}
// Calculates the value for the pizza
double cPizza::value()
{
float cost_per_inch;
area = (radius * radius) * PIE;
cost_per_inch = area / cost;
return cost_per_inch;
}
cPizza* add_pizza(int diameter, double cost);
int main(int argc, char *argv[])
{
int diameter;
float radius, area, value;
double cost;
cPizza *pizza;
cout << "Pizza calculator\n\n";
cout << "Size of pizza (Diameter in inches.): ";
cin >> diameter;
cout << "Cost of pizza (Pounds): ";
cin >> cost;
/*cPizza Pizza(diameter, cost);*/
pizza = add_pizza(diameter, cost);
cout << "The pizza costs £" << pizza->value() <<" per square inch\n";
delete(pizza);
system("PAUSE");
return 0;
}
cPizza* add_pizza(int diameter, double cost)
{
cPizza *new_pizza;
new_pizza = new cPizza(diameter, cost);
return new_pizza;
}