C classes in depth
Download
1 / 55

C++ Classes in Depth - PowerPoint PPT Presentation


  • 189 Views
  • Uploaded on

C++ Classes in Depth. Topics. Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects. Objectives. At the completion of this topic, students should be able to:. Design classes for use in a C++ program

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' C++ Classes in Depth' - keenan


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Topics
Topics

Designing Your Own Classes

Attributes and Behaviors

Writing Classes in C++

Creating and Using Objects


Objectives
Objectives

At the completion of this topic, students should be able to:

Design classes for use in a C++ program

Explain the difference between a class and an object

Explain what attributes and behaviors are

Explain the terms encapsulation and data hiding

Create a program that uses a programmer designed class



Objects are used to model real-world things.

You can think of an object as a rather complex

variable that contains multiple chunks of data


Let’s consider an object

that represents a time.


Real world objects have attributes
Real world objects have attributes

An object’s attributes describe its

“state of being”

What things do we normally associate with the state of a time?

minutes

seconds

am/pm

hours


We hide the data from anyone outside

of the object by making it “private”

Joe WageEarner

12356

$12.50

40

A Time object


The attributes of an object are

stored inside the object as

data members.

The hour value

is stored as an integer

3

The minute value

is stored as an integer

35

pm

am or pm is

stored as a string

An “Time” object


An object also has behaviors
An object also has behaviors

behaviors define how you interact with the object

What can you do with a time?

set the

hours

Get the hours


Behaviors are expressed as methods that

give us controlled access to the data.

These are “public” so that we can see them.

Joe WageEarner

12356

getHour ( )

setHour( )

$12.50

40

An “Time” object


Then we write statements that

send messages to the objects

Joe WageEarner

joe.getHours( );

12356

getHour ( )

setHour ( )

$12.50

40

An “Time” object named joe



Encapsulation
Encapsulation

Time object

getHour( )

member methods are

declared as public

calling method

we should not allow code

outside of the object to reach in and change the data directly. Instead, we call methods in the

object to do it for us.

hour

minute

member data is declared as private

The terms public and private are called access modifiers


A Class is a blueprint that a program

uses when it creates an object.

A class reserves no space in memory

When an object is created from the class

blueprint, memory is reserved to hold the

object’s attributes.

An object is known as an instance of the class.

Each object has it’s own space and data.


A class is said to be an abstraction of the

real world object that we are modeling.

Classes are sometimes called

Abstract Data Types.


We use a UML Class Diagram to document

the data and methods contained in

our class.


A UML class diagramis used

to describe a class in a very precise

way.

A class diagram is a rectangle.

At the top of the rectangle is the

class name. A line separates the

class name from the rest of the

diagram.

Time


Time

Following the class name we write

the data members of the class. A

line separates the data members

from the rest of the diagram.

- hour: int

access modifier:

+ public

- private

data member name

data type


Time

Following the data members, we

write the member methods.

  • hour: int

  • minute: int

+ getHour( ): int

access modifier

+ public

- private

method name

return type

parameters


It is important that class diagrams be drawn

precisely and that they conform to the form

shown in these examples.


In C++ the class definition usually goes

into a separate file called the header file.

It has a file extension of .h

time.h

class Time

{

private:

int hour;

int minutes;

string amOrPm

public:

Time( );

intgetHour( ) const;

intgetMinutes( ) const;

string getAmOrPm( );

void setHour( int n);

void setMinutes( int n);

void setAmOrPm(string a);

};


In C++ the class definition usually goes

into a separate file called the header file.

It has a file extension of .h

time.h

class Time

{

private:

int hour;

int minutes;

string amOrPm

public:

Time( );

intgetHour( ) const;

intgetMinutes( ) const;

string getAmOrPm( );

void setHour( int n);

void setMinutes( int n);

void setAmOrPm(string a);

};

Notice that getter functions (functions

that do not change the state of the

object) are declared as const functions.


Declaring Member Data

Member data is always “private”

class Time

{

private:

int hour;

int minutes;

string amOrPm;

}

Indent each line

inside the block

variable name

data type


Declaring Member Data

class Time

{

private:

int hour;

int minutes;

string amOrPm;

}

We call data members of a class “Instance Data”

because each instance (object) of the class

will contain its own unique copy of this data.


Declaring Member Methods

public class Time

{

private:

. . .

public:

Time( );

intgetHour( ) const;

intgetMinutes( ) const;

string getAmOrPm( ) const;

void setHour( int n);

void setMinutes( int n);

void setAmOrPm(string a);

Member methods are

usually public

When would you make a

method private?

These are called

function prototypes

return type

parameters

function name


In C++ the code that implements the member

functions of the class goes into a related file

with the file extension .cpp

time.cpp

always #include

the .h file for

this class

#include “time.h”

. . .

string Time::getHour( ) const

{

return hour;

}

void Time::setHour( double n)

{

hour = n

}

. . .

Class name followed by ::


Setter”Methods

they are usually named

“set” plus the name of the

instance variable they

will store the value in.

void Time::setHour(int hr)

{

hour = hr;

}

setters never

return anything

setters always take

a parameter


Getter”Methods

getters take no

parameters

getters always

return something

int Time::getHour( ) const

{

return hour;

}

The const keyword

is required to tell the

compiler that the function

does not alter the object.

they are usually named

“get” plus the name of the

instance variable they

will return the value of.


The methods of a class

are the public services or the public interface that

this class offers to its clients.

Clients are neither aware of nor involved in the implementation

details of these methods. Clients care what a method does, but

not how it does it.


Static data
Static Data

Every instance object has its own copy of all of

the instance variables in a class.

What if you want to have all objects of a class

share a single instance of a variable? For example,

all savingsAccount objects might have the same

interest rate.

To do this, declare the variable as

private static double interestRate = 0.0725;


Static methods
Static Methods

A static method can be called without creating

an object of the class that it belongs to. Static

methods don’t operate on data in an object unless

the data itself is declared as static. When you

invoke a static method you use the name

of the class instead of an object name.


The this reference
The this reference

Every instance object contains a variable named this

that is a pointer to itself.

The keyword is often used when you want to

explicitly name a variable as a data member of

this object. For example, in the Time class we have

discussed is a method

void setHour( inthr)

{

hour = hr;

}


The this reference1
The this reference

Using the this reference we could write:

void setHour( inthr)

{

this->hour = hr;

}

More to come on pointers and “this” in a few weeks.



Class definition

Time startTime;

class Time

{

private:

int hour;

int minute;

. . .

}

this statement takes the Time class

definition and uses it to create

the object “startTime”.

When creating the object, storage

is allocated for the each of the data

members defined in the class.

hour

minute

startTime


Constructors

When an object is created, the data members inside

the object are not initialized. They take on the values

in the bits in the memory that the object occupies.

This is pretty dangerous, so we initialize the data inside

an object by using a constructor. The constructor gets

called when the object is created.


Constructors

A constructor has the

same name as the class.

No return type

is mentioned

Time::Time( )

{

hour = 0;

minutes = 0;

}

If you do not write a constructor, the compiler creates

one for you. It takes no parameters and its body is

empty, i.e. it does nothing.


You can also write a

parameterized

constructor.

Constructors

Time::Time(inthr, int min )

{

hour = hr;

minutes = min;

}

If you write a parameterized constructor, you must also

write a non-parameterized one. The non-parameterized

constructor is called the default constructor.



parameter

startTime.setHour(8);

message

.

method

name

object

name

hour

minute

startTime


parameter

startTime.setHour(8);

This statement send the setHour

message to the object named startTime.

As the method executes, the value of

the parameter hr is stored in the

instance variable hour.

message

hour

minute

startTime

void setHour(int hr)

{

hour = hr;

}


The code that creates objects and sends messages to

them is typically written in main( ) in what we call a

driver. This code is saved in a separate .cpp file.

driver.cpp

always #include

the .h file for

any class used in main

#include <iostream>

#include “time.h”

using namespace std;

int main( )

{

Time newTime;

. . .

myTime.setHour(3);

. . .

}



Classes model real world things, like …

* an employee

* a student

* a car

* a rectangle

* a circle

* a bowling team

. . .


* A Rectangle

What words would you use

to describe a rectangle, i.e.

what are its attributes?

These become data members of the class.

it’s width

it’s height

it’s color


Draw a Class Diagram

Rectangle

  • width: int

  • height: int


* A Rectangle

What can I do with/to a Rectangle?

These become member functions of the class.

create one

change it’s height or width

calculate it’s area


Draw a Class Diagram

Rectangle

  • width: int

  • height: int

  • color: string

+ Rectangle(:int, :int, :string)

+ setHeight(:int)

+ setWidth(:int)

+ getArea( ): int


Create the .h File

class Rectangle

{

private:

int height;

int width;

string color;

public:

Rectangle ( );

Rectangle (int, int, string);

void setWidth( int );

void setHeight( int );

intgetArea( );

};

For brevity I have not included the function prologues


Design Summary

  • List

  • attributes

  • operations

Idea of a

Rectangle

Class

Diagram

.h File


The following code creates

a Rectangle object using a

non-parameterized constructor.

Rectangle joe;

Don’t do this!

Rectangle joe( );


When an object is created like this

Employee joe;

the employee object is stored on

the stack. It is a local variable.


Passing Objects as Parameters

We always want to pass objects by reference.

To keep from having side effects, pass by

constant reference. For example:

void printRectangle(const Rectangle&);


Lab #3 will give you some practice

designing a class and writing

a program in C++ to use it.


ad