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.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
 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.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 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.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 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.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
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.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
 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. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Programming Fundamentals Enumerations and Functions.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
 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
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects and Strings
Chapter 3 Introduction to Classes and Objects
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
3-4-5 Introduction.
Introduction to Classes and Objects
Object Oriented Programming in java
3-4-5 Introduction.
Introduction to Classes and Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

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 of  Function main and  One or more classes  Each containing data members and member functions

3.3 Overview of the Chapter Examples Seven simple examples ◦ Examples used to build a GradeBook class Topics covered: ◦ Member functions ◦ Data members ◦ Clients of a class  Other classes or functions that call the member functions of this class ’ s objects ◦ Separating interface from implementation ◦ Data validation  Ensures that data in an object is in a particular format or range

3.4 Defining a Class With a Member Function Class definition ◦ Tells compiler what member functions and data members belong to the class ◦ Keyword class followed by the class ’ s name ◦ Class body is enclosed in braces ( {} )  Specifies data members and member functions  Access-specifier public:  Indicates that a member is accessible to other functions.

Beginning of class definition for class GradeBook Access specifier public ; makes members available to the public Member function displayMessge, which returns nothing Use dot operator to call GradeBook ’s member function

Note We usually write the declaration of a class and its definition separately. In the examples of this part, we may directly put the function definition in the class just for simplicity.

Common Programming Error 3.1 Forgetting the semicolon at the end of a class definition is a syntax error. class Gradebook { public: void displayMessage(); } Indicate any syntax error in this declaration.

3.4 Defining a Class With a Member Function Using a class ◦ A class is a user-defined type (or programmer-defined type)  Can be used to create objects  Variables of the class type ◦ Dot operator (. )  Used to access an object ’ s data members and member functions  Example  myGradeBook.displayMessage()  Call member function displayMessage of GradeBook object myGradeBook

3.5 Defining a Member Function with a Parameter A string class ◦ Represents a string of characters ◦ An object of C++ Standard Library class std::string  Defined in header file Library function getline ◦ Used to retrieve input until newline is encountered ◦ Example  getline( cin, nameOfCourse );  Inputs a line from standard input into string object nameOfCourse

Member function parameter Include string class definition Use the function parameter as a variable

Passing an argument to the member function

3.6 Data Members, set Functions and get Functions Attributes ◦ Exist throughout the life of the object ◦ Represented as data members  Variables in a class definition ◦ Each object of class maintains its own copy of attributes

3.6 Data Members, set Functions and get Functions Access-specifier private ◦ Makes a member accessible only to member functions of the class. ◦ private is the default access for class members

3.6 Data Members, set Functions and get Functions ◦ Data hiding ◦ Problem: How to access the private data members? ◦ Write public member functions that allow callers to set or get the values of private data members

set function modifies private data get function accesses private data

Use set and get functions, even within the class Accessing private data outside class definition private members accessible only to member functions of the class

Modifying private data outside class definition

Indicate Any Error class GradeBook { public: … private: string courseName; }; int main() { GradeBook gb; cout << gb.courseName << endl; return 0; }

Indicate Any Error class GradeBook { public: string getCourseName() { return courseName; } private: string courseName; }; int main() { GradeBook gb; cout courseName << endl; return 0; }

Indicate Any Error and Problem class GradeBook { public: string getCourseName() { return courseName; } private: string courseName; }; int main() { GradeBook *gb = new GradeBook; cout courseName << endl; return 0; }

Software Engineering Observation 3.1 Data members should be declared private and member functions should be declared public. class GradeBook { public: string getCourseName(); … private: string courseName; }; list all the public members of a class first in one group and then list all the private members in another group.

Software Engineering Observation 3.4 The class designer need not provide set or get functions for each private data item. These capabilities should be provided only when appropriate. If a service is useful to the client code, that service should typically be provided in the class ’ s public interface.

3.7 Initializing Objects with Constructors Constructors ◦ Functions used to initialize an object ’ s data when it is created.  Called implicitly when object is created  Must be defined with the same name as the class  Cannot return values  Not even void ◦ Default constructor has no parameters  The compiler will provide one when a class does not explicitly include a constructor  Compiler ’ s default constructor only calls constructors of data members that are objects of classes

Constructor has same name as class and no return type Initialize data member

Creating objects implicitly calls the constructor

Error-Prevention Tip 3.2 Provide a constructor to ensure that your class ’ s data members are initialized with meaningful values when each new object of your class is created.

Software Engineering Observation 3.5 Ensure that an object is fully initialized before the client code invokes the object ’ s member functions. You should not rely on the client code to ensure that an object gets initialized properly.