classes are pain

This is a discussion on classes are pain within the C++ Programming forums, part of the General Programming Boards category; i know i've started aot of threads recently but i really want to learn c++. classes are killing me. my ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    classes are pain

    i know i've started aot of threads recently but i really want to learn c++. classes are killing me. my tutorial just isn't doing me anygood. its been great till now though look at it, ill put in comments where im having trouble
    Code:
    #include <iostream>
    using namespace std;
    
    class Dog {
    private:
        int age;
        int weight;
    public:
        Dog();      //Constructor?how and why does it work?
        ~Dog();    //Destructor?"                                     "
        void setAge(int age);//whats with void? what is it, how does it
    //work? and o this is killing me, please just explain every line of code. i know its alot to ask but ill give you drugs for it(joking)
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    };
    
    Dog::Dog()
    {
        age = 0;
        weight = 0;
        cout << "Dog Constructor Called" << endl;
    }
    
    Dog::~Dog()
    {
        cout << "Dog Destructor Called" << endl;
    }
    
    void Dog::setAge(int age)
    {
        this->age = age;
    }
    
    int Dog::getAge()
    {
        return age;
    }
    
    void Dog::setWeight(int weight)
    {
        this->weight = weight;
    }
    
    int Dog::getWeight()
    {
        return weight;
    }
    
    void Dog::speak()
    {
        cout << "BARK!!" << endl;
    }
    
    int main()
    {
        Dog fido;
        Dog rover;
    
        cout << "Rover is " << rover.getAge() << " years old." << endl;
        cout << "He weighs " << rover.getWeight() << " lbs." << endl;
        cout << endl;
    
        cout << "Updating Rover's Age and Weight" << endl;
        rover.setAge(1);
        rover.setWeight(10);
    
        cout << "Rover is " << rover.getAge() << " years old." << endl;
        cout << "He weighs " << rover.getWeight() << " lbs." << endl;
        cout << endl;
    
        cout << "Fido is " << fido.getAge() << " years old." << endl;
        cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    
        cout << "Setting Fido to be the same as Rover" << endl;
        fido = rover;
    
        cout << "Fido is " << fido.getAge() << " years old." << endl;
        cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    
        rover.speak();
        fido.speak();
        
        cin.get();
    
        return 0;
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Constructor: when you declare a new object of that class, you invoke its constructor. The default constractor takes no arguments, and it should initilize that classes private members, so in your example:
    Code:
    dog(){
       age = -1;  /* these do not have to equal '-1' they could be your special default value */
       weight = -1; /*it could be beneficial to set both to '-1' however as it will act as a flag */
    your constructor could have parameters as well; lets say that before you make a new class object you ask the user for the age and weight of the dog, and they are 3 and 75 respectively, then your constructor might look like:
    Code:
    dog( int ageInput, int weightInput ){
      age = ageInput;  /* now the members of the class will be set to the user inputed values */
      weight = weightInput;
    }
    you could also have just one parameter, age or weight...when the program is running it will match the proper constructor.

    destructor: not used very widely anymore, but it is used for clean up when the object is deleted; lets say you dynamically allocate memory somewhere in you class, one place that you could deallocate it is inside the destructor.

    void: your class could have any sort of function inside of it; this one is of type void...I'm not exactly sure what your question here is...

    all the functions are declared in the class, and are defined below; the class declaration should be placed in a .h file, so dog.h and the definitions should be placed in dog.cpp


    I suggest you do some research on google; there are TONS of great tutorials. Also pick up a book for Christ's sake.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    why axon are always faster ?

    I suggest you do some research on google
    and he always mention "google"

    blow me ... ...

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    A generally better way of initializing variables in the constructor is the initialization list. It doesn't matter so much for ints, but for more complex data types it is required or just more efficient:
    Code:
    Dog::Dog() : age(0), weight(0)
    {
        cout << "Dog Constructor Called" << endl;
    }
    Quote Originally Posted by axon
    destructor: not used very widely anymore, but it is used for clean up when the object is deleted; lets say you dynamically allocate memory somewhere in you class, one place that you could deallocate it is inside the destructor.
    ? Not used very widely anymore ? The destructor is actually very usefull. Besides cleaning up resources like dynamically allocated memory, it is used with RAII in designing exception safe code, one example being a smart pointer. Another example is an fstream object. When the fstream goes out of scope, the file is closed automatically, so you don't have to worry about closing the file if there are exceptions or if you just want to return early from your function. The destructor takes care of it for you.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    destructor: not used very widely anymore
    Uh buddy hate to tell you but the destructor is used on every object that is created. Now if you want to say that they aren't written by the user much any more then thats ok

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    If you're having trouble understanding classes, go back and make sure you understand structures. Structures are like classes without functions. (Technically, structures can have functions in C++.)

    whats with void?
    Void means that the function doesn't return a value. Go back and re-study functions! Everything in C++ is done with functions, and if you don't understand functions, you should not be trying to understand classes & objects yet.

    void setAge(int age); // This is an accessor function. It allows you to "get to" the private variable age.

    The constructor creates an object in the computer's memory and initializes it. You can't call fido.SetAge() if fido hasn't been constructed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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