learn to write a class

This is a discussion on learn to write a class within the C++ Programming forums, part of the General Programming Boards category; can anyone suggest a website/s that teaches how to write a class?...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    Question learn to write a class

    can anyone suggest a website/s that teaches how to write a class?

  2. #2
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    I can help. I'm only good at defining, though, not usage.

    A class is an object very much like struct. However, it is much more useful. Using class, you can make an int function, however, thats pointless since int is already defined.

    First, think of what type you want. We'll use Cat.

    Code:
    class Cat { \\define class Cat
    private:      \\Every thing here is it 'private' area. Meaning no one          
                      \\allowed but it.
    char name[50];
    int height;
    int weight;
    int lengthwt; \\legnth with tail;
    int lengthwnt; \\length with no tail :D
    public:   \\The world can see all this anytime, its what the cat 
                 \\and if it wants to eat, it does it, meow, same, etc.
    Cat();    \\Give birth to the cat
    ~Cat();  \\Kill the cat
    void eat();
    void poop();
    void meow();
    };  \\note the ';'
    There, class Cat is made. But before implementing it, you need to build it.

    Code:
    Cat::Cat()
    {
      char name[50];
     int height;
     int weight;
     int lengthwt; \\legnth with tail;
     int lengthwnt; \\length with no tail :D
    }
    
    Cat::~Cat()
    {
    
    }
    
    void Cat::eat()
    {
    statement;
    }
    
    void Cat::poop()
    {
    statement, like weight -= 100;
    }
    
    void Cat::meow()
    {
    statement;
    }
    Thats the class, built!

    To use them, you do something like

    Cat Cat1 = ("Joe", 5, 9, 3);

    But i can't get it to work.

    Hope i helped!
    This war, like the next war, is a war to end war.

  3. #3
    nag
    nag is offline
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    you can give definitions of class member functions in class itself like
    Code:
    class Cat
    {
    private:
        char name[50];
        int height;
        int weight;
        int lengthwt; \\legnth with tail;
        int lengthwnt; \\length with no tail
    public:
        Cat()      //default constructor
        {
            strcpy(name,"mycat");
            height=30;
            weight=90;
        }
        void eat()      //member function
        {
            cout<<"cat is eating "<<endl;
        }
    };
    Constructor Cat() function with same name as that of class is called when you create an object of type Cat in your code,
    and function ~Cat()
    is called when scope of that object ends,

    both these functions are not very necessary to write ,if you omit them compiler will write itself for you but it would not inialize your member variables,like i have done in above code...

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Liberty's TYC++ is online in a slightly outdated version. The discussions regarding classes are reasonable, though not to everybody's taste. The cost however is free, and therefore unbeatable. Here is one link to same.

    http://www.stud.fim.ntnu.no/~oystesk/CPP/index.htm

  5. #5
    nag
    nag is offline
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    Originally posted by Blizzarddog

    To use them, you do something like

    Cat Cat1 = ("Joe", 5, 9, 3);

    But i can't get it to work.

    Hope i helped!
    how it can work,there is no default constructor which takes "Joe",5,9,3 as parameters,but you can write one urself

    like altering above it would be used as
    Code:
    Cat()      //default constructor
        {
            strcpy(name,"mycat");
            height=30;
            weight=90;
        }
    Cat(char *n,int h=10,int w=50)     //constructor with parameters
        {
            strcpy(name,n);
            height=h;
            weight=w;
        }
    Now you can construct objects in main like
    Code:
    int main()
    {
        Cat c1;      //c1 is created with default constructor
        Cat c2("bar");
        Cat c3("doop",20,30);   //constructor with parameters is called
        return 0;
    }
    Two men looked out from Prison Bars,One saw the mud,the other saw stars.

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    oh, okay, i'll try it and give you a ring.

    Uhm, do i HAVE to use strcopy()?
    This war, like the next war, is a war to end war.

  7. #7
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Sorry to use someone else board for help, but can i use, like
    if (player.y == enemy.y && player.x == enemy.x)
    by any chance with a class?

    How would i go about making Cat c4("Plastique", 10, 50); inside a program?
    This war, like the next war, is a war to end war.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if x and y are something other than Cstyle strings then you can probably use the == operator on them for comparison. Or, more precisely, if x and y are simple variables like int, float, double, char (not the best idea to use == on float or double, but it's not illegal at least) or if the == operator is overloaded for a user defined type, then you can use == like that.

    If the object containing the x and y members isn't a pointer, then the dot operator as you use it is appropriate. If the objects are pointers, then you would need the arrow (->) operator to reference the desired members.
    Last edited by elad; 04-10-2003 at 11:47 AM.

  9. #9
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    I mean, i want to make a text rpg with masters, enemies, magics, runes, people, etc. So if i have class Master, and then make master(daddadadad); with an int x and int y, could i use if (player.y == master.y && player.x == master.x) do this. Is that possible? Or do i have to do a if if (player.y ==bob.y && player.x == bob.x) and do that with all 679 masters?

    See, i have a hard time believing that it can decipher which masters which just by the x y coordinates, thats why i was wondering.
    Last edited by Blizzarddog; 04-10-2003 at 12:02 PM.
    This war, like the next war, is a war to end war.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if player and master are the names of a class, then you can't compare members like that. You can only use objects (aka instances of classes).
    Code:
    //declare user types
    class player
    {
       public:
          int age;
    };
    
    class master
    {
       public:
          int age;
    };
    
    //declare instances of user types
    player One;
    master Two;
    
    //initialze members somehow, NOT like this in your code, probably
    One.age = 21;
    Two.age = 23;
    
    //use the age members to compare the two objects
    if(One.age == Two.age)
      //do something
    else
      //do something else

  11. #11
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    But i still have to user if (one.y == master.y) for every thing. Isn't there some easier way??
    This war, like the next war, is a war to end war.

  12. #12
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    and none of the stuff on this page compiles
    This war, like the next war, is a war to end war.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You mean like looping through a container of objects checking all items in one container versus a given value so you don't have to write it out 679 times by hand?


    Providing compilable code wasn't the purpose I suspect. Speaking only for myself I was attempting to provide example/explanations, not runable code. If you want a compilable example it can be provided, but thought the above was enough as is.

  14. #14
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Originally posted by elad
    You mean like looping through a container of objects checking all items in one container versus a given value so you don't have to write it out 679 times by hand?
    Yes, thats what i meant.
    This war, like the next war, is a war to end war.

  15. #15
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    oh, and, uh, mine doesn't even compile.
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 10:13 AM
  3. I am trying to write a code to search a class
    By jrb47 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2006, 02:33 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21