Results 1 to 11 of 11

Thread: Beginning Classes

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    47

    Beginning Classes

    I started learning c++ a few weeks ago, and now I got to the chapter with Basic Classes. I kind of understood the whole chapter, but when I tried to make my own program using classes, it didn't work. So I tried to copy one from the book. It doesn't work also. Can someone tell me whats wrong with this program?
    Ps. I just started learning c++, so please don't use those advanced methods. Thank You.

    Here's the program:
    #include <iostream.h>
    #include <stdlib.h>

    class cat
    {
    public: // begins public section
    int getAge();
    void setAge(int age);
    void meow();
    private: // begins private section
    int itsAge();
    };
    // GetAge, public accesor function
    // returns value of itsAge member
    int cat::getAge()
    {
    return itsAge;
    }
    // definition of setAge, public
    // accesor dunction
    // returns sets itsAge member
    void cat::setAge(int age)
    {
    // set member variable its age to
    // value passed in by parameter age
    itsAge = age;
    }
    //definition of Meow method
    // returns : void
    // parameters : none
    // action : Prints "meow" to screen
    void cat::meow()
    {
    cout << "Meow.\n";
    }
    // Create a cat, set its age, have it
    // meow, tell us its age, then move again.

    int main()
    {
    cat Frisky;
    Frisky.setAge(5);
    Frisky.meow();
    cout << "Frisky is a cat who is " ;
    cout << Frisky.getAge() << " years old.\n";
    Frisky.meow();
    system("PAUSE");
    return 0;
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    int itsAge();
    Change this to:
    Code:
    int itsAge;
    It's not a function, it's only a variable. Also, you should prefix member variables in some way for readability, although Bruce Eckel (author of the wonderful Thinking in C++, available online) disagrees. In this case though, he can go and boil his head
    I have the convention of m_iAge for things like this (m_ for member, i for int).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oh BTW, use [code][/code] tags to preserve the indentation which you *did* use, didn't you
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    evileconomist
    Guest
    You should separate the code into a .cpp file and a .h file.

    Also add a basic constructor cat().

    When you write this in main, "cat Frisky;" you're using a constructor which you havn't included. So C++ uses a default
    constructor.

    The header file cat.h

    class cat
    {
    public:
    cat(); // Simple constructor
    cat(int age); // Another constructor

    int getAge();
    void setAge(int age);
    void meow();
    private:
    int itsAge;
    };


    The implementation file cat.cpp

    #include "cat.h"

    // The constroctor
    cat::cat()
    { // The constructor lets you set initial values.
    itsAge = 0;
    }

    //Another constructor
    cat::cat(int age)
    {
    itsAge = age; // In main() you can write "Cat Felix(5);"
    }

    int cat::getAge()
    {
    return itsAge;
    }
    void cat::setAge(int age)
    {
    itsAge = age;
    }

    void cat::meow()
    {
    cout << "Meow.\n";
    }


    And finally your main() function in its own file main.cpp

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    cat Frisky;
    cat Felix(4);
    Frisky.setAge(5);
    Frisky.meow();
    Felix.meow();
    cout << "Frisky is a cat who is " ;
    cout << Frisky.getAge() << " years old.\n";
    cout << "Felix is a cat who is " ;
    cout << Felix.getAge() << " years old.\n";
    system("PAUSE");
    return 0;
    }


    //Joachim
    PS: If you're using a non microsoft compiler such as GNU you should change the .cpp extension to .cc instead

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can still use .cpp for gcc.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Zaei
    Guest
    I hate separating my files into .h and .cpp. If i do, i cant add the cpp implementation files to my project, and its a PAIN to go an open one if i need it. So, i just put the implementation right into the header. No problems.

    Z.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Why can't you add the .cpp files to your project? It's bad form to include code in a header file (immoral, not illegal).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Zaei
    Guest
    Because then MS tries to compile them, and then tells me that "Blah Blah Blah function already has a body...", or some kind of redefinition error.

    So, im immoral, whats yer point =)?

    Z.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I have never had that problem

    If you use header guarding, and put all the implementation into the .cpp file then there shouldn't be anything wrong.

    And yep, you're immoral j/k
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I have a question, how is this:

    Code:
    class Cat
    {
    public:
      int getAge() {return age;}
    private:
      int age;
    };
    different from this:
    Code:
    class Cat
    {
    public:
      int getAge();
    private:
      int age;
    };
    int Cat::getAge()
    {
      return age;
    }
    ?
    Alcohol & calculus don't mix.
    Never drink & derive.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The first one has everything inlined into the definition (this is different from compiler inlining). The object code generated in both cases will be identical. The second way just means you can split into a .h/.cpp pair to prevent needless recompilation of unchanged code.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width