Thread: Classes help

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Question Classes help

    i'm using Dev C++ Ver. 4.0
    I can't get a class to work
    Code:
    class animal {
    int wieght;
    int fleas;
    string name;
    };
    
    {
     {40, 10, "JohnDo"}
    };
    whats wrong with this?

    I can do structures... Whats the difference between the 2?
    This war, like the next war, is a war to end war.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Code:
    {
     {40, 10, "JohnDo"}
    };
    What is this? You can't have an unnamed block at global scope, and it's not even a part of your class. If you want to make an object and assign values, try using a constructor. Also keep in mind that the default access of a class is private, so with your class definition you have no way of getting at the members.
    Code:
    #include <string>
    
    using namespace std;
    
    class animal {
    public:
      int wieght;
      int fleas;
      string name;
    
      animal(int w, int f, string n)
      {
        wieght = w;
        fleas = f;
        name = n;
      }
    };
    
    int main()
    {
      animal my_animal(40, 10, "JohnDo");
    };
    It's better to not make your members public, but for now you don't have to worry about it.
    *Cela*

  3. #3
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    To confusing... mind meltdown in 3...2...1....0.... KABOOOOOOOM
    a little explanation.. cuz looking at that gives me headache!
    No offense.. i just don't know what everything is..
    This war, like the next war, is a war to end war.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>i just don't know what everything is..
    It's a simple concept, just open any book on C++ and look for "constructor" in the index. They can probably explain it better than I can.
    *Cela*

  5. #5
    PHP Code:
    class ANIMAL //This is our ANIMAL Class Declaration
    {
      public:  
    //This declares that the following Variables or Functions are public - there are also private and protected specifiers.
        
    int a;
        
    int b;
        
    string c;

       
    ANIMAL(); //Every class has a CONSTRUCTOR
      
    ~ANIMAL(); //Every class has a Deconstructor

      //Here's where we declare functions contained within the class.
      
    int AnimalType(int num);

    }; 
    //Make sure u have a ';' at the end of the bracket, in a class

    //Here's where we DEFINE the class functions, including the contrutor and destructor
    ANIMAL::ANIMAL(){ //Constructor Def

    }

    ANIMAL::~ANIMAL(){ //Destructor DEF

    }

    int ANIMAL::AnimalType(int num){ //AnimalType Fxn DEF
    ...

    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    I see.... maybe C++ isn't for me.. cuz i still dont know what Animal::~animal is! its the :: throwing me off i think.. i have 2 c++ books, Primer C++, and Al stevens Teach yourself C++, they have different tings. and now this is also different...... am i getting mixed messages?
    This war, like the next war, is a war to end war.

  7. #7
    Classes are SOOOOOOOO simple, really.

    Get a different book at the library or something and "READ" the chapters on classes. It requires alot more explanation than can be given on a board, but it is simple as HEck.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8

  9. #9
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Originally posted by Blizzarddog
    I see.... maybe C++ isn't for me.. cuz i still dont know what Animal::~animal is! its the :: throwing me off i think.. i have 2 c++ books, Primer C++, and Al stevens Teach yourself C++, they have different tings. and now this is also different...... am i getting mixed messages?

    Animal::~Animal(){}

    is the equivalent to:

    Code:
    class Animal
    {
    public:
         ~Animal(){} // this line
    };
    the :: is just the scope resolution operator.

    If you want a better book, try C++ the complete reference by Herbert Schildt, the fourth edition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM