130 likes | 144 Views
C++ Lecture 6. Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features. Object Lifetimes. See example of first lamp class where switch and bulb objects are part of the lamp.
E N D
C++ Lecture 6 • Object Life-times • Creating objects using new • Using pointers to objects • Aggregation (Containment –UML speak) • Other C++ class features
Object Lifetimes • See example of first lamp class where switch and bulb objects are part of the lamp. • The life of the enclosed objects is linked to the lifetime of the enclosing object, namely the lamp. • They are all created together and all destroyed together. • Does this accurately model the real world of lamps
Object lifetimes (contd.) • See second lamp class • Uses pointers to the bulb and switch • The lamp class is responsible for constructing and destructing the bulb and the switch. • We can changed the parts that make up the lamp (see the change_bulb method) • But should it be the responsibility of a lamp to know how to create and change it own bulb? • Does this model reality?
Object lifetimes (contd.) • See third lamp class • Uses pointers to the bulb and switch as previous lamp class • The bulb and switch are created independently of the lamp – They have separate lifetimes. • We can change the parts • The lamp class does NOT need to be aware of how to create bulbs and switches • Does this more closely model the real world?
Nomenclature • Confusion exists over naming between different groups – Programmers, designers etc. • Aggregation is a general term • Aggregating using embedded objects is often called composition • Rational Rose implementation of UML • the uses relationship : Class A uses class B • This is called an association • C++ implementation is by pointers • the has a relationship : Class A has a B • This is called aggregation in Rational Rose • C++ implementation is by embedding instances I.e class A has an instance of a class B
Member instances vs.member pointers to instances • Which is better? • In general use class instances as members - restricts flexibility but offers more protection against error • because the compiler • automatically constructs and destroys class instance members. • Copies them on assignment and initialisation. • If the member instance is private access is limited to members functions and friend functions • Using member pointers requires more effort on the part of the programmer and fraught with pitfalls • You must remember to explicitly construct and destroy the referenced object and initialise the pointer to it.
Member instances vs.member pointers to instances • As a general rule avoid member pointer variables unless you need to : • use an instance with a different lifetime • share an instance with other objects • take advantage of polymorphism -coming to a lecture near you soon • de-couple specification files • Unfortunately these situations frequently occur in real programs
The this pointer • instances of a class each have their own private data • for practical reasons there is only a single copy of the class functions which are shared by all the instances. • So how do the member function access the correct object? Answer- using a pointer to the object • The pointer is called this and is automatically passed to the function by the compiler. • The this pointer is used to access the member data. E.g. this->data_item
The this pointer Example CPatient x; x.set_age(arg); //is interpreted by the compiler as set_age(&x,arg); Every member function has a hidden argument void CPatient::setage(CPatient * this, int a) the data of instance x is accessed via the this pointer so when the programmer has written age = a; // the compiler actually uses this->age =a;
The this pointer • Normally we can ignore the this pointer • We can use it if we require access to the invoking instance within a member function. See copy constructor in patient class. • *this – de-references the pointer and refer to the actual object. • de-referencing the invoking instance is often used to return the invoking instance. (See the functions operator= in patient class)
Static data in classes • use keyword static in front of declaration • e.g static int serial_number; • Static data is instance independent; it exists even if no instances of the class are created. • There is only a single copy of the data • The data is shared by all instances of the class
Static data class CAny { private: static int next_serial_number; etc. • The static data can be initialise with a single definition and this is normally placed in the header file after the end of the class definition :-int CAny::next_serial_number = 10000; Note: The keyword static is not repeated. • Static data is often used for instance counting, time stamps and sharing of common data.
Static Functions • Normal member functions (i.e. non static) are always invoked with an instance of the class. • To access static data an instance would have to be created. • Static functions are instance independent. They are declared in the same way as ordinary member functions but are preceded with the keyword static. • Static member functions are not called by using an instance of the class but by using the class name and the scope resolution operator:- CAny::func(args); • Static functions are used primarily to access static data • They have no this pointer and therefore cannot access non-static data i.e. normal class data members.