Menu

Alloc/Dealloc objects

2002-10-17
2012-09-26
  • Nobody/Anonymous

    Good morning, I'm a newbie;
    Why this simple program:

    #include <iostream>
    #include <cstdlib>
    using namespace std;

    class Radio{
            int Volume;
            float Frequency;
          public:
            Radio();
            Radio(int InitV, float InitF);
            ~Radio();
            void SetVolume(int i);
            void SetFrequency(float f);
            int GetVolume(void);
            float GetFrequency(void);
            };
           
    Radio::Radio(){
    cout<<"Creates Radio object (with default)"<<endl;
    Volume=5;
    Frequency=105.500;
    }
    Radio::Radio(int InitV, float InitF){
    cout<<"Creates Radio object (with parameters)"<<endl;
    Volume=InitV;
    Frequency=InitF;
    }
    Radio::~Radio(){
    cout<<"Destructs Radio object"<<endl;
    }
    void Radio::SetVolume(int i){
       Volume=i;
    }
    void Radio::SetFrequency(float f){
       Frequency=f;
    }
    int Radio::GetVolume(){
       return Volume;
    }
    float Radio::GetFrequency(){
       return Frequency;
    }       

    /*******************************************************************/
    void PrintParameters(Radio CustomRadio){
      cout<<"()Parameters :"<<endl;
      cout<<"\t()Volume = "<<CustomRadio.GetVolume()<<endl;
      cout<<"\t()Frequency = "<<CustomRadio.GetFrequency()<<endl;
    }
    /*******************************************************************/
    int main(int argc, char *argv[])
    {
      {
      Radio MyRadio;

      PrintParameters(MyRadio);
     
      Radio YourRadio(10,102.500);

      PrintParameters(YourRadio);

      }
      system("PAUSE");
      return 0;
    }

    return this :

    Creates Radio object (with default)
    ()Parameters :
        ()Volume = 5
        ()Frequency = 105.5
    Destructs Radio object
    Destructs Radio object
    Creates Radio object (with parameters)
    ()Parameters :
        ()Volume = 10
        ()Frequency = 102.5
    Destructs Radio object
    Destructs Radio object
    Destructs Radio object
    Destructs Radio object

    2 Create and 6 Destruct ?

    Thanks, Rene

     
    • Nobody/Anonymous

      Have you heard of a copy construstor?  This is what is causing the extra destructs.  Add this code

      //In Radio Class
      Radio(const Radio &Org)//Org is the copy being passed in, New is the version you should normally return after changing it
      {
           cout<<"In Copy Constructor\n";
      }

      goto  Ftp://66.222.198.62/Directory%20to%20Put%20Stuff%20in/CopyCnstr/  for an example  You will need MS Word

      Now I have this as output:

      Creates Radio object (with default)
      In Copy Constructor
      ()Parameters :
              ()Volume = 1245056
              ()Frequency = 5.88665e-039
      Destructs Radio object
      Creates Radio object (with parameters)
      In Copy Constructor
      ()Parameters :
              ()Volume = 10
              ()Frequency = 102.5
      Destructs Radio object
      Destructs Radio object
      Destructs Radio object
      Press any key to continue . . .

      4 construct, and 4 destruct
      The first two Destructs are for the copy construct

      Hope this helps.

      Curtis

       
    • Nobody/Anonymous

      Thank you, but can you try the same original source with Visual C++ ? Once upon a time I tested it with a Visual C++ of my friend and I remember the output wasn't the same.
      May be different directives compiler ?
      Rene.

       
    • Nobody/Anonymous

      That Code is VC++  I was at school with no Dev-Cpp :( 

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.