C++ Classes (Introduction)
C. Papachristos
Robotic Workers Lab
University of Nevada, Reno
CS-202
Course , Projects , Labs:
Your 2nd Project Deadline is this Wednesday 2/5.
Monday | Tuesday | Wednesday | Thursday | Friday | | Sunday |
|
|
| Lab (8 Sections) |
| | |
| CLASS | PASS Session | CLASS |
| | |
PASS Session | | Project DEADLINE | NEW Project | PASS Session | | PASS Session |
Course Week
CS-202 C. Papachristos
Today’s Topics
CS-202 C. Papachristos
C++ Classes
Classes as Abstract Data Types
Protection Mechanisms
Abstraction
CS-202 C. Papachristos
Programming Abstraction
All programming languages provide some form of Abstraction.
In C and Procedural Programming:
In C++ and Object-Oriented Programming
Remember: Procedural vs Object-Oriented
CS-202 C. Papachristos
Procedural
Procedural Decomposition:
Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded.
Focus on: Processes.
Object-Oriented (OO)
Object-Oriented Design:
Identifies various objects composed of data and operations, that can be used together to solve the problem.
Focus on: Data Objects.
A collection of Objects
A hierarchy of functions
Remember: Procedural vs Object-Oriented
CS-202 C. Papachristos
Procedural
Focused on the question: “What should the program do next?” Structure program by:
Object-Oriented (OO)
Package-up self-sufficient modular pieces of code.
Pack away details into boxes (objects) keep them in mind in their abstract form.
A collection of Objects
A hierarchy of functions
Remember: Procedural vs Object-Oriented
CS-202 C. Papachristos
Procedural
“What should the program do next?”
Object-Oriented (OO)
Self-sufficient, modular, interacting pieces of code.
A collection of Objects
A hierarchy of functions
Remember: Procedural vs Object-Oriented
CS-202 C. Papachristos
Procedural
“What should the program do next?”
Object-Oriented (OO)
Self-sufficient, modular, interacting pieces of code.
A collection of Objects
A hierarchy of functions
Remember: Procedural vs Object-Oriented
CS-202 C. Papachristos
Procedural
“What should the program do next?”
Object-Oriented (OO)
Self-sufficient, modular, interacting pieces of code.
A collection of Objects
A hierarchy of functions
Object-Oriented Programming
CS-202 C. Papachristos
Basic Principles
Data Abstraction:
Information Hiding:
Encapsulation:
Object-Oriented Programming
CS-202 C. Papachristos
Classes Etymology: (ἡ) κλάσις
According to the dictionary:
A “Class” according to OOP principles:
Classes
CS-202 C. Papachristos
Blueprints
Classes are “blueprints” for “instantiating” (creating) Objects.
The blueprint defines:
Classes
CS-202 C. Papachristos
Objects
Variables of Class types may be created just like variables of built-in types:
We can create as many instances of a Class as needed:
The challenge is to properly define Classes and create Objects that address solving our programming problems:
Classes
CS-202 C. Papachristos
1. Class public Interface
The requests you can make of an Object are determined by its interface.
Do we need to know?
All we need to know is:
How to get one?
How to operate one?
Classes
CS-202 C. Papachristos
1. Class public Interface
The requests you can make of an Object are determined by its interface.
Car Class |
Acquisition price/scheme |
Operate steering wheel |
Operate gas pedal |
Operate brake pedal |
Operate clutch |
Operate transmission |
Switch lights |
… |
Type
Interface
Classes
CS-202 C. Papachristos
2. Class Implementation
What actually lies inside the Class. It is the:
that satisfy requests made to it (and/or its Objects).
Every request made of an Object must have an associated Method (i.e. Function) that will be called.
Classes
CS-202 C. Papachristos
Class Declaration
class Car
{
public:
bool addGas(float gallons);
float getMileage();
// other operations
private:
float m_currGallons;
float m_currMileage;
// other data
};
Class (Type) Name
Classes
CS-202 C. Papachristos
Class Declaration
class Car
{
public:
bool addGas(float gallons);
float getMileage();
// other operations
private:
float m_currGallons;
float m_currMileage;
// other data
};
Class (Type) Name
Protection Mechanism
Protection Mechanism
Classes
CS-202 C. Papachristos
Class Declaration
class Car
{
public:
bool addGas(float gallons);
float getMileage();
// other operations
private:
float m_currGallons;
float m_currMileage;
// other data
};
Class (Type) Name
Protection Mechanism
Protection Mechanism
Data
Classes
CS-202 C. Papachristos
Class Declaration
class Car
{
public:
bool addGas(float gallons);
float getMileage();
// other operations
private:
float m_currGallons;
float m_currMileage;
// other data
};
Class (Type) Name
Protection Mechanism
Protection Mechanism
Operations
Data
Classes
CS-202 C. Papachristos
Class Conventions
Standards for coding with Classes:
class Car
Integrated Development Environments (IDEs) can sometimes save the day with their smart features:
But:
This is already Italicized !
Classes
CS-202 C. Papachristos
Class Conventions
Class names:
Examples: Car, Shoe, Dog, StudyBook, BoxOfDVDs, …
class Car
{
…
};
Classes
CS-202 C. Papachristos
Class Conventions
Class data (member variables):
Examples: float m_fuel, char * m_title, …
Note: Another widespread convention is to end the name with underscore:
Examples: float fuel_, char * title_, …
class Car
{
…
float m_currGallons;
float m_currMileage;
};
Classes
CS-202 C. Papachristos
Class Conventions
Class operations/methods:
Examples: addGas(), accelerate(), modifyTitle(), removeDVD(), …
class Car
{
…
bool addGas(float gallons);
float getMileage();
};
Classes
CS-202 C. Papachristos
Encapsulation
Main principle in Object-Oriented Design / Programming.
How:
Goal:
Someone can still use the code without any knowledge of how it works!
Classes
CS-202 C. Papachristos
Encapsulation
Classes encapsulate both Data and Functions.
Member Variables are the Data of a Class.
(e.g. breed of Dog, size of Shoe, make of Car …)
Class Methods are used to act on that Data.
(e.g., play() with Dog, inspect() a Car, …)
BankAccount |
Member Vars: |
m_accountNr |
m_ownerName |
m_balance |
Class Methods: |
depositMoney() |
withdrawMoney() |
checkBalance() |
Classes
CS-202 C. Papachristos
Class Components – 5 Crucial Questions
Member variables:
Class Methods/Member Functions:
Constructor(s):
Destructor:
For later …
Classes by-Example
CS-202 C. Papachristos
Class Method(s) Declaration & Implementation
// Represents a Day of the Year
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
// Output method – displays a DayOfYear
void DayOfYear::output( )
{
cout << m_month << "/" << m_day;
}
Class Name
Access Specifier
Method(s)
Data
Classes by-Example
CS-202 C. Papachristos
Class Method(s) Declaration & Implementation
// Represents a Day of the Year
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
// Output method – displays a DayOfYear
void DayOfYear::output( )
{
cout << m_month << "/" << m_day;
}
Class Name
Access Specifier
Method(s)
Data
Method Prototype inside
Class Declaration
Method Implementation outside of Class Declaration
Classes by-Example
CS-202 C. Papachristos
Class Method Implementation
The Method Implementation:
// Output method – displays a DayOfYear
void DayOfYear::output( )
{
cout << m_month << "/" << m_day;
}
return type
Method Name & Parameters List
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
Classes by-Example
CS-202 C. Papachristos
Class Method Implementation
The Method Implementation:
// Output method – displays a DayOfYear
void DayOfYear::output( )
{
cout << m_month << "/" << m_day;
}
Class Name
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
Resolution Operator (::)
Indicates which Class Method this definition implements.
Simpler: Which Class does the defined function belong to?
Classes by-Example
CS-202 C. Papachristos
Class Method Implementation
The Method Implementation:
// Output method – displays a DayOfYear
void DayOfYear::output( )
{
cout << m_month << "/" << m_day;
}
Class Name
Method Body
has direct access to ALL
Class Member Variables
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
Resolution Operator (::)
Indicates which Class Method this definition implements.
Simpler: Which Class does the defined function belong to?
Classes by-Example
CS-202 C. Papachristos
Class Separation into Files
// Represents a Day of the Year
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
// Output method – displays a DayOfYear
void DayOfYear::output( )
{
cout << m_month << "/" << m_day;
}
Class Declaration:
<ClassName.h>
<DayOfYear.h>
Class Definition:
<ClassName.cpp>
<DayOfYear.cpp>
Classes by-Example
CS-202 C. Papachristos
Class Separation into Files
// class DayOfYear Header file
#ifndef DAYOFYEAR_H_
#define DAYOFYEAR_H_
/** A class to represent a day of a year
*/
class DayOfYear
{
public:
/** Outputs the day (1-31) and the
* month (Jan-Dec) to the console
*/
void output();
// NOTE: Data members are normally
// private !!! (More on that later)
// private:
int m_month; //*< The month(1-12) */
int m_day; //*< The day (1-31) */
};
#endif
// class DayOfYear Source file
#include <iostream>
#include "DayOfYear.h"
void DayOfYear::output()
{
switch (m_month)
{
case 1: {
std::cout << "January ";
} break;
case 2: {
std::cout << "February ";
} break;
// … remaining cases …
case 12: {
std::cout << "December ";
} break;
default: {
std::cout << "Unrecognized month ... ";
}
}
std::cout << m_day << std::endl;
}
DayOfYear.h
DayOfYear.cpp
Classes by-Example
CS-202 C. Papachristos
Class Separation into Files
// Program main executable
#include <iostream>
#include "DayOfYear.h"
using namespace std;
int main()
{
DayOfYear today;
cout << "Enter today’s date:\n";
cout << "First the month (1-12): ";
cin >> today.m_month;
cout << "Then the day (1-31): ";
cin >> today.m_day;
cout << "Today the date is:\n";
today.output();�
return 0;
}
MainProgram.cpp
Classes by-Example
CS-202 C. Papachristos
Class Usage
Simple Instantiation and Member-access:
// Inside a main() somewhere
DayOfYear july4th;
july4th . m_month = 7;
july4th . m_day = 4;
july4th . output();
(Default) Constructor-based�Instantiation
Classes by-Example
CS-202 C. Papachristos
Class Usage
Simple Instantiation and Member-access:
// Inside a main() somewhere
DayOfYear july4th;
july4th . m_month = 7;
july4th . m_day = 4;
july4th . output();
Dot Operator (.) – Member-Access
Indicates which Object this Class Member references.
Simpler: The Member-of which Object?
Object
Name
Classes by-Example
CS-202 C. Papachristos
Class Usage
Simple Instantiation and Member-access:
// Inside a main() somewhere
DayOfYear july4th;
july4th . m_month = 7;
july4th . m_day = 4;
july4th . output();
Class Member Variables
&
Class Methods
Object
Name
Dot Operator (.) – Member-Access
Indicates which Object this Class Member references.
Simpler: The Member-of which Object?
Classes by-Example
CS-202 C. Papachristos
Class Usage
Member-access through Pointer:
// Inside a main() somewhere
DayOfYear july4th;
DayOfYear * july4th_Pt = &july4th;
july4th_Pt -> m_month = 7;
july4th_Pt -> m_day = 4;
july4th_Pt -> output();
Pointer to Class Type
Classes by-Example
CS-202 C. Papachristos
Class Usage
Member-access through Pointer:
// Inside a main() somewhere
DayOfYear july4th;
DayOfYear * july4th_Pt = &july4th;
july4th_Pt -> m_month = 7;
july4th_Pt -> m_day = 4;
july4th_Pt -> output();
Arrow Operator (->) – Pointer to Member-access
Class Pointer Dereference Operator (The C++ standard just calls it “arrow” (§5.2.5)).
Simpler: “Works out” similarly to Member-access (.), just through a Pointer !
Pointer to Class Type
Object
Pointer
Classes
CS-202 C. Papachristos
A Class’ Place
A Class type is fully-fledged C++ Type :
Hence, we can have Variables of a Class Type:
Therefore, we can have Function Parameters of a Class Type !
Classes
CS-202 C. Papachristos
Pass-by-Value
Hence, we can also have Function Parameters of a Class derivatives:
DayOfYear july4th;
july4th.m_month = 7; july4th.m_day = 4;
printNextDay(july4th);
void printNextDay(DayOfYear date){
date.m_day++;
if(date.m_day … && date.m_month …){
date.m_day = …;
date.m_month = …;
}
date.output();
}
class DayOfYear{
public:
void output();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
Pass-by-Value
Hence, we can also have Function Parameters of a Class derivatives:
DayOfYear july4th;
july4th.m_month = 7; july4th.m_day = 4;
printNextDay(july4th);
void printNextDay(DayOfYear date){
date.m_day++;
if(date.m_day … && date.m_month …){
date.m_day = …;
date.m_month = …;
}
date.output();
}
class DayOfYear{
public:
void output();
int m_month;
int m_day;
};
Note:
Will work with Local Object Copy !
Classes
CS-202 C. Papachristos
Pass-by-Reference
Hence, we can also have Function Parameters of a Class derivatives:
DayOfYear july4th;
july4th.m_month = 7; july4th.m_day = 4;
shiftNextDay(july4th);
july4th.output();
void shiftNextDay(DayOfYear & date){
date.m_day++;
if(date.m_day … && date.m_month …){
date.m_day = …;
date.m_month = …;
}
}
class DayOfYear{
public:
void output();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
Pass-by-Reference
Hence, we can also have Function Parameters of a Class derivatives:
DayOfYear july4th;
july4th.m_month = 7; july4th.m_day = 4;
shiftNextDay(july4th);
july4th.output();
void shiftNextDay(DayOfYear & date){
date.m_day++;
if(date.m_day … && date.m_month …){
date.m_day = …;
date.m_month = …;
}
}
class DayOfYear{
public:
void output();
int m_month;
int m_day;
};
Note:
Will modify Object Data !
Classes
CS-202 C. Papachristos
Pass-by-Address
Hence, we can also have Function Parameters of a Class derivatives:
DayOfYear july4th;
DayOfYear* july4th_Pt = &july4th;
shiftNextDay_Pt(july4th_Pt);
july4th.output();
void shiftNextDay_Pt(DayOfYear * date_p){
date_p->m_day++;
if(date_p->m_day … && date_p->m_month …){
date_p->m_day = …;
date_p->m_month = …;
}
}
class DayOfYear{
public:
void output();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
Pass-by-Address
Hence, we can also have Function Parameters of a Class derivatives:
DayOfYear july4th;
DayOfYear* july4th_Pt = &july4th;
shiftNextDay_Pt(july4th_Pt);
july4th.output();
void shiftNextDay_Pt(DayOfYear * date_p){
date_p->m_day++;
if(date_p->m_day … && date_p->m_month …){
date_p->m_day = …;
date_p->m_month = …;
}
}
class DayOfYear{
public:
void output();
int m_month;
int m_day;
};
Note:
Will modify Object Data !
Classes
CS-202 C. Papachristos
Encapsulation
In one sense, it means “bringing together under the same roof/shell”:
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
void DayOfYear::output(){
cout << m_month << "/" << m_day;
}
DayOfYear july4th;
july4th.m_month = 7;
july4th.m_day = 4;
july4th.output();
Classes
CS-202 C. Papachristos
Encapsulation
In one sense, it means “bringing together as one”:
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
void DayOfYear::output(){
cout << m_month << "/" << m_day;
}
DayOfYear july4th;
july4th.m_month = 7;
july4th.m_day = 4;
july4th.output();
Classes
CS-202 C. Papachristos
Encapsulation
In one sense, it means “bringing together as one”:
class DayOfYear
{
public:
void output();
int m_month;
int m_day;
};
void DayOfYear::output(){
cout << m_month << "/" << m_day;
}
DayOfYear july4th;
july4th.m_month = 7;
july4th.m_day = 4;
july4th.output();
Classes
CS-202 C. Papachristos
Encapsulation
Class Methods do not need to be passed information about that Class Object!
void DayOfYear::output(){
cout << m_month << "/" << m_day;
}
Classes
CS-202 C. Papachristos
Encapsulation
Class Methods do not need to be passed information about that Class Object!
void DayOfYear::output(){
cout << m_month << "/" << m_day;
}
Member Functions are called on a Class Object.
It is the one that contains the Data, and its class contains the code.
DayOfYear july4th, november19th;
july4th . output();
november19th . output();
Classes
CS-202 C. Papachristos
Encapsulation
Class Methods do not need to be passed information about that Class Object!
void DayOfYear::output(){
cout << m_month << "/" << m_day;
}
Member Functions are called on a Class Object.
It is the one that contains the Data, and its class contains the code.
DayOfYear july4th, november19th;
july4th . output();
november19th . output();
7 / 4
11 / 19
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
void DayOfYear::shiftNextDay(){
m_day++;
if(m_day … && m_month …){
m_day = …; m_month = …;
}
}
void DayOfYear::printDay() const{
cout << m_month <<
"/" << m_day;
}
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
void DayOfYear::printDay() const{
cout << m_month <<
"/" << m_day;
}
Classes
CS-202 C. Papachristos
“Promises” to leave Data untouched
void DayOfYear::shiftNextDay(){
m_day++;
if(m_day … && m_month …){
m_day = …; m_month = …;
}
}
Classes
CS-202 C. Papachristos
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
Makes no such “promise”.
void DayOfYear::printDay() const{
cout << m_month <<
"/" << m_day;
}
“Promises” to leave Data untouched
void DayOfYear::shiftNextDay(){
m_day++;
if(m_day … && m_month …){
m_day = …; m_month = …;
}
}
Classes
CS-202 C. Papachristos
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
void DayOfYear::printDay() const{
cout << m_month <<
"/" << m_day;
}
Note (more on this later in CS-202) :
In the body of a cv-qualified function, the this pointer is cv-qualified, e.g. in a const member function, only other const member functions may be called normally.
Classes
CS-202 C. Papachristos
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
DayOfYear july4th;
DayOfYear * july4th_Pt = &july4th;
july4th.shiftNextDay();
july4th.printDay();
july4th_Pt->shiftNextDay();
july4th_Pt->printDay();
Reminder (Syntax)
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
DayOfYear july4th;
DayOfYear * july4th_Pt = &july4th;
july4th.shiftNextDay();
july4th.printDay();
july4th_Pt->shiftNextDay();
july4th_Pt->printDay();
Reminder (Syntax)
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
DayOfYear july4th;
const DayOfYear * july4th_Pt = &july4th;
july4th.shiftNextDay();
july4th.printDay();
july4th_Pt->shiftNextDay();
july4th_Pt->printDay();
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
Protection Mechanisms – The const Qualifier
The keyword const for Member Function(s):
const DayOfYear july4th;
const DayOfYear * july4th_Pt = &july4th;
july4th.shiftNextDay();
july4th.printDay();
july4th_Pt->shiftNextDay();
july4th_Pt->printDay();
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
int m_month;
int m_day;
};
Classes
CS-202 C. Papachristos
Protection Mechanisms – Access Specifiers
Access Specifiers:
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
};
The CHANGE:
Data are now private.
Direct Object Interface to Member Data is broken!
DayOfYear |
Member Vars: |
m_month |
m_day |
Class Methods: |
printDay() |
shiftNextDay() |
Classes
CS-202 C. Papachristos
Protection Mechanisms – Access Specifiers
Access Specifiers:
class DayOfYear{
public:
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
};
DayOfYear july4th;
july4th.m_month = 7;
july4th.m_day = 4;
july4th.shiftNextDay();
july4th.output();
cout << july4th.m_day;
Impossible
Impossible
The CHANGE:
Data are now private.
Direct Object Interface to Member Data is broken!
Classes
CS-202 C. Papachristos
Protection Mechanisms – Access Specifiers
Access Specifiers syntax style:
Can mix & match public & private order in a Class declaration:
private data is “hidden”, so irrelevant to users of Class.
Classes
CS-202 C. Papachristos
Protection Mechanisms
Accessor & Mutator Functions:
Object needs to “do something” with its data !
An Object-Interface to read Member Data.
Typically: “getMember” Functions.
An Object-Interface to change Member Data.
Data manipulation, or “setMember” Functions, based on application.
DayOfYear |
Member Vars: |
m_month |
m_day |
Class Methods: |
getDay() |
setDay() |
shiftNextDay() |
Classes
CS-202 C. Papachristos
Protection Mechanisms
Accessor & Mutator Functions:
Object needs to “do something” with its data !
An Object-Interface to read Member Data.
Typically: “getMember” Functions.
An Object-Interface to change Member Data.
Data manipulation, or “setMember” Functions, based on application.
DayOfYear |
Member Vars: |
m_month |
m_day |
Class Methods: |
getDay() |
setDay() |
shiftNextDay() |
Classes
CS-202 C. Papachristos
Protection Mechanisms
Accessor & Mutator Functions:
Object needs to “do something” with its data !
An Object-Interface to read Member Data.
Typically: “getMember” Functions.
An Object-Interface to change Member Data.
Data manipulation, or “setMember” Functions, based on application.
DayOfYear |
Member Vars: |
m_month |
m_day |
Class Methods: |
getDay() |
setDay() |
shiftNextDay() |
Overview
CS-202 C. Papachristos
Abstract (User-Defined) Data Types
The concept of “Programming Abstraction” :
Abbreviated “ADT” :
A C++ Class “defines” the ADT.
A Data Structure :
Abstract (User-Defined) Data Types
Interface: A wall of ADT operations that isolates a Data Structure:
Overview
CS-202 C. Papachristos
DataStructure |
Member Vars: |
… |
… |
Class Methods: |
add() |
contains() |
remove() |
…() |
MyProgram |
Variables: |
… |
Functions: |
… |
Important Actions: |
… |
… |
End. |
Add
Contains
Remove
Overview
CS-202 C. Papachristos
Abstract Data Types & Encapsulation (Reminder)
Main principle in Object-Oriented Design / Programming.
How:
Goal:
Keep state separate from users via private Data, public Member Functions.
Someone can still use the code without any knowledge of how it works!
Overview
CS-202 C. Papachristos
Encapsulation (a correlation to Classes)
Any data type has specifications regarding its:
Example: The int data type.� Data: -2147483648 to 2147483647 (for 32-bit int – a.k.a. int32_t)� Operations: +, -, *, /, %, logical, etc.
The same holds with Classes:
Time for Questions !
CS-202
CS-202 C. Papachristos