We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,039 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Default Constructor?

My initial thoughts on the default constructor are that it was called automatically. From what I can see, it seems that this is so but a constructor called automatically doesn't initialize int or char variables for exampe, to Zero as I originally thought. It appears that they initialize the variable with a garbage value or something associated with the memory location.

**Is it true that the default constructor (if you don't provide a constructor yourself) is called automatically though does not initialize the class variables to any meaningful value? ** I initially thought that it would initialize class variables to zero but it seems that I am wrong on that. It could be a floor in my inderstanding. My code below provides three garbage values for the output if I don't initialize them.

#include <iostream>
#include <cstdlib>

using namespace std;

class test
{
public:

    char a,b,c;

  char testfunct(char d, char e, char f)
    {
        a = d;
        b = e;
        c = f;
    }
};



int main()
{
test classtest;

cout << classtest.a << "\n";
cout << classtest.b << "\n";
cout << classtest.c << "\n";

system("pause>nul");
}
3
Contributors
3
Replies
1 Hour
Discussion Span
8 Months Ago
Last Updated
4
Views
Question
Answered
danny2000
Junior Poster
186 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

If undeclared/unimplemented, C++ will create default base constructor (no arguments), copy constructor, destructor, and assignment operator. The base constructor will do no initialization of variables whatsoever. The copy constructor will do a bit-wise copy of the copied data (member variables), as will the assignment operator. Often these are not safe. In any case, you should always create your own default constructor, and initialize all member variables. Example:

class foo
{
private:
    int a;
    int b;
    int c;
public:
    foo() : a(0), b(0), c(-1) {}
};
rubberman
Posting Maven
2,572 posts since Mar 2010
Reputation Points: 365
Solved Threads: 306
Skill Endorsements: 52

Thanks rubberman. That makes sense. For some reason I always thought the default constructor would initialize values to zero (in the case of ints for example).

The reason this is confusing is I watched a tutorial recently where the instructor showed us how to create a default constructor and initialized everything to zero in the function body so I thought if it's default, wouldn't it do this automatically. Maybe the instructor shouldn't have called it the default constructor, but a constructor which initializes everything to zero.

danny2000
Junior Poster
186 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

See the posts by arkoenig (towards the end) in this thread:
http://www.daniweb.com/software-development/cpp/threads/360216/c-value-initialization

vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11
Question Answered as of 8 Months Ago by vijayan121 and rubberman

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0713 seconds using 2.82MB