1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Advertisements

 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
What is UML? A modeling language standardized by the OMG (Object Management Group), and widely used in OO analysis and design A modeling language is a.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Introduction to C++ Part II.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
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.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
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 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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 13: Introduction to Classes.
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 16 is a copy of C++ How to Program Chapter.
Chapter 3 Introduction to Classes, Objects and Strings C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes in C++ By Ms Nashandi. Placing a class in a separate file for Reusability When building an object C++ Program, it is customary to define reusable.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects and Strings
Introduction to Classes
Introduction to Classes
3-4-5 Introduction.
Introduction to Classes and Objects
Introduction to Classes and Objects
Introduction to Classes and Objects
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3

2 Class vs Object Blueprint House Class Object

3 Class Definition/Interface GradeBook.h Function prototypes Forgetting ; is a syntax error

4 Class Implementation File GradeBook.cpp Define class member functions in separate source-code file Including the header file causes the class definitions to be copied into the file

5 Constructor and Default Constructor C++ requires a constructor call for each object created –No return type, not even void –Compiler provides default constructor when a constructor is not explicitly provided Default constructor –Constructor with no parameters –Two ways to provide default constructor The compiler implicitly creates a default constructor in a class without a constructor (may contain garbage value for its data members) The programmer explicitly defines a constructor that takes no arguments

6 Data Member and Member Function Data members –Variables declared inside a class definition but outside the class’s member function body –Represents the attributes of the class –Each object has its own copy of data members Member functions –Get functions (accessors) to access private data members –Set functions (mutators) to change the values of private data members –All objects of the same class share one copy of each member function

7 Scope Resolution Operator Scope resolution operator (::) is used to define the member functions outside the class A token to allow access to static members of a class Scope resolution operator is preceded by a class name Example: void GradeBook::setCourseName(string name) {…} string GradeBook:getCourseName() {…}

8 Dot Member Selection Operator Dot operator (.) is used to access a member of an object Preceded by an object name Example gradeBook1.setCourseName(“COMP1520: C++”); gradeBook1.displayMessage();

9 Main Program (Client Code) Testing Class GradeBook

10 Class Diagram for GradeBook Class Minus indicates a private member Plus indicates public members Name of the class in boldface Class attributes Class operations

11 Separating Interface from Implementation Interfaces –Define and standardize the ways in which things interact with each other –Describes what services class’s client can use and how to request those services –Not how the class carries out the services –Consists only a class’s public members Why separate interface from the implementation –Ensures programmers do not write client code that depends on the class’s implementation details –Client codes are less likely to break if the implementation changes –The class is reusable –The clients of the class know what member functions the class provides, how to call them and what return types to expect –The clients do not know how the class's member functions are implemented How –Defining a class’s interface with function prototypes Function prototypes describes the class's public interface without revealing the class's member function implementations –Defining member functions in a separate source-code file

12 How Header Files Are Located When the preprocessor encounters a header file name in quotes (e.g., "GradeBook.h"), the preprocessor –Attempts to locate the header file in the same directory as the file in which the #include directive appears. –If failed, searches for it in the same location(s) as the C++ Standard Library header files. When the preprocessor encounters a header file name in angle brackets (e.g., ), it –assumes that the header is part of the C++ Standard Library

13 The Compilation and Linking Process

14 Unified Modeling Language (UML) General-purpose modeling language Create abstract model of a system using graphical notation Used to specify, visualize, construct, and document software-intensive systems Allow software developers to concentrate more on design and architecture

15 UML Diagrams

16 Software Engineering Case Study: ATM Class Diagram Determine the classes used in the system –ATM –Screen –Keypad –Cash dispenser –Deposit slot –Account –Bank database –Balance inquiry –Withdrawal –Deposit

17 Representing a class in the UML using a class diagram Name of the class Class’s attributes Class’s operations Compartments can be suppressed to create more readable diagrams.

18 Association between Classes Solid line represents association between classes Multiplicity value One object of class ATM executes zero or one objects of class Withdrawal.

19 Multiplicity Types SymbolMeaning 0None 1One mAn integer value 0..1Zero or one m, nm or n m..nAt least m, but not more than n *Any nonnegative integer (zero or more) 0..*Zero or more (identical to *) 1..*One or more

20 Composition Relationship Composition relationship or “has-a” relationship 1.The diamond can be placed on only one end of the association line. 2.The whole is responsible for creating and destructing its parts. 3.A part may belong to only one whole at a time. An ATM has a screen, a keypad, a cash dispenser and a deposit slot.

21 Class Diagram for the ATM System Model

22 Class Diagram Showing Composition Relationship of a Class Car