0% found this document useful (0 votes)
85 views13 pages

COEN 244 - 3 - Introduction To Classes and Object

The document introduces object-oriented programming and classes. It discusses how programming languages can be categorized into procedural languages that focus on actions and object-oriented languages that focus on objects. It also describes how object-oriented design models software after real-world objects by encapsulating attributes and behaviors into objects. Classes are used to create objects, with data members storing attributes and function members storing behaviors. The implementation of a class is separated from its definition for reusability, with the definition in a header file and implementation in a source code file.

Uploaded by

Maxime PAULOIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views13 pages

COEN 244 - 3 - Introduction To Classes and Object

The document introduces object-oriented programming and classes. It discusses how programming languages can be categorized into procedural languages that focus on actions and object-oriented languages that focus on objects. It also describes how object-oriented design models software after real-world objects by encapsulating attributes and behaviors into objects. Classes are used to create objects, with data members storing attributes and function members storing behaviors. The implementation of a class is separated from its definition for reusability, with the definition in a header file and implementation in a source code file.

Uploaded by

Maxime PAULOIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

1

3
Introduction to
Classes and
Objects

2008 Pearson Education, Inc. All rights reserved.

3.1 Introduction
Programming languages may be classified into
two categories,

. Procedural languages :
Focus is on actions ( verbs), Ex: walking,
running. Ex. languages: C, Pascal.

Object-0riented languages:

Focus is on objects (nouns), Ex: cars, planes,


elevators, houses. Ex. languages: C++, Java.

2008 Pearson Education, Inc. All rights reserved.

We live in a world of objects. Object-oriented design


(OOD) models software similar to those that people
describe the real-world objects. OOD performs
information hiding and software reuse.
- Objects have attributes ( e.g. shape, color, size)
and behavior ( e.g. ball rolls, car runs, plane flies).
- OOD encapsulates attributes and operations
(behavior) into objects.
- In C++, the unit of programming is class while in C is
a function. Classes perform information hiding.
Classes have two types of members,
- Data members to store attributes
- Function members to store behavior
- Classes are used to create objects.

2008 Pearson Education, Inc. All rights reserved.

Typically Programs will consist of,

- Function main
- One or more classes
Each class consists of two parts:
1. Class definition:
- Define data members
- Prototypes of member functions
2. Class implementation
- It gives implementation of
member functions

2008 Pearson Education, Inc. All rights reserved.

Class Members
Attributes
Exist throughout the life of the object
Represented as data members
Variables in a class definition

Each object of class maintains its own copy of attributes

Local variables
Variables declared in a function definitions body
Cannot be used outside of that function body

When a function terminates


The values of its local variables are lost

2008 Pearson Education, Inc. All rights reserved.

Class Member Access Specifiers

Access-specifier: private
Makes a data member or member function accessible only
to member functions of the class
private is the default access for class members
Data hiding

Access-specifier: public
A data member or member function will be accessible also
to the clients of the class

Returning a value from a function


A function that specifies a return type other than void
Returns a value to its calling function

2008 Pearson Education, Inc. All rights reserved.

3.8 Placing a Class in a Separate File


for Reusability

Source code file: Class implementation is stored in a


file known as a source code file and file name has an
extension .cpp .
Header files

Class definitions are placed in separate files known as


header files.
Allow compiler to recognize the classes when used elsewhere

Generally have .h filename extensions

Driver files
Program used to test software (such as classes)
Contains a main function so it can be executed

2008 Pearson Education, Inc. All rights reserved.

1 //

Fi g. 3. 11: Gr adeBook. h

Outline

2 / / Gr adeBook cl ass defi ni t i on. Thi s fi l e pr esent s Gr adeBook' s publ i c


3 / / i nt er f ace wi t hout r eveal i ng t he i mpl ement at i ons of Gr adeBook' s
member
4 / / f unct i ons, whi ch ar e defi ned i n Gr adeBook. cpp.
5 #i ncl ude <st r i ng> / /

cl ass Gr adeBook uses C++ st andar d st r i ng cl ass

6 usi ng st d: : st r i ng;
7
8 //

fig03_11.cpp

Interface contains data members (1 of 1)


and member function prototypes

Gr adeBook cl ass defi ni t i on

9 cl ass Gr adeBook
10 {
11 publ i c :
12

Gr adeBook( st r i ng ) ;

//

const r uct or t hat i ni t i al i zes cour seName

13

voi d set Cour seName( st r i ng ) ;

14

st r i ng get Cour seName( ) ;

15

voi d di spl ayMessage( ) ;

//
//

//

f unct i on t hat set s t he cour se name

f unct i on t hat get s t he cour se name


f unct i on t hat di spl ays a wel come message

16 pr i vat e:
17

st r i ng cour seName; / /

18 }; / /

cour se name f or t hi s Gr adeBook

end cl ass Gr adeBook

2008 Pearson Education,


Inc. All rights reserved.

Outline

GradeBook implementation is
placed in a separate source-code file

fig03_12.cpp

(1 of 2)
Include the header file to access
the class name GradeBook

Binary scope resolution operator


ties a function to its class

2008 Pearson Education,


Inc. All rights reserved.

Outline

10

fig03_12.cpp

(2 of 2)

2008 Pearson Education,


Inc. All rights reserved.

Outline

11

fig03_13.cpp

(1 of 1)

2008 Pearson Education,


Inc. All rights reserved.

3.9 Separating Interface from


Implementation (Cont.)

12

The Compilation and Linking Process


Source-code file is compiled to create the classs object code
(source-code file must #include header file)
Class implementation programmer only needs to provide
header file and object code to client

Client must #include header file in their own code


So compiler can ensure that the main function creates and
manipulates objects of the class correctly

To create executable application


Object code for client code must be linked with the object
code for the class and the object code for any C++ Standard
Library object code used in the application

2008 Pearson Education, Inc. All rights reserved.

13

Fig.3.14 | Compilation and linking process that produces an executable application.

2008 Pearson Education, Inc. All rights reserved.

You might also like