CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.

Slides:



Advertisements
Similar presentations
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 Introduction to Object Oriented Programming Chapter 10.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC202 Computer Science II for Majors Lecture 05 – References Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review Prof. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson Prof. Jeremy Dixon.
CMSC202 Computer Science II for Majors Lecture 09 – Overloaded Operators and More Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
EECS 183.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
User-Written Functions
UMBC CMSC 104 – Section 01, Fall 2016
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CompSci 101 Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
CMSC201 Computer Science I for Majors Lecture 13 – Program Design
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
CMSC201 Computer Science I for Majors Lecture 27 – Final Exam Review
Introduction to Classes
CSC 113 Tutorial QUIZ I.
Announcements General rules about homeworks
Announcements General rules about homeworks
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Review for Final Exam.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Announcements Homework 1 will be assigned this week,
CMSC202 Computer Science II for Majors Lecture 03 – Arrays and Functions Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Review for Final Exam.
From C to C++: Summary of weeks 1 - 4
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Midterm Exam Information
Final Review Bina Ramamurthy 4/5/2019 BR.
CMSC201 Computer Science I for Majors Final Exam Information
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
EECE.2160 ECE Application Programming
Final Review Bina Ramamurthy 4/15/2019 BR.
Announcements General rules about homeworks
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review
Lecture 8 Object Oriented Programming (OOP)
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Classes and Objects Systems Programming.
SPL – PS3 C++ Classes.
Presentation transcript:

CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC

Student Disability Services If you need to take the exam at SDS: You need to set it up with them You need to make sure I know about it This needs to be done ahead of time This should already have been done by now!

Exam Rules The exam is closed everything: No books No notes No cheat sheets No laptops No calculators No phones

Exam Rules Place your bookbag under your desk/chair NOT on the seat next to you You may have on your desk: Pens, pencils, erasers Water bottle UMBC ID

Exam Rules DO NOT CHEAT!!! Cheating will be dealt with severely and immediately If a TA or instructor sees you looking at another student’s paper (or anything other than your own exam) they will take your test from you Space allowing, you will sit every other seat, so that you are not next to another student

Exam Format True/False Multiple Choice Short Answer Debugging Explain basic concepts in writing Debugging Find and correct errors

Exam Format Code Evaluations Code Completions Coding Problems Given code, what does it do? Code Completions Given a partial piece of code Correctly fill in blanks to complete code Coding Problems Given a problem, write code to solve it

Exam Tips: General Write down your name and circle your section Flip through the exam and get a feel for the length of it and the types of questions If a problem is unclear or you think there is an error on the exam, raise your hand Most questions have partial credit You should at least attempt every problem

Exam Tips: Coding When coding: Read the question carefully Plan out what your code needs to do After you are done coding the programming problems, try “running” your program with some input and making sure it works the way you think it does

Exam Content Everything we’ve covered so far! Including... C++ Syntax Loops, data types, cin, cout, C-strings, etc. Functions Prototype, definition, call, return value, parameters Pointers, arrays Passing arrays to functions, &, *, addresses, etc. Classes! Access modifiers, class methods, member variables, constructors, objects, dot operator And more!

Questions about Exam 1?

Last Class We Covered Classes Livecoding: Rectangle class Access modifiers Methods Mutators Accessors Facilitators Constructors Livecoding: Rectangle class

Any Questions from Last Time?

Today’s Objectives To learn about overloading methods “Regular” class methods Overloaded constructors To complete our Rectangle class To review for Exam 1

Overloading

Overloading We can define multiple versions of the constructor – we can overload it Different constructors for: When all values are known When no values are known When some subset of values are known

All Known Values Have the constructor set user-supplied values Date::Date (int m, int d, int y) { SetMonth(m); SetDay(d); SetYear(y); } invoked when constructor is called with all arguments

No Known Values Have the constructor set all default values Date::Date () { SetMonth(DEFAULT_MON); SetDay(DEFAULT_DAY); SetYear(DEFAULT_YEAR); } invoked when constructor is called with no arguments

Some Known Values Have the constructor set some default values Date::Date (int m, int d) { SetMonth(m); SetDay(d); SetYear(DEFAULT_YEAR); } invoked when constructor is called with two arguments

Overloaded Date Constructor so far we have the following constructors: Date::Date (int m, int d, int y); Date::Date (int m, int d); Date::Date (); would the following be a valid constructor? Date::Date (int m, int y);

Avoiding Multiple Constructors Defining multiple constructors for different sets of known values is a lot of unnecessary code duplication We can avoid this by setting default parameters in our constructors

Default Parameters In the function prototype only, provide default values you want the constructor to use Date (int m , int d , int y );

Default Parameters In the function prototype only, provide default values you want the constructor to use Date (int m = 1, int d = 12, int y = 1967); (You should, of course, use constants when providing default parameters.)

Default Parameters In the function definition nothing changes Date::Date (int m, int d, int y) { SetMonth(m); SetDay(d); SetYear(y); }

Using Default Parameters the following are all valid declarations: Date graduation(5,19,2016); Date gritBDay; Date halloween(10,31); Date july(4); // graduation: 5/19/2016 // gritBDay: 1/12/1967 // halloween: 10/31/1967 // july: 4/12/1967 NOTE: when you call a constructor with no arguments, you do not give it empty parentheses

Default Constructors A default constructor is provided by compiler Will handle declarations of Date instances This is how we created Date objects in the slides before we declared and defined our constructor

Default Constructors But, if you create any other constructor, the compiler doesn’t provide a default constructor So if you create a constructor, make a default constructor too, even if its body is just empty Date::Date () { /* empty */ }

Function Overloading Functions in C++ are uniquely identified by both their names and their parameters But NOT their return type! We can overload any kind of function We can even use default values, like with constructors

Overloading Example void PrintMessage (void) { cout << "Hello World!" << endl; } void PrintMessage (string msg) { cout << msg << endl;

Time for… LIVECODING!!!

Livecoding Exercise Update our Rectangle class with Overloaded Constructor Implemented through default parameters Create a class method to: Print all of a Rectangle’s information Update our main() function

Announcements Project 1 is due tonight by 9:00 PM Make sure you have correctly submitted all of your files! Exam 1 will be held on Thursday (the 25th) during our regular class time