stupid compiler

This is a discussion on stupid compiler within the C++ Programming forums, part of the General Programming Boards category; look at this code Code: #include <iostream> using namespace std; class Dog { private: int age; int weight; public: Dog(); ...

  1. #1
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    Question stupid compiler

    look at this code
    Code:
    #include <iostream>
    using namespace std;
    
    class Dog {
    private:
        int age;
        int weight;
    public:
        Dog();      //Constructor
        ~Dog();    //Destructor
        void setAge(int age);
        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();
    
        return 0;
    }
    instead of the first 2 lines of text being dog constructor called, dog destructor called, it just has dog constructor twice im confused has my compiler got spell check or something, i use Dev-C++

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    It says "dog constructor called" twice because you are creating two Dog objects.
    Code:
        Dog fido;
        Dog rover;
    The constructor is called for each Dog object created. The destructor doesnt get called until the object is destroyed.

  3. #3
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    but it doesn't output dog destructor called once

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I'm not sure, but it could be:

    fido = rover;

    I know very little about c++, but this line looks like it stops the rover destructor to be called so it is only calling the fido destructor. I may be entirely incorrect, I don't know.

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    you make two dogs. at the beginning, it calls the constructor twice; at the end, the destructor twice. nothing's wrong
    hello, internet!

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    I know very little about c++, but this line looks like it stops the rover destructor to be called so it is only calling the fido destructor. I may be entirely incorrect, I don't know.
    That is incorrect. It doesnt stop the rover destructor from being called.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    Code:
    fido = rover;
    Be careful about asigning one object to another like that. If the object has member variables/structures/objects allocated from the heap, then the equals sign will not do what you want. It will just make the pointers equal - not the data pointed to by the pointers. In this case you would want to over-ride the equals sign.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >but it doesn't output dog destructor called once
    You mean when you run your code you don't get this?

    Dog Constructor Called
    Dog Constructor Called
    Rover is 0 years old.
    He weighs 0 lbs.

    Updating Rover's Age and We
    Rover is 1 years old.
    He weighs 10 lbs.

    Fido is 0 years old.
    He weighs 0 lbs.
    Setting Fido to be the same
    Fido is 1 years old.
    He weighs 10 lbs.
    BARK!!
    BARK!!
    Dog Destructor Called
    Dog Destructor Called
    Last edited by swoopy; 09-10-2004 at 11:50 PM.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by bithub
    Code:
    fido = rover;
    Be careful about asigning one object to another like that. If the object has member variables/structures/objects allocated from the heap, then the equals sign will not do what you want. It will just make the pointers equal - not the data pointed to by the pointers. In this case you would want to over-ride the equals sign.
    So is putting fido = rover; incorrect or correct? I think you should put:

    fido.setAge(1);
    fido.setWeight(10);

    That's my opinion.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    In his case, putting fido = rover; would work just fine. This is because he doesnt have any pointers in his object.

    If you dont over-ride equals, then doing fido = rover is essentially doing:

    memcpy(&fido,&rover,sizeof(Dog));

  11. #11
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    in response to swoopys, it doesn't show dog destructor called at bottom

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    I just tested it on my machine, and it called both destructors.

  13. #13
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    wat complier

    wat complier r u using, i use Dev-C++

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by bithub
    In his case, putting fido = rover; would work just fine. This is because he doesnt have any pointers in his object.

    If you dont over-ride equals, then doing fido = rover is essentially doing:

    memcpy(&fido,&rover,sizeof(Dog));
    For other classes this isn't necessarily true depending on what types of members they have.

    The default copy constructor and assignment operators do a memberwise copy or assignment - so for example if you have an std::string as a member variable, that would be copied correctly using its copy constructor or assignment operator. If a memcpy had been called the std::string would probably not get copied correctly.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    Im using MSVC++, but compiler shouldnt matter here.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Problem: Stupid Compiler Error
    By iSlak in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2005, 06:34 AM
  2. Stupid compiler
    By chris1985 in forum C Programming
    Replies: 5
    Last Post: 07-01-2005, 08:22 AM
  3. Dev C++ Compiler, Indentation?
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 07:13 AM
  4. lcc win32 compiler download problems
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-01-2004, 08:39 PM
  5. Special Compiler for win app's
    By Unregistered in forum Windows Programming
    Replies: 19
    Last Post: 04-26-2002, 04:52 PM

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