Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Starting Out with C++: Early Objects 5th Edition
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11: Classes and Data Abstraction
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
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.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Introduction to Classes
Introduction to Classes and Objects
Chapter 11: Inheritance and Composition
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes

Chapter 13 slide 2 Topics 13.1 Procedural and Object-Oriented Programming 13.2 Introduction to Classes 13.3 Defining an Instance of a Class 13.4 Why Have Private Members? 13.5 Software Design Considerations 13.6 Using Private Member Functions 13.7 Inline Member Functions 13.8 Constructors

Chapter 13 slide 3 Topics 13.9 Destructors Constructors That Accept Arguments Input Validation Objects Overloading Constructors Only One Default Constructor and One Destructor Arrays of Objects An Object-Oriented System Development Primer

Chapter 13 slide Procedural and Object- Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented programming is based on the data and the functions that operate on it. Objects are instances of ADTs that represent the data and its functions

Chapter 13 slide 5 Problems with Procedural Programming Use of global data may allow data corruption Programs based on complex function hierarchies are: – difficult to understand and maintain –difficult to modify and extend –easy to break

Chapter 13 slide 6 Object-Oriented Programming Terminology class: like a struct (allows bundling of related variables), but variables and functions in the class can have different properties than in a struct object: an instance of a class, in the same way that a variable can be an instance of a struct

Chapter 13 slide 7 Object-Oriented Programming Terminology attributes: members of a class methods or behaviors: member functions of a class

Chapter 13 slide 8 More on Objects data hiding: restricting access to certain members of an object public interface: members of an object that are available outside of the object. This allows the object to provide access to some data and functions without sharing its internal details and design, and provides some protection from data corruption objects can be general-purpose or application-specific

Chapter 13 slide Introduction to Classes Objects are created from a class Format: class { declaration; };

Chapter 13 slide 10 Class Example class Square { private: int side; public: void setSide(int s) { side = s; } int getSide() { return side; } };

Chapter 13 slide 11 Access Specifiers Used to control access to members of the class public: can be accessed by functions outside of the class private: can only be called by or accessed by functions that are members of the class

Chapter 13 slide 12 More on Access Specifiers Can be listed in any order in a class Can appear multiple times in a class If not specified, the default is private

Chapter 13 slide Defining an Instance of a Class An object is an instance of a class Defined like structure variables: Square sq1, sq2; Access members using dot operator: sq1.setSide(5); cout << sq2.getSide(); Compiler error if attempt to access private member using dot operator

Chapter 13 slide 14 Pointer to an Object Can define a pointer to an object: Square *sqPtr; Can access public members via pointer: sqPtr = &sq1; sqPtr->setSide(12); sqPtr = &sq2; sqPtr->setSide(sq1.getSide()); cout getSide();

Chapter 13 slide Why Have Private Members? Making data members private provides data protection Data can be accessed only through public functions Public functions define the class’s public interface

Chapter 13 slide 16 Set vs. Get Functions Set function: function that stores a value in a private member variable Get function: function that retrieves a value from a private member variable Common class design practice: make all member variables private, provide public set and get functions

Chapter 13 slide Software Design Considerations –Place class declaration in a header file that serves as the class specification file. Name the file.h, for example, square.h –Place member function definitions in.cpp, for example, square.cpp File should #include the class specification file –Programs that use the class must #include the class specification file, and be compiled and linked with the member function definitions

Chapter 13 slide 18 Defining a Member Function When defining a member function: –Put prototype in class declaration –Define function using class name and scope resolution operator (::) int Square::getSide() { return side; }

Chapter 13 slide 19 Input/Output with Objects Class should be designed to provide functions to store, retrieve data Functions that use objects of a class should perform input/output operations, not class member functions Exceptions can occur: class designed to display a menu, or class designed specifically to perform I/O

Chapter 13 slide Using Private Member Functions A private member function can only be called by another member function It is used for internal processing by the class, not for use outside of the class

Chapter 13 slide Inline Member Functions Member functions can be defined –inline: in class declaration –after the class declaration Inline appropriate for short function bodies: int getSide() { return side; }

Chapter 13 slide 22 Tradeoffs – Inline vs. Regular Member Functions Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Code for an inline function is copied into program in place of call – larger executable program, but no function call overhead, hence faster execution

Chapter 13 slide Constructors Member function that is called when an object is created Called automatically Constructor function name is class name Has no return type

Chapter 13 slide 24 Constructor Example class Square { public: Square(); // prototype... }; Square::Square() // heading { side = 1; }

Chapter 13 slide Destructors Member function automatically called when an object is destroyed Destructor name is ~ classname, e.g., ~Square Has no return type; takes no arguments Only 1 destructor per class, i.e., it cannot be overloaded If constructor allocates dynamic memory, destructor should release it

Chapter 13 slide Constructors That Accept Arguments Default constructor: constructor that takes no arguments To create an object using the default constructor, use no argument list and no () : Square square1; To create a constructor that takes arguments: –indicate parameters in prototype, definition –provide arguments when object is created: Square square2(12);

Chapter 13 slide 27 Constructors That Accept Arguments Constructor may have default arguments: Square(int = 1); // prototype Square::Square(int s) // heading { side = s; }

Chapter 13 slide Input Validation Objects Objects can be designed to validate user input: –Acceptable menu choice –Test score in range of valid scores –etc.

Chapter 13 slide Overloading Constructors A class can have > 1 constructor Overloaded constructors in a class must have different parameter lists: Square(); Square(int);

Chapter 13 slide 30 Member Function Overloading Non-constructor member functions can also be overloaded: void setSide(); void setSide(int); Must have unique parameter lists as for constructors

Chapter 13 slide Only One Default Constructor and One Destructor Do not provide > 1 default constructor for a class: one that takes no arguments and one that has default arguments for all parameters Square(); Square(int = 0); // will not compile Since a destructor takes no arguments, there can only be one destructor for a class

Chapter 13 slide Arrays of Objects Objects can be the elements of an array: Square lottaSquares[10]; Default constructor for object is used when array is defined Must use initializer list to invoke constructor that takes arguments: Square triSqu[3] = {5,7,11}; More complex initialization if constructor takes > 1 argument

Chapter 13 slide 33 Accessing Objects in an Array Objects in an array are referenced using subscripts Member functions are referenced using dot notation: lottaSquares[3].setSide(6); cout << triSqu[i].getSide;

Chapter 13 slide An Object-Oriented System Development Primer Procedural Programming: –program is made up of procedures: sets of programming statements that perform tasks Object-Oriented Programming: –program is made up of objects: entities that contain data (attributes) and actions (methods)

Chapter 13 slide 35 Benefits of Object-Oriented Programming Simplification of software development for graphical (GUI) applications –visual components (menus, text boxes, etc.) modeled as objects –visual components (e.g., windows) may be made up of multiple objects –some objects (e.g., buttons) may have methods associated with them

Chapter 13 slide 36 Benefits of Object-Oriented Programming Simplification of software development for non-GUI applications - the problem: –procedural programming enforces separation between code and data –changes in data format require extensive analysis, testing to ensure program functionality

Chapter 13 slide 37 Benefits of Object-Oriented Programming Simplification of software development for non-GUI applications - a solution: –OO programming addresses the problem through encapsulation: combination of data and actions in an object data hiding: protection of an object’s data by its public member functions

Chapter 13 slide 38 Component Reusability Component: a software object that performs a well-defined task or that provides a service Component Reusability: the ability to use a component in multiple programs without (or with little) modification

Chapter 13 slide 39 Relationships Between Objects A program may contain objects of different classes Objects may be related by one of the following: –Access (‘knows’ relationship) –Ownership (‘has a’ relationship) –Inheritance (‘is a’ relationship)

Chapter 13 slide 40 Messages and Polymorphism Message: request to an object to perform a task. Implemented as a member function call Polymorphism: ability to take many forms. Sending the same message to different objects may produce different behaviors

Chapter 13 slide 41 Object Oriented Analysis Used to create the logical design of a system, what the system is to do Usually includes –examine the problem domain; model the system from within that perspective –identify the objects that exist within the boundaries of that system –identify the relationships between the objects –realize that there may be many ‘right’ solutions

Chapter 13 slide 42 Object Oriented Design Used to determine how requirements from OO Analysis will be implemented Usually includes –determine hierarchical relation between objects –determine object ownership of attributes –implement and test; refine as required

Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes