1 of 72

C++ Classes (Introduction)

C. Papachristos

Robotic Workers Lab

University of Nevada, Reno

CS-202

2 of 72

Course , Projects , Labs:

Your 2nd Project Deadline is this Wednesday 2/5.

  • PASS Sessions held Friday-Sunday-&-Monday-Wednesday, get all the help you need!

  • 24-hrs delay after Project Deadline incurs 20% grade penalty.
  • Past that, NO Project accepted. Better send what you have in time!

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

3 of 72

Today’s Topics

CS-202 C. Papachristos

C++ Classes

  • Definitions
  • Declaration, Implementation
  • Members, Methods
  • Usage, Coding Standards

Classes as Abstract Data Types

Protection Mechanisms

4 of 72

Abstraction

CS-202 C. Papachristos

Programming Abstraction

All programming languages provide some form of Abstraction.

  • In its most basic form also called “Information Hiding”.
  • Separates code use from code implementation.

In C and Procedural Programming:

  • Data Abstraction: Data Structures. struct somethingComplex{ … };
  • Control Abstraction: Functions. void makeItHappen( … );

In C++ and Object-Oriented Programming

  • Data and Control Abstraction: Using Classes

5 of 72

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

6 of 72

Remember: Procedural vs Object-Oriented

CS-202 C. Papachristos

Procedural

Focused on the question: “What should the program do next?” Structure program by:

  • Splitting into sets of tasks and subtasks.
  • Make functions for tasks.

  • Data and operations are not bound to each other

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.

  • “The world is made up of interacting objects”.

  • Data and operations are bound to each other.

A collection of Objects

A hierarchy of functions

7 of 72

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

8 of 72

Remember: Procedural vs Object-Oriented

CS-202 C. Papachristos

Procedural

“What should the program do next?”

  • Calculate the area of a circle given the specified radius.
  • Sort this class list given an array of students.
  • Calculate the car’s expected mileage given its gas and road conditions.

Object-Oriented (OO)

Self-sufficient, modular, interacting pieces of code.

A collection of Objects

A hierarchy of functions

9 of 72

Remember: Procedural vs Object-Oriented

CS-202 C. Papachristos

Procedural

“What should the program do next?”

  • Calculate the area of a circle given the specified radius.
  • Sort this class list given an array of students.
  • Calculate the car’s expected mileage given its gas and road conditions.

Object-Oriented (OO)

Self-sufficient, modular, interacting pieces of code.

  • Circle, you know your radius, what is your area?

  • Class list, sort your students.

  • Car, when will you run out of gas on this trip?

A collection of Objects

A hierarchy of functions

10 of 72

Object-Oriented Programming

CS-202 C. Papachristos

Basic Principles

Data Abstraction:

  • Details of what exact attributes (data members) exist within the “Abstract Data Type (ADT)” (e.g., the Class) are not known to the user.

Information Hiding:

  • Details of how operations (behaviors – functions) are implemented are not known to the user of the Class.

Encapsulation:

  • Bring together data and operations under the same roof/shell (while keeping implementation details hidden).

11 of 72

Object-Oriented Programming

CS-202 C. Papachristos

Classes Etymology: () κλάσις

According to the dictionary:

  • “A kind or category.”
  • “A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.”

A “Class” according to OOP principles:

  • A group of objects with similar properties, common behavior, common relationships with other objects, and common semantics.
  • We use Classes for Abstraction purposes.

12 of 72

Classes

CS-202 C. Papachristos

Blueprints

Classes are “blueprints” for “instantiating” (creating) Objects.

  • A Dog Class to instantiate dog1 , dog2, dogN Objects.
  • A Shoe Class to instantiate shoe1 , shoe2, shoeN Objects.
  • A Car Class to instantiate car1 , car2, carN Objects.

The blueprint defines:

  • The Class’s state/attributes as class member variables .

  • The Class’s operations as class methods .

13 of 72

Classes

CS-202 C. Papachristos

Objects

Variables of Class types may be created just like variables of built-in types:

  • Each instance of a class is called an “Object” of that Class type.
  • Using a set of Car blueprints we can create a “my_car” Object.

We can create as many instances of a Class as needed:

  • Just like a regular data type, int, float, etc.
  • There can be more than one Dog, Car, Shoe (and might differ a lot)!

The challenge is to properly define Classes and create Objects that address solving our programming problems:

  • Do we need a Car class? (Do we need to represent such an entity?)
  • What should it be able to do?
  • How would it do that?

14 of 72

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?

  • How the Car manufacturing chain works in order to acquire one? → No!
  • How a car operates internally in order to drive one? → No!

All we need to know is:

  • How to request a specific Car from the company (e.g., dealership).

How to get one?

  • How a car’s pedals, signals, switches, and steering wheel work.

How to operate one?

15 of 72

Classes

CS-202 C. Papachristos

1. Class public Interface

The requests you can make of an Object are determined by its interface.

  • How to get one?
  • How to operate one?

Car Class

Acquisition price/scheme 

 Operate steering wheel

Operate gas pedal

Operate brake pedal

Operate clutch

Operate transmission

Switch lights

Type

Interface

16 of 72

Classes

CS-202 C. Papachristos

2. Class Implementation

What actually lies inside the Class. It is the:

  • Code,
  • Hidden Data,

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.

  • In the OOP context, the class user is sending a request message to the Object, which should respond by executing the appropriate code.
  • “The world is made up of interacting objects”.

17 of 72

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

18 of 72

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

19 of 72

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

20 of 72

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

21 of 72

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:

  • Real-time search for Declaration.
  • Auto-completion, Function alternatives.

But:

  • Learn to adopt a set of conventions (not rules), same as with every other language.

This is already Italicized !

22 of 72

Classes

CS-202 C. Papachristos

Class Conventions

Class names:

  • Always begin with capital letter.
  • Use CamelCase for phrases.
  • General word for Class (Type) of Objects.

Examples: Car, Shoe, Dog, StudyBook, BoxOfDVDs, …

class Car

{

};

23 of 72

Classes

CS-202 C. Papachristos

Class Conventions

Class data (member variables):

  • Always begin names with m_ (stands for “member”).

Examples: float m_fuel, char * m_title, …

Note: Another widespread convention is to end the name with underscore:

Examples: float fuel_, char * title_, …

  • Use camelCase (lowercase initial).

class Car

{

float m_currGallons;

float m_currMileage;

};

24 of 72

Classes

CS-202 C. Papachristos

Class Conventions

Class operations/methods:

  • Use camelCase (lowercase initial).

Examples: addGas(), accelerate(), modifyTitle(), removeDVD(), …

class Car

{

bool addGas(float gallons);

float getMileage();

};

25 of 72

Classes

CS-202 C. Papachristos

Encapsulation

Main principle in Object-Oriented Design / Programming.

  • A form of “Information Hiding” and Abstraction.

How:

  • Data and Functions that act on that data are located in the same place.
  • Encapsulated inside the Class.

Goal:

  • Separate Interface from Implementation.

Someone can still use the code without any knowledge of how it works!

26 of 72

Classes

CS-202 C. Papachristos

Encapsulation

Classes encapsulate both Data and Functions.

  • Class definitions must contain both!

Member Variables are the Data of a Class.

  • Its attributes, characteristics, an Object’s state.

(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()

27 of 72

Classes

CS-202 C. Papachristos

Class Components – 5 Crucial Questions

Member variables:

  • What data are necessary to fully define an Object’s State?

Class Methods/Member Functions:

  • What functions are necessary for the Object to perform all Internal Mechanics?
  • What functions are necessary Interact with the Object?

Constructor(s):

  • How do you build an instance (create an Object)?

Destructor:

  • How do you clean up an after an instance (after Object is destroyed)?

For later …

28 of 72

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

29 of 72

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

30 of 72

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;

};

31 of 72

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?

32 of 72

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?

33 of 72

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:

  • Goes into Class header file.

<ClassName.h>

<DayOfYear.h>

Class Definition:

  • Goes into Class source file.

<ClassName.cpp>

<DayOfYear.cpp>

34 of 72

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

35 of 72

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

36 of 72

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

37 of 72

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

38 of 72

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?

39 of 72

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

40 of 72

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

41 of 72

Classes

CS-202 C. Papachristos

A Class’ Place

A Class type is fully-fledged C++ Type :

  • It is just a User-Defined type (in contrast to a primitive (built-in) type int, double, etc.)

Hence, we can have Variables of a Class Type:

  • We simply call them “Objects”.

Therefore, we can have Function Parameters of a Class Type !

  • Pass-by-Value.
  • Pass-by-Reference.
  • Pass-by-Address.

42 of 72

Classes

CS-202 C. Papachristos

Pass-by-Value

Hence, we can also have Function Parameters of a Class derivatives:

  • Function Parameter by-Value.

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;

};

43 of 72

Classes

CS-202 C. Papachristos

Pass-by-Value

Hence, we can also have Function Parameters of a Class derivatives:

  • Function Parameter by-Value.

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 !

44 of 72

Classes

CS-202 C. Papachristos

Pass-by-Reference

Hence, we can also have Function Parameters of a Class derivatives:

  • Function Parameter by-Reference.

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;

};

45 of 72

Classes

CS-202 C. Papachristos

Pass-by-Reference

Hence, we can also have Function Parameters of a Class derivatives:

  • Function Parameter by-Reference.

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 !

46 of 72

Classes

CS-202 C. Papachristos

Pass-by-Address

Hence, we can also have Function Parameters of a Class derivatives:

  • Function Parameter by-Address.

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;

};

47 of 72

Classes

CS-202 C. Papachristos

Pass-by-Address

Hence, we can also have Function Parameters of a Class derivatives:

  • Function Parameter by-Address.

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 !

48 of 72

Classes

CS-202 C. Papachristos

Encapsulation

In one sense, it means “bringing together under the same roof/shell”:

  • Declare & Define a Class.

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();

49 of 72

Classes

CS-202 C. Papachristos

Encapsulation

In one sense, it means “bringing together as one”:

  • Declare & Define a Class Get an Object.

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();

50 of 72

Classes

CS-202 C. Papachristos

Encapsulation

In one sense, it means “bringing together as one”:

  • Declare & Define a Class Get an Object.
  • The Object is an “Encapsulation” of: a) Data values, b) Data operations.

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();

51 of 72

Classes

CS-202 C. Papachristos

Encapsulation

Class Methods do not need to be passed information about that Class Object!

  • Remember how the DayOfYear::output() Method has no parameters?

void DayOfYear::output(){

cout << m_month << "/" << m_day;

}

52 of 72

Classes

CS-202 C. Papachristos

Encapsulation

Class Methods do not need to be passed information about that Class Object!

  • Remember how the DayOfYear::output() Method has no parameters?

void DayOfYear::output(){

cout << m_month << "/" << m_day;

}

Member Functions are called on a Class Object.

  • They know everything about that object already. Why?
  • It is the Object itself that applies the Data operations (Method).

It is the one that contains the Data, and its class contains the code.

DayOfYear july4th, november19th;

july4th . output();

november19th . output();

53 of 72

Classes

CS-202 C. Papachristos

Encapsulation

Class Methods do not need to be passed information about that Class Object!

  • Remember how the DayOfYear::output() Method has no parameters?

void DayOfYear::output(){

cout << m_month << "/" << m_day;

}

Member Functions are called on a Class Object.

  • They know everything about that object already. Why?
  • It is the Object itself that applies the Data operations (Method).

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

54 of 72

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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;

}

55 of 72

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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 = …;

}

}

56 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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 = …;

}

}

57 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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.

58 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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;

};

59 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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;

};

60 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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;

};

61 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms – The const Qualifier

The keyword const for Member Function(s):

  • Member Functions have direct access to ALL Member Variables.
  • Use const function signature to “promise” it won’t change Member Data.

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;

};

62 of 72

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()

63 of 72

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!

64 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms – Access Specifiers

Access Specifiers syntax style:

Can mix & match public & private order in a Class declaration:

  • But, more typically we place public first (the Class Interface).
  • Allows easy viewing of portions that actually can be used by programmers using the Class.

private data is “hidden”, so irrelevant to users of Class.

  • Outside of Class definition, cannot change (or access) private data.

65 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms

Accessor & Mutator Functions:

Object needs to “do something” with its data !

  • Accessor Member Functions.

An Object-Interface to read Member Data.

Typically: “getMember” Functions.

  • Mutator Member 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()

66 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms

Accessor & Mutator Functions:

Object needs to “do something” with its data !

  • Accessor Member Functions.

An Object-Interface to read Member Data.

Typically: “getMember” Functions.

  • Mutator Member 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()

67 of 72

Classes

CS-202 C. Papachristos

Protection Mechanisms

Accessor & Mutator Functions:

Object needs to “do something” with its data !

  • Accessor Member Functions.

An Object-Interface to read Member Data.

Typically: “getMember” Functions.

  • Mutator Member 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()

68 of 72

Overview

CS-202 C. Papachristos

Abstract (User-Defined) Data Types

The concept of “Programming Abstraction” :

  • Programmers don’t (need to) know the details!

Abbreviated “ADT” :

  • An ADT is a collection of data values together with set of basic operations defined for the values, ADTs are often language-independent.
  • In C++ we ADTs are implemented with Classes.

A C++ Class “defines” the ADT.

A Data Structure :

  • An ADT implementation within a programming language.

69 of 72

Abstract (User-Defined) Data Types

Interface: A wall of ADT operations that isolates a Data Structure:

  • from the program that actually uses it.

Overview

CS-202 C. Papachristos

DataStructure

Member Vars:

Class Methods:

add()

contains()

remove()

()

MyProgram

Variables:

Functions:

Important Actions:

End.

Add

Contains

Remove

70 of 72

Overview

CS-202 C. Papachristos

Abstract Data Types & Encapsulation (Reminder)

Main principle in Object-Oriented Design / Programming.

  • A form of “Information Hiding” and Abstraction.

How:

  • Data and Functions acting on that data are placed in same code unit.
  • Encapsulated inside the Class.

Goal:

  • Separate Interface from Implementation.

Keep state separate from users via private Data, public Member Functions.

Someone can still use the code without any knowledge of how it works!

71 of 72

Overview

CS-202 C. Papachristos

Encapsulation (a correlation to Classes)

Any data type has specifications regarding its:

  • Data (representation of binary Data).
  • Operations (that can be performed on binary Data).

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:

  • But Data & their Ranges are specified by user/programmer/you(!),� and operations to be allowed on that Data (and their implementation)� by you(!) as well.

72 of 72

Time for Questions !

CS-202

CS-202 C. Papachristos