Don't know what a empty default constructor does

  • Thread starter torquerotates
  • Start date
  • Tags
    Empty
In summary, the conversation discusses the purpose of a default constructor in a class and whether it is necessary or not. The person asking the question is confused about the need for a default constructor when a regular constructor already initializes the data fields. The response states that a default constructor is not necessary but can be useful as a placeholder for future initialization code.
  • #1
torquerotates
207
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);
cout << f.get_name() << " is " <<
f.get_age() << " years old.\n";
}
 
Physics news on Phys.org
  • #2
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.
 
  • #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.
 

1. What is a default constructor?

A default constructor is a special type of constructor that is automatically created by the compiler if no constructor is defined in a class. It initializes the object's instance variables to their default values.

2. What is the purpose of a default constructor?

The purpose of a default constructor is to ensure that an object is always initialized properly, even if the user does not provide any values. It also allows objects to be created without passing any arguments to the constructor.

3. What happens if a class doesn't have a default constructor?

If a class does not have a default constructor, the compiler will not be able to create one and the class will have no constructor at all. This means that objects of that class cannot be created without passing arguments to the constructor.

4. How do you define a default constructor in a class?

To define a default constructor in a class, you simply need to create a constructor method without any parameters. This will be automatically used as the default constructor by the compiler.

5. Can a default constructor have parameters?

No, a default constructor cannot have parameters. Its purpose is to initialize the object's instance variables to their default values, so it does not make sense to pass any arguments to it. However, a class can have multiple constructors, including one with parameters.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
940
  • Engineering and Comp Sci Homework Help
Replies
8
Views
838
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
893
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
Back
Top