I’m needing to turn a code into c++ for c (I know, backward), I’ve already transformed the prototypes part of the functions, which would be the .h. file. c he calls this class separated with ::
What you have to do is what you have to do, neither backward nor progressive :)
Just the mission of the moment
//codigo em c++ :
class A{
public:
A();\\ELE MEIO QUE INICIALIA ALGO AQUI (??)
void inserir(int);
void imprime();
void remover(int);
bool buscaValor(int);
private:
NO *raiz;
NO *busca(int);
void insere(NO**, int);
};
This is a simple class, it doesn’t involve inheritance, complicated destructors or difficult-to-port features, except for the private/public thing that will go away. And polymorphism in case inserir()
.
In C++ these functions are called methods and are associated with each class, and each class variable is called an instance.
Nomenclature aside, what matters to understand is that when you write
A um_A;
um_A.insere(123);
The builder of A
, that function A()
is called, with the parameters you passed, and a new A
is returned, with variables and functions. In your case the constructor is minimal, has not even parameters, and is unique, which helps well at the time of porting
And then the function inserir()
is called, with that int
as parameter. Only implicitly this call of inserir()
is associated with the data and methods of that particular instance of A
at the moment, um_A
And how it looks in C?
You can only replicate that behavior. Of course it could, as suggested in the comments, use pointers to functions within the struct, and it would be important if you used virtual classes and inheritance in A
, only that it does not use and I think it would be a waste of time and too many asterisks: all struct will use the same inserir()
and the same A()
. If it’s not clear write back.
So write something like
A* init();
and pass the parameters accordingly. And how is "accordingly"? Read your program
A::A(){
raiz = new NO;
for(int i=0;i<4;i++){raiz->ponteiros[i] = 0;}
raiz->quantDados = 0;
raiz->pai = 0;
}
Simple then:
A* init();
And it could be something like
A* init()
{
raiz = (NO*) malloc(sizeof(NO));
for(int i=0;i<4;i++){raiz->ponteiros[i] = 0;}
raiz->quantDados = 0;
raiz->pai = NULL;
return raiz;
}
And I could write in C
A* um_A = init();
And inserir()
?
In C++ inserir()
already has access to an instance of A
. Not in C, so in C pass a:
int inserir(int valor, A* estrutura);
But there’s another inserir()
. This is called polymorphism. The compiler knows which to call because the parameters are different. In C just write two functions, like inserir_int()
and inserir_no()
and will be ok.
Post the whole program
Help others help you: post something you can compile. With include and data structures. It’s really boring having to keep trying to guess what’s on the show
This structure A::A is the constructor of the class in C++. Since in C there is no native class concept, there are also no constructors. By making an extra comment on your code, to make your code closer to a conversion you could add function pointers in your struct to store the reference to the functions.
– user215609
@user215609 can turn this class A into a struct A and then insert pointers typeNo? the root and the search -- example: struct A{ NO root Noquest;
– Laura Aguiar Martinho