SPL – PS3 C++ Classes.

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
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.
Programming with ANSI C ++
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Constructor & Destructor
System Programming Practical Session 8 C++ classes.
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 9 Classes: A Deeper Look, Part 1
Constructors and destructors
Constructors and Destructors
9-10 Classes: A Deeper Look.
Java Programming Language
Classes: A Deeper Look, Part 1
COP 3330 Object-oriented Programming in C++
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Rule Of Three Part 3.
SPL – PS2 C++ Memory Handling.
Threads and concurrency / Safety
Introduction to Classes and Objects
Presentation transcript:

SPL – PS3 C++ Classes

Overview Object oriented programming in C++ Classes with resources Basic syntax Differences from Java

Parameter passing Parameters can be passed to a function in different ways. Correct parameter passing can reduce number of bugs and create a more efficient code. Parameters can be passed to a function by value, by reference or by pointer*. The same principles that apply to parameter passing also apply to function return values.

Parameter passing – Passing by value The value is being copied to the argument list of the called function. Usually (But not always) done for primitive values. When an object is passed by value, it’s copy constructor will be implicitly invoked. (Could be slow for large objects)

Parameter passing – Passing by pointer A pointer to the object is being copied to the function argument list. Can be used when we want the function to change the object. Can also pass null pointers. Actually passing by value.

Parameter passing – Passing by reference Allows us to send a link to the original object without using pointers. Usually safer than passing by pointer. Must be initialized at start, so cannot pass a NULL value.

C++ classes – header example

C++ classes – implementation example

Syntax differences between C++ and Java Separation of declaration and implementation - Done in cases when you want to share the class with another class. Semicolon at the end of the class declaration. Private and public sections, unlike Java where each method/variable visibility is declared separately. Const methods

Const methods const functions do not change the state of the object. Can only return const references and const pointers of object’s members. Can only access const methods. Once declared const, you can only use const methods on this variable.

Member initialization list Used to initialize class members. Executed before the body of the function. Not initializing in member initialization list – means implicit call to default constructor. Const members can only be initialized in member initialization list. It is a convention to keep the order of the list as the order of the declaration.

Reminder – Header files Contain declarations of variables, functions, and classes. Usually no implementation in header files. (Not allowed in this course) Use preprocessor variable definition to avoid including same header twice.

C++ objects In C++ objects hold values, not references. If no construction parameters supplied, default constructor implicitly invoked. When one object is assigned to another, an actual copy of the object is made.

C++ objects (cont) When modifying an object in a function, pass object by reference. Two objects cannot jointly access same object, use references for that effect. Object can only hold value of a particular type, use pointers if you want to hold objects from different sub-classes. If you want variable to be either null or an actual object, use pointers.

The this pointer The this pointer holds the address of the object instance. Not accessible from static member functions. The this pointer is not modifiable.

Operator . and operator ->

The rule of 3 A class that hold resources must define and implement the following functions. A destructor – tasked with freeing the resources held by the class. A copy constructor – tasked with creating a copy of the class. Copy assignment operator – copies other object data to override it’s own.

Destructor Called when the object is about to “go away”. When the object goes out of scope. When dynamically allocated object is freed. Frees dynamically allocated storage used by this object. Up to the programmer to make sure no other pointers exist for freed memory.

Copy constructor Called implicitly when an object initialized to be a copy of an existing object. When passed by value. When an object returned by value. When an object is initialized from an existing object of the same class.

Copy assignment operator If no copy assignment operator is overloaded, default copy assignment operator shallow copies class’s fields. Which can cause trouble if the class has pointer members. Returns a reference to the object. Copy assignment operator Copy constructor Object already initialized, must free memory to avoid memory leaks Initializes and creates the object. Self assignment is possible, and need to be checked for by the programmer Self assignment not possible Must return a reference to the object after assignment. Supports chained assignment. A constructor and hence, doesn’t return a value.

Exception safety in copy assignment operator If copying the other object threw an exception, that can lead to trouble.