Constructors and destructors - C++
Hello people! Today I will talk about how to create object in C++. So we have few options:
- default constructor (available when no other exist),
- constructor without parameters,
- constructor with parameters,
- copy constructor (as argument parameter from your class).
In constructors we can prepare objects. As example we can use cake class.
cake() {};
cake(string name) { cake_name = name };
cake(cake Cake) { //make copy of object };
So without constructor you can make:
cake Cake;
With constructors you can write: cake Cake; - no parameters
cake Cake2("Brownie"); - 1 parameter function
cake Cake3(Cake2); - copy constructor (copy "Brownie cake")
Destructors: When object's life is going to end we need to clean memory, so we need to release memory and sometimes we need to do some extra things, but we can talk about it later. Bye!