Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Slides:



Advertisements
Similar presentations
Chapter 13 – Introduction to Classes
Advertisements

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.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
1 Introduction to Classes. C++: Classes & Objects - 12 Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 6: A First Look at Classes
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Starting Out with Java: From Control Structures through Objects.
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.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
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.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
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.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
A Second Look at Classes and Objects - 1 Static Class Members
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
Chapter 3 Classes & Objects
A first Look at Classes.
Classes.
Review: Two Programming Paradigms
classes and objects review
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
CS212: Object Oriented Analysis and Design
Introduction to Classes
Today’s topics UML Diagramming review of terms
Lecture 5- Classes, Objects and Methods
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Chapter 6: A First Look at classes
Presentation transcript:

Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Procedural and Object- Oriented Programming Concept: Procedural Programming: A method of writing software centered on the procedures or actions that take place in a program. Object-Oriented Programming: A method of writing software centered around the object, which are created from abstract data types that encapsulate data and functions together.

variables double width; double length; functions setData() displayWidth() attributes member functions( methods) attributes member functions( methods) Procedural ProgrammingObject – Oriented Programming

Advantages For large programs, data format changes do not introduce bugs or extra work for the programmer Separates data from outside code, protecting the object’s algorithm. This is known as Data Hiding Only the objects member functions have access to the data Outside code interact only with the member functions, so changes to object can be made transparent to the outside code.

Object reusability: Makes sharing objects (buying and selling) easier

Classes and Objects A class is code that specifies the attributes and member functions that a particular type of object may have. It is a “blueprint”. A class is not an object. When the program is running, it uses the class to create, in memory, as many objects of that class as needed. Each of these objects is called an instance of the class. Classes we’ve seen before: The string class

Intro to classes Concept: In C++, the class is the construct primarily used to create objects They are similar to structures, however, unlike structures, the members of a class are private by default, meaning they cannot be accessed by outside code. class ClassName { declaration; // more declarations // may follow }; class Rectangle { double width; double length; };

Access Specifiers C++ provides the keywords private and public to specify how class members may be accessed. class ClassName { private: // Declaration of private // members appear here. public: // Declaration of public // members appear here. };

Public member functions To allow access to a class’s private member variables, you create public member functions that work with the private member variables class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; };

Tips the const keyword after the member function definition specifies to the compiler that the function will not change any data stored in the calling object. const must also appear in the function header There is no rule regarding placement of public and private members

Defining Member Functions //************************************************** // setWidth assigns a value to the width member. * //************************************************** void Rectangle::setWidth(double w) { width = w; } //************************************************** // setLength assigns a value to the length member. * //************************************************** void Rectangle::setLength(double len) { length = len; } //************************************************** // getWidth returns the value in the width member. * //************************************************** double Rectangle::getWidth() const { return width; } //**************************************************** // getLength returns the value in the length member. * //**************************************************** double Rectangle::getLength() const { return length; } //***************************************************** // getArea returns the product of width times length. * //***************************************************** double Rectangle::getArea() const { return width * length; } ReturnType ClassName::functionName(ParameterList)

Accessors and Mutators Accessor: A function that gets a value from a class’s member variable but does not change it (getter) Mutator: A member function that stores a value in member variables or changes the value of member variables in some other way (setter) You should use const with Accessors to help you avoid problems!

Defining an instance of a class Concept: Class objects must be defined after the class is declared You can access the objects members by using the dot (.) operator ClassName objectName; Rectangle box; box.setWidth(12.7);

// This program demonstrates a simple class. #include using namespace std; // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void Rectangle::setWidth(double w) { width = w; } //************************************************** // setLength assigns a value to the length member. * //************************************************** void Rectangle::setLength(double len) { length = len; } //************************************************** // getWidth returns the value in the width member. * //************************************************** double Rectangle::getWidth() const { return width; } //**************************************************** // getLength returns the value in the length member. * //**************************************************** double Rectangle::getLength() const { return length; } //***************************************************** // getArea returns the product of width times length. * //***************************************************** double Rectangle::getArea() const { return width * length; } //***************************************************** // Function main * //***************************************************** int main() { Rectangle box; // Define an instance of the Rectangle class double rectWidth; // Local variable for width double rectLength; // Local variable for length // Get the rectangle's width and length from the user. cout << "This program will calculate the area of a\n"; cout << "rectangle. What is the width? "; cin >> rectWidth; cout << "What is the length? "; cin >> rectLength; // Store the width and length of the rectangle // in the box object. box.setWidth(rectWidth); box.setLength(rectLength); // Display the rectangle's data. cout << "Here is the rectangle's data:\n"; cout << "Width: " << box.getWidth() << endl; cout << "Length: " << box.getLength() << endl; cout << "Area: " << box.getArea() << endl; return 0; }

// This program creates three instances of the Rectangle class. #include using namespace std; // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void Rectangle::setWidth(double w) { width = w; } //************************************************** // setLength assigns a value to the length member. * //************************************************** void Rectangle::setLength(double len) { length = len; } //************************************************** // getWidth returns the value in the width member. * //************************************************** double Rectangle::getWidth() const { return width; } //**************************************************** // getLength returns the value in the length member. * //**************************************************** double Rectangle::getLength() const { return length; } //***************************************************** // getArea returns the product of width times length. * //***************************************************** double Rectangle::getArea() const { return width * length; } //***************************************************** // Function main * //***************************************************** int main() { double number; // To hold a number double totalArea; // The total area Rectangle kitchen; // To hold kitchen dimensions Rectangle bedroom; // To hold bedroom dimensions Rectangle den; // To hold den dimensions // Get the kitchen dimensions. cout << "What is the kitchen's length? "; cin >> number; // Get the length kitchen.setLength(number); // Store in kitchen object cout << "What is the kitchen's width? "; cin >> number; // Get the width kitchen.setWidth(number); // Store in kitchen object // Get the bedroom dimensions. cout << "What is the bedroom's length? "; cin >> number; // Get the length bedroom.setLength(number); // Store in bedroom object cout << "What is the bedroom's width? "; cin >> number; // Get the width bedroom.setWidth(number); // Store in bedroom object // Get the den dimensions. cout << "What is the den's length? "; cin >> number; // Get the length den.setLength(number); // Store in den object cout << "What is the den's width? "; cin >> number; // Get the width den.setWidth(number); // Store in den object // Calculate the total area of the three rooms. totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea(); // Display the total area of the three rooms. cout << "The total area of the three rooms is " << totalArea << endl; return 0; }

Avoid Stale Data Do not store in member variables the results of calculated data that could become stale. Instead, provide a member function that returns the result of the calculation

Why Have Private Members? Concept: In OOP, an object should protect its important data by making it private and providing a public interface to access that data Mutators not only store, but can validate data

Separating Class Specifications from Implementations Concept: Usually classes declarations are stored in their own header files. Member definitions are stored in their own.cpp files Class specification file: A header file that contains the class declaration. The name of the class specification is usually the same as the name of the class, with a.h extension. Example: Rectangle.h Class definition file: The member functions for a class are stored in a separate.cpp file. The file usually has the same name as the class. Example: Rectangle.cpp

Any program that uses the class should #include the class’s header file #include “Rectangle.h” Example: Take the last example and separate it into Rectangle.h, Rectangle.cpp, and main.cpp

Inline Member Functions Concept: When the body of a member function is written inside a class definition, it is declared inline // Specification file for the Rectangle class // This version uses some inline member functions. #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const { return width; } double getLength() const { return length; } double getArea() const { return width * length; } }; #endif // Implementation file for the Rectangle class. // In this version of the class, the getWidth, getLength, // and getArea functions are written inline in Rectangle.h. #include "Rectangle.h" // Needed for the Rectangle class #include // Needed for cout #include // Needed for the exit function using namespace std; //*********************************************************** // setWidth sets the value of the member variable width. * //*********************************************************** void Rectangle::setWidth(double w) { if (w >= 0) width = w; else { cout << "Invalid width\n"; exit(EXIT_FAILURE); } //*********************************************************** // setLength sets the value of the member variable length. * //*********************************************************** void Rectangle::setLength(double len) { if (len >= 0) length = len; else { cout << "Invalid length\n"; exit(EXIT_FAILURE); }