0

Here is a simple rectangle area calculating cpp code and I have some questions around it:

#include <iostream>
#include <conio.h>
using namespace std;
class CRectangle
{
        int *width, *heigth;
    public:
        CRectangle(int, int);
        ~CRectangle();
        int area() { return (*width * *heigth);}
};

CRectangle :: CRectangle(int a, int b)
{
    width = new int;
    heigth = new int;
    *width = a;
    *heigth = b;
}

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

void main()
{
    CRectangle rect1(3,4), rect2(5,6);
    cout << "rect1 area = " << rect1.area() << "\n";
    cout << "rect2 area = " << rect2.area();
    getch();
}
  1. why in such object oriented codes we use pointers,I mean what's the advantage(s)?
  2. in this code after creating the object rect1(3,4) we create rect2(5,6) ,with doing this, logically (I think) 5 and 6 are replaced instead of 3 and 4 in the memory sections that width and height are pointing to , so 3 and 4 are not available anymore ,but they are.

Please explain what exactly happens?

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
Güneş
  • 3
  • 1
  • 14
    There's absolutely no advantage to using pointers here, and some pretty obvious disadvantages. The code might have been written by someone more familiar with a different language like Java where *all* variables are created with `new`. – Mark Ransom Sep 06 '13 at 17:23
  • 1
    1) There is no advantage to using pointers like this. This is not a good usage of pointers. 2) You create/allocate two *new* integers for (5, 6). You're not using the old memory. They don't overlap. They don't overwrite. They're completely separate, as each one has its own `new` call. – Cornstalks Sep 06 '13 at 17:24
  • As you're new to this, let me point out that using standard library's pointers are much more helpful than using naked pointers. For example `` or `` – Games Brainiac Sep 06 '13 at 17:24
  • 1
    And each CRectangle object has its own pointers to width and height. They are not shared. – dcaswell Sep 06 '13 at 17:25
  • and just a minor nitpick... "height" is usually not speeled "heigth"... ;-P – twalberg Sep 06 '13 at 20:32

4 Answers4

3

1-why in such object oriented codes we use pointers,I mean what's the advantage(s)?

There are none.

2, 3 and 4

With

    width = new int;
    heigth = new int;

you are always reserving new separate memory locations in each constructor call. Every object has separate memory locations for width and height, so there will be no overwriting.

However, there is a situation when two object will share same memory locations - if you copy one object to another, using copy-constructor or assignment operator:

CRectangle rect1(3,4);
CRectangle rect2 = rect1;
CRectangle rect3(4, 5);
rect3 = rect1;

In this case, width and height get the same values as rect1's, which will make all three objects to point to the same memory locations. In the case of rect2, the default constructor will not be called, so no new memory will be allocated, and in the case of rect3, old values of width and height will be lost, and the variables will be updated with the memory locations from rect1 which will lead you to the memory leak (because memory allocated in the default constructor of rect3 will never be freed).

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
2

In your case pointers gives nothing at all.

So pointer is array, to prove it look at opeator [] inline implementation. a[b] actually is *(a + b). So if you use new to create single value (as your case) it is bad experience, you could use it to create arrays, advantage in this case would be that creating with new memory is allocated in heap, creating in usual way memory allocated is in stack.

C++ left this for programmers to choose if they want pointers or not, advantage about memory allocation can be reached using containers, for example std::vector and this way is much less in error prone.

Another thing, pointer left in C++ because of compatibility with C, because there lots of stuff written in C but C++ developers also using that.


5 and 6 are replaced instead of 3 and 4 in the memory sections that width and height are pointing to , so 3 and 4 are not available anymore

After reading this sentence, I think you need to read about C++ or another objective orentied programming language.

My short explanation, why all values are accesable, rect1 and rect2 are completely different and self standing object, so they have their own memory and cannot effect eachother.


BTW: forgot to mention disadvantage of allocation in heap - it is much slower then allocation in stack.

ST3
  • 8,826
  • 3
  • 68
  • 92
0

why in such object oriented codes we use pointers,I mean what's the advantage(s)?

No advantages here... Here are some explanations why.

There is some disadvantages like: the allocation on the heap which is much slower than allocation on the stack.

in this code after creating the object rect1(3,4) we create rect2(5,6) ,with doing this, logically (I think) 5 and 6 are replaced instead of 3 and 4 in the memory sections that width and height are pointing to , so 3 and 4 are not available anymore ,but they are.

No each object created has its own memory space.

CRectangle rect1(3,4), rect2(5,6);

Here you create two distinct objects. The second object created does not use the memory of the first object. So each object has its own space for width and height members.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • according to answers it proved to me that here there isn't advantages , but could you explain what's the advantage of this in bst tees , because i had seen that in bst trees they use pointers a lot with this oprator(->) and as a second question what is this oprator? – Güneş Sep 06 '13 at 17:45
  • @darksky That's what I say :) – Pierre Fourgeaud Sep 06 '13 at 17:46
  • absolutely , but I made a mistake :( and edited my comment , see edited comment:) – Güneş Sep 06 '13 at 17:54
  • @darksky It depends on how your BST Trees `class` is implemented and used. We can't tell without seeing why it is good or bad. – Pierre Fourgeaud Sep 06 '13 at 17:56
  • for example in university management programs with adding new student , adding new lessons , presenting student status like passed lessons and grade and etc. – Güneş Sep 06 '13 at 18:01
-1

why in such object oriented codes we use pointers,I mean what's the advantage(s)?

As far as we can tell, it was your idea. The advantage is extremely limited in this case if at all. It's more likely a disadvantage in this case.

in this code after creating the object rect1(3,4) we create rect2(5,6) ,with doing this, logically (I think) 5 and 6 are replaced instead of 3 and 4 in the memory sections that width and height are pointing to , so 3 and 4 are not available anymore ,but they are.

No, you're mistaken about "logically (I think) 5 and 6 are replaced". They're both in-scope and valid until the end of the main() block.

Here is how you could make them invalid:

void main()
{
    {
        CRectangle rect1(3,4), rect2(5,6);
    }

    // Note that this is no longer a valid program and will fail at compile-time
    cout << "rect1 area = " << rect1.area() << "\n";
    cout << "rect2 area = " << rect2.area();
    getch();
}

If your question is how you could reap rect1 and then witness rect2's heap allocations take place exactly where rect1's were, then I'm afraid you can't really count on this behavior even if you got it to happen.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88