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?

Recommended Answers

All 10 Replies

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

commented: string is an excellent example to use; default constructor is an empty string ready to be used, or you can construct it with letters already in it. Both obviously useful in so many situations. +8

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 "".

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?

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

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?

commented: I share that opinion. +13

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.

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.

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?

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);

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.