Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.

Slides:



Advertisements
Similar presentations
Chapter 13 – Introduction to Classes
Advertisements

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 14: Overloading and Templates
Starting Out with C++: Early Objects 5th Edition
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Review of C++ Programming Part II Sheng-Fang Huang.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
Starting Out with C++ 2nd Edition, by Tony Gaddis 1 Chapter 7 – Classes and Structured Data.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
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.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
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.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
CS1201: Programming Language 2 Classes and objects.
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.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
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.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
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
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Classes.
Review: Two Programming Paradigms
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Chapter 5 Classes.
Introduction to Classes
Chapter 9 Objects and Classes
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
Presentation transcript:

Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method of writing software. It is a programming practice centered on the procedures, or actions that take place in a program. Object-Oriented programming is centered around the object. Objects are created from abstract data types that encapsulate data and functions together.

Starting Out with C++, 3 rd Edition 2 What’s Wrong with Procedural Programming? Programs with excessive global data. Complex and convoluted programs. Programs that are difficult to modify and extend.

Starting Out with C++, 3 rd Edition 3 What is Object-Oriented Programming? OOP is centered around the object, which packages together both the data and the functions that operate on the data.

Starting Out with C++, 3 rd Edition 4 In OOP, an object’s member variables are often called its attributes Member functions are sometimes referred to as its behaviors or methods. Member Variables float width; float length; float area; Member Functions void setData(float w, float l) { … function code … } void calcArea(void) { … function code … } void getWidth(void) { … function code … } void getLength(void) { … function code … } void getArea(void) { … function code … }

Starting Out with C++, 3 rd Edition 5 Interface with Object

Starting Out with C++, 3 rd Edition 6 General Purpose Objects Creating data types that are improvements on C++’s built-in data types. For example, an array object could be created that works like a regular array, but additionally provides bounds-checking. Creating data types that are missing from C++. For instance, an object could be designed to process currencies or dates as if they were built-in data types. Creating objects that perform commonly needed tasks, such as input validation and screen output in a graphical user interface.

Starting Out with C++, 3 rd Edition 7 Application-Specific Objects Data types created for a specific application. For example, in an inventory program. HAVE IT YOUR WAY!

Starting Out with C++, 3 rd Edition 8 Introduction to the Class Looks similar to a struct The syntax is: class class-name { // declaration statements here };

Starting Out with C++, 3 rd Edition 9 Example Class Declaration Note how methods have been moved inside the brackets. Also, the private data is assumed. class Rectangle { private: float width, length, area; public: void setData ( float, float ); void calcArea ( void ); float getWidth ( void ); float getLength ( void ); float getArea ( void ); }; // Rectangle

Starting Out with C++, 3 rd Edition 10 Compare struct code with classes struct Rectangle1 { float width, length, area; } void setData(Rectangle1 & r, float w, float l){ r.width =r; r.length = l; r.area = r*l; }

Starting Out with C++, 3 rd Edition 11 With classes Method moves inside Don’t need to pass the Rectangle type in class Rectangle { float width, length, area; void setData(float w, float l){ width =r; length = l; area = r*l; } };

Starting Out with C++, 3 rd Edition 12 Notice how we morph a struct into a class However, now the class is getting so huge that it is hard to see what is going on. To make it easier to see just the part the user needs (how to call methods), we pull the implementation of the methods into a separate file. Irritating to have to make the two files match. Design first! Copy the declaration file into the implementation file.

Starting Out with C++, 3 rd Edition 13 Access Specifiers The key words private and public are access specifiers. private means the data/methods can only be accessed/called by the member functions. public means the data/methods can be accessed called from statements outside the class. –Note: the default access of a class is private, but it is still a good idea to use the private keyword to explicitly declare private members. This clearly documents the access specification of the class.

Starting Out with C++, 3 rd Edition 14 Defining Member Functions Class member functions are defined similarly to regular functions. void Rectangle::setData ( float w, float l ) { width = w; length = l; } // setData

Starting Out with C++, 3 rd Edition 15 Defining an Instance of a Class Class objects must be defined after the class is declared. Defining a class object is called the instantiation of a class. For example: Rectangle box; // just like for getting an instance of a struct

Starting Out with C++, 3 rd Edition 16 Accessing an Object’s Members For the previous example, to access a member function you would use the statement: box.calcArea(); This is like calling calcArea(box). box is actually the first argument that is passed in. However, we put the class out it front. You have seen that before (e.g. cin.getline(…)) It makes accessing the box’s methods like accessing its variables.

Starting Out with C++, 3 rd Edition 17 Pointers to Objects Rectangle box; Rectangle *boxPtr; boxPtr = &box; boxPtr->setData( 15, 12 ); boxPtr->length; // would work if not private

Starting Out with C++, 3 rd Edition 18 Rectangle.h class Rectangle {private: float width; float length; float area; public: void setData ( float, float ); void calcArea ( void ); float getWidth ( void ); float getLength ( void ); float getArea ( void ); }; // Rectangle

Starting Out with C++, 3 rd Edition 19 Rectangle.cpp notice the inclusion of class name before the method #include “rectangle.h” // Note “” not <> void Rectangle::setData( float w, float l ) {width = w; length = l; } // setData void Rectangle::calcArea ( void ) {area = width * length; } // calcArea

Starting Out with C++, 3 rd Edition 20 Why Have Private Members? In object-oriented programming, an object should protect its important data by making it private and providing a public interface to access that data. In the class Rectangle, this was done with the getLength, getWidth, and getArea methods. Sounds dumb – but notice how we let the user get the area, but not set it! We make sure it is set correctly. You could make the variables public, and then you wouldn’t need getters/setters, BUT this violates good design principles.

Starting Out with C++, 3 rd Edition 21 Some Design Considerations Usually class declarations are stored in their own header files (.h file). Member function definitions are stored in their own.cpp file. This is nice as we can give someone just the public part (.h) and they aren’t burdened with knowing too much. Also, allows separate compilation. One pass compiler can compile something that uses Rectangle without actually seeing it. Makes it more efficient when one file changes – as only recompiles the piece that has changed, not all of it.

Starting Out with C++, 3 rd Edition 22 Some Design Considerations The #ifndef directive is called an include guard allows a program to be conditionally compiled. If flag after #ifndef is already known, skip until the #endif. This prevents a header file from accidentally being included more than once – causing you to get multiple copies of things (which sometimes causes problems).

Starting Out with C++, 3 rd Edition 23 Rectangle.h #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle {private: float width; float length; float area; public: void setData ( float, float ); void calcArea ( void ); float getWidth ( void ); float getLength ( void ); float getArea ( void ); }; // Rectangle #endif

Starting Out with C++, 3 rd Edition 24 Putting the Files Together Need to include rectangle.h in both rectangle.cpp and main.cpp. Need to compile: –Rectangle.cpp –main.cpp The object files –Rectangle.obj and –main.obj are linked together. Need to have defined a project! Then execute: –Recttest.exe

Starting Out with C++, 3 rd Edition 25 Using Private Member Functions A private member function may only be called from a function that is a member of the same object. If try to access a private member, get the message ‘myClassName::getPrivate' : cannot access private member declared in class ‘myClassName‘ Practice making mistakes so you can tell how computer will inform you what you did wrong. I wonder what would happen if …

Starting Out with C++, 3 rd Edition 26 Inline Member Functions When the body of a member function is defined inside a class declaration, it is declared inline. Member functions are only defined inside the class declaration if they are small. Avoids call/return in code – no call, actually substituted. Can’t inline code with loops.

Starting Out with C++, 3 rd Edition 27 Constructors A constructor is a member function that is automatically called when a class object is instantiated (defined). Constructors have the same name as the class. Constructors must be declared publicly. Constructors have no return type.

Starting Out with C++, 3 rd Edition 28 Constructor – Program class Demo { private: int num; public: Demo(void); }; // Demo Demo::Demo ( void ) { num = 0; cout << "Welcome to the Demo constructor!\n"; } // Demo constructor Demo::Demo(int v){ num = v; } void main ( void ) {Demo demoObj; Demo d1(15); cout << "This program demonstrates an object\n"; cout << "with a constructor.\n"; } // main

Starting Out with C++, 3 rd Edition 29 Destructors A destructor is a member function that is automatically called when an object is destroyed. –Destructors have the same name as the class, preceded by a tilde character (~) –In the same way that a constructor is called then the object is created, the destructor is automatically called when the object is destroyed. –In the same way that a constructor sets things up when an object is created, a destructor performs shutdown procedures when an object is destroyed.

Starting Out with C++, 3 rd Edition 30 WARNING When you don’t pass a class to a function by reference, both the copy constructor (a constructor whose argument is the same type) and the destructor are called. This can cause some crazy things to happen if the constructor dynamically allocates some memory, and the destructor deletes it.

Starting Out with C++, 3 rd Edition 31 Destructors – Program #include class Demo { public: Demo ( void ) { cout << "Welcome to the constructor!\n"; } ~Demo ( void ) { cout << "The destructor is now running.\n“ }; }; // Demo void main ( void ) {Demo demoObj; cout << "This program demonstrates an object\n"; cout << "with a constructor and destructor.\n"; } // main

Starting Out with C++, 3 rd Edition 32 Constructor Arguments When a constructor does not have any arguments, it is called an object’s default constructor. Like regular functions, constructors may accept arguments, have default arguments, be declared inline, and be overloaded.

Starting Out with C++, 3 rd Edition 33 Constructors with Arguments – Program class InvItem {private: char *desc; int units; public: InvItem ( void ) { desc = new char [51]; strcpy( desc, ""); units = 0; } InvItem ( char *dscr, int un ) { desc = new char[51]; strcpy( desc, dscr ); units = un; } ~InvItem ( void ) { delete desc }; char *getDesc ( void ) { return desc; } int getUnits ( void ) { return units; } }; // InvItem

Starting Out with C++, 3 rd Edition 34 Only One Default Constructor and One Destructor – as which one is wanted is determined by parameters. A class may only have one default constructor (one without any arguments). A class may only have one destructor.

Starting Out with C++, 3 rd Edition 35 Arrays of Objects You may declare and work with arrays of class objects. InvItem inventory[40]; In this case – the default constructor is called for each of the 40 InvItems