Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Starting Out with C++: Early Objects 5th Edition
Chapter 7: 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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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 © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
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.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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,
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
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.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Chapter 7: Introduction to Classes and Objects
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
Chapter 7: Introduction to Classes and Objects
Chapter 7: Introduction to Classes and Objects
Introduction to Object-oriented Programming
Introduction to Classes
Classes and Data Abstraction
Classes and Objects.
NAME 436.
Standard Version of Starting Out with C++, 4th Edition
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
Introduction to Classes and Objects
Presentation transcript:

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes

Chapter 7 slide 2 Topics 7.8 Abstract Data Types 7.9 Object-Oriented Programming 7.10 Introduction to Classes 7.11 Introduction to Objects

Chapter 7 slide 3 Topics (Continued) 7.12 Defining Member Functions 7.13 Design Considerations 7.14 Using a Constructor with a Class 7.15 Overloading Constructors 7.16 Destructors 7.18 Using Private Member Functions

Chapter 7 slide Abstract Data Types A programmer-created data type that specifies –legal values that can be stored –operations that can be done on the values The user of an abstract data type does not need to know any implementation details (e.g., how the data is stored or how the operations on it are carried out)

Chapter 7 slide 5 Abstraction and Data Types Abstraction: a definition that captures general characteristics without details –Ex: abstract triangle is a 3-sided polygon. A specific triangle may be scalene, isosceles, or equilateral Data Type: defines the kind of values that can be stored in a variable and the operations that can be performed on it

Chapter 7 slide 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.

Chapter 7 slide 7 Limitations of Procedural Programming Use of global data may allow data corruption Programs are often based on complex function hierarchies –difficult to understand and maintain – difficult to modify and extend – easy to break

Chapter 7 slide 8 Object-Oriented Programming Terminology class: similar to a struct –Allows bundling of related variables (member data) and the functions that operate on them (member functions) –Describes the properties that all instances of the class will have object: an instance of a class, in the same way that a variable can be an instance of a struct

Chapter 7 slide 9 More Object-Oriented Programming Terminology attributes: member data of a class methods or behaviors: member functions of a class data hiding: restricting access to certain members of an object

Chapter 7 slide 10 Public Interface Class objects are accessed from outside the class via a public interface –This is done by allowing some of the member functions to be called from outside the class –Normally all the class’s member variables are accessed only through these “public” functions – provides some protection from data corruption

Chapter 7 slide Introduction to Classes A class declaration describes the member variables and member functions that its objects will have It is a pattern for creating objects Class Declaration Format: class className { declaration; }; Notice the required ;

Chapter 7 slide 12 Access Specifiers Used to control access to members of the class. Each member is declared to be either public: can be accessed by functions outside of the class or private: can only be called by or accessed by functions that are members of the class

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

Chapter 7 slide 14 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 7 slide Introduction to Objects 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 7 slide Defining Member Functions Member functions are part of a class declaration Can place entire function definition inside the class declaration or Can place just the prototype inside the class declaration and write the function definition after the class

Chapter 7 slide 17 Defining Member Functions Inside the Class Declaration Member functions defined inside the class declaration are called inline functions Only very short functions, like the one below, can be inline functions int getSide() { return side; }

Chapter 7 slide 18 Inline Member Function Example class Square { private: int side; public: void setSide(int s) { side = s; } int getSide() { return side; } }; inline functions

Chapter 7 slide 19 Defining Member Functions After the Class Declaration To define a member function after the class declaration –Put a function prototype in the class declaration –In the function definition, precede function name with class name and scope resolution operator ( :: ) int Square::getSide() { return side; }

Chapter 7 slide 20 Tradeoffs of Inline vs. Regular Member Functions When a regular function is called, control passes to the called function –the compiler stores return address of call, allocates memory for local variables, etc. Code for an inline function is copied into the program in place of the call –larger executable program –but no function call overhead, hence faster execution

Chapter 7 slide Design Considerations Class should be designed to provide functions to store and retrieve data In general, I/O should be done by functions that use class objects, rather than by class member functions (Exceptions can occur, as with a class designed to display a menu)

Chapter 7 slide 22 More Design Considerations Separating class declaration, member function definitions, and program that uses the class into separate files is considered good design

Chapter 7 slide 23 Using Separate Files Place class declaration in a header file that serves as the class specification file. Name the file classname.h ( for example, Square.h) Place member function definitions in a class implementation file. Name the file classname.cpp (for example, Square.cpp) This 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 class implementation file.

Chapter 7 slide Using a Constructor with a Class As with a structure, a constructor can be used to initialize data members of a class Must be a public member function Must be named the same as the class Must have no return type Is called automatically when an object of the class is created

Chapter 7 slide 25 The Default Constructor Constructors can have any number of parameters, including none A default constructor is one that takes no arguments either due to –No parameters or –All parameters have default values

Chapter 7 slide 26 Default Constructor Example class Square { private: int side; public: Square() // default { side = 1; } // constructor // Other member // functions go here }; Has no parameters

Chapter 7 slide 27 Another Default Constructor Example class Square { private: int side; public: Square(int s = 1) // default { side = s; } // constructor // Other member // functions go here }; Has parameter but it has a default value

Chapter 7 slide 28 Invoking a Constructor Invoking a constructor for a class object is done just like invoking a constructor for a structure variable To create an object using the default constructor, use no argument list and no () Square square1; To create an object using a constructor that has parameters, include an argument list Square square1(8);

Chapter 7 slide Overloading Constructors A class can have > 1 constructor Overloaded constructors in a class must have different parameter lists Square(); Square(int); Only one default constructor is allowed

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

Chapter 7 slide Destructors Public 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 is allowed per class (i.e., it cannot be overloaded)

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

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes