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.