~Constructor

1
2
3
4
5
6

class apple
{
public:
 ~apple(void);
}


my request is:
1. do i still have to come out the Implementation on the constructor? if yes, how is it to be done?

2. the void in the constructor does not required any value, am i right?
That is the destructor, not a constructor.
1. If you provide a declaration of a constructor or a destructor you need to implement them, if you don't provide a declaration the compiler would provide the default ones
2. A destructor doesn't take arguments
1. how to i implement the destructor?

2. what if the constructor has a void? does it mean that it does not expecting any value to be pass?
 
orange(void) // constuctor 
1) your original example with the apple was a destructor. Simply the name of the class with a ~ before it.

2) constructors that don't take any arguments (or whose arguments all have default values) are "default constructors". They are used when no arguments are supplied for the constructor.

3) you don't need the 'void' in there. That's a carryover from C. orange() is the same as orange(void) in this case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// an example class
class Test
{
public:
  Test()
  {
    cout << "default ctor called\n";
  }
  Test(int v)
  {
    cout << "ctor called with v=" << v << "\n";
  }
  ~Test()
  {
    cout << "dtor called\n";
  }
};

void func()
{
  Test a[3];  // calls default ctor 3 times
  Test b;     // calls default ctor once
  Test c(4);  // calls 'int v' ctor with v=4
}  // end of function, objects lose scope/die.  calls the dtor for each object (5 times total) 
Last edited on
tks alot. =]
Topic archived. No new replies allowed.