SPL – PS4 C++ Advanced OOP.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object-Oriented Programming in C++ More examples of Association.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Object-Oriented Programming Chapter Chapter
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.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming Elhanan Borenstein Lecture #7.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 2 Objects and Classes
Web Design & Development Lecture 9
OOP: Encapsulation &Abstraction
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS 3370 – C++ Object-oriented Programming
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
Programming with ANSI C ++
Chapter 1 C++ Basics Review
University of Central Florida COP 3330 Object Oriented Programming
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Midterm Review Computer Graphics Hardware Point and Line Drawing
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Programming Language
Contents Introduction to Constructor Characteristics of Constructor
Lecture 22 Inheritance Richard Gesick.
Advanced Java Programming
Polymorphism CMSC 202, Version 4/02.
Practical Session 4 Rule of 5 R-value reference Unique pointer
Java Inheritance.
CISC/CMPE320 - Prof. McLeod
Objects Managing a Resource
Fundaments of Game Design
Chapter 8: Class Relationships
Overview of C++ Polymorphism
Java Programming Language
Web Design & Development Lecture 4
Final and Abstract Classes
Destructors, Copy Constructors & Copy Assignment Operators
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Polymorphism Reference and pointer implicit type casting
Destructors, Copy Constructors & Copy Assignment Operators
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

SPL – PS4 C++ Advanced OOP

The rule of 5 The rule of 3 has some problems. Starting from C++11, two additional functions were added to the rule of 3. Move constructor Move assignment operator Like the rule of 3, implies only for classes that manage resources

The destructor Similar to the destructor we’ve built in the previous PS

The move constructor Called when an object is initialized from an rvalue reference. Initialization std::string a = “str” Function argument passing v.push_back(“str”) Function return by value “Steals” resources from other object.

The move constructor (cont) A new charArray object will be created with the given string The move constructor will create a new object inside the vector, and will steal the string The pointer in the first created object will become nullptr The first object will be destroyed, since it’s pointer became null, the object on the vector won’t be affected.

The move assignment operator Similar to the copy assignment operator Like the move constructor, “steals” resources from other object

Smart pointers Included in the standard library Provides some garbage collection facility In this practical session we’ll talk about unique pointer

Unique pointer Provides limited garbage-collection facility Automatically deletes the pointer they manage, when they are destroyed or assigned another value. Can also delete the pointer they manage by calling unique_ptr::reset explicitly Owns their pointer uniquely, no other pointer should point to managed object

Unique pointer example

C++ Inheritance A relationship among classes, in which one class shares the structure or functionality of another class. In C++ there are three levels of inheritance visibility

C++ Inheritance example

C++ Inheritance example(cont)

C++ Inheritance example(cont)

C++ Inheritance types

Public Inheritance example isEmpty() is a part of the base class intArray Insert(int,int) is a part of the base class intArray but his functionality is extended in the derived class. biggest() exists only in the derived class

Private inheritance Makes all public and protected methods in base class private in derived class. Defines a “has-a” and not a “is-a” relation between base and derived class.

Constructor of derived class The C++ compiler generates a call to the default constructor of the base class, unless explicitly instructed otherwise. A better solution is to call directly to the CTOR of the base class, using the following syntax

Virtual functions Virtual functions are functions whose behavior can be overridden by derived class. Unlike non-virtual function, overridden behavior is preserved even if no compile-time data is available on the type of the class. If derived virtual function has a different access level then base function, their access level might not be respected.

Virtual function example

Virtual destructor Without garbage collector, polymorphism can be problematic when attempting to release memory.

Virtual destructor(cont) Can be fixed by declaring destructors virtual

Pure virtual functions and abstract classes Pure virtual functions don’t include implementation in their base class. Therefore, cannot make instantiation of base class.

Namespaces Similar to Java packages.

Namespaces(cont)