C++ Lecture 6 Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Classes: A Deeper Look Systems Programming.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 15: Operator Overloading
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Lecture 7 Function/operator overloading
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Object-Oriented Programming in C++ More examples of Association.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
More C++ Features True object initialisation
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
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 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
Function Overloading and References
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Programming Techniques Classes II Important Class Features Spring 2009.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Eine By: Avinash Reddy 09/29/2016.
Learning Objectives Pointers as dada members
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?
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.
Andy Wang Object Oriented Programming in C++ COP 3330
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
Andy Wang Object Oriented Programming in C++ COP 3330
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Object Oriented Programming (OOP) Lecture No. 12
SPL – PS3 C++ Classes.
Presentation transcript:

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.