Don't know what a empty default constructor does

  • Thread starter Thread starter torquerotates
  • Start date Start date
  • Tags Tags
    Empty
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
torquerotates
Messages
207
Reaction score
0

Homework Statement

I don't know what the


Person::Person()
{
}

means. I thought a default constructor is meant to initialize private data fields. There is not initialization. That code segment is from
#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
Person();
Person(string pname, int page);
string get_name() const;
int get_age() const;
private:
string name;
int age; /* 0 if unknown */
};

Person::Person()
{
}

Person::Person(string pname, int page)
{
name = pname;
age = page;
}

string Person::get_name() const
{
return name;
}

int Person::get_age() const
{
return age;
}

void main()
{
Person f("Fred", 20);
count << f.get_name() << " is " <<
f.get_age() << " years old.\n";
}
 
Physics news on Phys.org
It's not necessary, the compiler will silently create this behind the scenes.
It is good practice to write this, just as a place holder for when you do have to add some initialisation code.
 
Oh i see. So default constructors are usually unnecessary? I'm guessing that its because that regular constructor already initializes the data in the data field.