More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Advertisements

Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
Enhancing classes Visibility modifiers and encapsulation revisited
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Constructors and Other Tools Version 1.0 Topics Constructors & Destructors Composition const Parameter Modifier const objects const functions In-line.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Internet Software Development Classes and Inheritance Paul J Krause.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ECE122 Feb. 22, Any question on Vehicle sample code?
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 9 Classes: A Deeper Look, Part I Part II.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of 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?
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Introduction to Object Oriented Programming Chapter 10.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Classes and Objects Introduced
Classes (Part 1) Lecture 3
Chapter 14: More About 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?
Static data members Constructors and Destructors
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Review What is an object? What is a class?
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
CISC181 Introduction to Computer Science Dr
Classes Object-oriented programming: Example: Bank transactions
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
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?
Object-Oriented Programming: Classes and Objects
The dirty secrets of objects
group work #hifiTeam
Chapter 14: More About Classes.
User-Defined Classes and ADTs
Chapter Three - Implementing Classes
Corresponds with Chapter 7
Classes: Implementation and Testing
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
JAVA CLASSES.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Class.
CS410 – Software Engineering Lecture #5: C++ Basics III
Constructors & Destructors
Class Circle { Float xc, yc, radius;
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
(4 – 2) Introduction to Classes in C++
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes may have static methods and fields – these belong to the class rather than an individual object. Methods may be declared to be const which means that they do not change the fields of the object. Class declarations may be nested.

Constructors A constructor is a piece of code that is called when an object is created. It is often used to fill in the values of the fields of the object. If you don't specify any constructors, a default constructor of no arguments is automatically created. All it does is creates the object without setting the fields. User-defined constructors may have parameters.

More on Constructors Constructors return the newly created object. They do not have a return type. The name of the constructor is the same as the name of the class. Constructors should always be public since there are called by functions outside the class.

Constructors - Example class Clock { public: Clock(int h, int m, int s, bool am) { hours = h; minutes = m; seconds = s; if(!am) hours += 12; } private: int hours, minutes, seconds; };

Alternative Example A constructor can initialize the value of fields rather than using assignment statements. The syntax is: class Clock { public: Clock(int h, int m, int s, bool am) : hours(h), minutes(m), seconds(s) { if(!am) hours += 12; } private: int hours, minutes, seconds; };

Other Constructors You can define more than one constructor. The constructors must differ by the number and/or types of the parameters. If you do define any constructors, there is no longer a default (zero-argument) constructor. In that case, if you need a zero-argument constructor, you must explicitly define it yourself.

Example class Clock { public: Clock(int h, int m, int s, bool am); Clock(int h, int m, int s); Clock(int h, int m); Clock(int h); Clock() {} private: int hours, minutes, seconds; }; Note that constructors may be defined in the class declaration or outside of it.

Calling a Constructor The constructor is called when an object of the class is created, that is, defined. If the constructor has parameters, the appropriate arguments must be given: Clock c1(12,10,5,false), c2(1,2,3), c3(8), c4; Note that when the zero-argument constructor is invoked, no parentheses are used.

Static Fields A field can be declared to be static. That means it is associated with the entire class, and not with one particular object. All objects of the class share the data object of the field. If one object changes the value, it is changed for all of them. Since a static field is not associated with a particular object, it is referenced by using its name qualified with the class name.

Static Field - Example Suppose we had a circle class which represented circles: class Circle { public: Circle(double r) : radius(r) {} const static double Pi = 3.1415926; private: double radius; };

Another Example Suppose that we wanted to keep track of the number of clock objects that were created: class Clock { public: Clock(int h, int m, int s, bool am) : hours(h), minutes(m), seconds(s) { if(am) hours += 24; numClocks++; } private: static int numClocks; int hours, minutes, seconds; };

Example (cont'd) Outside of the class, we define numClocks: int Clock::numClocks = 0;

Static Methods Methods may also be static, that is, associated with the entire class, and not with any particular object. Static methods are usually called by qualifying the name with the class name, e.g., cout << Clock::getNumClocks() << endl; Since static methods are not associated with an object, they may use only static fields.

Const Methods A method may be declared to be const. A const method cannot change the values of the fields of the object upon which it is called. For example, printing out an object should not modify the fields of the object. class Clock { public: void print() const; };

Nested Class Declarations Class declarations may be nested. That is, one class may be declared within another class: class StudentRecord { private: class Date {...} }; The members of the inner class (Date) may be used by the methods of the outer class (StudentRecord) by not by any other (outside) methods.

Exercise Write a class BankAccount which implements a checking account with interest. Constructor – set balance, account number,and the APR. Transfer - transfer money from one account to another, record transaction in the audit log AddInterest – monthly add interest Print – print out the balance. PrintAuditLog – print out the audit log.