Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.

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 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 13: Object-Oriented Programming
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
1 Using Structures and Classes COSC 1557 C++ Programming Lecture 4.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
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.
1 CSC241: Object Oriented Programming Lecture No 06.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
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.
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.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Eine By: Avinash Reddy 09/29/2016.
Procedural and Object-Oriented Programming
Programming Logic and Design Seventh Edition
Chapter 7: User-Defined Functions II
Examples of Classes & Objects
COMPUTER 2430 Object Oriented Programming and Data Structures I
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Object-Oriented Programming
Chapter 15: Overloading and Templates
Section 11.1 Class Variables and Methods
Object-Oriented Programming Using C++
Introduction to Classes
Classes and Data Abstraction
Object-Oriented Programming
Classes & Objects: Examples
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
Object-Oriented Programming Using C++
Object-Oriented Programming
Submitted By : Veenu Saini Lecturer (IT)
Defining Classes and Methods
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Introduction to Classes and Objects
Presentation transcript:

Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes

Object-Oriented Programming Using C++, Third Edition2 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

Object-Oriented Programming Using C++, Third Edition3 Creating Classes (continued) Student aSophomore; aSophomore.idNum = 7645; cout<<aSophomore.idNum; Error! By default, all members of a class are private

Object-Oriented Programming Using C++, Third Edition4 Creating Classes (continued) Access modifier

Object-Oriented Programming Using C++, Third Edition5 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

Object-Oriented Programming Using C++, Third Edition6 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

Object-Oriented Programming Using C++, Third Edition7 Implementing Class Functions (continued)

Object-Oriented Programming Using C++, Third Edition8 Using Public Functions to Alter Private Data

Object-Oriented Programming Using C++, Third Edition9 Using Public Functions to Alter Private Data (continued) …

Object-Oriented Programming Using C++, Third Edition10 Using Private Functions and Public Data

Object-Oriented Programming Using C++, Third Edition11 … …

Object-Oriented Programming Using C++, Third Edition12 Considering Scope when Defining Member Functions

Object-Oriented Programming Using C++, Third Edition13 Considering Scope when Defining Member Functions (continued)

Object-Oriented Programming Using C++, Third Edition14 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

Object-Oriented Programming Using C++, Third Edition15 Defining Static Data Members Since it is not const, anyone can modify it

Object-Oriented Programming Using C++, Third Edition16 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

Object-Oriented Programming Using C++, Third Edition17 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

Object-Oriented Programming Using C++, Third Edition18 Using Static Functions (continued)

Object-Oriented Programming Using C++, Third Edition19 Understanding the this Pointer … … …

Object-Oriented Programming Using C++, Third Edition20 Understanding the this Pointer (continued)

Object-Oriented Programming Using C++, Third Edition21 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

Object-Oriented Programming Using C++, Third Edition22 Using the this Pointer Explicitly

Object-Oriented Programming Using C++, Third Edition23 Using the Pointer-to-Member Operator

Object-Oriented Programming Using C++, Third Edition24 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(); };

Object-Oriented Programming Using C++, Third Edition25 Using a static Field class Letter { private: string title; string recipient; static int count; public: void setRecipient(string, string); void displayGreeting(); static void displayCount(); };

Object-Oriented Programming Using C++, Third Edition26 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

Object-Oriented Programming Using C++, Third Edition27 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