Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

PHP functions What are Functions? A function structure:
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Homework #2: Functions and Arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Eiffel examples. Example 1 class C creation default_create feature {ANY} -- the public class data count: INTEGER element: DOUBLE end; features - the data.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Tutorial 6 February 4th/5th, 2015
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
1 Lab Session-12 CSIT121 Fall 2004 Structures and Their Usage Passing Struct Variables to Functions.
1 CSC241: Object Oriented Programming Lecture No 07.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Homework #3: Classes and Constructors
Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012.
Homework #5: Pointers, Dynamic Arrays and Inheritance
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
TELLING TIME LINKS Identifies days, weeks, and months on a calendar.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Objects and Classes Project 2 description.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.
Homework #1: C++ Basics, Flow of Control, and Function Basics By J. H. Wang Mar. 13, 2012.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
Arithmetic, Class Variables and Class Methods Week 11
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Data Structures: CSCI Chapter 1 Data Structures: CSCI Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah.
Homework #1: C++ Basics, Flow of Control, and Function Basics
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang Jun. 5, 2009.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang May 31, 2011.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Homework #3: Classes and Constructors
Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.
Homework #4: Operator Overloading and Strings By J. H. Wang May 22, 2015.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
Classes and Objects.
Andy Wang Object Oriented Programming in C++ COP 3330
Review Session.
CompSci 230 Software Construction
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Classes & Objects: Examples
Implementing Classes Chapter 3.
Defining Classes and Methods
CSC 111 Exam 3 Review Fall 2005.
Review of Previous Lesson
Introduction to Classes and Objects
Presentation transcript:

Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014

Part I: Hand-Written Exercises 1. Suppose your program contains the following class, class CellPhone { public: void setPrice(double newPrice); void setProfit(double newProfit); double getPrice(); private: double price; double profit; double getProfit(); }; (To be continued on the next slide…)

(…continued from the previous slide) And suppose the main function of your program contains the following declaration and that the program somehow sets the values of all the member variables to some values: CellPhone nokia, samsung; double aPrice, aProfit; (To be continued on the next slide…)

(…continued from the previous slide) Which of the following statements are then allowed in the main function of your program? For illegal ones, please explain the reasons why they are illegal. (a) nokia.price = ; (b) samsung.setPrice( ); (c) aPrice = samsung.getPrice(); (d) aProfit = samsung.getProfit(); (e) aProfit = nokia.getProfit(); (f) nokia = samsung;

2. (a) Explain what public: and private: mean in a class definition. (b) When you define a C++ class, should you make the member variable public or private? Should you make the member functions public or private? (c) When you define a C++ class, what items are considered part of the interface ? What items are considered part of the implementation ? 3. What is a default constructor ? Does every class have a default constructor?

Part II: Programming Exercises 4.Define a class for a type called CounterType. An object of this type is used to count things, so it records a count that is a nonnegative integer. Include the following member functions: (a) a function that sets the counter to a count given as an argument, (b) functions that increase the count by one and decrease the count by one. Be sure that no member function allows the value of the counter to become negative or out of range for nonnegative whole numbers, (c) a function that returns the current count value, and (d) a function that outputs the count. Embed your class definition in a C++ test program.

5.Define a class called Time that is an abstract data type for a particular point of time. Your class will have member variables to represent hour, minute, and second in 24-hour clock format. Include all the following member functions: (a) a default constructor, (b) a constructor to set the time using three arguments, (c) the get functions to get the current hour, minute, and second, respectively, (To be continued on the next slides…)

(… continued from the previous slides) (d) an output function that displays the time in universal-time format (e.g. 13:30:07), (e) an output function that displays the time in standard-time format (e.g. 1:27:06 PM), (f) and a member function tick() that increments the time stored in a Time object by one second. Write a C++ program that tests all the member functions to illustrate that they work correctly. Be sure to test the following cases: incrementing into the next minute, hour, or day.

Homework Submission Due: 2 weeks (Apr. 28, 2014) Hand-written exercises –Please write your name and answers on papers Programs –Please submit to – File names : Please name your file according to our homework. For example, _HW1.zip or _quiz2.rar

Any Questions?