Thread: Constructor excercise

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40

    Constructor excercise

    This excercise is from C++ primer 5th edition.

    I have not understood everything about constructors, mainly about how they function.

    Work prior to the excercise was creating a class called screen with some specifications.
    This is the class:
    Code:
    class screen{
    public:
        using pos = string::size_type;
        screen() = default;
    private:
        pos height = 0;
        pos width = 0;
        pos cursor = 0;
        string contents;
    };
    The excercise goes as follows:
    Excercise 7.24: Give your Screen class three constructors: a defaultconstructor; 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.

    Giving the screen a default constructor was easy. The next part is probably easy aswell, I just dont understand what they mean when they say "and initalize the contents to hold the given number of blanks" and something in the 3rd part when they say "character to use as the contents of the screen". If anyone of you guys could explain it to me, it would make my day!

    Edit: Think I have made a breakthrough... Would the constructor for the second part look like this:

    screen(pos ht, pos wd) : contents(ht*wd) {}

    or something?
    Last edited by Iceboxes; 02-15-2014 at 12:18 PM.
    "Derp, derp, derp" - Me

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Iceboxes
    I just dont understand what they mean when they say "and initalize the contents to hold the given number of blanks"
    In other words, you need to make use of the values for height and width to call the appropriate string constructor to initialise the member variable named contents with blanks (presumably a "blank" is a space character).

    Quote Originally Posted by Iceboxes
    something in the 3rd part when they say "character to use as the contents of the screen"
    Instead of initialising contents with blanks, you would initialise it with the given character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You see that variable called contents? You need to set it equal to something. In the first case, you need to set it equal to ' ' repeated a bunch of times, and in the second case, equal to a given character repeated a bunch of times. Hence when you construct contents in your constructor (as part of the initializer list), you can use the constructor for std::string that sets the value equal to a character repeated a bunch of times.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Do I set "Contents" equal to something in the constructor, or am I supposed to do that when I declare it?

    Are these constructers what the book is asking for?

    Code:
    screen(pos ht, pos wd) : height(ht), width(wd), contents(ht*wd, ' ')    
    screen(pos ht, pos wd, char) : height(ht), width(wd), contents(ht*wd, c)
    "Derp, derp, derp" - Me

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Think of a constructor like this, if I have object in my private section in my class I must initialize them, or if I have pointers in my class "privates", I need a copy constructor.

    Code:
    class my_class
    {
      private:
                int a[10];
    }
    
    //a constructor would be used like this
    my_class::my_class()
    {
     for(int i=0;i<10;i++)
    {
         a[i]=0;
    }
    
    }
    
    //this code was not tested and it is just an example
    If the copy constructor confused you watch this:
    C++ Copy Constructors - YouTube

    he give a good down a dirty example.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    You made me understand constructors more, but moving on, I dont think I am supposed to use a copy constructor here.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Or a "function" outside the class

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Iceboxes
    Are these constructers what the book is asking for?
    I think so (since you only have one string, rather than a rectangular array of strings), except that you forgot to name the char parameter c and include the constructor body in both cases.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Quote Originally Posted by laserlight View Post
    forgot to name the char parameter c and include the constructor body in both cases.
    Was writing on my phone, so some details was easily forgotten >.>

    Thanks to everyone that helped me, you guys are awesome!!!!!
    "Derp, derp, derp" - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Who can improve this excercise?
    By tahmod in forum C Programming
    Replies: 24
    Last Post: 01-08-2012, 01:00 PM
  2. Excercise help again
    By gin in forum C++ Programming
    Replies: 17
    Last Post: 07-05-2008, 11:00 AM
  3. excercise
    By luigi40 in forum C# Programming
    Replies: 9
    Last Post: 11-22-2005, 03:25 AM
  4. Help with Excercise
    By Blanket in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2003, 05:21 PM
  5. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 10:18 AM