Managing Memory in C++ - Dynamic Memory Allocation with Classes

Code:

Person.cpp

#include "Person.h"

#include <stdio.h>
#include <iostream>

Person::Person()
{

}

Person::Person(std::string fName, std::string lName)
{
    firstName = fName;
    lastName = lName;
}

Person::Person(std::string fName, std::string lName, int age)
{
    firstName = fName;
    lastName = lName;

    ageData = age;
}

Person::~Person()
{
    std::cout << "Person destructor called " << std::endl;
}

void Person::SetFirstName(std::string fName)
{
    this ->firstName = fName;
}

std::string Person::GetFirstName()
{
    return this ->firstName;
}

void Person::SetLastName(std::string lName)
{
    this->lastName = lName;
}

std::string Person::GetLastName()
{
    return this -> lastName;
}

void Person::SetAge(int age)
{
    this ->ageData = age;
}

int Person::GetAge()
{
    return this -> ageData;
}

void Person::SayHello()
{
    std::cout << "Hello" << std::endl;
}

The main program

main.cpp

The header file

Person.h

Last updated

Was this helpful?