I am very new to object oriented programming in c++ and I am having some difficulty with syntax when access specifiers are used inline with variables, classes, or functions. The code below is not mine. It is from this post. Judging by the popularity of the thread I'm sure others got it to compile. However I am having difficulty.
public class CartEntry
{
public float Price;
public int Quantity;
}
public class CartContents
{
public CartEntry[] items;
}
public class Order
{
private CartContents cart;
private float salesTax;
public Order(CartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}
public float OrderTotal()
{
float cartTotal = 0;
for (int i = 0; i < cart.items.Length; i++)
{
cartTotal += cart.items[i].Price * cart.items[i].Quantity;
}
cartTotal += cartTotal*salesTax;
return cartTotal;
}
}
I tried a simple class to identify the problem
This compiles:
#include <iostream>
class C{
int i;
};
int main(){}
This does not:
#include <iostream>
class C{
public int i;
};
int main(){}
nor does this:
#include <iostream>
public class C{
int i;
};
int main(){}
This does:
#include <iostream>
class C{
public: int i;
};
int main(){}
This does not:
#include <iostream>
public: class C{
int i;
};
int main(){}
I am using the GNU c++ compiler V6.3.1 without any build flags. If this syntax is incorrect, why do I see it demonstrated so often. If it is correct, am I missing a compiler option, header file, or something else?