Lecture07-Class_Part_3 - Introduction to Object-Oriented...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Object-Oriented Programming Classes 3 Lecture 7
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Learning Objectives: l To understand when to use pass object by value, reference, and pointer l To understand how to do dynamic allocation of objects l To understand the difference among arrays of objects, arrays of pointers to objects, vector of objects l To understand composition (objects within classes) l To understand the importance of pointer to object l To know when to use forward class declaration
Background image of page 2
Pass Object by Value l Pass by value creates a cloned/duplicate copy of the object, hence invoke copy constructor and destructor of parameter in function class Person { string name; public: // Overloaded constructor Person (string name="") : name(name) { cout << name << " : Overloaded constructed\n"; } // Copy constructor : name(p.name) { cout << name << " : Copy constructor\n"; } void print() { cout << "I'm " << name << endl; } ~Person () { cout << name << " destructed\n"; } }; void PassByValue (Person p) { // Call p's copy constructor p.print(); // p is a clone } // Call p's destructor int main() { Person p1("Ali"); // Call p1's // overloaded constructor cout << "Call PassByValue\n"; PassByValue (p1); } // Call p1's destructor Output: Ali : Overloaded constructor Call PassByValue Ali : Copy constructor I'm Ali Ali : Destructor Ali : Destructor
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
l Pass by reference or pointer does not creates a cloned copy of the object, hence does not invoke copy constructor and destructor of parameter in function class Person { ... }; void PassByReference (Person& p) { p.print(); } void PassByPointer (Person* p) { p->print(); } int main() { Person p1("Ali"); // Call p1's // overloaded constructor cout << "Call PassByReference\n"; PassByReference (p1); cout << "Call PassByPointer\n"; PassByPointer (&p1); } // Call p1's destructor Output: Ali : Overloaded constructor Call PassByReference I'm Ali Call PassByPointer I'm Ali Ali : Destructor
Background image of page 4
Dynamic Allocation of Objects l Objects may be dynamically created and deleted via pointer l Pointer to object is very important in enabling Dynamic Polymorphism (Lecture 11) class Point { int x, y; public: Point (int x = 0, int y = 0) : x(x), y(y) {} void print() const { cout << "(" << x << ", " << y << ")" << endl; } }; int main() { Point *p ; // declares pointer to a Point object p = new Point ; // dynamically allocate a Point object p->print(); // call print() with pointer operator (*p).print(); // call print() by dereferencing the pointer delete p ; // delete the Point object p = NULL; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays of Objects l We can create arrays of objects // Static array (automatically allocated array) Point points[]
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 19

Lecture07-Class_Part_3 - Introduction to Object-Oriented...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online