1

I am writing this code where the user can input a name and an age. The program should then print out the name and if the age is over 19 or not, it should print out whether that person is an adult or not.

[Input]
Name : Rivf
Age : 20

[Output]
Rivf is adult.

I was just introduced to copy constructors and destructors so I believe there is something wrong with my implementation of them. The code runs and prints out the desired output but then I get the following error:

Makefile:6: recipe for target 'cpp_run' failed
make: *** [cpp_run] Error 134
munmap_chunk(): invalid pointer
Aborted (core dumped)

I would greatly appreciate some help with this since I am just starting to learn about constructors and these other tools that come with classes. Since this is an assignment I cannot change anything in the class person. Here is my code:

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

class person{
private:
    string name;
    int*    age;
    bool  adult;
public:
    person();
    person(const person& other);
    void setName(string n);
    string getName();
    void setAge(int a);
    bool isAdult();
        ~person();
};
// (1) Implement class functions with constructor

person::person(){
}

void person::setName(string n){
    name = n;
}

void person::setAge(int a){
    age = new int;
    *age = a;
}


person::person( const person& other){
    this->name = other.name;
    this->age = other.age;
    this->adult = other.adult;
}


person::~person(){
    delete(age);
}

string person::getName(){
    return name;
}

bool person::isAdult(){

    if(*age > 19){
        adult = true;
    }
    else{
        adult = false;
    }
    return adult;
}

int main()
{
    // (2) make 'defultP' and 'newP' object

    person defaultP;
    person newP = defaultP;

    string name; int age;
    cout<<"Name : "; getline(cin, name);
    cout<<"Age : "; cin>>age;

    defaultP.setName(name);
    defaultP.setAge(age);
    // (3) print values with member functions of new object

    cout<<defaultP.getName();
    if(defaultP.isAdult() == true){
    cout << " is adult." << endl;
}
else{
    cout << " is kid." << endl;
}

    return 0;
}
Rivf
  • 133
  • 7
  • 2
    Why are you using int* instead of just int for your age field? – Eric Oct 30 '20 at 13:28
  • You didn't assign the adult field for newP it copies uninitialised value.and is compile error or makefile error? – moon shines Oct 30 '20 at 13:31
  • @Eric You mean why is ``age`` declared as an ``int*`` in the class? That's part of the assignment so I cannot change it. I think it is because they are trying to make me use a pointer as a member of the class. – Rivf Oct 30 '20 at 13:31
  • @moonshines Could you elaborate? I am not sure I am getting what you are trying to say. You mean this ``this->adult = other.adult;`` is coping an uninitialized value? – Rivf Oct 30 '20 at 13:35
  • 4
    I think after both objects go out of scope it calls the destructor and fails at the second destructor as pointer is already deallocated. – moon shines Oct 30 '20 at 13:35
  • Yes since other.Adult is not yet initialised. – moon shines Oct 30 '20 at 13:36
  • 3
    you are double deleting int* age, this results in undefined behaviour – zpasztor Oct 30 '20 at 13:37
  • 1
    @Rivf -- Look at the **Managing resources** section of the duplicate link. In addition, your class lacks a user-defined assignment operator, you failed to initialize your members in the default constructor, etc. Thus a one line program `int main() { person p; }` invokes undefined behavior. – PaulMcKenzie Oct 30 '20 at 13:40
  • have simplely int instead of int* – Build Succeeded Oct 30 '20 at 13:43
  • 1
    Your `adult` member is completely unnecessary; all you need is `bool person::isAdult() { return *age > 19; }`. – molbdnilo Oct 30 '20 at 13:44
  • @PaulSanders I am aware of that. But it is done so I get practice with copy constructors and destructors. I cannot change ``int*`` into ``int`` for this assignment. – Rivf Oct 30 '20 at 13:47
  • @PaulMcKenzie I understand now. Thanks. – Rivf Oct 30 '20 at 13:51

0 Answers0