classes and objects review

Slides:



Advertisements
Similar presentations
Chapter 13 – Introduction to Classes
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
C++ Classes & Data Abstraction
Classes and Objects April 6, Object Oriented Programming Creating functions helps to make code that we can reuse. When programs get large it becomes.
Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes.
Programming Languages and Paradigms Object-Oriented Programming.
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.
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 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Memory Management.
Motivation for Generic Programming in C++
Chapter 12 Classes and Abstraction
Chapter 16: Linked Lists.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
C++ Programming:. Program Design Including
COMP 53 – Week Eight Linked Lists.
Classes C++ representation of an object
Pointers and Linked Lists
Pointers and Linked Lists
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Introduction to Linked Lists
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Andy Wang Object Oriented Programming in C++ COP 3330
A first Look at Classes.
Review: Two Programming Paradigms
Introduction to Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
LinkedList Class.
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
– Introduction to Object Technology
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Introduction to Linked Lists
C++ Classes & Object Oriented Programming
Introduction to Classes
Chapter 18: Linked Lists.
Classes and Data Abstraction
Doubly Linked List Implementation
Today’s topics UML Diagramming review of terms
COP 3330 Object-oriented Programming in C++
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes C++ representation of an object
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Doubly Linked List Implementation
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

classes and objects review A more advanced way to capture “Object-Oriented Programming” Encapsulate the attributes and behavior of an object E.g. a car, a person, , a document Provide an interface to create and manipulate data abstractions Inheritance, polymorphism (to come later) E.g. a linked list “hides” the complex structure from the user and “feels like” using a regular array, but with extra flexibility

Objects Allows securing the object attributes against outside tampering Imagine the “simple” interface to drive a vehicle: it “hides” very complex functionality from the user A “Class” is a description of how to construct an instance of a particular object

Example

Rectangle Class class Rectangle { private: double width; double length; public: void setWidth(double) double getWidth() const; void setLength(double); double getLength() const; double getArea() const { return this.width * this.length; } }; Can have either functions or variables in private and public Functions can be defined later, and only prototypes in the class definition Similar to `const` for variables, means the function cannot change the internal state of the object. Only accessible from member functions! Only accessible from member functions or outside code!

Member functions To define members outside the class: void Rectangle::setLength(double len){ length = len; } Scope resolution operator puts `length` in scope. Is the instance variable associated to the rectangle object

Calling member functions Usage: the “dot operator” Rectangle myRect; myRect.setLength(10.5); myRect.length = 10.5; // Compiler error! Out of scope myRect.setWidth(10); double x = myRect.getLength(); // will be assigned 10.5 double area = myRect.getArea(); // Calculates from internal data

Object Pointers Declaring a variable as `ClassName variableName` works directly with the object It is often more efficient to work with pointers: Rectangle* myRect = nullptr; Rectangle rect; myRect = ▭

Object Pointers When using object pointers, need to dereference to use the member functions and variables using the `->` operator Performs dereference and calls the right hand argument on the resulting object Rectangle *myRect = ▭ double x = myRect->getArea(); // Same as: double x = (*myRect).getArea(); delete myRect; // reclaim memory myRect = nullptr; // don’t allow future access

Constructors Use the ”new” operator to keep your objects on the heap! Rectangle *rect = new Rectangle(); // Calls the class constructor Defined as a member function of the class: Rectangle::Rectangle(){ // No return type! // .. Code here gets called when a new Rectangle is instantiated. // code has access to the “this” variable when it runs }

Constructors Useful for setting and overriding class default values for attributes Ex. Rectangle::Rectangle(){ width = 0; length = 0; } Rectangle::Rectangle(double wid, double len){ width = wid; length = len;

constructors Can be called with “new” operator to get a pointer Rectangle* rect = new Rectangle(); Can be called to create the object by value Rectangle myRect(10.3, 5.0); Note: the constructor with no parameters is called the default constructor. If there is none, then you must instantiate with one of the defined constructors, or will result in a compiler error.

destructors Similar to constructor, gets run when object is dynamically allocated and then freed via the “delete” operator Definition: Rectangle::~Rectangle(){ // Code to clear any dynamically allocated members // Called, e.g., on: delete myRect; }

Object Arrays It is possible to put objects into an array: Rectangle rects[10]; Rectangle rects2[2] = {Rectangle(5,10), Rectangle(14,2)}; If you have a single parameter constructor, can use an initializer list: // Will call MyClass(string) with each of the following, and aggregate // into the array classArr MyClass classArr[3] = {“Object1Param”, “Object2Param”, “Object3Param”};

Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management We can do dynamic arrays, but can be cumbersome to add more cells if needed Internal re-structuring With an array, to insert or delete elements after array creation and population, we must move potentially many elements around

Linked lists Each element (node) of the list will have a pointer to the next one. There will always be a list “head” which is just a pointer to the first element of the list

Simple Linked List class Will be defined in an ad-hoc way, for a list of a specific data type Needs to have certain operations: Insert Delete Append Print

Basic Singly Linked list class NumberList { private: struct ListNode{ double value; struct ListNode *next; }; ListNode *head; public: NumberList(){ head = nullptr; } ~NumberList(); void appendNode(double); void insertNode(double); void deleteNode(double); bool contains(double); void displayList() const; }

List Append General algorithm: Start with the head of the list, if it is null, point it to the new element If the head was not null, check the *next pointer to see if it is null. If it is null, point it at the new element If it is not null, Repeat step 2 on this element Head / 5 myList->append(6) cursor 6

List search Given: element value “x” to determine if it is present Iterate each element of the array, using the “next” pointer If the current node is not “x”, move to the “next” element and repeat If the current node is “x”, return true myList->contains(-2) / 3 -2 10 cursor return true;

List (linear) Search bool NumberList::contains(double val){ ListNode *cursor = head; while ( cursor && cursor->value != val ) cursor = cursor->next; return cursor != nullptr; // will be null at list end }

List Insert General algorithm: If the list is empty, point head at the new element Otherwise, find the node “before” the new one (or as specified) Set the *next pointer of the new element to the previous element’s *next Set the *next of the previous element to the new element Head / 5 10 cursor myList->insertAfter(5,6) 6

list delete Given: value “x” of element we want to remove Procedure: Use same search as before, but modify so that when you find the element, cut it out of the list If you find “x” Join the previous node’s “next” pointer to x’s next node Delete the object from memory myList->delete(-2) / 3 -2 10 cursor