Object Oriented Programming COP3330 / CGS5409

Size: px
Start display at page:

Download "Object Oriented Programming COP3330 / CGS5409"

Transcription

1 Object Oriented Programming COP3330 / CGS5409

2 C++ Automatics Copy constructor () Assignment operator = Shallow copy vs. Deep copy DMA Review c-strings vs. concept of string class

3 Constructor Destructor Copy Constructor Assignment operator = if you do not explicitly define one in a class, a default version will automatically be built for you by the compiler

4 The automatic versions of the constructor and destructor don't do anything, but they will be there if you do not build them. The constructor you get is the "default constructor" -- no parameters The automatic versions of the Copy Constructor and the Assignment operator overload are similar to each other, and their default versions are always built in a standard way.

5 A copy constructor IS a constructor, so it is a function with the same name as the class and no return type (just like any constructor). However, it is invoked implicitly when something is done that causes a COPY of an existing object to be created. This happens when: An object is defined to have the value of another object of the same type An object is passed by value into a function An object is returned (by value) from a function

6 Here's an example of item #1 in the above list: Fraction f1, f2(3,4); // declaration of two //fractions Fraction f3 = f2; // declaration of f3 being // initialized as a copy of f2 Note: This last line of code calls the copy constructor, since the initialization is on the same line as the declaration of f3. Contrast this with the following: f1 = f2; // this uses the assignment // operator, since f1 and f2 already exist

7 Since the purpose of a copy constructor is to not only initialize the data in an object, but to initialize it as a copy of another existing object, the original object must be passed in as a parameter. So, a copy constructor always has one parameter, which is of the same type as the class itself. It is always passed by reference, as well (it has to be - - since to pass by value, we must invoke a copy constructor, and this is what we are defining!) Format: classname(const classname &);

8 The const is not required, but it is usally a good idea, because we only want to make a copy -- we don't want to change the original. Here are some examples of copy constructor declarations for classes we have seen: Fraction(const Fraction & f); Timer(const Timer & t); Directory(const Directory & d); Store(const Store & s);

9 The default version of the copy constructor (created by the compiler) makes what is known as a shallow copy. This simply means that the object is copied exactly as is -- each member data value is copied exactly over to the corresponding member data location in the new object. This is sufficient for many cases, but not for ALL cases.

10 Example: Fraction f1(3,4); This fraction object has a numerator of 3 and a denominator of 4. If this object is passed into a function by value, a copy will be made, and the new object's numerator will be 3, denominator 4. In this case, the shallow copy is sufficient.

11 Consider, however, the Directory class of the phone book example. The member data variables were currentsize and maxsize (both of type int), and a pointer, entrylist (of type Entry * ), which pointed to dynamically allocated data outside the actual object. This is the situation in which a shallow copy is not sufficient.! For instance, if the original object is storing the address 1024 in entrylist, the copy will also get the 1024, and therefore the copy will be pointing to the original dynamic data!

12 This will especially pose problems if, when the copy goes out of scope, it cleans up the dynamic data along with it. When there is a pointer (inside an object) that points to dynamic data, the shallow copy is not sufficient, because it does not copy the dynamic data, only the pointer. A deep copy is needed. The following slide is what we might write for a copy constructor definition in the Directory class (from the phonebook database example):

13 Directory::Directory(const Directory & d) // copies object 'd' into the new object being created (this one) { // copy the static variables normally maxsize = d.maxsize; currentsize = d.currentsize; // create a new dynamic array for the // new object's pointer entrylist = new Entry[d.maxsize]; // copy the dynamic data } for (int i = 0; i < currentsize; i++) entrylist[i] = d.entrylist[i];

14 The assignment operator = is similar to the copy constructor. It is called when one object is assigned to another. Example call: Fraction f1, f2; f1 = f2; // this call invokes the //assignment operator Like the copy constructor, the assignment operator has to make a copy of an object. The default version makes a shallow copy. If a deep copy is desired for assignments on a user-defined type (e.g. a class), then the assignment operator should be overloaded for the class. The task done by the assignment operator is very similar to that of a copy constructor, but there are a couple of differences.

15 The copy constructor is initializing a brand new object as a copy of an existing one. The new object's data is being initialized for the first time. An assignment operator sets an existing object's state to that of another existing object. In situations with dynamic allocation, this may mean that old dynamic space must be cleaned up first before the copy is made. Also, an assignment operator also returns the value that was assigned (the copy constructor has no return). Consider the case of integers. In the statement: a = b = c = 4; The first operation is (c = 4), and this operation returns the assigned value (4), so that the result can be used as an operand in the next assignment (b = 4). This value should be returned by reference when overloading = for objects. To return the object, we need to be able to refer to an object from inside the object itself.

16 From inside any member function, an object has access to its own address through a pointer called this, which is a keyword in C++. In an assignment operator, you must return the object itself (by reference), so you can return the target of the this pointer (which would be *this) Like the copy constructor, the original object needs to be passed in, so there will be one parameter (of the same type as the object itself). The parameter is the same as in the copy constructor. Declaration examples for a few classes: Directory& operator= (const Directory &); Fraction& operator=(const Fraction &); Timer& operator=(const Timer &); Circle& operator=(const Circle &);

17 Directory& Directory::operator=(const Directory & d); // copies object 'd' into the new object being created (this one) { if (this!= &d) // only copy if object passed is not this one { // since this is not a brand new object, we // should delete any information currently attached delete [] entrylist; } // similar to the copy constructor definition maxsize = d.maxsize; currentsize = d.currentsize; entrylist = new Entry[d.maxsize]; for (int i = 0; i < currentsize; i++) entrylist[i] = d.entrylist[i]; } return *this; // return the object itself (by reference)

18 This example shows a class called PFArrayD, which is declared in the file "pfarrayd.h" and defined in "pfarrayd.cpp". This class stores a list of values of type double -- the list is stored using a dynamically allocated array, so there is no size limit. There are tracking variables in the member data for keeping track of the allocated space and the used space. The class also has a copy constructor and assignment operator -- both of them to do the "deep copy". And of course the destructor cleans up the space. The file "10-12.cpp" contains a main program that demonstrates some of the class features.

19 0-10,%20-11,%20-12/Chapter%2011%20Version/ In this particular example, once the capacity of the array is set (when the object is created), it becomes the upper limit of storage for the list. How to use the array resizing technique to add functionality to the class? Specifically, to be able to remove the boundary check in the addelement function, and have no upper limit on the size of the list of values. (This would entail writing a Grow function, or perhaps a more generic "Resize" function -- to allow the allocated space to vary -- then calling it from appropriate places).

20 h11/fig11_17_18/ This is similar to the previous example, involving dynamic allocation inside a class. It is used to build a safer array type -- the class in this one is called Array, and it stores a dynamically created integer array. Both use dynamic memory management, but they vary a little in specific implementation details.

21 7_18/ Some highlights of this example: Dynamic creation of array in the constructor Copy constructor and assignment operator, for deep copy Operator overloads provided (equality comparisons and for I/O) Two subscript operators (the brackets [] ). Note: when bracket operators are provided, the programmer does have the option to provide TWO versions -- one that returns by reference, and one that returns by value or by const reference (and must be a const member function). The difference is that one will return an L-value (storage location that could be modified), and one will return only an R-value (a read-only value).

22

Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array

Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array Recursion review Thinking recursion: if I know the solution of the problem of size N-, can I use that to solve the problem of size N. Writing recursive routines: Decide the prototype Can formulate both

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Dynamic Allocation in Classes Review of CStrings Allocate dynamic space with operator new, which returns address of the allocated item. Store in a pointer:

More information

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Special Member Functions

Special Member Functions CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) U Waterloo CS247 (Spring 2017)

More information

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil Midterm Exam #2 Review CS 2308 :: Spring 2016 Molly O'Neil Midterm Exam #2 Wednesday, April 13 In class, pencil & paper exam Closed book, closed notes, no cell phones or calculators, clean desk 20% of

More information

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Constructors and Destructors OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani A constructor guarantees that an object created by the class will be initialized automatically. Ex: create an object integer

More information

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F Announcements Test next week (whole class) Covers: -Arrays -Functions -Recursion -Strings -File I/O Highlights - Destructors - Copy constructors -

More information

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions Special Member Functions CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) C++

More information

Programming in C ++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C ++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C ++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 27 Copy Constructor and Copy Assignment Operator (Contd.) Welcome

More information

Dynamic Memory Allocation

Dynamic Memory Allocation Dynamic Memory Allocation Lecture 15 COP 3014 Fall 2017 November 6, 2017 Allocating memory There are two ways that memory gets allocated for data storage: 1. Compile Time (or static) Allocation Memory

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer October 10, 2016 OOPP / C++ Lecture 7... 1/15 Construction and Destruction Kinds of Constructors Move Semantics OOPP / C++ Lecture 7... 2/15

More information

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1 CS32 - Week 1 Umut Oztok June 24, 2016 Administration Email:umut@ucla.edu Office hours: R 09:30am-12:30pm (BH 4663) Constructor Special member function to initialize an instance of a class. Called whenever

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Classes & Objects DDU Design Constructors Member Functions & Data Friends and member functions Const modifier Destructors Object -- an encapsulation of data

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

Implementing Abstractions

Implementing Abstractions Implementing Abstractions Pointers A pointer is a C++ variable that stores the address of an object. Given a pointer to an object, we can get back the original object. Can then read the object's value.

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

Constructors.

Constructors. Constructors Initializing New Objects Fraction g(4, 5); Initializing New Objects Fraction g(4, 5); Initializing New Objects Fraction g(4, 5); Initializing New Objects Fraction g(4, 5); Initializing New

More information

Launchpad Lecture -10

Launchpad Lecture -10 Saturday, 24 September Launchpad Lecture -10 Dynamic Allocation, Object Oriented Programming-1 Prateek Narang 2 Recap 1. How to define pointers? 2. Address of Operator? 3. Dereference operator? 4. Arithmetic

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

Exam 3 Chapters 7 & 9

Exam 3 Chapters 7 & 9 Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False

More information

More class design with C++ Starting Savitch Chap. 11

More class design with C++ Starting Savitch Chap. 11 More class design with C++ Starting Savitch Chap. 11 Member or non-member function? l Class operations are typically implemented as member functions Declared inside class definition Can directly access

More information

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I Polynomial Class Polynomial(); Polynomial(const string& N, const vector& C); Polynomial operator+(const Polynomial& RHS) const; Polynomial operator-(const Polynomial& RHS) const; Polynomial operator*(const

More information

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University (5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University Key Concepts Function templates Defining classes with member functions The Rule

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED. Constructor and Destructor Member Functions Constructor: - Constructor function gets invoked automatically when an object of a class is constructed (declared). Destructor:- A destructor is a automatically

More information

04-19 Discussion Notes

04-19 Discussion Notes 04-19 Discussion Notes PIC 10B Spring 2018 1 Constructors and Destructors 1.1 Copy Constructor The copy constructor should copy data. However, it s not this simple, and we need to make a distinction here

More information

CSCI 262 Data Structures. The Big 3. Copy Constructor. Destructor. Assignment Operator 3/4/ The Big 3

CSCI 262 Data Structures. The Big 3. Copy Constructor. Destructor. Assignment Operator 3/4/ The Big 3 CSCI 262 Data Structures 12 The Big 3 What are THE BIG 3 The Big 3 Three (optional) methods for your class: Copy constructor: creates copies of object When passing by value When used in variable initializer

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

Assignment of Structs

Assignment of Structs Deep Copy 1 Assignment of Structs 2 Slides 1. Table of Contents 2. Assignment of Structs 3. Dynamic Content 4. Shallow Copying 5. Assignment Operator 6. Deep Assignment Copy 7. Assignment Problems 8. Assignment

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer September 26, 2016 OOPP / C++ Lecture 4... 1/33 Global vs. Class Static Parameters Move Semantics OOPP / C++ Lecture 4... 2/33 Global Functions

More information

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a. Intro to OOP - Object and class - The sequence to define and use a class in a program - How/when to use scope resolution operator - How/when to the dot operator - Should be able to write the prototype

More information

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo Chapter 10 Pointers and Dynamic Arrays 1 Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 33 Overloading Operator for User - Defined Types: Part 1 Welcome

More information

Operator overloading. Conversions. friend. inline

Operator overloading. Conversions. friend. inline Operator overloading Conversions friend inline. Operator Overloading Operators like +, -, *, are actually methods, and can be overloaded. Syntactic sugar. What is it good for - 1 Natural usage. compare:

More information

Name: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Name: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015 CMSC 202 Section 06 Fall 2015 Computer Science II Midterm Exam II Name: Username: Score Max Section: (check one) 07 - Sushant Athley, Tuesday 11:30am 08 - Aishwarya Bhide, Thursday 11:30am 09 - Phanindra

More information

Assignment operator string class c++ Assignment operator string class c++.zip

Assignment operator string class c++ Assignment operator string class c++.zip Assignment operator string class c++ Assignment operator string class c++.zip Outside class definitions; Addition assignment: a += b: The binding of operators in C and C++ is specified (character string)

More information

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil More About Classes Gaddis Ch. 14, 13.3 CS 2308 :: Fall 2015 Molly O'Neil Pointers to Objects Just like pointers to structures, we can define pointers to objects Time t1(12, 20, true); Time *tptr; tptr

More information

Intermediate Programming & Design (C++) Classes in C++

Intermediate Programming & Design (C++) Classes in C++ Classes in C++ A class is a data type similar to a C structure. It includes various local data (called data members) together with constructors, destructors and member functions. All of them are called

More information

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2 Steven J. Zeil November 13, 2013 Contents 1 Destructors 2 2 Copy Constructors 11 2.1 Where Do We Use a Copy Constructor? 12 2.2 Compiler-Generated Copy Constructors............................................

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer May 13, 2013 OOPP / C++ Lecture 7... 1/27 Construction and Destruction Allocation and Deallocation Move Semantics Template Classes Example:

More information

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 10 Pointers and Dynamic Arrays Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic

More information

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell

More information

Initializing and Finalizing Objects

Initializing and Finalizing Objects 4 Initializing and Finalizing Objects 147 Content Initializing and Finalizing Objects 4 Constructors Default Constructor Copy Constructor Destructor 148 Initializing Objects: Constructors Initializing

More information

CGS 2405 Advanced Programming with C++ Course Justification

CGS 2405 Advanced Programming with C++ Course Justification Course Justification This course is the second C++ computer programming course in the Computer Science Associate in Arts degree program. This course is required for an Associate in Arts Computer Science

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428

More information

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Classes and Objects. Class scope: - private members are only accessible by the class methods. Class Declaration Classes and Objects class class-tag //data members & function members ; Information hiding in C++: Private Used to hide class member data and methods (implementation details). Public

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

explicit class and default definitions revision of SC22/WG21/N1582 =

explicit class and default definitions revision of SC22/WG21/N1582 = Doc No: SC22/WG21/ N1702 04-0142 Project: JTC1.22.32 Date: Wednesday, September 08, 2004 Author: Francis Glassborow & Lois Goldthwaite email: francis@robinton.demon.co.uk explicit class and default definitions

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

More information

Assignment of Objects

Assignment of Objects Copying Objects 1 Assignment of Objects 2 Slides 1. Table of Contents 2. Assignment of Objects 3. Dynamic Content 4. Shallow Copying 5. Deep Copying 6. this Pointer 7. Improved Deep Copy 8. Passing an

More information

Separate Compilation Model

Separate Compilation Model Separate Compilation Model Recall: For a function call to compile, either the function s definition or declaration must appear previously in the same file. Goal: Compile only modules affected by recent

More information

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture!

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture! Dynamic Memory In Classes [Chapter 4, p 156-157] Class Vector: Interface Vector ( ); bool isempty( ); int length ( ); void vectorinsert (int newposition, Item newitem); Item vectordelete (int position);

More information

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Review: C++ Basic Concepts. Dr. Yingwu Zhu Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to

More information

CSCI 123 Introduction to Programming Concepts in C++

CSCI 123 Introduction to Programming Concepts in C++ CSCI 123 Introduction to Programming Concepts in C++ Brad Rippe Brad Rippe More Classes and Dynamic Arrays Overview 11.4 Classes and Dynamic Arrays Constructors, Destructors, Copy Constructors Separation

More information

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Assignment 1: grid. Due November 20, 11:59 PM Introduction CS106L Fall 2008 Handout #19 November 5, 2008 Assignment 1: grid Due November 20, 11:59 PM Introduction The STL container classes encompass a wide selection of associative and sequence containers. However,

More information

Chapter 13: Copy Control. Overview. Overview. Overview

Chapter 13: Copy Control. Overview. Overview. Overview Chapter 13: Copy Control Overview The Copy Constructor The Assignment Operator The Destructor A Message-Handling Example Managing Pointer Members Each type, whether a built-in or class type, defines the

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

C++ (classes) Hwansoo Han

C++ (classes) Hwansoo Han C++ (classes) Hwansoo Han Inheritance Relation among classes shape, rectangle, triangle, circle, shape rectangle triangle circle 2 Base Class: shape Members of a class Methods : rotate(), move(), Shape(),

More information

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n Static and Dynamic Memory Allocation In this chapter we review the concepts of array and pointer and the use of the bracket operator for both arrays and pointers. We also review (or introduce) pointer

More information

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory Outline 1 Chapter 10: C++ Dynamic Memory Proper Memory Management Classes which must allocate memory must manage it properly. Default behavior of operations in C++ are insufficient for this. The assignment

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects Vectors of Objects As we have mentioned earlier, you should almost always use vectors instead of arrays. If you need to keep track of persons (objects of class Person), you must decide what to store in

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

IS0020 Program Design and Software Tools Midterm, Fall, 2004

IS0020 Program Design and Software Tools Midterm, Fall, 2004 IS0020 Program Design and Software Tools Midterm, Fall, 2004 Name: Instruction There are two parts in this test. The first part contains 22 questions worth 40 points you need to get 20 right to get the

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015 CMSC 202 Section 06 Fall 2015 Computer Science II Midterm Exam I Name: Username: Score Max Section: (check one) 07 - Sushant Athley, Tuesday 11:30am 08 - Aishwarya Bhide, Thursday 11:30am 09 - Phanindra

More information

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once. CSE 12, Week Seven, Lecture One Iterator: A way to sequentially access ( traverse ) each object in a container or collection - Access is done as needed, not necessarily at once. - Implemented by a private

More information

CLASSES AND OBJECTS. Summer 2018

CLASSES AND OBJECTS. Summer 2018 CLASSES AND OBJECTS Summer 2018 OBJECT BASICS Everything in Java is part of a class This includes all methods (functions) Even the main() function is part of a class Classes are declared similar to C++

More information

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington CSE 333 Lecture 11 - constructor insanity Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia Exercises: - New exercise out today, due Monday morning

More information

Written by John Bell for CS 342, Spring 2018

Written by John Bell for CS 342, Spring 2018 Advanced OO Concepts Written by John Bell for CS 342, Spring 2018 Based on chapter 3 of The Object-Oriented Thought Process by Matt Weisfeld, with additional material from other sources. Constructors Constructors

More information

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017 Next week s homework Classes: Methods, Constructors, Destructors and Assignment Read Chapter 7 Your next quiz will be on Chapter 7 of the textbook For : COP 3330. Object oriented Programming (Using C++)

More information

Classes in C++98 and C++11

Classes in C++98 and C++11 Classes in C++98 and C++11 January 10, 2018 Brian A. Malloy Slide 1 of 38 1. When we refer to C++98, we are referring to C++98 and C++03, since they differ only slightly. C++98 contained 3 types of constructors,

More information

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology Constructors and destructors 1 1 Department of Computer Science Poznan University of Technology 2012.10.07 / OOP Laboratory Outline 1 2 3 4 5 Outline 1 2 3 4 5 Outline 1 2 3 4 5 Outline 1 2 3 4 5 Outline

More information

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation Review from Lectures 7 Algorithm Analysis, Formal Definition of Order Notation Simple recursion, Visualization

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object

More information

Concepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.

Concepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups. CSC 143 Classes with Dynamically Allocated Data Concepts (this lecture) Constructors and destructors for dynamic data The this pointer Deep versus shallow copy Defining assignment (operator=) Copy constructor

More information

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods. Classes: Methods, Constructors, Destructors and Assignment For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Classes: Member functions // classes

More information

Recharge (int, int, int); //constructor declared void disply();

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 08 Constants and Inline Functions Welcome to module 6 of Programming

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 6, 2017 Outline Outline 1 Chapter 10: C++ Dynamic Memory Outline 1 Chapter 10: C++ Dynamic Memory Proper Memory Management

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++ Introduction to Programming in C++ Course Text Programming in C++, Zyante, Fall 2013 edition. Course book provided along with the course. Course Description This course introduces programming in C++ and

More information

C++ 8. Constructors and Destructors

C++ 8. Constructors and Destructors 8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of

More information

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay Classes - 2 Data Processing Course, I. Hrivnacova, IPN Orsay OOP, Classes Reminder Requirements for a Class Class Development Constructor Access Control Modifiers Getters, Setters Keyword this const Member

More information

Constructors for classes

Constructors for classes Constructors for Comp Sci 1570 Introduction to C++ Outline 1 2 3 4 5 6 7 C++ supports several basic ways to initialize i n t nvalue ; // d e c l a r e but not d e f i n e nvalue = 5 ; // a s s i g n i

More information

Pointers Ch 9, 11.3 & 13.1

Pointers Ch 9, 11.3 & 13.1 Pointers Ch 9, 11.3 & 13.1 Highlights - const & passing-by-referencence - pointers - new and delete object vs memory address An object is simply a box in memory and if you pass this into a function it

More information

Advanced Systems Programming

Advanced Systems Programming Advanced Systems Programming Introduction to C++ Martin Küttler September 19, 2017 1 / 18 About this presentation This presentation is not about learning programming or every C++ feature. It is a short

More information

... IntArray B (100); // IntArray with 100 elements, each initialized to 0

... IntArray B (100); // IntArray with 100 elements, each initialized to 0 Types with External Resources A class constructor is invoked when an object comes into scope. The constructor prepares the object by creating an environment in which the member functions operate. For many

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

struct Properties C struct Types

struct Properties C struct Types struct Properties 1 The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct types encapsulate data members struct Location

More information

Programming in C and C++

Programming in C and C++ Programming in C and C++ 5. C++: Overloading, Namespaces, and Classes Dr. Neel Krishnaswami University of Cambridge (based on notes from and with thanks to Anil Madhavapeddy, Alan Mycroft, Alastair Beresford

More information

Constructor - example

Constructor - example Constructors A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked whenever

More information