Constructors

Got a bunch of questions about it, hopefully someone can help me out. These are exercises from a book.

1) Give your Screen class three constructors:

- a default constructor;
- a constructor that takes values for height and width and initializes the contents to hold the given number of blanks;
- and a constructor that takes values for height, width and a character to use as the contents of the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef SCREEN_H_INCLUDED
#define SCREEN_H_INCLUDED

class Screen
{
    public:
        Screen() = default;
        Screen(pos ht, pos wt) {std::string ctt = ' '}
        Screen(pos ht, pos wt, std::string ctt = 'a')

        using pos = string::size_type;
    private:
        pos cursor = 0;
        pos height = 0, width = 0;
        std::string contents;
};


#endif // SCREEN_H_INCLUDED 


Did I get it right?

2) Can someone please explain the operators being used in the following?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Sales_data
{
    public:
        Sales_data() = default;
        Sales_data(const std::string &s): bookNo(s) { } //Over here
        Sales_data(const std::string &s, unsigned n, double p):  //Over here
                   bookNo(s), units_sold(n), revenue(p*n) { } //Over here
        Sales_data(std::istream &);
        std::string isbn() const { return bookNo; }
        Sales_data &combine(Sales_data&);
    private:
        std::string bookNo;
        unsigned units_sold = 0;
        double revenue = 0.0;

};


What does the colon actually do?

3) Lastly, there's one exercise that says:

"Add constructors to your Sales_data class and write a program to use each of the constructors".

What does it mean to use a constructor? As far as I understand, constructors are used to initialize class member functions.
Last edited on
The colon is basically "assign on creation", just like:
 
int a = 5;


instead of
1
2
int a;
a = 5;

So it's a little faster/more efficient.

I don't think you got the ctor right for the Screen class, first of all, the passed values need to be assigned to internal data members, or else they'll just be on the stack of the constructor, where they will be removed after the ctor ends.

You could make it: Screen(pos ht, pos wt) : height(ht), width(wt), contents(' ') {}
You are also declaring a new string in the ctor, why is that? It'll just be on the stack for a second and doesn't do anything.

Your third constructor has no body, it should be:

Screen(pos ht, pos wt, std::string ctt = 'a') : height(ht), width(wt), contents(ctt){}


2.

The operators used like in bookNo(s) is equivalent to bookNo = s, but we use the () operator in class assignment there.

3.

You could use a constructor through operator overloading:

1
2
3
Sales_data obj1; // uses the default ctor
Sales_data obj2("BoohYeah"); // Calls the second ctor
Sales_data obj3("Foo", 12, 18.20); // Calls the third ctor for the object. 
Last edited on
Constructor can be overloaded to give proper response based on what the input is. Other than that i am not familiar of any other use for it aside from initializing methods.
Thanks for the quick reply, clears many things up.

I definitely think I got my constructors wrong =).

...first of all, the passed values need to be assigned to internal data members...


Is that where the colon comes into play? So are constructors like gate-keepers to the data members? And when I pass arguments I'm passing it through the constructor?

You are also declaring a new string in the ctor, why is that? It'll just be on the stack for a second and doesn't do anything.


Do you mean when I did...

{std::string ctt = ' '}

...that? Its because of ignorance. Still not very sure how constructor function bodies work.

Lastly, I suppose the use of the colon is pretty much a must have to make things faster/more efficient. Would the constructor still work without the use of the colon?
Yes, the colon is completely optional, because you can ALSO assign in the body of the constructor {}.

Exactly right on the gatekeeper analogy :), you pass data through the constructors, and you use that data to set up the internals of the class object.

The function body of the constructor is just like any other function, except that it has access to everything inside the class object, and the data you sent as arguments. The body is used for more complex (non-assign) operations.
Alright got it, thank you so much Bourgond =). Too much technical mumbo jumbo used to explain things in this book, my mind needs simpler methods to "get it".

Appreciate the input frank, helps simplifies understanding constructors for the time being.
Topic archived. No new replies allowed.