Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,212 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,264 people online right now. Registration is fast and FREE... Join Now!




The practical difference of constructors over member functions

 
Reply to this topicStart new topic

The practical difference of constructors over member functions

Shrikharan
2 Jun, 2008 - 05:43 AM
Post #1

New D.I.C Head
*

Joined: 15 Dec, 2005
Posts: 14


My Contributions
I am a novice eat programming. I have some confusion of the real value of constructors of classes in C++. Basically what is the real practical use of having a constructor over a member function?

I know that constructors are useful in initiation of the date member of a class and if the data members are not initialized then they can have garbage values and result in strange things. If that is the purpose of Constructors over the member functions then are we to have equal number of constructors as the member functions so as to initialize all data members found in a class? We need that much of constructors as the member functions if we are to initialize all the data members by constructors.

Why cannot the normal member functions do the same task? What is the difference of data members being initialized by constructors and member functions? I find it difficult to see the difference. Can anyone help in enlightening me as I am sure I am missing something?

Thanks
Shri

User is offlineProfile CardPM
+Quote Post

KYA
RE: The Practical Difference Of Constructors Over Member Functions
2 Jun, 2008 - 01:43 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,056



Thanked: 124 times
Dream Kudos: 1200
My Contributions
There really isn't a difference in my opinion. Short of having easier code and objects initialized with their respective data when you create them, functions can do the same thing, but why call an extra function to set a Cat object's age when you could just do Cat(5) or something similar.
User is online!Profile CardPM
+Quote Post

skater_00
RE: The Practical Difference Of Constructors Over Member Functions
2 Jun, 2008 - 02:10 PM
Post #3

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
Not sure if I totally understand your question. My reply is just a reply to what I think the question was.

cpp

if (everyone_would_write_proper_English)
Make_skater_00_reread_text_50_times = false;


Classes don't need multiple constructors/destructors actually. One constructor/destructor per class can do the job. It is possible though to have more than one constructor/destructor.

In most cases, every class has its own private members that are initialized in the class constructor. These members are declared privately because you are most likely going to use them more than once and in multiple functions. Those are the global variables.

There are also variables that can be initialized (and declared) in a function itself, so not declared privately or initialized in the constructor initialization list. This is done because these members are only used once or so in a function in the whole application. Those are the local variables.

Why would programmers make a variable global if they only use it once? Or why would programmers declare the same variable (with same objective) 5 times in different functions when you can declare it globally and once?

Constructors: Initializing globally declared members.
Functions: Initializing (and declaring) local members.

That's the only difference I can think of. Hope this helped clarifying what you needed. If it didn't, explain your question better.
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: The Practical Difference Of Constructors Over Member Functions
2 Jun, 2008 - 02:56 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(Shrikharan @ 2 Jun, 2008 - 06:43 AM) *

Why cannot the normal member functions do the same task? What is the difference of data members being initialized by constructors and member functions? I find it difficult to see the difference. Can anyone help in enlightening me as I am sure I am missing something?


You can, and it's often recommended in a sense... In cases where one has multiple constructors, it is often easier to create an initialization method, which can be called from each constructor, which reduces code repetition. However, for members variables that are of class-types, one has to use a constructor to initialize them as well:

cpp

class Tail
{
public:
enum TailType { Short, Long };
Tail(TailType t) { .... }
};

class Cat
{
public:
Cat() : tail(Tail::Short)
{
// Do nothing
}
private:
Tail tail;
};


You can't call tail's constructor explicitly (at least in an obvious way) outside of a constructor.

There are a few properties of constructors that make them rather desirable:

1.) Constructors are automatically called when they need to be. One needs not rely on one's tendency to remember to do something, which is almost always a good thing.

2.) Explicitly defined copy-constructors for classes that require deep copying (i.e. those that hold on to pointers or sometimes in the case of handles) usually makes life easier for the programmer. Instead of having to duplicate by calling a method, or passing by reference (which at times is not desireable), a copy-constructor (a specialized method that the compiler recognizes) is called to actually do the work.

For example, I have class X { public: int* y; };

X v1;
v1.y = new int;
X v2;
// This copies the pointer value, not the value pointed to by member X::y
v1 = v2;

3.) RAII (resource acquisition is initialization) isn't nearly as foolproof without them. Don't worry if you've never seen this terminology... it tends to come later for most.

User is offlineProfile CardPM
+Quote Post

KYA
RE: The Practical Difference Of Constructors Over Member Functions
2 Jun, 2008 - 03:52 PM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,056



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Overloading constructors is good if you need to initialize with varying amounts of detail per object.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:18PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month