Class I

May 20, 2011 at 10:57am
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;
}

CRectangle::~CRectangle () {
delete width;
delete height;
}

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
May 20, 2011 at 11:47am
as far as I understood constructor/destructors and new/delete have the same functionallity.
No. 'new' allocates memory an calls the constructor. 'delete' calls the destructor and frees the memory
May 20, 2011 at 11:48am
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.
May 20, 2011 at 12:58pm
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?
May 20, 2011 at 1:15pm
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
May 20, 2011 at 5:12pm
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:
1
2
3
4
int x;
cin >> x;
int y[x];
//... 

But you CAN do this:
int *y = new int[x];

That is the point of dynamic memory.
May 20, 2011 at 5:36pm
L B wrote:
Whether you use the default constructor or define your own, it is only called when the object is destroyed.
You certainly mean destructor.

The destructor is also called when local variables are going out of scope.
1
2
3
{
a b(100);
} // destructor of b is called at this point 


bbcc wrote:
I don't understand what is the difference of assigning memory during runtime?
It's not different from assigning any other type of variable.
May 20, 2011 at 6:57pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * 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...
 */
new int;
/*
 * 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 = new int;
/*
 * now we can later delete the memory
 */
//time passes as the pointer gets copied and thrown around our program...
delete b;


Example program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <iostream>
#include <string>

using namespace 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.
Last edited on May 20, 2011 at 7:09pm
Topic archived. No new replies allowed.