I am a new to in programming, especially to C++.

At first I learned how I make functions (it was amazing).

But now that I've learned about classes (and objects) I am so confused... when I should use them? What makes them more efficient than functions?

Most example problems I try to solve using functions, not classes.

Is that good or bad?

Is there a good an example problem that shouldn't be solved using functions and should be solved using classes? Something that will make clear the benefits of using classes?


3 Answers 3


Learn what a class is, and you will understand where and when to use them.

In general: you should create a class when you have multiple "objects" of one kind.
E.g. you could store the hours, minutes and seconds of multiple times using 3 arrays, or you can store them in an array of DateTime, where everything is encapsulated that belongs together.
The objects of a class will be used in regular functions in the same way AS other variables.


Classes are most useful when they are used to guard invariants. Invariants are conditions on one or more variables that must always be met by the program. E.g. calling size() on a std::vector will always yield the number of elements stored in the vector, regardless of the operations we performed it. If, instead, you'd store a pointer to the first and last element of a dynamic array, you will have to inspect all the code using these variables to be sure that the size is calculated correctly.


The question suggests that both classes and functions can't both be used together, this is not true. I'll cover the most basic level of functions here since the suggested duplicate question covers other benefits/uses of classes over static functions.

At the most basic level classes simply store a state so that it can be passed to and from functions. Note that examples are in C# but that should not affect the answer or understanding at all.

public IEnumerable<OrderDetail> GetOrderDetailsForDisplay(string sapCustomerID, DateTime deliveryDate, string orderType)

public IEnumerable<OrderDetail> GetOrderDetailsForDisplay(Order order)

The first function above does not use a class in its parameter list, giving us 4 parameters. The second function above just passes an order object since the parameters sapCustomerID, deliveryDate, and orderType describe an order.

Likewise, image if the function(s) above did not return a collection of OrderDetail objects. The OrderDetail class has many properties associated to it (see below).

public class OrderDetail
    public long ID { get; set; }
    public long OrderID { get; set; }
    public int ProductID { get; set; }
    public short Quantity { get; set; }
    public bool IsClosed { get; set; }
    public DateTime DeliveryDate { get; set; }
    public DateTime BranchDeliveryDate { get; set; }
    public DateTime TransferToSAPOn { get; set; }

    public string CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }

    public int? InventoryQuantity { get; set; }
    public bool IsProductAvailable { get; set; }

So you could see how having to return this many fields/values could be tedious or difficult.

For even more information on this topic I suggest reviewing the links provided in the comments by gnat and CandiedOrange. I hope this helps answer your question at least a little bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.