1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Don't know what a empty default constructor does

  1. Feb 27, 2010 #1
    1. The problem statement, all variables and given/known data I dont know what the


    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
    Person(string pname, int page);
    string get_name() const;
    int get_age() const;
    string name;
    int age; /* 0 if unknown */


    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);
    cout << f.get_name() << " is " <<
    f.get_age() << " years old.\n";
  2. jcsd
  3. Feb 27, 2010 #2


    User Avatar
    Science Advisor
    Homework Helper

    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.
  4. Feb 28, 2010 #3
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook