Topics. Constructor. Constructors

Similar documents
2 2

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter

Topics. Functions. Functions

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

C:\Temp\Templates. Download This PDF From The Web Site

C++ 8. Constructors and Destructors

C++ Classes, Constructor & Object Oriented Programming

CS 376b Computer Vision

Inheritance and Overloading. Week 11

Introduction to Classes

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

Ch 6. Functions. Example: function calls function

CSCE 2004 Final Exam Spring Version A

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Lecture #1. Introduction to Classes and Objects

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

Abstract Data Types (ADT) and C++ Classes

Common Misunderstandings from Exam 1 Material

Topics. Passing objects to functions. Sections 7.9, 7.10, Objects and functions

CSc 328, Spring 2004 Final Examination May 12, 2004

RAII and Smart Pointers. Ali Malik

Polymorphism. Zimmer CSCI 330

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

CS 162, Lecture 25: Exam II Review. 30 May 2018

Classes and Pointers: Some Peculiarities

Fast Introduction to Object Oriented Programming and C++

Faculty of Information and Communication Technologies

05-01 Discussion Notes

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Study Guide for Test 2

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CS 115 Exam 3, Spring 2010

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Data Structures and Algorithms

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

COMP 2355 Introduction to Systems Programming

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

Variable Definitions and Scope

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Object oriented programming

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Constructor - example

Programming in C++: Assignment Week 8

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

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

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

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Function Overloading

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.

Pointers, Dynamic Data, and Reference Types

Computer Programming

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Linked List using a Sentinel

Object Oriented Programming in C#

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

SDV4001 Object Oriented Programming with C++ and UI Midterm 2013

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Increment and the While. Class 15

CS

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

7.1 Optional Parameters

dynamically allocated memory char* x = new char; int* x = new int[n]; ???...?

Intermediate Programming, Spring 2017*

Short Notes of CS201

Computer Programming

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

G52CPP C++ Programming Lecture 17

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Programming Abstractions

CS201 - Introduction to Programming Glossary By

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

Object oriented programming

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

C++_ MARKS 40 MIN

Come and join us at WebLyceum

BEng (Hons) Telecommunications. Examinations for 2016 / Semester 2

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

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

CPSC 427: Object-Oriented Programming

Constructors and Destructors. Chapter 6

CSCI 102 Fall 2010 Exam #1

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

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

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

Transcription:

Topics 1) How can we initialize an object when it's created? 2) How can we do things when an object is destroyed? 3) How can we write functions with different parameters? Slides #11 - Text 7.6-7.7 Constructors & Destructors How to build objects (right) 16/07/11 1 CMPT 125 Dr. B. Fraser 16/07/11 2 Constructor A constructor is: Constructors It's like other functions except: its name must be it has... (not even void!). cout << "Running Demo's constructor."<<endl; 16/07/11 3 16/07/11 4

Constructor demo cout << "Running Demo's constructor."<<endl; cout << "Before instantiating."<<endl; Demo demoobj; cout << "After instantiating."<<endl; 16/07/11 5 Initialization Constructors are used to... BankBalance() { balance = 0; double getbalance() { return balance; BankBalance myacct; cout << fixed << setprecision(2); cout << "My account balance: " << myacct.getbalance() << endl;... 16/07/11 bankbalance.cpp 6 Inline vs not-inline Review Constructor can be inline, or non-inline. Inline constructor: BankBalance() { balance = 0; Non-Inline constructor: BankBalance(); { balance = 0; Implement Circle's constructor as a non-inline function. Initialize its size to 0. class Circle { double radius; Circle(); void setradius(double r); double getarea(); 16/07/11 7 16/07/11 8

Destructor A destructor is: Destructors It's like other functions except: its name is the... it has no return type (not even void!) ~ cout << "Running Demo's destructor."<<endl; 16/07/11 9 16/07/11 10 Destructor Demo cout << "+++ Running Demo's constructor."<<endl; ~ cout << "--- Running Demo's destructor."<<endl; void test cout << "1. Before making the object."<<endl; Demo d1; cout << "2. After making the object."<<endl; testdemo(); 1. Before making the object. +++ Running Demo's constructor. 2. After making the object. --- Running Demo's destructor. 16/07/11 11 Destructor Use Constructors run when created: used for... Destructors run when being destroyed: used for... Examples: closing a file. freeing dynamically allocated memory. 16/07/11 12

Review Implement Circle's destructor as a non-inline function. Have it output the message "Bye". class Circle { double radius; ~Circle(); void setradius(double r); double getarea(); Overloading Text 6.14 Note: We will most often create constructors, but only rarely create destructors. 16/07/11 13 16/07/11 14 Function Overloading Function overloading: Overloading a truck is bad, but overloading in C++ it's good. Overloading examples Given: int sum (int a, char b); C++ can find the right function by its signature: Parameters: int sum (int a, long b); int sum (char a, int b); int sum (int a, char b, int c); int sum (); // Different type(s) // Diff. order of types // Diff. # parameters // Diff. # parameters Function Overloading Examples: int sum (int a, int b) { return a + b; int sum (int a, int b, int c) { return a + b + c; int sum (bool x, int a) { if ( x ) { a; else { 0; long sum (int a, char b); // void sum (int a, char b); // int sum (int b, char a); // 16/07/11 15 16/07/11 16

Overloading Constructors Default Constructor Overloading is very useful for constructors: You can create an object by passing in different sets of values. class Drink { Drink(); Drink(string name); Drink(string name, bool alchoholic); Default constructor: Automatically created for us if we specify no constructors for an object. Demonstrate creating an object for each constructor. class Drink { Drink() {... Drink(string name) {... Drink(string name, bool alchoholic) {... int main() { Drink d1; Drink d2("water"); Drink d3("coke", false); 16/07/11 17 16/07/11 18 Demo / Review Summary Implement a Pen class that stores the colour of ink. Make a default constructor, and a constructor to initialize the Pen's colour (a string). Constructors are functions to initialize objects. Same name as class. No return type. Destructors called when object being destroyed. Similar to constructors but prefix name with ~ and cannot accept arguments. Overloading possible based on name, and parameters: #, order, and type. Good for constructors. Default constructor is one with no parameters. 16/07/11 19 16/07/11 20