Struct or Classes?

2

I made the code using structure, wanted to know if classes would be more efficient. I'm doing it in C ++ Builder. The idea of the code is as follows: create a list of problems, where during the execution of the code will be added and removed numerous problems (type struct or class )

struct {
 TList *cidadesvisitadas; // lista iniciada com uma cidade ,vou acrescentando ate 30 cidades
 float  distancia;
 TList *cidadescanditadas; // lista iniciada com 29 cidades , que no decorrer  do codigo vai sendo retirada uma a uma e adicionada na lista de cidades visitadas
};

In the list of problems, an initial (unresolved) problem will be added, and then removed this problem turns into three new (unresolved) problems that are added back to the list of problems, then I take a problem back from the list of problems and turns into three other problems and so on. The list at the end of the run is empty (adds and removes, when the problem is resolved it does not return to the list). Problems already stuck dynamically (they will be millions of problems) if I change the struct (Problem) to class (Problem), is the code more correct?

    
asked by anonymous 30.07.2016 / 03:14

2 answers

4

In C ++ structures and classes are virtually the same (classes have private members by default), then it makes no difference, no, not performance.

How you're going to build and use the code makes a difference. It is common for people to leave struct for simple structures, without methods, that is, POD (Plain Old Data), and classes when they are more complete objects. There are those who prefer to use struct when the type will be most used as value and class for objects that will be used with the reference. But it does not always work so black in white.

Of course, classes can not be used with C. Depending on how you use struct you can not either.

    
30.07.2016 / 03:39
-3

First, struct is not the same as a class as many have responded, although both are extremely similar, both in terms of syntax (written in the code itself) and in the result displayed.

  • Class: A class is a structure that attributes and methods . To access these characteristics, you need to create an object. You can access the information of a class after the creation of the object through the nomenclature objeto->atributo; or objeto->funcao();

Note: One class can communicate with another just as one object can communicate with another.

  • Struct: A struct is a structure , just like a class, but it only has attributes . There is no way to create methods. In addition, to access the characteristics of a structure an access mechanism is used, which is completely different from an object. To access the information of a struct you can use the nomenclature acesso.atributo;

Note: A struct can not communicate directly with another struct just as an access mechanism can not communicate with another.

In short, a class is an extremely elaborate structure, with interactions and direct communications with the functionality of your program. A struct is only an aesthetic structure, which serves to couple similar data in only one place, although it is also functional because it can create multiple access mechanisms and therefore access various attributes.

Your question is not the difference between the two, but through this information you can deduce when developing your project, which is the best option. From what I could understand, class would be a better option, since you could work out the solution of the problems displayed through methods induced in the class itself.

    
02.08.2016 / 21:50