lecture04: Constructors and Destructors

Size: px
Start display at page:

Download "lecture04: Constructors and Destructors"

Transcription

1 lecture04: Largely based on slides by Cinda Heeren CS 225 UIUC 13th June, 2013

2 Announcements lab debug due Saturday night (6/15) mp1 due Monday night (6/17)

3 Warmup: what happens? main.cpp */ void changegrade(student stu) if(stu.getgrade() == F ) stu.setgrade( A ); void main() Student s; s.setgrade( F ); while(s.getgrade() == F ) changegrade(s); student.h */ #ifndef STUDENT_H #define STUDENT_H class Student public: void setgrade(char letter); char getgrade() const; private: char grade; ; #endif

4 Another warmup: what happens here? PNG* allocate(size_t w, size_t h) PNG ret(w, h); return &ret; int main() PNG* image = allocate(256, 512); image->writetofile("blank.png"); delete image; return 0;

5 Motivation are a mechanism for running a function during an object s creation They are not explicitly called; rather, the system invokes them when required If no constructors are written, a default no-argument constructor is provided by the system Sphere s(4.0); Student joe("joe Ciurej", A ); Student chase("chase Geigle", D ); Student tom("tom Bogue", Q );

6 Writing constructors sphere.h */ #ifndef SPHERE_H #define SPHERE_H class Sphere public: Sphere(double newradius); void setradius(double newradius); private: double radius; ; #endif sphere.cpp */ #include "sphere.h" Sphere::Sphere(double newradius) radius = newradius; Sphere::setRadius(double newradius) radius = newradius;

7 A shortcut for constructors Usually, constructors just set a bunch of variables. Therefore, there is a succint syntax called an initializer list that does just that: // one-parameter constructor Sphere::Sphere(double newradius): radius(newradius) /* nothing */ // "default" constructor sets the radius to zero Sphere::Sphere(): radius(0.0) /* nothing */ // this constructor takes two arguments Sphere::Sphere(double newradius, const std::string & newcolor): radius(newradius), color(newcolor) /* nothing */ Note that we still need the braces after the function declaration even if there are no statements in the function.

8 Course staff is questionable What should the two-parameter constructor look like to enable the following functionality of the Student class? Student jason("jason Cho", F ); Can you write both implementations: initializer list and function body? Hint: Jason Cho is a string object.

9 Notes on constructors If you write any constructor, the system no longer provides a default constructor If you do not set member variables in the constructor, their values will be uninitialized...so you should always set all your member variables in the constructor!

10 Motivation: what happens here? sphere.h */ #ifndef SPHERE_H #define SPHERE_H main.cpp */ #include "sphere.h" void main() Sphere s(4.0); // or... class Sphere public: Sphere(double newradius); private: double* radius; ; #endif Sphere* s = new Sphere(4.0); delete s; sphere.cpp */ #include "sphere.h" Sphere::Sphere(double newradius) radius = new double(newradius);

11 Our first destructor sphere.h */ #ifndef SPHERE_H #define SPHERE_H class Sphere public: Sphere(double newradius); ~Sphere(); private: double* radius; ; #endif sphere.cpp */ #include "sphere.h" Sphere::Sphere(double newradius) radius = new double(newradius); Sphere::~Sphere() delete radius;

12 , not deconstructors are the opposite of constructors: they are run when an object on the stack goes out of scope or when an object on the heap is deleted are not explicitly called by the user; the system calls them as necessary Think of it this way: calling delete on a pointer to an object on the heap invokes the destructor If your object allocates any dynamic memory, then you need a destructor!

13 The Collage class collage.h */ #ifndef COLLAGE_H #define COLLAGE_H #include <iostream> #include "png.h" using namespace std; class Collage public: Collage(size_t numpics); ~Collage(); private: PNG* pics; ; collage.cpp */ #include "collage.h" Collage::Collage(size_t numpics) cout << "Created!" << endl; pics = new PNG[numPics]; Collage::~Collage() cout << "Destructed!" << endl; delete [] pics; #endif

14 What is printed in each of these functions? void onstack() cout << "Creating A..." << endl; Collage cola(24); cout << "Creating B..." << endl; Collage colb(12); void onheap() Collage* col = new Collage(4); delete col;

15 Bonus: RAII Resource Acquisition is Initialization (RAII) is a C++ idiom The constructor acquires a resource, and the destructor frees it Useful for writing exception-safe C++ code Examples: Controlling locks in multithreaded applications Opening files (acquiring file descriptors) Smart pointers Can you think of any more?

CS 225. Data Structures

CS 225. Data Structures CS 5 Data Structures 1 2 3 4 5 6 7 8 9 10 11 #include using namespace std; int main() { int *x = new int; int &y = *x; y = 4; cout

More information

CS 225. Data Structures. Wade Fagen-Ulmschneider

CS 225. Data Structures. Wade Fagen-Ulmschneider CS 225 Data Structures Wade Fagen-Ulmschneider 5 6 7 8 9 10 11 int *x; int size = 3; x = new int[size]; for (int i = 0; i < size; i++) { x[i] = i + 3; delete[] x; heap-puzzle3.cpp Upcoming: Theory Exam

More information

CS 225. Data Structures. Wade Fagen-Ulmschneider

CS 225. Data Structures. Wade Fagen-Ulmschneider CS 225 Data Structures Wade Fagen-Ulmschneider 11 15 16 17 18 19 20 21 22 23 24 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(const

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

lecture30: Beyond CS 225

lecture30: Beyond CS 225 lecture30: Beyond CS 225 Sean Massung CS 225 UIUC 31st July, 2013 Announcements lab graphs due tomorrow, 8/1 at 11pm final exam this Friday (8/2), 10:30am-12:30pm in this room Anything but Hello World

More information

lecture09: Linked Lists

lecture09: Linked Lists lecture09: Largely based on slides by Cinda Heeren CS 225 UIUC 24th June, 2013 Announcements mp2 due tonight mt1 tomorrow night! mt1 review instead of lab tomorrow morning mp3 released tonight, mp3.1 extra

More information

Important when developing large programs. easier to write, understand, modify, and debug. each module understood individually

Important when developing large programs. easier to write, understand, modify, and debug. each module understood individually Chapter 3: Data Abstraction Abstraction, modularity, information hiding Abstract data types Example-1: List ADT Example-2: Sorted list ADT C++ Classes C++ Namespaces C++ Exceptions 1 Fundamental Concepts

More information

Memory, Arrays, and Parameters

Memory, Arrays, and Parameters lecture02: Largely based on slides by Cinda Heeren CS 225 UIUC 11th June, 2013 Announcements hw0 due tomorrow night (6/12) Linux tutorial tonight in the lab mp1 released tomorrow night (due Monday, 6/17)

More information

Introducing C++ to Java Programmers

Introducing C++ to Java Programmers Introducing C++ to Java Programmers by Kip Irvine updated 2/27/2003 1 Philosophy of C++ Bjarne Stroustrup invented C++ in the early 1980's at Bell Laboratories First called "C with classes" Design Goals:

More information

04-17 Discussion Notes

04-17 Discussion Notes 04-17 Discussion Notes PIC 10B Spring 2018 1 RAII RAII is an acronym for the idiom Resource Acquisition is Initialization. What is meant by resource acquisition is initialization is that a resource should

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

CS93SI Handout 04 Spring 2006 Apr Review Answers

CS93SI Handout 04 Spring 2006 Apr Review Answers CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly

More information

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1) lecture12: Largely based on slides by Cinda Heeren CS 225 UIUC 27th June, 2013 Announcements mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1) Source: http://i.dailymail.co.uk/i/pix/2010/08/04/article-1300471-0ab0a1d2000005dc-451

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

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

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Class Destructors constant member functions

Class Destructors constant member functions Dynamic Memory Management Class Destructors constant member functions Shahram Rahatlou Corso di Programmazione++ Roma, 6 April 2009 Using Class Constructors #include Using std::vector; Datum average(vector&

More information

Constants, References

Constants, References CS 246: Software Abstraction and Specification Constants, References Readings: Eckel, Vol. 1 Ch. 8 Constants Ch. 11 References and the Copy Constructor U Waterloo CS246se (Spring 2011) p.1/14 Uses of const

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

Object Reference and Memory Allocation. Questions:

Object Reference and Memory Allocation. Questions: Object Reference and Memory Allocation Questions: 1 1. What is the difference between the following declarations? const T* p; T* const p = new T(..constructor args..); 2 2. Is the following C++ syntax

More information

A class is a user-defined type. It is composed of built-in types, other user-defined types and

A class is a user-defined type. It is composed of built-in types, other user-defined types and Chapter 3 User-defined types 3.1 Classes A class is a user-defined type. It is composed of built-in types, other user-defined types and functions. The parts used to define the class are called members.

More information

การทดลองท 8_2 Editor Buffer Array Implementation

การทดลองท 8_2 Editor Buffer Array Implementation การทดลองท 8_2 Editor Buffer Array Implementation * File: buffer.h * -------------- * This file defines the interface for the EditorBuffer class. #ifndef _buffer_h #define _buffer_h * Class: EditorBuffer

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.... COMP6771 Advanced C++ Programming Week 5 Part One: Exception Handling 2016 www.cse.unsw.edu.au/ cs6771 2.... Memory Management & Exception Handling.1 Part I: Exception Handling Exception objects

More information

Midterm Review. PIC 10B Spring 2018

Midterm Review. PIC 10B Spring 2018 Midterm Review PIC 10B Spring 2018 Q1 What is size t and when should it be used? A1 size t is an unsigned integer type used for indexing containers and holding the size of a container. It is guarenteed

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

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

CSCI 1370 APRIL 26, 2017

CSCI 1370 APRIL 26, 2017 CSCI 1370 APRIL 26, 2017 ADMINISTRATIVIA Quarter Exam #3: scores ranged from 0.70 points to 10.05 points, with a median score of 7.07. Note: a total bonus of 1.00 points (+.5 curve, +.5 group reward) was

More information

Implementing an ADT with a Class

Implementing an ADT with a Class Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak! //Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new

More information

Exam duration: 3 hours Number of pages: 10

Exam duration: 3 hours Number of pages: 10 INFOMGEP 2011 Retake exam Student name: Student number: Exam duration: 3 hours Number of pages: 10 All the answers have to be written in the corresponding boxes. It is allowed to have: - lecture notes

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

C++ Constructor Insanity

C++ Constructor Insanity C++ Constructor Insanity CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon

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

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

lecture24: Disjoint Sets

lecture24: Disjoint Sets lecture24: Largely based on slides by Cinda Heeren CS 225 UIUC 22nd July, 2013 Announcements mp6 due tonight mp7 out soon! mt3 tomorrow night (7/23) Optional review instead of lab tomorrow Code Challenge

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 14 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2016 ENCM 339 Fall 2016 Slide Set 14 slide 2/35

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

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

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III Distributed Real-Time Control Systems Lecture 14 Intro to C++ Part III 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

C++ For Science and Engineering Lecture 12

C++ For Science and Engineering Lecture 12 C++ For Science and Engineering Lecture 12 John Chrispell Tulane University Monday September 20, 2010 Comparing C-Style strings Note the following listing dosn t do what you probably think it does (assuming

More information

Homework 4. Any questions?

Homework 4. Any questions? CSE333 SECTION 8 Homework 4 Any questions? STL Standard Template Library Has many pre-build container classes STL containers store by value, not by reference Should try to use this as much as possible

More information

C++ Object-Oriented Programming

C++ Object-Oriented Programming C++ Object-Oriented Programming Templates Deep copy Deep delete September 26, 2017 Cinda Heeren / Geoffrey Tien 1 PA1 LinkedList In PA1, you are asked to implement a linked list whose nodes store Kebab

More information

GEA 2017, Week 4. February 21, 2017

GEA 2017, Week 4. February 21, 2017 GEA 2017, Week 4 February 21, 2017 1. Problem 1 After debugging the program through GDB, we can see that an allocated memory buffer has been freed twice. At the time foo(...) gets called in the main function,

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED Class Example student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED #include #include using namespace std; class student public:

More information

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Using pointers Pointers are polymorphic Pointer this Using const with pointers Stack and Heap Memory leaks and dangling pointers

More information

lecture14: Tree Traversals

lecture14: Tree Traversals lecture14: Tree Largely based on slides by Cinda Heeren CS 225 UIUC 2nd July, 2013 Announcements lab trees due Saturday night (7/6) Reminder: no class 7/4 mp4.1 extra credit due Friday night (7/5) Source:

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

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

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14) NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14) Some Material for your reference: Consider following C++ program. // A program

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

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008 COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008 Lab topic: 1) Take Quiz 4 2) Discussion on Assignment 2 Discussion on Assignment 2. Your task is to

More information

Smart Pointers. Some slides from Internet

Smart Pointers. Some slides from Internet Smart Pointers Some slides from Internet 1 Part I: Concept Reference: Using C++11 s Smart Pointers, David Kieras, EECS Department, University of Michigan C++ Primer, Stanley B. Lippman, Jesee Lajoie, Barbara

More information

Lab 1: First Steps in C++ - Eclipse

Lab 1: First Steps in C++ - Eclipse Lab 1: First Steps in C++ - Eclipse Step Zero: Select workspace 1. Upon launching eclipse, we are ask to chose a workspace: 2. We select a new workspace directory (e.g., C:\Courses ): 3. We accept the

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation Announcements Lab 3 was a Frankenstein assembly of a new group exercise with part of a gdb lab. I hear many of you

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

ADTs in C++ In C++, member functions can be defined as part of a struct

ADTs in C++ In C++, member functions can be defined as part of a struct In C++, member functions can be defined as part of a struct ADTs in C++ struct Complex { ; void Complex::init(double r, double i) { im = i; int main () { Complex c1, c2; c1.init(3.0, 2.0); c2.init(4.0,

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini bertini@dsi.unifi.it http://www.dsi.unifi.it/~bertini/ Resource Management Memory, auto_ptr and RAII The most commonly used resource in

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

System Design and Programming II

System Design and Programming II System Design and Programming II CSCI 194 Section 01 CRN: 20220 Spring 2016 David L. Sylvester, Sr., Assistant Professor Chapter 15 Inheritance, Polymorphism, And Virtual Functions What is Inheritance?

More information

University of Toronto

University of Toronto University of Toronto Faculty of Applied Science and Engineering Midterm November, 2010 ECE244 --- Programming Fundamentals Examiners: Tarek Abdelrahman, Michael Gentili, and Michael Stumm Instructions:

More information

Lecture 14: more class, C++ streams

Lecture 14: more class, C++ streams CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 14:

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

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 14. Dr Jason Atkin G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used

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

C++ For Science and Engineering Lecture 27

C++ For Science and Engineering Lecture 27 C++ For Science and Engineering Lecture 27 John Chrispell Tulane University Monday November 1, 2010 Classes and the This pointer Every C++ object has a curious pointer called this. If we want to extend

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING Classes and Objects So far you have explored the structure of a simple program that starts execution at main() and enables you to declare local and global variables and constants and branch your execution

More information

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15) Print Your Name: Signature: USC email address: CSCI 101L Fundamentals of Computer Programming Midterm Exam #2 Spring 2013 (1:00-3:00pm, Friday, March 15) Instructor: Prof Tejada Problem #1 (20 points):

More information

CS11 Intro C++ Spring 2018 Lecture 1

CS11 Intro C++ Spring 2018 Lecture 1 CS11 Intro C++ Spring 2018 Lecture 1 Welcome to CS11 Intro C++! An introduction to the C++ programming language and tools Prerequisites: CS11 C track, or equivalent experience with a curly-brace language,

More information

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................

More information

2. It is possible for a structure variable to be a member of another structure variable.

2. It is possible for a structure variable to be a member of another structure variable. FORM 1(put name, form, and section number on scantron!!!) CS 162 Exam I True (A) / False (B) (2 pts) 1. What value will the function eof return if there are more characters to be read in the input stream?

More information

Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor

Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 2 Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Arrays and Pointers void myprint(const char *); int main() { char *phrasey = C++Fun ; myprint(phrasey);

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism 1 Inheritance extending a clock to an alarm clock deriving a class 2 Polymorphism virtual functions and polymorphism abstract classes MCS 360 Lecture 8 Introduction to Data

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

Unified Modeling Language a case study

Unified Modeling Language a case study Unified Modeling Language a case study 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h

More information

05-01 Discussion Notes

05-01 Discussion Notes 05-01 Discussion Notes PIC 10B Spring 2018 1 Exceptions 1.1 Introduction Exceptions are used to signify that a function is being used incorrectly. Once an exception is thrown, it is up to the programmer

More information

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang C++11 Move Constructors and Move Assignment For Introduction to C++ Programming By Y. Daniel Liang C+11 provides move constructors and move assignment to improve performance by moving a large rvalue object

More information

Common Misunderstandings from Exam 1 Material

Common Misunderstandings from Exam 1 Material Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself

More information

G52CPP C++ Programming Lecture 16

G52CPP C++ Programming Lecture 16 G52CPP C++ Programming Lecture 16 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Casting static cast dynamic cast const cast reinterpret cast Implicit type conversion 2 How

More information

CS 31 Discussion: Final Week. Taylor Caulfield

CS 31 Discussion: Final Week. Taylor Caulfield CS 31 Discussion: Final Week Taylor Caulfield Overview for this Week Review Practice Problems Review Topics to Study Control Flow (if/switch branch, for/while/do-while loop) Functions/Parameters Arrays

More information

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies Distributed Real-Time Control Systems Chapter 13 C++ Class Hierarchies 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

C++ Primer for CS175

C++ Primer for CS175 C++ Primer for CS175 Yuanchen Zhu September 10, 2014 This primer is pretty long and might scare you. Don t worry! To do the assignments you don t need to understand every point made here. However this

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Program template-smart-pointers-again.cc

Program template-smart-pointers-again.cc 1 // Illustrate the smart pointer approach using Templates 2 // George F. Riley, Georgia Tech, Spring 2012 3 // This is nearly identical to the earlier handout on smart pointers 4 // but uses a different

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 14: Object Oriented Programming in C++ (ramviyas@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Classes (cont d) More on Classes and Members Group presentations Last time

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2014 Tuesday, February 25, 7-10p Name: NetID: Lab Section

More information

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4 Department of Computer Science COS121 Lecture Notes Chapter 2- Memento design pattern Copyright c 2015 by Linda Marshall and Vreda Pieterse. All rights reserved. Contents 2.1 Introduction.................................

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

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

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