1,105,399 Community Members

constructors

Member Avatar
mc3330418
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey All! This isn't an important qusetion just an I don't get it question.
So, this is out of my book....this is a default constructor

clockType();

Ok. This is a constructor with parameters

clockType(int, int, int)

Thats all fine and good. Here is my quesiton. The default constructor is basically like initalizing variables, I understand that. What I don't understand is, would a real world program have a default constructor AND a constructor with parameters?

Member Avatar
NathanOliver
Posting Virtuoso
1,657 posts since Apr 2009
Reputation Points: 284 [?]
Q&As Helped to Solve: 310 [?]
Skill Endorsements: 4 [?]
 
2
 

Yes and no. It really depends on what you are trying to accomplish. If you look at the STL string it has both.

Member Avatar
deceptikon
Eternally Awesome
4,691 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

What I don't understand is, would a real world program have a default constructor AND a constructor with parameters?

Certainly, if there's a case where the class could initialize itself to a predictable state without help from the caller. One example might be an empty string, so std::string has a default constructor that initializes itself to the equivalent of "".

Member Avatar
mc3330418
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok. Another question I had with the constructor with parameters was, you enter the variables for that constructor. But then you have a set and get, so what are the advantages of useing the constructor wih parameters?

Member Avatar
rubberman
Senior Poster
3,999 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
0
 

Brevity? A default constructor, copy constructor, and assignment operator are created by the compiler by default, even if not specified by the developer of the class. However, the member variables and such are not necessarily constructed as you may want (caveate programmer). As for the constructor with arguments, consider this:

class 2dpoint {
private:
    int x_pos;
    int y_pos;
public:
    2dpoint() : x_pos(0), y_pos(0) {}   // Default ctor
    2dpoint(int x, int y) : x_pos(x), y_pos(y) {}
.
.
.
};

So, you have a dynamic game, and you want to position something at a specific position when constructed, some other element may look at it in the wrong place (0,0). So, you create it where you want it to be viewed/positioned

Member Avatar
Moschops
Nearly a Posting Virtuoso
1,383 posts since Sep 2008
Reputation Points: 414 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 16 [?]
Featured
 
1
 

But then you have a set and get,

WARNING: THE FOLLOWING CONTAINS OPINION

Why would you have a set and a get? Providing a set and get for everything is a reflex action I seem to see a lot in Java code, but one of the whole points of a class is that it takes care of its own state. If you simply provide a set and get for every private variable inside it, what's the point?

Member Avatar
mc3330418
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Opinions encourage!

Let me clarify, I'm in my second level c++ class and the book and "teacher" both use the default constructor, a constructor with parameters and a get and set. I'm use to using a constructor and then using a get and set in Java. That makes sense to me. The constructor intiatlizes and then you get info from the user, get and set. There is a program in our book that we had to do and it had all 4 of those things and the whole time I said this is dumb and overkill. In the real world you always have a default constructor, and at some point you will need to get values so you will need a get and set. This is just me trying to process informaation. Like is said I encourage opinions. I'll never learn if I don't get different ideas and processes.

Member Avatar
Moschops
Nearly a Posting Virtuoso
1,383 posts since Sep 2008
Reputation Points: 414 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 16 [?]
Featured
 
1
 

In the real world you always have a default constructor, and at some point you will need to get values so you will need a get and set.

More opinion; I frequently write classes without a default constructor (which thus forces the coder to provide some values at creation - sometimes a class simply doesn't have a sensible default state and if I don't provide a default constructor, I can ensure that the class will be in a sensible state from creation) and I try very hard to avoid having to write setters and getters. A class should be responsible for its own internals as far as possible and if I'm using a class for something as mundane as storing public data, I'll firstly make it a struct (which is identical to a class except for default privacy, but there is a strong convention in C++ to use a struct for plain old data without functions) and secondly rethink the design and see if I can come up with something more elegant.

Member Avatar
mc3330418
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Moschops, how do you get info from the user if you don't use set and get? From what I understand you have to get the info and then assign it to the class variable. Is that why you use a struct?

Member Avatar
Moschops
Nearly a Posting Virtuoso
1,383 posts since Sep 2008
Reputation Points: 414 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

Moschops, how do you get info from the user if you don't use set and get?

There is an awful lot more to programming than getting numbers and letters from a keyboard, storing them and putting them back out to the monitor.

For example, I might write a class that fetches data from the internet regarding stock prices and tells me the peak and low prices in a given date range. I would never feed it all the data myself, and maybe it doesn't even hold the peak and low values; it could just calculate them on the fly and return them.

// create new stockWatcher with constructor - identifies stock to gather data on and provides
//     start and end date
stockWatcherObject newStockWatcher("GOOG", start_date, end_date);

double peak, low;
// populates the variables peak and low with peak and low values
newStockWatcher.calculatePeakAndLow(&peak, &low);
Member Avatar
mc3330418
Junior Poster in Training
55 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ahh, see now you're using real world usage case. I have only encountered class assignments and that is why I am trying so hard to grasp concepts. I'm trying to relate them to real world programming.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article