Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects.

Slides:



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

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 15: User-Defined Types: enum and struct.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Encapsulation by Subprograms and Type Definitions
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.
Chapter 11: Classes and Data Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 12: Adding Functionality to Your Classes.
Values, variables and types © Allan C. Milne v
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Object Oriented Programming Elhanan Borenstein Lecture #4.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
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 C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Eine By: Avinash Reddy 09/29/2016.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Separate Compilation and Namespaces
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Lecture 9 Concepts of Programming Languages
Separate Compilation and Namespaces
Introduction to Classes and Objects
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
Review of Previous Lesson
Creating and Using Classes
SPL – PS3 C++ Classes.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects

In This Lesson We Will: Introduce the concept of classes Introduce the concept of classes Identify the two types of class members Identify the two types of class members Re-visit the concept of variable scope Re-visit the concept of variable scope Discuss the use of external classes Discuss the use of external classes

Topic Classes

What is a Class? A class defines a new data type A class defines a new data type Allows a programmer to associate information that is of different types Allows a programmer to associate information that is of different types Allows a programmer to define variables that contain the proper information Allows a programmer to define variables that contain the proper information A class is a mechanism used to model the real world A class is a mechanism used to model the real world Associates data with appropriate actions Associates data with appropriate actions Structures pertain to real-world objects Structures pertain to real-world objects

Class Members Properties: data “fields” which describe an object of that class Properties: data “fields” which describe an object of that class usually made private usually made private Methods: actions that may be performed by objects of that class Methods: actions that may be performed by objects of that class usually made public usually made public properties should be modified only through well-defined methods properties should be modified only through well-defined methods

Example (Part 1) Define a class: Pie (on paper) Define a class: Pie (on paper) Give it two properties: Give it two properties: flavor and price flavor and price Assign a get and set method for each of those properties Assign a get and set method for each of those properties

Example (Part 2) Define a class: Register (on paper) Define a class: Register (on paper) Give it one property: Give it one property: total total Create four methods: Create four methods: initialize initialize getTotal getTotal getReceipts getReceipts addMoney addMoney

Topic A Class As a User-Defined Type

User-Defined Types enum : a discrete set of possibilities; very similar to a group of constants enum : a discrete set of possibilities; very similar to a group of constants struct : an association between elements of different data types struct : an association between elements of different data types generally data elements are public generally data elements are public usually used JUST to store values usually used JUST to store values class : a description of real-world objects class : a description of real-world objects generally data elements are private generally data elements are private used to model objects that take action used to model objects that take action

Topic Member Modifiers

Visibility Modifiers One type of modifier influences the visibility of a class member One type of modifier influences the visibility of a class member public : A class member that is visible to external users of the class public : A class member that is visible to external users of the class private : A class member that is not visible outside of the class private : A class member that is not visible outside of the class protected : roughly equivalent to public within a limited scope, private outside of it protected : roughly equivalent to public within a limited scope, private outside of it

Visibility Modifiers (cont.) In practice: In practice: Use private for properties Use private for properties Use public for methods Use public for methods Methods: define actions which may be invoked externally Methods: define actions which may be invoked externally Properties: define the object, should be modified only through methods Properties: define the object, should be modified only through methods

Visibility Modifiers (cont.) The protected modifier allows a compromise between the others The protected modifier allows a compromise between the others It is used primarily within the context of inheritance, which topic will be left for a more advanced class It is used primarily within the context of inheritance, which topic will be left for a more advanced class

Other Modifiers const : Used to identify things that should not be allowed to change const : Used to identify things that should not be allowed to change We have seen data constants We have seen data constants May be used with methods and classes; that use is covered in another course May be used with methods and classes; that use is covered in another course static : Used to identify items that belong to an entire class of objects static : Used to identify items that belong to an entire class of objects

Example (Declaration) With that information, should be possible to create the class declarations for our Pie and Register classes With that information, should be possible to create the class declarations for our Pie and Register classes Use separate files for each one Use separate files for each one

Topic Mechanics

Defining Classes In C++ the relationship between files and classes is loose In C++ the relationship between files and classes is loose Permitted to define more than one class in a file and to split a class's definition and implementation across more than one file Permitted to define more than one class in a file and to split a class's definition and implementation across more than one file For our purposes we will try to keep one class to a file For our purposes we will try to keep one class to a file

Complications Compilation order is important Compilation order is important Must ensure that classes are compiled EXACTLY once Must ensure that classes are compiled EXACTLY once Must declare class before providing its implementation Must declare class before providing its implementation Use #define statements to help control these things Use #define statements to help control these things (Add those in now) (Add those in now)

Topic Scope

Property Scope We have encountered the importance of scope several times We have encountered the importance of scope several times The data members of a class have a scope that encompasses the entire class - meaning that they are visible to all of the methods defined within that class The data members of a class have a scope that encompasses the entire class - meaning that they are visible to all of the methods defined within that class

Concepts Re-visited This affects the parameter-passing discussion: there is no need to pass properties at all This affects the parameter-passing discussion: there is no need to pass properties at all Allows methods to make permanent changes to the properties of a class Allows methods to make permanent changes to the properties of a class In essence, allows a function to return more than one value, since a method may modify several properties of a class In essence, allows a function to return more than one value, since a method may modify several properties of a class

Implementation Scope Scope is also important as we go to implement the methods defined within the class Scope is also important as we go to implement the methods defined within the class Must explicitly associate each method with the class that owns it Must explicitly associate each method with the class that owns it Once that is done, complete the implementation in much the way we did ordinary functions Once that is done, complete the implementation in much the way we did ordinary functions

Example (Implementation) Complete the methods defined in the earlier classes Note that the implementation may be placed in the same file as the declarations

Topic Using Externally-Defined Classes

Example (Finish) In a third file (Store.cpp) write a program which uses the Pie and Register classes to model the operation of a small bakery In a third file (Store.cpp) write a program which uses the Pie and Register classes to model the operation of a small bakery Use an array of six pies to model the store's display shelf Use an array of six pies to model the store's display shelf Enter into a loop which will allow the store owner to sell pies, replace pies, and close the store Enter into a loop which will allow the store owner to sell pies, replace pies, and close the store

Worth Noting Note the use of double quotes when including user-defined classes; causes the #include to search current directory Note the use of double quotes when including user-defined classes; causes the #include to search current directory Note the use of the scope-resolution operator ( :: ) when providing method implementations Note the use of the scope-resolution operator ( :: ) when providing method implementations Note the use of the #define statements to ensure that file is included only once Note the use of the #define statements to ensure that file is included only once

Also Worth Noting Most “real-life” development efforts place the class declaration in a header (.h) file, and the implementation in a separate (.cpp) file Most “real-life” development efforts place the class declaration in a header (.h) file, and the implementation in a separate (.cpp) file Purpose is to reduce interdependence, since most bugs occur in implementation Purpose is to reduce interdependence, since most bugs occur in implementation

In This Lesson We Have: Introduced the concept of classes Introduced the concept of classes Discussed properties and methods Discussed properties and methods Described the scope of class properties Described the scope of class properties Explained the modifiers used for class members Explained the modifiers used for class members Described the use of externally-defined classes Described the use of externally-defined classes