Need a helping hand...

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Need a helping hand...

      Hello, me and my partner recently started learning C++. We have been learning from the following book : C++ Programming in easy steps by Mike McGrath : we are having a few problems along the way with understanding some of the code, mostly due to it not being explained properly. We also have Game Coding complete 2nd edition, but most of it looks very confusing right now.

      Can we get some advise or help?

      Right now we are trying to figure out derived classes, heres where we are lost, earlier in the book it told us that in a class you need to use setter and getter methods, yet in the current program its telling us to make it doesnt use a setter for the cats name (i hope this makes sense...), we have a mammal base class with age and weight variables and they both use setters and getters, but with the cat class there is not setter for the name variable, we have tried playing around with the code and it doesnt accept a setter for name in cat.
      Can someone please explain why it does not work?
      If need be i can copy and paste the code that the book is telling us to write, and point out where we are puzzled.

      EDIT: If someone knows a better book to learn C++ from then please say so.

      The post was edited 2 times, last by Drucygoddess ().

    • RE: Need a helping hand...

      (classes.cpp)

      #include "cat.h"

      int main()
      {
      Cat MyCat("Tiddles");

      MyCat.setAge(3);
      MyCat.setWeight(2);
      MyCat.purr();
      cout << "My cat\'s name is " << MyCat.getName();
      cout << endl;
      cout << MyCat.getName() << " is ";
      cout << MyCat.getAge() << " years old\n";
      cout << MyCat.getName() << " weighs ";
      cout << MyCat.getWeight() << " lbs\n";
      MyCat.speak();
      }

      (mammal.h)

      #include <iostream>
      using namespace std;

      class Mammal
      {
      public:
      Mammal() {};
      ~Mammal() {};

      void setAge(int yrs) { age = yrs; }
      void setWeight(int lbs) { weight = lbs; }
      int getAge() const { return age; }
      int getWeight () const { return weight; }

      void speak() const { cout << "MAMMAL SOUND!\n"; }

      protected:
      int age;
      int weight;
      };

      (cat.h)

      #include "mammal.h"
      #include <string>

      class Cat : public Mammal
      {
      public:
      Cat(string initName)
      {
      name = initName;
      };
      ~Cat() {};

      //used to having void setName here
      string getName() const { return name; }

      void purr() const { cout << "PRRR!\n"; }

      protected:
      string name;
      };

      The post was edited 2 times, last by Drucygoddess ().

    • Well then add

      void SetName(string newName)
      {
      name = newName;
      }

      to your Cat class - if it makes you feel comfortable. Basically you need to provide a name for the new cat in the class constructor and it's unlikely you'll change the cat's name, so the author didn't feel a SetName() method was necessary (I assume).

      Setter/getter methods are a way of protecting the integrity of your data. Technically you don't have to use them - but you risk all sorts of havoc if you don't. People make mistakes and using getter/setters helps catch some of them.

      Object Oriented Programming in C++ is a good book that starts off with the basics and from there begins to lay the groundwork for fairly solid class design concepts. Check it out.
      "Your job is not to die for your country. Your job is to make some other poor sod die for his."
    • Originally posted by Nebuchadnezzar
      Well then add

      void SetName(string newName)
      {
      name = newName;
      }

      to your Cat class - if it makes you feel comfortable. Basically you need to provide a name for the new cat in the class constructor and it's unlikely you'll change the cat's name, so the author didn't feel a SetName() method was necessary (I assume).

      Setter/getter methods are a way of protecting the integrity of your data. Technically you don't have to use them - but you risk all sorts of havoc if you don't. People make mistakes and using getter/setters helps catch some of them.

      Object Oriented Programming in C++ is a good book that starts off with the basics and from there begins to lay the groundwork for fairly solid class design concepts. Check it out.


      Thank you for clearing that up, done a search for the book you said who was the author? found 2 with that title, one by Robert Lafore, and one by N. Josuttis.
    • I don't really have a good suggestion for intro books. My first programming book was The Absolute Beginner's Guide to C, which wouldn't really be appropriate here. However, here are a few recomendations for continued reading once you have the basics of C++ down.

      I found Object Oriented Programming, by Peter Coad to be very helpful in learning how to program using object oriented concepts. Also, Effective C++, More Effective C++, and Effective STL are all must-haves. All three of those are by Scott Meyers.

      The best reference books I have are Jamsa's C/C++ Programmer's Bible (by Kris A. Jamsa) and C++ Primer (looks like we're up to 5th edition, this one by Stephen Prata). I wouldn't recommend reading these books straight through; they're much better as references in my opinion.

      On a side note, Rat Race uses almost no getter/setter functions. Almost all the data is public. It drives me nuts. ;)

      -Rez