Jump to content

constructor help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hello guys!

Can someone give me an example of a simply constructor with some explains...
I'm really freaking out with this thing! The s**ts we got from school are all wrong and I never managed to run it on dev c++.That have confused me!

Someone who can help?I really want to understand how this thing is working!

Any help is highly appreciated!

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Try a different compiler, if that is not beyond possibility. Otherwise, this is how I know it to be (I could be wrong, but this is what I know; not as experienced in C/C++):
class Computer { 

  void Computer;                                // The constructor 

        // function name is equal to the name of the class. 

  void ~Computer;      // The destructor is the same, save with a leading '~' . 

} 

void Computer::Computer (){ 

  // do some stuff... 

  this.ram_size= get_ram_size ("this computer") /* or something ... */; 

} 

void Computer::~Computer (){ 

  // clean up... 

  GlobalFree (this.memory) /* or something like this... */; 

  fclose (this.some_file) /* etc. */; 

} 


Again, I might be wrong anywhere, but this is to the best of my knowledge, off the top of my head. Anyone can correct me if I'm wrong (though try not to be too mean in doing so :)).

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Constructors (ctors) and destructors (dtors) have no return type. Also, you can have more than 1 ctor but only 1 dtor, which doesn't take any arguments.
class SomeClass {
    public:
        SomeClass();  //default ctor
        SomeClass(int id);

        ~SomeClass()

    private:
        int* _pointer;
        int   _id;
};

SomeClass::SomeClass()
    : _id(0), _pointer(NULL)
{
}

SomeClass::SomeClass(int id)
    : _id(id)//, _pointer(new int)
{
    _pointer = new int;
}

// clean up
SomeClass::~SomeClass() {
    delete _pointer;
}
You can read more about constructors here.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users