C++ Classes PDF
C++ Classes PDF
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
3
Object-Oriented Programming
• now that we’re using C++, we can start taking
advantage of object-oriented programming
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
6
OOP: Inheritance
• inheritance allows us to create and define
new classes from an existing class
7
OOP: Polymorphism
• polymorphism is when a single name
can have multiple meanings
– normally used in conjunction with inheritance
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);
16
Using a Struct
• if we want to print a date using the struct,
what should our function prototype be?
void PrintDate(DATE day);
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;
};
19
Morphing from Struct to Class
class date {
int month;
int day;
int year;
};
20
Morphing from Struct to Class
class Date {
int month;
int day;
int year;
};
21
Morphing from Struct to Class
class Date {
int m_month;
int m_day;
int m_year;
};
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
26
Functions in Classes
• unlike structs, classes have member functions
along with their member variables
27
Functions in Classes
• unlike structs, classes have member functions
along with their member variables
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();
32
OutputMonth() Prototype
void OutputMonth();
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();
40
Using the Date Class
Date today;
41
Using the Date Class
Date today;
variable today is an
instance of the class Date
42
Using the Date Class
Date today;
43
Using the Date Class
Date today;
44
Using the Date Class
Date today;
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
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
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?
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
69
Constructor Definition
70
Constructor Definition
m_month = m;
m_day = d;
m_year = y;
}
71
Constructor Definition
76
Overloading
• we can define multiple versions of the
constructor – we can overload it
77
All Known Values
• have the constructor set user-supplied values
78
All Known Values
• have the constructor set user-supplied values
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
82
Some Known Values
• have the constructor set some default values
83
Overloaded Date Constructor
• so far we have the following constructors:
84
Overloaded Date Constructor
• so far we have the following constructors:
85
Avoiding Multiple Constructors
• defining multiple constructors for different
sets of known values is a lot of unnecessary
code duplication
86
Default Parameters
• in the function prototype only, provide default
values you want the constructor to use
87
Default Parameters
• in the function prototype only, provide default
values you want the constructor to use
88
Default Parameters
• in the function definition nothing changes
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
93
Default Constructors
• but, if you create any other constructor, the
compiler doesn’t provide a default constructor
Date::Date ()
{
/* empty */
} 94
Function Overloading
• functions in C++ are uniquely identified by
both their names and their parameters
– but NOT their return type!
95
Overloading Example
void PrintMessage (void) {
cout << “Hello World!” << 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()
99