Classes and Structs in C++: Based On Materials by Bjarne Stroustrup
Classes and Structs in C++: Based On Materials by Bjarne Stroustrup
2
Classes
• The idea:
– A class directly represents a concept in a program
• If you can think of “it” as a separate entity, it is plausible that
it could be a class or an object of a class
• Examples: vector, matrix, input stream, string, FFT, valve
controller, robot arm, device driver, picture on screen, dialog
box, graph, window, temperature reading, clock
– A class is a (user-defined) type that specifies how
objects of its type can be created and used
– In C++ (as in most modern languages), a class is the
key building block for large programs
• And very useful for small ones also
– The concept was originally introduced in Simula67
3
Members and member access
• One way of looking at a class;
class X { // this class’ name is X
// data members (they store information)
// function members (they do things, using the information)
};
• Example
class X {
public:
int m; // data member
int mf(int v) { int old = m; m=v; return old; } // function member
};
X var; // var is a variable of type X
var.m = 7; // access var’s data member m
int x = var.mf(9); // call var’s member function mf()
4
Classes
6
Struct and class
• A struct is a class where members are public by default:
struct X {
int m;
// …
};
• Means
class X {
public:
int m;
// …
};
my_birthday.y = 12;
my_birthday.m = 30;
my_birthday.d = 1950; // oops! (no day 1950 in month 30)
// later in the program, we’ll have a problem
8
Date:
Structs my_birthday: y
m
// simple Date (with a few helper functions for convenience) d
struct Date {
int y,m,d; // year, month, day
};
// helper functions:
void init_day(Date& dd, int y, int m, int d); // check for valid date and initialize
init_day(my_birthday, 12, 30, 1950); // run time error: no day 1950 in month 30
9
Date:
Structs my_birthday: y 1950
m 12
// simple Date d
30
// guarantee initialization with constructor
// provide some notational convenience
struct Date {
int y,m,d; // year, month, day
Date(int y, int m, int d); // constructor: check for valid date and initialize
void add_day(int n); // increase the Date by n days
};
// …
Date my_birthday; // error: my_birthday not initialized
Date my_birthday(12, 30, 1950); // oops! Runtime error
Date my_day(1950, 12, 30); // ok
my_day.add_day(2); // January 1, 1951
my_day.m = 14; // ouch! (now my_day is a bad date)
10
Date:
Classes my_birthday: y 1950
m 12
// simple Date (control access) d 30
class Date {
int y,m,d; // year, month, day
public:
Date(int y, int m, int d); // constructor: check for valid date and initialize
// access functions:
void add_day(int n); // increase the Date by n days
int month() { return m; }
int day() { return d; }
int year() { return y; }
};
// …
Date my_birthday(1950, 12, 30); // ok
cout << my_birthday.month() << endl; // we can read
my_birthday.m = 14; // error: Date::m is private
11
Classes
12
Date:
Classes my_birthday: y 1950
m 12
// simple Date (some people prefer implementation details last) d 30
class Date {
public:
Date(int y, int m, int d); // constructor: check for valid date and initialize
void add_day(int n); // increase the Date by n days
int month();
// …
private:
int y,m,d; // year, month, day
};
Date::Date(int yy, int mm, int dd) // definition; note :: “member of”
:y(yy), m(mm), d(dd) { /* … */ }; // note: member initializers
void Date::add_day(int n) { /* … */ }; // definition
13
Date:
Classes my_birthday: y 1950
m 12
// simple Date (some people prefer implementation details last) d 30
class Date {
public:
Date(int y, int m, int d); // constructor: check for valid date and initialize
void add_day(int n); // increase the Date by n days
int month();
// …
private:
int y,m,d; // year, month, day
};
Month m = feb;
m = 7; // error: can’t assign int to Month
int n = m; // ok: we can get the numeric value of a Month
Month mm = Month(7); // convert int to Month (unchecked)
17
Enumerations
Month m1 = jan;
Month m2 = red; // error red isn’t a Month
Month m3 = 7; // error 7 isn’t a Month
int i = m1; // ok: an enumerator is converted to its value, i==0
18
Enumerations – Values
• By default
// the first enumerator has the value 0,
// the next enumerator has the value “one plus the value of the
// enumerator before it”
enum { horse, pig, chicken }; // horse==0, pig==1, chicken==2
19
Classes Date:
1950
// simple Date (use Month type) my_birthday: y
class Date { m 12
public:
d 30
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
Date(int y, Month m, int d); // check for valid date and initialize
// …
private:
int y; // year
Month m;
int d; // day
};
Date my_birthday(1950, 30, Date::dec); // error: 2nd argument not a Month
Date my_birthday(1950, Date::dec, 30); // ok
20
Const
class Date {
public:
// …
int day() const { return d; } // const member: can’t modify
void add_day(int n); // non-const member: can modify
// …
};
cout << d.day() << " – " << cd.day() << endl; // ok
d.add_day(1); // ok
cd.add_day(1); // error: cd is a const
21
Const
//
Date d(2004, Date::jan, 7); // a variable
const Date d2(2004, Date::feb, 28); // a constant
d2 = d; // error: d2 is const
d2.add(1); // error d2 is const
d = d2; // fine
d.add(1); // fine
22
Const member functions
// Distinguish between functions that can modify (mutate) objects
// and those that cannot (“const member functions”)
class Date {
public:
// …
int day() const; // get (a copy of) the day
// …
void add_day(int n); // move the date n days forward
// …
};
23
Classes
• What makes a good interface?
– Minimal
• As small as possible
– Complete
• And no smaller
– Type safe
• Beware of confusing argument orders
– Const correct
24
Classes
n Essen%al
opera%ons
n Default
constructor
(defaults
to:
nothing)
n No
default
if
any
other
constructor
is
declared
n Copy
constructor
(defaults
to:
copy
the
member)
n Copy
assignment
(defaults
to:
copy
the
members)
n Destructor
(defaults
to:
nothing)
n For
example
Date
d;
//
error:
no
default
constructor
Date
d2
=
d;
//
ok:
copy
ini3alized
(copy
the
elements)
d
=
d2;
//
ok
copy
assignment
(copy
the
elements)
25
Interfaces and “helper functions”
26
Helper functions
Date next_Sunday(const Date& d)
{
// access d using d.day(), d.month(), and d.year()
// make new Date to return
}
Date next_weekday(const Date& d) { /* … */ }
29