Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Similar documents
Chapter 8 Objects and Classes

ECOM 2324 COMPUTER PROGRAMMING II

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

UFCE3T-15-M Object-oriented Design and Programming

A A B U n i v e r s i t y

OO Programming Concepts

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming in C#

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Lecture 18 Tao Wang 1

Chapter 4. Defining Classes I

Chapter 10 Introduction to Classes

Chapter 4 Defining Classes I

Abstraction in Software Development

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Design

Lecture #1. Introduction to Classes and Objects

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CS1004: Intro to CS in Java, Spring 2005

Object-Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

An Introduction to C++

Introduction Of Classes ( OOPS )

Lecture 7. Log into Linux New documents posted to course webpage

Encapsulation in C++

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Module 7 b. -Namespaces -Exceptions handling

ITI Introduction to Computing II

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CS1150 Principles of Computer Science Objects and Classes

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Chapter 3 Objects and Classes

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Cpt S 122 Data Structures. Introduction to C++ Part II

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Notes on Chapter Three

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

ITI Introduction to Computing II

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

CS304 Object Oriented Programming Final Term

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Introduction to Classes

Chapter 8 Objects and Classes Part 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 5: Classes and Objects in Depth. Introduction to methods

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Friends and Overloaded Operators

Polymorphism Part 1 1

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Object Oriented Programming

Lecture 7: Classes and Objects CS2301

Friends and Overloaded Operators

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

EEE-425 Programming Languages (2013) 1

Inheritance and Polymorphism

CS 2530 INTERMEDIATE COMPUTING

OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF -

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

EEE-425 Programming Languages (2013) 1

by Pearson Education, Inc. All Rights Reserved. 2

CMSC 202 Midterm Exam 1 Fall 2015


24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Fast Introduction to Object Oriented Programming and C++

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Implementing an ADT with a Class

Java Object Oriented Design. CSC207 Fall 2014

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Chapter 9. Objects and Classes

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Object Oriented Design

Implementing Abstract Data Types (ADT) using Classes

AN OVERVIEW OF C++ 1

Object-Oriented Programming Concepts

Chapter 12: How to Create and Use Classes

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

C++ Classes, Constructor & Object Oriented Programming

Programming II (CS300)

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Computer Science II (20073) Week 1: Review and Inheritance

Chapter 5: Classes and Objects in Depth. Information Hiding

G52CPP C++ Programming Lecture 9

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

III. Classes (Chap. 3)

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Programming II (CS300)

Anatomy of a Class Encapsulation Anatomy of a Method

Classes and Methods: Classes

Topic 10: Introduction to OO analysis and design

Transcription:

Chapter 9 Objects and Classes 1

Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation ( 9.5). To process strings using the C++ string class ( 9.8). Private data members with get and set functions ( 9.9). Scope of data members ( 9.10). Passing objects as arguments to functions ( 9.12). Arrays of Objects ( 9.13) 2

OO Programming Concepts An object represents an entity in the real world that can be distinctly identified. (eg) a student, a desk, a circle, a button, a loan.... An object has a unique identity, state, and behavior The state of an object is defined by its state variables (data members, properties). The behavior of an object is defined by its member functions (methods). 3

Objects An object has both a state and behavior. The state defines the object, and the behavior defines what the object does. 4

Classes A Class defines objects. A class defines data members and member functions. And a special functionsknown as constructors, which are invoked to construct objects from the class. An object is an instance of a class 5

Classes 6

UML Class Diagram 7

Constructors The constructor has exactly the same name as the defining class. Like regular functions, constructors can be overloaded (i.e., multiple constructors with the same name but different signatures), making it easy to construct objects with different initial data values. A class normally provides a constructor without arguments (e.g., Circle()). Such constructor is called a no-arg or no-argument constructor. A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class. 8

Constructor abbrev. ctor Default Ctor: a constructor with no parameters Ctors have the same name as the class itself. Ctors do not have a return type not even void. Ctors are called automatically when an object is declared. Ctors initialize objects. 9

Object Names A constructor is invoked when an object is created. The syntax to create an object using the no-arg constructor is ClassName variablename; Example: Circle circle1; //invokes Default Ctor 10

Constructing with Arguments Example Circle circle2(5.5); //invokes ctor that accepts one arg 11

Access Operator (.) Member-selection operator After an object is created, its data can be accessed and its functions invoked using the dot operator (.), also known as the member access operator: objectname.datamember references a data field in the object. objectname.function(arguments) invokes a function on the object. 12

A Simple Circle Class Objective: Demonstrate creating objects, accessing data, and using functions. TestCircle Run 13

Naming Objects and Classes Classes are user-defined types, so capitalize the first letter of each word in a class name: Circle, Rectangle, RomanNumeral. Objects are variables so use lower case. 14

Naming Conventions #include <iostream> using namespace std; class X{ //... }; int main (){ X x1, x2, x3; //... } 15

Memberwise Copy In C++, you can also use the assignment operator = to copy the contents from one object to the other. By default, each data field of one object is copied to its counterpart in the other object. For example, circle2 = circle1; copies the radius in circle1 to circle2. After the copy, circle1 and circle2 are still two different objects, but with the same radius. 16

Class Replaces struct The C language has the struct type for representing records. For example, you may define a struct type for representing students as shown in (a). 17

Separating Declaration from Implementation C++ allows you to separate class declaration from implementation. The class declaration describes the contract of the class and the class implementation implements the contract. The class declaration simply lists all the data fields, constructor prototypes, and the function prototypes. The class implementation implements the constructors and functions. The class declaration and implementation are in two separate files. Both files should have the same name, but with different extension names. The class declaration file has an extension name.h and the class implementation file has an extension name.cpp. Circle.h Circle.cpp Run 18

Inline Declaration 5.16, Inline Functions, introduced how to improve function efficiency using inline functions. Inline functions play an important role in class declarations. When a function is implemented inside a class declaration, it automatically becomes an inline function. This is also known as inline declaration. 19

Inline Declaration Example For example, in the following declaration for class A, the constructor and function f1 are automatically inline functions, but function f2 is a regular function. class A { public: A() { // do something; } double f1() { // return a number } double f2(); }; 20

Inline Functions in Implementation File There is another way to declare inline functions for classes. You may declare inline functions in the class s implementation file. For example, to declare function f2 as an inline function, precede the inline keyword in the function header as follows: // Implement function as inline inline double A::f2() { // return a number } 21

Inline Declarations? As noted in 5.16, short functions are good candidates for inline functions, but long functions are not. 22

Data Field Encapsulation The data fields radius in the Circle class in Listing 9.1 can be modified directly (e.g., circle1.radius = 5). This is not a good practice for two reasons: First, data may be tampered. Second, it makes the class difficult to maintain and vulnerable to bugs. Suppose you want to modify the Circle class to ensure that the radius is non-negative after other programs have already used the class. You have to change not only the Circle class, but also the programs that use the Circle class. Such programs are often referred to as clients. This is because the clients may have modified the radius directly (e.g., mycircle.radius = -5). 23

Accessor and Mutator Colloquially, a get function is referred to as a getter (or accessor), and a set function is referred to as a setter (or mutator). A get function has the following signature: returntype getpropertyname() If the returntype is bool, the get function should be defined as follows by convention: bool ispropertyname() A set function has the following signature: public void setpropertyname(datatype propertyvalue) 24

Example: New Circle Class Circle2.h Circle2.cpp Run 25

The Scope of Variables Chapter 5, Functions, discussed the scope of global variables and local variables. Global variables are declared outside all functions and are accessible to all functions in its scope. The scope of a global variable starts from its declaration and continues to the end of the program. Local variables are defined inside functions. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. 26

The Scope of Variables The data fields are declared as variables and are accessible to all constructors and functions in the class. In this sense, data fields are like global variables. However, data fields and functions can be declared in any order in a class. For example, all the following declarations are the same: 27

Passing Objects to Functions You can pass objects by value or by reference. PassObjectByValue PassObjectByReference PassObjectToPointer Run Run Run 28

Array of Objects Circle circlearray[3] = {Circle(3), Circle(4), Circle(5)}; TotalArea Run 29

Class Abstraction and Encapsulation Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user. 30

A Class named ABC has 2 parts: Specification ( ABC.h) Implementation ( ABC.cpp) A program that declares variables of type ABC is called a client EXAMPLES Rectangle Cylinder 31