Object vector in C++

Asked

Viewed 2,327 times

0

How do I vector objects using c++? And how do I sort this array of objects, for example, using some sort algorithm (quicksort, mergesort, shellsort or radixsort)?

#include <iostream>   
using namespace std;

class Game {
public:
float preco;
char nome[25];
int idade;
char categoria[30];

};

int main (){
Game *jogo = new Game();
int i;

//for (i=0; i < 2; i++){
cout <<"Digite o preço do jogo: \n";
cin >> jogo->preco;
cout <<"Digite a nome do jogo: \n";
cin >> jogo->nome;
cout <<"Digite a idade indicativa para joga-lo : \n";
cin >> jogo->idade;
cout <<"Qual a categoria do jogo: \n";
cin >> jogo->categoria;
//}

cout << jogo->preco <<"\n";
cout << jogo->nome << "\n";
cout << jogo->idade << "\n";
cout << jogo->categoria << "\n";


return 0;   

}
  • Related: https://answall.com/a/210524/64969; has a whole interesting theoretical basis, despite being about chained list

  • I’m not a fan of this type of code because it is half C and Mio C++, I don’t even know what I offer since I don’t know if you want to program in C or C++. Looks like it’s C++, so that code should be rewritten.

  • @Maniero would be interanssante if you had indicated to him what he should change in the code and not simply mandalo rewrite it...

2 answers

1

1. Use the library C++ offers you.

Your code looks like C, not C++. You can use the STL to make your code simpler and clear.

2. "using namespace Std" is a bad practice.

When you write using namespace std, you are importing all the std for the global namespace and this can cause conflicts with another library.

3. You do not need to use pointers to create variables

Using pointers is unnecessary in your case, because if you don’t dislocate it using delete, you may have a Memory Leak.

4. You are making a mess of tongues

Write your code in English to make it more elegant and less confusing. And if you don’t know English, don’t be too lazy to learn.


The code should be rewritten like this:

#include <string>
#include <iostream>   

struct Game {
    float price;
    std::string name;
    int min_age;
    std::string category;
};

int main (){
    Game game;

    std::cout << "Digite o preço do jogo: " << std::endl;
    std::cin >> game.price;
    std::cout << "Digite a nome do jogo: " << std::endl;
    std::cin >> game.name;
    std::cout << "Digite a idade indicativa para joga-lo: " << std::endl;
    std::cin >> game.min_age;
    std::cout << "Qual a categoria do jogo: " << std::endl;
    std::cin >> game.category;

    std::cout << game.price << std::endl;
    std::cout << game.name << std::endl;
    std::cout << game.min_age << std::endl;
    std::cout << game.category << std::endl;

    std::cin.get();

    return 0;   

}

Even though it’s not what you want, you can do whatever you want from these good practices.

0

For each position in the Array you must create a new object and insert it in its proper position. And to sort, choose one of the class attributes as name or age to serve as sort index.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.