View Single Post
Old Feb 17th, 2006, 9:08 PM   #2
teencoder
Hobbyist Programmer
 
teencoder's Avatar
 
Join Date: Jul 2005
Posts: 158
Rep Power: 0 teencoder is an unknown quantity at this point
Okay I will cover methods and classes in one stone. Classes are an OOP object. This object can be any thing and can contain more objects. I won't use a duck but a dog.
class mydog
{
   private:
   mydog()
   {
   }
   ~mydog()
   {
   }
   bool barks=true;
   bool bites=true;
   bool loveypuppy=true;
   public:
   bool does_bark();
   bool does_bite();
   bool is_a_lovey_puppy();
};

definitions

mypuppy::does_bark()
{
    return barks;
}
mypuppy::does_bite()
{
    return bites;
}
mypuppy::is_a_lovey_puppy()
{
    return loveypuppy;
}
There I wrote a class and the method definitions. I declared the class. I put in a blank constructor and deconstructor so you could see they exist you can put in specific details of initialization and destruction there. There are function prototypes and definitions even though they could be inlined so you could see a good practice. I'm strongly assume you know about functions and data types now I'll so how to create instances of your object and use them.
main
#include <iostream>
using namespace std;
int main()
{
    mypuppy puppy;  //creating a mypuppy instance called puppy
    if (puppy.does_bite())
        cout << "Oww";
    if (puppy.does_bark())
        cout << "Shut up";
    if (puppy.is_a_lovey_puppy)
        cout << "I love my puppy.";
    system("pause");
}
So you use . to access a method or public variable in a class. Hope this helps.
P.S. Buy a good book I recommend C++ Programming For the Absolute Beginner.
__________________
Geeks may not be cool now but in the long run they prosper.
teencoder is offline   Reply With Quote