CIS 190: C/C++ Programming
Lecture 8
Classes in C++
1
Outline
• Procedural Programming vs OOP
• Classes
– Example: Morphing from Struct
– Basics
– Access
– Constructors
– Overloading
• Livecoding
2
Procedural Programming
• up until now, everything we’ve been doing has
been procedural programming
• code is divided into multiple procedures
– procedures operate on data (structures), when
given correct number and type of arguments
• examples: PrintTrain(), ReadSingerFile(),
DestroyList(), ProcessEvents(), etc.
3
Object-Oriented Programming
• now that we’re using C++, we can start taking
advantage of object-oriented programming
• adding OOP to C was one of the driving forces
behind the creation of C++ as a language
– C++’s predecessor was actually
called “C with Classes”
4
Object-Oriented Programming
• in OOP, code and data are combined into a
single entity called a class
– each instance of a given class is an
object of that class type
• principles of Object-Oriented Programming
– encapsulation
– inheritance
– polymorphism
5
OOP: Encapsulation
• encapsulation is a form of information
hiding and abstraction
• data and functions that act on that data are
located in the same place (inside a class)
• ideal: separate the interface/implementation
so that you can use the former without any
knowledge of the latter
6
OOP: Inheritance
• inheritance allows us to create and define
new classes from an existing class
• this allows us to re-use code
– faster implementation time
– fewer errors
– easier to maintain/update
7
OOP: Polymorphism
• polymorphism is when a single name
can have multiple meanings
– normally used in conjunction with inheritance
• we’ll look at one form of polymorphism today:
– overloading functions
8
Outline
• Procedural Programming vs OOP
• Classes
– Example: Morphing from Struct
– Basics
– Access
– Constructors
– Overloading
• Livecoding
9
Example: Date
typedef struct date {
int month;
int day;
int year;
} DATE;
10
Parts of a Struct
typedef struct date {
name of the struct
int month;
int day;
int year;
} DATE;
11
Parts of a Struct
typedef struct date {
name of the struct
int month;
int day;
int year;
} DATE;
(optional) shorter
name via typedef
12
Parts of a Struct
typedef struct date {
name of the struct
int month;
int day; member variables
int year; of the structure
} DATE;
(optional) shorter
name via typedef
13
Using a Struct
• if we want to print a date using the struct,
what should our function prototype be?
____ PrintDate(________);
14
Using a Struct
• if we want to print a date using the struct,
what should our function prototype be?
void PrintDate(DATE day);
15
Using a Struct
• if we want to print a date using the struct,
what should our function prototype be?
void PrintDate(DATE day);
• if we want to change the year of a date,
what should our function prototype be?
____ ChangeYear(__________________);
16
Using a Struct
• if we want to print a date using the struct,
what should our function prototype be?
void PrintDate(DATE day);
• if we want to change the year of a date,
what should our function prototype be?
void ChangeYear(DATE day, int year);
17
Morphing from Struct to Class
typedef struct date {
int month;
int day;
int year;
} DATE;
18
Morphing from Struct to Class
struct date {
int month;
int day;
int year;
};
• remove the typedef – we won’t
need it for the class
19
Morphing from Struct to Class
class date {
int month;
int day;
int year;
};
• change struct to class
20
Morphing from Struct to Class
class Date {
int month;
int day;
int year;
};
• capitalize date – according to the style guide,
classes are capitalized, while structs are not
21
Morphing from Struct to Class
class Date {
int m_month;
int m_day;
int m_year;
};
• add m_ to the variable names – classes are
more complicated, this can help prevent
confusion about which vars are member vars
22
Morphing from Struct to Class
class Date {
public:
int m_month;
int m_day;
int m_year;
};
• make the variables public,
to be able to access them
– by default, members of a class are private 23
Morphing from Struct to Class
class Date {
public:
int m_month;
int m_day;
int m_year;
};
• syntax highlighted colors change
24
Outline
• Procedural Programming vs OOP
• Classes
– Example: Morphing from Struct
– Basics
– Access
– Constructors
– Overloading
• Livecoding
25
Functions in Classes
• unlike structs, classes have member functions
along with their member variables
• member functions go inside the class
declaration
• member functions are called on an object of
that class type
26
Functions in Classes
• unlike structs, classes have member functions
along with their member variables
• member functions go inside the class
declaration
• member functions are called on an object of
that class type
iStream.open(“file.txt”);
27
Functions in Classes
• unlike structs, classes have member functions
along with their member variables
• member functions go inside the class
declaration
• member functions are called on an object of
that class type
iStream.open(“file.txt”);
28
Example: OutputMonth() Function
• let’s add a function to the class that will print
out the name of the month
class Date {
public:
int m_month;
int m_day;
int m_year;
};
29
Example: OutputMonth() Function
• let’s add a function to the class that will print
out the name of the month
class Date {
public:
int m_month;
int m_day;
int m_year;
void OutputMonth();
};
30
Example: OutputMonth() Function
• let’s add a function to the class that will print
out the name of the month, given the number
class Date {
public:
int m_month;
int m_day;
int m_year;
void OutputMonth();
function
}; prototype
31
OutputMonth()
void OutputMonth();
• nothing is passed in to the function – why?
32
OutputMonth() Prototype
void OutputMonth();
• nothing is passed in to the function
• because it only needs access to see
the variable m_month
– which is a member variable of the Date class
– just like OutputMonth() is a member function
33
OutputMonth() Definition
void Date::OutputMonth() {
}
34
OutputMonth() Definition
void Date::OutputMonth() {
specify class name;
more than one class
can have a function
with the same name
}
35
OutputMonth() Definition
void Date::OutputMonth() {
this double colon is called
the scope resolution
operator, and associates
the member function
OutputMonth() with
the class Date
}
36
OutputMonth() Definition
void Date::OutputMonth() {
switch (m_month) {
case 1: cout << “January ”; break;
case 2: cout << “February ”; break;
case 3: cout << “March ”; break;
/* etc */
default:
cout << “Error in Date::OutputMonth”;
}
}
37
OutputMonth() Definition
void Date::OutputMonth() {
switch (m_month) {
case 1: cout
we can <<directly
“January ”; break;
access m_month because
case 2: cout
it is a<< “February
member variable of”;
the break;
Date class,
case 3: cout << OutputMonth()
to which “March ”; break; belongs
/* etc */
default:
cout << “Error in Date::OutputMonth”;
}
}
38
Print Functions
• is the following valid code?
cout << today.OutputMonth();
39
Print Functions
• is the following valid code?
cout << today.OutputMonth();
• no, because OutputMonth() returns
nothing for cout to print
– if the function returned a string, this would be
valid code
40
Using the Date Class
Date today;
41
Using the Date Class
Date today;
variable today is an
instance of the class Date
it is an object of type Date
42
Using the Date Class
Date today;
cout << “Please enter dates as DD MM YYYY”
<< endl;
cout << “Please enter today’s date: ”;
cin >> today.m_day >> today.m_month
>> today.m_year;
43
Using the Date Class
Date today;
cout << “Please enter dates as DD MM YYYY”
<< endl;
cout << “Please enter today’s date: ”;
cin >> today.m_day >> today.m_month
>> today.m_year;
when we are not inside the class (as we
were in the OutputMonth() function)
we must use the dot operator to access
today’s member variables
44
Using the Date Class
Date today;
cout << “Please enter dates as DD MM YYYY”
<< endl;
cout << “Please enter today’s date: ”;
cin >> today.m_day >> today.m_month
>> today.m_year;
cout << “Today’s date is “;
today.OutputMonth();
cout << today.m_day << “, “
<< today.m_year << endl;
45
Using the Date Class
Date today;
cout << “Please enter dates as DD MM YYYY”
<< endl;
we also use the dot operator to call the
memberenter
cout << “Please OutputMonth()
functiontoday’s date: on ”;
the Date object
cin >> today.m_day >> today
today.m_month
>> today.m_year;
again, note that we do not need to pass
in the member
cout << “Today’s is “; m_month
datevariable
today.OutputMonth();
cout << today.m_day << “, “
<< today.m_year << endl;
46
Outline
• Procedural Programming vs OOP
• Classes
– Example: Morphing from Struct
– Basics
– Access
– Constructors
– Overloading
• Livecoding
47
Access Specifiers
• in our definition of the Date class, everything
was public – this is not good practice!
• why?
48
Access Specifiers
• we have three different options for
access specifiers, each with their own role:
– public
– private
– protected
• specify access for members inside the class
49
Toy Example
class Date {
public:
int m_month;
private:
int m_day;
protected:
int m_year;
};
50
Using Public, Private, Protected
• public
– anything that has access to a Date object also has
access to all public member variables and functions
• not normally used for variables;
used for most functions
• need to have at least one item be public
51
Using Public, Private, Protected
• private
– private members variables and functions can only
be accessed by member functions of the Date
class; cannot be accessed in main(), etc.
• if not specified, members default to private
– should specify anyway – good coding practices!
52
Using Public, Private, Protected
• protected
– protected member variables and functions can only
be accessed by member functions of the Date
class, and by member functions of any derived
classes
– (we’ll cover this later)
53
Access Specifiers for Date Class
class Date {
???????:
void OutputMonth();
???????:
int m_month;
int m_day;
int m_year;
};
54
Access Specifiers for Date Class
class Date {
public:
void OutputMonth();
private:
int m_month;
int m_day;
int m_year;
};
55
New Member Functions
• now that m_month, m_day, and m_year
are private, how do we give them values, or
retrieve those values?
56
New Member Functions
• now that m_month, m_day, and m_year
are private, how do we give them values, or
retrieve those values?
• write public member functions to provide
indirect, controlled access for the user
– ideal: programmer only knows interface (public
functions) not implementation (private variables)
57
Member Function Types
• there are many ways of classifying types, but
here’s a few of the basics we’ll use:
• accessor functions
• mutator functions
• auxiliary functions
58
Member Functions: Accessor
• convention: start with Get
• allow retrieval of private data members
• examples:
int GetMonth();
int GetDay();
int GetYear();
59
Member Functions: Mutator
• convention: start with Set
• allow changing the value of a private data
member
• examples:
void SetMonth(int m);
void SetDay(int d);
void SetYear(int y);
60
Member Functions: Auxiliary
• provide support for the operations
– public if generally called outside function
– private/protected if only called by member functions
• examples:
void OutputMonth(); public
void IncrementDate(); private
61
Access Specifiers for Date Class
class Date {
public:
void OutputMonth();
int GetMonth();
int GetDay();
int GetYear();
void SetMonth(int m);
void SetDay (int d);
void SetYear (int y);
private:
int m_month;
int m_day;
int m_year;
};
62
Access Specifiers for Date Class
class Date {
public:
void OutputMonth();
int GetMonth();
int GetDay();
int GetYear();
for the sake of brevity,
void SetMonth(int m);
void SetDay (int d); we’ll leave out the
void SetYear (int y); accessor and mutator
private: functions from now on
int m_month;
int m_day;
int m_year;
};
63
Outline
• Procedural Programming vs OOP
• Classes
– Example: Morphing from Struct
– Basics
– Access
– Constructors
– Overloading
• Livecoding
64
Constructors
• special member functions used to create
(or “construct”) new objects
• automatically called when an object is created
– implicit: Date today;
– explicit: Date today(10, 15, 2014);
• initializes the values of all data members
65
Date Class Constructors
class Date {
public:
void OutputMonth();
Date (int m, int d, int y);
private:
int m_month;
int m_day;
int m_year;
};
66
Date Class Constructors
class Date {
public:
void OutputMonth();
Date (int m, int d, int y);
exact same
private:name as the
int m_month;
class
int m_day;
int m_year;
};
67
Date Class Constructors
class Date {
public:
void OutputMonth();
Date (int m, int d, int y);
no return
private:
type, not
intvoid
even m_month;
int m_day;
int m_year;
};
68
Constructor Definition
Date::Date (int m, int d, int y)
{
69
Constructor Definition
Date::Date (int m, int d, int y)
{
m_month = m;
m_day = d;
m_year = y;
}
70
Constructor Definition
Date::Date (int m, int d, int y)
{
m_month = m;
m_day = d;
m_year = y;
}
71
Constructor Definition
Date::Date (int m, int d, int y)
{
if (m > 0 && m <= 12) {
m_month = m; }
else { m_month = 1; }
if (d > 0 && d <= 31) {
m_day = d; }
else { m_day = 1; }
if (y > 0 && y <= 2100) {
m_year = y; }
else { m_year = 1; }
}
72
Constructor Definition
Date::Date (int m, int d, int y)
{
if (m > 0 && m <= 12) { is this the
m_month = m; } best way to
else { m_month = 1; } handle this?
if (d > 0 && d <= 31) {
m_day = d; }
else { m_day = 1; }
if (y > 0 && y <= 2100) {
m_year = y; }
else { m_year = 1; }
}
73
Constructor Definition
Date::Date (int m, int d, int y)
{
if (m > 0 && m <= 12) { is this the
m_month = m; } best way to
else { m_month = 1; } handle this?
if (d > 0 && d <= 31) {
m_day = d; } what might
else { m_day = 1; } be a better
if (y > 0 && y <= 2100) { solution?
m_year = y; }
else { m_year = 1; }
}
74
Constructor Definition
Date::Date (int m, int d, int y)
{
SetMonth(m);
SetDay(d);
SetYear(y);
}
• this allows us to reuse already written code
75
Outline
• Procedural Programming vs OOP
• Classes
– Example: Morphing from Struct
– Basics
– Access
– Constructors
– Overloading
• Livecoding
76
Overloading
• we can define multiple versions of the
constructor – we can overload it
• different constructors for:
– when all values are known
– when no values are known
– when some subset of values are known
77
All Known Values
• have the constructor set user-supplied values
Date::Date (int m, int d, int y)
{
SetMonth(m);
SetDay(d);
SetYear(y);
}
78
All Known Values
• have the constructor set user-supplied values
Date::Date (int m, int d, int y)
{ invoked when
SetMonth(m); constructor is called
SetDay(d); with all arguments
SetYear(y);
}
79
No Known Values
• have the constructor set all default values
Date::Date ()
{
SetMonth(1);
SetDay(1);
SetYear(1);
}
80
No Known Values
• have the constructor set all default values
Date::Date ()
invoked when
{ constructor is called
SetMonth(1); with no arguments
SetDay(1);
SetYear(1);
}
81
Some Known Values
• have the constructor set some default values
Date::Date (int m, int d)
{
SetMonth(m);
SetDay(d);
SetYear(1);
}
82
Some Known Values
• have the constructor set some default values
Date::Date (int m, int d)
{ invoked when
SetMonth(m); constructor is called
with two arguments
SetDay(d);
SetYear(1);
}
83
Overloaded Date Constructor
• so far we have the following constructors:
Date::Date (int m, int d, int y);
Date::Date (int m, int d);
Date::Date ();
84
Overloaded Date Constructor
• so far we have the following constructors:
Date::Date (int m, int d, int y);
Date::Date (int m, int d);
Date::Date ();
• would the following be a valid constructor?
Date::Date (int m, int y);
85
Avoiding Multiple Constructors
• defining multiple constructors for different
sets of known values is a lot of unnecessary
code duplication
• we can avoid this by setting default
parameters in our constructors
86
Default Parameters
• in the function prototype only, provide default
values you want the constructor to use
Date (int m , int d ,
int y );
87
Default Parameters
• in the function prototype only, provide default
values you want the constructor to use
Date (int m = 10, int d = 15,
int y = 2014);
88
Default Parameters
• in the function definition nothing changes
Date::Date (int m, int d, int y) {
SetMonth(m);
SetDay(d);
SetYear(y);
}
89
Using Default Parameters
• the following are all valid declarations:
Date graduation(5,18,2015);
Date today;
Date halloween(10,31);
Date july(4);
x
x
x
x4/6/2014
90
Using Default Parameters
• the following are all valid declarations:
Date graduation(5,18,2015);
Date today;
Date halloween(10,31);
Date july(4);
// graduation: 5/18/2015
// today: 10/15/2014
// halloween: 10/31/2014
// july: 4/15/2014
91
Using Default Parameters
• the following are all valid declarations:
Date graduation(5,19,2014);
Date today;
NOTE: when you
Date halloween(10,31);
call a constructor
Date july(4);
with no arguments,
you do not give it
// graduation:
empty5/19/2014
parentheses
// today: 10/15/2014
// halloween: 10/31/2014
// july: 4/15/2014
92
Default Constructors
• a default constructor is provided by compiler
– will handle declarations of Date instances
• this is how we created Date objects in the
slides before we declared and defined our
constructor
93
Default Constructors
• but, if you create any other constructor, the
compiler doesn’t provide a default constructor
• so if you create a constructor, make a default
constructor too, even if its body is just empty
Date::Date ()
{
/* empty */
} 94
Function Overloading
• functions in C++ are uniquely identified by
both their names and their parameters
– but NOT their return type!
• we can overload any kind of function
– we can even use default values,
like with constructors
95
Overloading Example
void PrintMessage (void) {
cout << “Hello World!” << endl;
}
void PrintMessage (string msg) {
cout << msg << endl;
}
96
Outline
• Procedural Programming vs OOP
• Classes
– Example: Morphing from Struct
– Basics
– Access
– Constructors
– Overloading
• Livecoding
97
Example: Rectangle Class
• width and height member variables
• accessors and mutators
• functions for IsSquare(), CalcArea(),
CalcPerim(), and PrintRectInfo()
• what happens when we give a constructor
that uses default parameters and calls
mutators invalid arguments?
LIVECODING 98
Parts of a Class (so far)
• class name private
• member variables public
protected
• member functions
– constructors
• default parameters
– accessors
– mutators
– auxiliary (private and public)
99