-3

the code is running properly but I just wondering why calling function Show() by using pointer can return the value of x, y, z to the objectA, why not the address? Because the pointer stored the address. But how can it give value to the function Show()?

#include <iostream>
using namespace std;

class _3D {
    double x, y, z;
public:
    _3D(); 
    _3D(double initX, double initY, double initZ);

void Show() {
        cout << x << " " << y << " " << z << "\n";
    }
};

// Overloaded constructor
_3D::_3D(double initX, double initY, double initZ) {
    x = initX;
    y = initY;
    z = initZ;
    cout << "With arguments!!!\n";
}

// Class _3D constructor without arguments
_3D::_3D() {
    x = y = z = 0;
    cout << "No arguments!!!\n";
}

void main() {
    _3D A(3, 4, 0);
    _3D B;

// Creating a pointer and adding with the A object's address to the object of _3D type
    _3D*PA = &A;

    // Calling the function Show()
    PA->Show();


    system("pause");
}
Gunasekar
  • 611
  • 1
  • 8
  • 21
  • So you are wondering why PA is printing the values of x,y,z when PA is a pointer? – g-radam Oct 23 '18 at 06:12
  • PA is a pointer to an object of type _3D. You are storing address of A in it. When you do PA->, the '->' part dereference the pointer. You should also be able to do (*PA).Show() which gives you the same output. Dereference means https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean – Asela Oct 23 '18 at 06:15
  • 1
    Are you confused about how pointers work? This question is pretty unclear. Can you give more detail about what you want to know? – Fantastic Mr Fox Oct 23 '18 at 06:17
  • I think the question is not clear - can you explain your problem in more detail using references to the variables in your code? – Laurenz Albe Oct 23 '18 at 06:17
  • BTW: you shouln'd use underscores at the beginning of an identifier, read [this](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Jabberwocky Oct 23 '18 at 07:18

1 Answers1

1

PA is a pointer variable which points to a memory address. In your case, you have made PA point to the memory location of A by using: PA = &A. When you use the -> operator on PA, it will goto the memory location you specified earlier (&A) and READ the data out of that address. Thats It.

Example: A 3D class called A at the address of 0x1000. (eg: random memory location)

3D A; // Creating a 3D structure variable

// This is your 3D structure in memory now.
A: 0x1000: x    (x = offset 0)
   0x1004: y    (y = offset 4)
   0x1008: z    (z = offset 8)

// Create pointer to A variable
3D* PA = &A; // PA == 0x1000

// Shows the pointers are the same
std::cout << &A    << std::endl; // Prints 0x1000 (THE ADDRESS of A!!)
std::cout << PA    << std::endl; // Prints 0x1000 (THE ADDRESS of A!!)

// Remember, when using just PA->foo ... will READ the value by default
std::cout << PA->x << std::endl; // goto Address 0x1000, offset 0 (read x)
std::cout << PA->y << std::endl; // goto Address 0x1000, offset 4 (read y)
std::cout << PA->z << std::endl; // goto Address 0x1000, offset 8 (read z)

// If you want the get GET the address of x, y or z, do use & as normal.
// &PA->x  mean goto address PA, offset x, y or z, and then get the address
std::cout << &PA->x << std::endl; // Prints 0x1000
std::cout << &PA->y << std::endl; // Prints 0x1004
std::cout << &PA->z << std::endl; // Prints 0x1008

Just try experimenting with pointers and memory and you should come to the same conclusions.

g-radam
  • 527
  • 1
  • 7
  • 20