Question: Pointers, Constructors, and Destructors in C++ Today, we are going to modify our WitPerson class from last week to add constructors and a destructor Then
Pointers, Constructors, and Destructors in C++ Today, we are going to modify our WitPerson class from last week to add constructors and a destructor Then we will modify our main ) function to create each instance we ask for dynamically from the heap, instead of statically on the stack. But first, a tutorial on pointers. In Cand C++, a pointer is a variable that holds the memory address of another variable. That's it. Pointers are declared using the unary version of the operator. (The binary version of the operator is, of course, multiplication.) So for example #include "Foo.h" int main (void) Foo a; Foo b; Foo pFoo; declares 2 instances of class Foo, called 'a' and 'b'. It also declares a pointer to a Foo, called pFoo. The last statement can be interpreted as, "assign the address of the Foo named 'b' to pFoo". If we look at the stack, assuming a Foo is 32 bytes in size, we would have: Address 0x0000 0x0020 Variable Foo 'a Foo b Value According to the default ctor According to the default ctor 0x0020 (the address of 'b 0x0040 If my class Foo had a member function, int Getint(void), I would use the pointer operator to access the function with pFoo, or I could use the "dot" operator with the name directly. In other words: int x, y
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
