-4

Read the comments too.

Basically, I am trying to figure out object constructors, destructor, and classes. I created a class with a few public member variables, and a few private member variables. At this point I am only utilizing the public members within my code.

My question is, to put it quite simply, how do I utilize the constructor, destructor, and print off the objects info to the console.

Thanks.

#include <iostream>

// Class -> NPC
// Contains generic stats for an NPC in a game
class NPC
{
public:
  char name;
  int age;
  char favoriteItem;
private:
  char quest;
  char nemesis;
  int karma;
}

// Object Constructor
NPC::NPC (char newName, int newAge, char newFavoriteItem)
{
  name = newName;
  age = newAge;
  favoriteItem = newFavoriteItem;
}

// Object Deconstructor
NPC::~NPC()
{
  // Do nothing
}

// Here I would like to create a new NPC, bob, with a name of "Bob", age of 28, and his favorite items being a Sword
// Next, I attempt to use this information as output.
int main()
{
NPC bob("Bob",28, "Sword");
std::cout << bob << std::endl;
}
karafar
  • 496
  • 4
  • 14
  • Read up on `operator<<` overloading. _Edit:_ and member initializer lists. – Torbjörn Aug 22 '16 at 06:18
  • Off topic: `char newName` will be exactly one character. That's a pretty short name in most cultures. Recommend looking up `std::string`. – user4581301 Aug 22 '16 at 06:23
  • You don't need to (should prefer not to) write an empty user-defined destructor. The compiler will generate that for you and it'll be "trivial" which yours won't. If you'd like to be explicit, then declare it as `~NPC() = default;` see also http://en.cppreference.com/w/cpp/language/destructor . Additionally, you should prefer the initialization list over the constructor body. – Jesper Juhl Aug 22 '16 at 06:23
  • Also read up on the [Rules of Three, Five, and Zero](http://en.cppreference.com/w/cpp/language/rule_of_three). You'll find the Rule of Zero makes the same point as @JesperJuhl along with a few others useful guideliens, and you can't do effective C++ programming without knowing and observing the other three and five. – user4581301 Aug 22 '16 at 06:26
  • Call back to the point about `char newName`: In `NPC bob("Bob",28, "Sword");` `"Bob"` is a `const char *`, a pointer to an unmodifiable array of characters AKA a string, not a `char` and can't be used here. But not a `std::string`, although it can be converted to one without any effort on your part. Highly recommend [doing some reading](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and working through some exercises before continuing. – user4581301 Aug 22 '16 at 06:30

1 Answers1

0

Fixed the char (only one character) to std::string. I added initialization list and std::ostream &operator << operator.

http://en.cppreference.com/w/cpp/language/default_constructor

#include <iostream>
#include <memory>
#include <string.h>

class NPC
{
    public:
        std::string name;
        int age;
        std::string favoriteItem;

        NPC(std::string const& name, int age, std::string favoriteItem)
            : name(name), age(age), favoriteItem(favoriteItem)
        {};

    private:
        char quest;
        char nemesis;
        int karma;

};

std::ostream &operator << (std::ostream &os, NPC const& npc)
{ 
    os << npc.name <<  " " << npc.age << " " << npc.favoriteItem << "\n";
    return os;
};
int main()
{
    NPC npc("Bob", 28, "Sword");

    std::cout << npc;

    return 0;
}
Roby
  • 2,011
  • 4
  • 28
  • 55