-5

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?

candied_orange
  • 102,279
  • 24
  • 197
  • 315
  • Possible duplicate of [Significant amount of the time, I can't think of a reason to have an object instead of a static class. Do objects have more benefits than I think?](https://softwareengineering.stackexchange.com/questions/242940/significant-amount-of-the-time-i-cant-think-of-a-reason-to-have-an-object-inst) – gnat Apr 23 '17 at 20:32
  • 1
    my question is about function instead of class NOT object instead of class – Hegazy Tarek Apr 23 '17 at 21:14
  • Well objects are instances of classes. Classes can be an organizational tool for grouping non-instance functions, but usually, they are a way of hiding data; you modify the data using the class members, instead of directly. In this way you can create levels of abstraction, ignoring the details in the lower levels as you work on the higher levels. – Frank Hileman Apr 23 '17 at 22:24
  • I've heavily edited this question to make it's meaning clear. [Discussed on meta](https://softwareengineering.meta.stackexchange.com/q/8519/131624) – candied_orange Apr 23 '17 at 23:40
  • Cross site duplicate: [Why use classes instead of functions?](http://stackoverflow.com/questions/6480676/why-use-classes-instead-of-functions) – candied_orange Apr 23 '17 at 23:47
  • what is this heavily edited @CandiedOrange – Hegazy Tarek Apr 24 '17 at 05:16
  • Possible duplicate of [c++ coding practice class vs. "free" functions](https://softwareengineering.stackexchange.com/questions/256241/c-coding-practice-class-vs-free-functions) – Doc Brown May 15 '17 at 13:57

3 Answers3

2

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.

1

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.

D Drmmr
  • 290
  • 2
  • 6
0

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.

Shelby115
  • 465
  • 3
  • 16