Lesson 13 Introduction to Classes CS1 Lesson 13 -- Introduction to Classes1.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Chapter 13 – Introduction to Classes
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 14 Classes, Continued CS1 Lesson More Classes1.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Chapter 14: Overloading and Templates
Classes and Objects Systems Programming.
Starting Out with C++: Early Objects 5th Edition
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
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.
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.
C++ Review (3) Structs, Classes, Data Abstraction.
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.
 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.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
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.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
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
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?
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Review: Two Programming Paradigms
classes and objects review
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
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?
Introduction to Classes
Classes and Data Abstraction
Chapter 9 Objects and Classes
Classes C++ representation of an object
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1

Procedural versus Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program. The program starts at the beginning, does something, and ends. Object-Oriented programming is based on the data and the functions that operate on it. Objects are instances of abstract data types that represent the data and its functions CS1 Lesson Introduction to Classes2

Key Point An object or class contains the data and the functions that operate on that data. Objects are similar to structs but contain functions, as well. CS1 Lesson Introduction to Classes3

Limitations of Procedural Programming If the data structures change, many functions must also be changed Programs that are based on complex function hierarchies are: –difficult to understand and maintain –difficult to modify and extend –easy to break CS1 Lesson Introduction to Classes4

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 CS1 Lesson Introduction to Classes5

Classes and Objects A Class is like a blueprint and objects are like houses built from the blueprint CS1 Lesson Introduction to Classes6

Object-Oriented Programming Terminology attributes: members of a class methods or behaviors: member functions of a class CS1 Lesson Introduction to Classes7

More Object Terms 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 CS1 Lesson Introduction to Classes8

Creating a Class Objects are created from a class Format: class ClassName { declaration; }; CS1 Lesson Introduction to Classes9

Classic Class Example CS1 Lesson Introduction to Classes10

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 In the example on the next slide, note that the functions are prototypes only (so far) CS1 Lesson Introduction to Classes11

Class Example CS1 Lesson Introduction to Classes12

Access Specifiers CS1 Lesson Introduction to Classes13 Private Members Public Members

Access Specifiers (continued) Can be listed in any order in a class Can appear multiple times in a class If not specified, the default is private CS1 Lesson Introduction to Classes14

Using const With Member Functions const appearing after the parentheses in a member function declaration specifies that the function will not change any data in the calling object. CS1 Lesson Introduction to Classes15

Defining a Member Function When defining a member function: –Put prototype in class declaration –Define function using class name and scope resolution operator (::) int Rectangle::setWidth(double w) { width = w; } CS1 Lesson Introduction to Classes16

Global Functions Functions that are not part of a class, that is, do not have the Class::name notation, are global. This is what we have done up to this point. CS1 Lesson Introduction to Classes17

Accessors and Mutators Mutator: a member function that stores a value in a private member variable, or changes its value in some way Accessor: function that retrieves a value from a private member variable. Accessors do not change an object's data, so they should be marked const. CS1 Lesson Introduction to Classes18

Defining an Instance of a Class An object is an instance of a class Defined like structure variables: Rectangle r; Access members using dot operator: r.setWidth(5.2); cout << r.getWidth(); Compiler error if you attempt to access a private member using dot operator CS1 Lesson Introduction to Classes19

Derived Attributes Some data must be stored as an attribute. Other data should be computed. If we stored “area” as a field, its value would have to change whenever we changed length or width. In a class about a “person,” store birth date and compute age CS1 Lesson Introduction to Classes20

Pointers to Objects Can define a pointer to an object: Rectangle *rPtr; Can access public members via pointer: rPtr = &otherRectangle; rPtr->setLength(12.5); cout getLength() << endl; CS1 Lesson Introduction to Classes21

Dynamically Allocating Objects Rectangle *r1; r1 = new Rectangle(); This allocates a rectangle and returns a pointer to it. Then: r1->setWidth(12.4); CS1 Lesson Introduction to Classes22

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 CS1 Lesson Introduction to Classes23

Private Members CS1 Lesson Introduction to Classes24 Code outside the class must use the class's public member functions to interact with the object.

Separating Specification from Implementation Place class declaration in a header file that serves as the class specification file. Name the file ClassName.h, for example, Rectangle.h Place member function definitions in ClassName.cpp, for example, Rectangle.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 CS1 Lesson Introduction to Classes25

Inline Member Functions Member functions can be defined – inline: in class declaration – after the class declaration Inline appropriate for short function bodies: int getWidth() const { return width; } CS1 Lesson Introduction to Classes26

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 CS1 Lesson Introduction to Classes27

Constructors Member function that is automatically called when an object is created Purpose is to construct an object and do initialization if necessary Constructor function name is class name Has no return type specified (What is the real return type?) CS1 Lesson Introduction to Classes28

Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing. A simple instantiation of a class (with no arguments) calls the default constructor: Rectangle r; CS1 Lesson Introduction to Classes29

Passing Arguments to Constructors To create a constructor that takes arguments: – indicate parameters in prototype: Rectangle(double, double); – Use parameters in the definition: Rectangle::Rectangle(double w, double len) { width = w; length = len; } CS1 Lesson Introduction to Classes30

Passing Arguments to Constructors You can pass arguments to the constructor when you create an object: Rectangle r(10, 5); CS1 Lesson Introduction to Classes31

More About Default Constructors If all of a constructor's parameters have default arguments, then it is a default constructor. For example: Rectangle(double = 0, double = 0); Creating an object and passing no arguments will cause this constructor to execute: Rectangle r; CS1 Lesson Introduction to Classes32

Classes with No Default Constructor When all of a class's constructors require arguments, then the class has NO default constructor When this is the case, you must pass the required arguments to the constructor when creating an object CS1 Lesson Introduction to Classes33

Destructors Member function automatically called when an object is destroyed Destructor name is ~ classname, e.g., ~Rectangle Has no return type; takes no arguments Only one destructor per class, i.e., it cannot be overloaded If constructor allocates dynamic memory, destructor should release it CS1 Lesson Introduction to Classes34

Constructors, Destructors, and Dynamically Allocated Objects When an object is dynamically allocated with the new operator, its constructor executes: Rectangle *r = new Rectangle(10, 20); When the object is destroyed, its destructor executes: delete r; CS1 Lesson Introduction to Classes35

Overloading Constructors A class can have more than one constructor Overloaded constructors in a class must have different parameter lists: Rectangle(); Rectangle(double); Rectangle(double, double); CS1 Lesson Introduction to Classes36

Only One Default Constructor and One Destructor Do not provide more than one 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 CS1 Lesson Introduction to Classes37

Member Function Overloading Non-constructor member functions can also be overloaded: void setCost(double); void setCost(char *); Must have unique parameter lists as for constructors CS1 Lesson Introduction to Classes38

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 If you wrote a class that had a public sort function and needed a function to swap two elements, you’d make that private CS1 Lesson Introduction to Classes39

Arrays of Objects Objects can be the elements of an array: Rectangle rooms[8]; Default constructor for object is used when array is defined CS1 Lesson Introduction to Classes40

Arrays of Objects Must use initializer list to invoke constructor that takes arguments: Rectangle rArray[3]={Rectangle(2.1,3.2), Rectangle(4.1, 9.9), Rectangle(11.2, 31.4)}; CS1 Lesson Introduction to Classes41

Arrays of Objects It isn't necessary to call the same constructor for each object in an array: Rectangle rArray[3]={Rectangle(2.1,3.2), Rectangle(), Rectangle(11.2, 31.4)}; CS1 Lesson Introduction to Classes42

Accessing Objects in an Array Objects in an array are referenced using subscripts Member functions are referenced using dot notation: rArray[1].setWidth(11.3); cout << rArray[1].getArea(); CS1 Lesson Introduction to Classes43

The Unified Modeling Language UML stands for Unified Modeling Language. The UML provides a set of standard diagrams for graphically depicting object-oriented systems CS1 Lesson Introduction to Classes44

UML Class Diagram CS1 Lesson Introduction to Classes45 A UML diagram for a class has three main sections.

Example: A Rectangle Class CS1 Lesson Introduction to Classes46 class Rectangle { private: double width; double length; public: bool setWidth(double); bool setLength(double); double getWidth() const; double getLength() const; double getArea() const; };

UML Access Specification Notation CS1 Lesson Introduction to Classes47 In UML you indicate a private member with a minus (-) and a public member with a plus(+). These member variables are private. These member functions are public.

UML Data Type Notation CS1 Lesson Introduction to Classes48 To indicate the data type of a member variable, place a colon followed by the name of the data type after the name of the variable. - width : double - length : double

UML Parameter Type Notation To indicate the data type of a function’s parameter variable, place a colon followed by the name of the data type after the name of the variable. CS1 Lesson Introduction to Classes49 +setWidth(w : double)

UML Function Return Type Notation CS1 Lesson Introduction to Classes50 To indicate the data type of a function’s return value, place a colon followed by the name of the data type after the function’s parameter list. + setWidth(w : double) : void

The Rectangle Class CS1 Lesson Introduction to Classes51

Showing Constructors and Destructors CS1 Lesson Introduction to Classes52 Constructors Destructor No return type listed for constructors or destructors