Hello,
I am trying to learn c++ by myself.
I found this example in the tutorial documentations:
// example on constructors and destructors
#include <iostream>
using namespace std;
class CRectangle {
int *width, *height;
public:
CRectangle (int,int);
~CRectangle ();
int area () {return (*width * *height);}
};
CRectangle::CRectangle (int a, int b) {
width = new int;
height = new int;
*width = a;
*height = b;
}
int main () {
CRectangle rect (3,4), rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
My problem is with the meaning of (new)
as far as I understood constructor/destructors and new/delete have the same functionallity. Do they have any diifference?
I am working with a code which has many of these (new) functions.e.g.
A * a= new A();
I have problem with new A(), I am not sure if this new makes a new object of my class A ,the constructor meaning)
if it is the case what is the differenece of it with a constructor?
many thanks in advance
Did you read the section "Dynamic memory"?
new allocates memory for an object and then calls it's constructor. A constructor only initializes that memory.
o.k. thank you for your descriptive responses.
I have another question from coder777.............is the destructor made by default, in that case we might not need (delete) to call it?
I have another question from hamsterman. I 've just read dynamic memory part, more or less I understand but I don't understand what is the difference of assigning memory during runtime?
If we write like bobby = new int [5];
I think it's the same as doing it before the runtime, But how about using a variable as its size, it means that we allocate an undefined size of memory ???
I will appreciate if you help me with this
You must always delete memory when you are done with it. Whether you use the default constructor or define your own, it is only called when the object is destroyed.
No, the size is always defined by you. You can't do this:
/*
* here we create an int,
* it gets destroyed when it leaves scope
*/
int a;
//---------------------------------------------------------------------------
/*
* here we create an int by manually allocating memory,
* it gets destroyed when we call delete...
*/
newint;
/*
* Whoops! new will return the memory address of the new int,
* but then it gets thrown away so we can't call delete on it;
* this is a memory leak.
*/
//---------------------------------------------------------------------------
/*
* so let's store that memory pointer somewhere
*/
int *b = newint;
/*
* now we can later delete the memory
*/
//time passes as the pointer gets copied and thrown around our program...
delete b;
#include <iostream>
#include <string>
usingnamespace std;
class Person {
string name;
public:
Person(string n) {
name = n;
cout << '"' << name << "\" is being created.\n";
}
Person(Person &p) {
name = p.name;
name.append("'s clone");
cout << '"' << p.name << "\" is being copied to \"" << name << "\".\n";
}
~Person() {
cout << '"' << name << "\" is being destroyed.\n";
}
};
Person passAround(Person p) {
return p;
}
Person babyMaker(string name) {
Person baby(name);
return baby;
}
Person* pointerMaker(string name) {
Person *dot = new Person(name);
return dot;
}
void murder(Person *victum) {
delete victum;
}
int main() {
{
Person kim("Kimberly");
cout << endl;
Person *dex = new Person("Dexter"); //memory leak
cout << endl;
}
cout << endl;
Person kat = babyMaker("Kathleen");
cout << endl;
Person *chris = pointerMaker("Christopher");
cout << endl;
passAround(kat);
cout << endl;
murder(chris);
cout << endl;
return 0;
}
"Kimberly" is being created.
"Dexter" is being created.
"Kimberly" is being destroyed.
"Kathleen" is being created.
"Kathleen" is being copied to "Kathleen's clone".
"Kathleen" is being destroyed.
"Christopher" is being created.
"Kathleen's clone" is being copied to "Kathleen's clone's clone".
"Kathleen's clone's clone" is being copied to "Kathleen's clone's clone's clone".
"Kathleen's clone's clone" is being destroyed.
"Kathleen's clone's clone's clone" is being destroyed.
"Christopher" is being destroyed.
"Kathleen's clone" is being destroyed.