Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
Example
4
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!
5
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
6
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
7
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 = ▭
8
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
9
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 }
10
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;
11
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.
12
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; }
13
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”};
14
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
15
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
16
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
17
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; }
18
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
19
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;
20
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 }
21
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
22
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.