Not really understanding the concept, why did they use "Person *pPerson = new Person("Susan Wu", 32);" and where did ".length();" come from? Also when "Rectangle *pRect" is put into a parameter, pRect is pointing at the address of the object rect, right?

#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    string name;
    int  age;
public:
    Person(string name1, int age1)
    {
        name = name1;
        age = age1;
    }   
    int getAge() { return age; }
    string getName(){ return name; }
};


struct Rectangle
{
    int width, height;
};


void magnify(Rectangle *pRect, int mfactor);
int lengthOfName(Person *p);
void output(Rectangle *pRect);


int main()
{ 
    Rectangle rect;
    rect.width = 4;  
    rect.height = 2;
    cout << "Initial size of  rectangle is ";
    output(&rect);
    magnify(&rect, 3);
    cout << "Size of Rectangle after magnification is ";
    output(&rect);
    
    Person *pPerson = new Person("Susan Wu", 32);
    cout << "The name " << pPerson->getName()
         << " has length " << lengthOfName(pPerson) << endl;
  
    return 0;
}

void output(Rectangle *pRect)
{
   cout << "width: " << pRect->width << " height: " << pRect->height << endl;
}

int lengthOfName(Person *p)
{
    string name = p->getName();
    return name.length();
}

void magnify(Rectangle *pRect, int factor)
{
    pRect->width = pRect->width * factor;
    pRect->height = pRect->height * factor;
}

>why did they use "Person *pPerson = new Person("Susan Wu", 32);"
There's no real reason in this example, and it's also best practice to delete any memory you new.

>where did ".length();" come from?
It's a member function of the std::string class.

>Also when "Rectangle *pRect" is put into a parameter, pRect
>is pointing at the address of the object rect, right?

That's correct.

Also note the constructor for the Person class, that the member variables are initialized inside the body of the constructor. This is very much beginner practice/mistake, and "not good", the reasons for which I shall not get into here (long-winded lecture material). However, each constructor has an initialization block between the end of the argument list and the beginning of the constructor body, which is where member variables are properly initialized. So, the correct way would be:

Person(string name1, int age1)
      : name(name1), age(age1)
    {
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.