Object-Oriented Programming Using C++

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
EC-241 Object-Oriented Programming
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Object-Oriented Programming
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Using Structures and Classes COSC 1557 C++ Programming Lecture 4.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 CSC241: Object Oriented Programming Lecture No 06.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Procedural and Object-Oriented Programming
Programming Logic and Design Seventh Edition
Programming Logic and Design Seventh Edition
Examples of Classes & Objects
Pointers, Polymorphism, and Memory Allocation
COMPUTER 2430 Object Oriented Programming and Data Structures I
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Introduction to Classes
Object-Oriented Programming
Section 11.1 Class Variables and Methods
Lecture 4-7 Classes and Objects
Object Oriented Analysis and Design
Abstract Data Types and Encapsulation Concepts
CS212: Object Oriented Analysis and Design
Object Based Programming
Introduction to Classes
Classes and Data Abstraction
Object-Oriented Programming
Classes & Objects: Examples
Object-Oriented Programming Using C++ Second Edition
Object-Oriented Programming Using C++ Second Edition
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object-Oriented Programming
Chapter 7: User-Defined Functions II
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object-Oriented Programming Using C++
Object-Oriented Programming
Submitted By : Veenu Saini Lecturer (IT)
Defining Classes and Methods
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS3 C++ Classes.
Chapter 5 Classes.
Presentation transcript:

Object-Oriented Programming Using C++ Chapter 4 Using Classes

Objectives Create classes Learn about encapsulating class components Implement class functions Use private functions and public data Use the scope resolution operator with class fields and functions Use static class members Learn about the this pointer Understand the advantages of polymorphism

Creating Classes A class is a category of objects; it is a new data type Classes provide a description of an object Classes provide a convenient way to group related data and the functions that use the data When you create an object from the class, you automatically create all the related fields You think about them and manipulate them as real-life classes and objects Abstract data type (ADT): a type that you define

Creating Classes (continued) Student aSophomore; aSophomore.idNum = 7645; cout<<aSophomore.idNum; Error! By default, all members of a class are private

Creating Classes (continued) Access modifier

Encapsulating Class Components To encapsulate components is to contain them Encapsulation is an example of a black box An interface intercedes between you and the inner workings of an object

Designing Classes If you need a class for students, you should ask: What shall we call it? What are its attributes? What methods are needed by Student? Any other methods? In most cases, you declare both fields and functions You declare a field using a data type and an identifier You declare a function by writing its prototype, which serves as the interface to the function

Designing Classes To instantiate an object is to declare or create it Student aSophomore; aSophomore.displayStudentData(); A function that uses your class is a class client

Implementing Class Functions When you construct a class, you create two parts: Declaration section: contains the class name, variables (attributes), and function prototypes Implementation section: contains the functions Use both the class name and the scope resolution operator (::) when you implement a class function

Implementing Class Functions (continued)

Using Public Functions to Alter Private Data

Using Public Functions to Alter Private Data (continued) …

Using Private Functions and Public Data

Considering Scope when Defining Member Functions

Considering Scope when Defining Member Functions (continued)

Using Static Class Members When a class field is static, only one memory location is allocated All members of the class share a single storage location for a static data member of that same class When you create a non-static variable within a function, a new variable is created every time you call that function When you create a static variable, the variable maintains its memory address and previous value for the life of the program

Defining Static Data Members Since it is not const, anyone can modify it

Defining Static Data Members (continued) Static variables are sometimes called class variables, class fields, or class-wide fields because they don’t belong to a specific object; they belong to the class

Using Static Functions A static function can be used without a declared object Non-static functions can access static variables (provided there is an object) Static functions cannot access non-static variables

Using Static Functions (continued)

Understanding the this Pointer …

Understanding the this Pointer (continued)

Understanding the this Pointer (continued) The this pointer holds the memory address of the current object that is using the function The this pointer is automatically supplied when you call a non-static member function of a class For example, clerk.displayValues(); Is actually displayValues(&clerk); The actual argument list used by the compiler for displayValues() is displayValues(Employee *this) The this pointer is a constant pointer

Using the this Pointer Explicitly

Using the Pointer-to-Member Operator

Understanding Polymorphism Polymorphism is the object-oriented program feature that allows the same operation to be carried out differently depending on the object For example, clerk.displayValues(); shirt.displayValues(); XYZCompany.displayValues();

Understanding Polymorphism (continued)

You Do It: Creating and Using a Class class CollegeCourse { private: string department; int courseNum; int seats; public: void setDepartmentAndCourse(string, int); void setSeats(int); void displayCourseData(); };

void College::setDepartmentAndCourse(string dept, int num) { } void College::setSeats(int seats) void College::displayCourseData()

Using a static Field class Letter { private: string title; string recipient; static int count; public: void setRecipient(string, string); void displayGreeting(); static void displayCount(); };

Understanding How static and Non-static Fields are Stored

Summary A class is a category of objects When you create a class, you hide, or encapsulate, the individual components When you construct a class, you create the declaration section and the implementation section When you create a class, usually you want to make data items private, and to make functions public The scope resolution operator (::) identifies a member function as being in scope within a class

Summary (continued) Each class object gets its own block of memory for its data members You can access a static, class-wide field using a static function One copy of each class member function is stored no matter how many objects exist Within any member function, you can explicitly use the this pointer to access the object’s data fields Polymorphism allows the same operation to be carried out differently depending on the object