Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes."— Presentation transcript:

1 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.

2 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.

3 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.

4 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; };

5 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; };

6 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.

7 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.

8 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.

9 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.

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

11 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; };

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

13 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.

14 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; };

15 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.

16 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.


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

Similar presentations


Ads by Google