Overloaded Constructors in Computer Science II

Overloaded Constructors in Computer Science II
Slide Note
Embed
Share

Dr. Katherine Gibson's lecture covers topics related to overloaded constructors in Computer Science II for Majors. The content also includes important exam rules, disability services information, and exam formats and tips for students at UMBC. It emphasizes the significance of exam integrity, preparation strategies, and coding best practices.

  • Computer Science
  • Overloaded Constructors
  • Exam Rules
  • UMBC
  • Programming

Uploaded on Mar 06, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


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

  2. 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! 2 www.umbc.edu

  3. Exam Rules The exam is closed everything: No books No notes No cheat sheets No laptops No calculators No phones 3 www.umbc.edu

  4. 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 4 www.umbc.edu

  5. 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 5 www.umbc.edu

  6. Exam Format True/False Multiple Choice Short Answer Explain basic concepts in writing Debugging Find and correct errors 6 www.umbc.edu

  7. Exam Format Code Evaluations 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 7 www.umbc.edu

  8. 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 8 www.umbc.edu

  9. 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 9 www.umbc.edu

  10. 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! 10 www.umbc.edu

  11. Questions about Exam 1? www.umbc.edu

  12. Last Class We Covered Classes Access modifiers Methods Mutators Accessors Facilitators Constructors Livecoding: Rectangle class 12 www.umbc.edu

  13. Any Questions from Last Time? www.umbc.edu

  14. Todays Objectives To learn about overloading methods Regular class methods Overloaded constructors To complete our Rectangle class To review for Exam 1 14 www.umbc.edu

  15. Overloading www.umbc.edu

  16. 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 16 www.umbc.edu

  17. 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 17 www.umbc.edu

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

  19. 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 19 www.umbc.edu

  20. 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); 20 www.umbc.edu

  21. 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 21 www.umbc.edu

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

  23. 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.) 23 www.umbc.edu

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

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

  26. 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 26 www.umbc.edu

  27. 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 */ } 27 www.umbc.edu

  28. 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 28 www.umbc.edu

  29. Overloading Example void PrintMessage (void) { cout << "Hello World!" << endl; } void PrintMessage (string msg) { cout << msg << endl; } 29 www.umbc.edu

  30. Time for LIVECODING!!! 30 www.umbc.edu

  31. 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 31 www.umbc.edu

  32. 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 32 www.umbc.edu

Related


More Related Content