OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.

Slides:



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


Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
OBJECT ORIENTED PROGRAMMING LECTURE 12 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Pointer Data Type and Pointer Variables
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
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+
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
C++ Memory Overview 4 major memory segments Key differences from Java
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
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.
System Programming Practical Session 7 C++ Memory Handling.
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
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 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Overview 4 major memory segments Key differences from Java stack
OBJECT ORIENTED PROGRAMMING
CISC181 Introduction to Computer Science Dr
Pointers and Pointer-Based Strings
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Memberwise Assignment / Initialization
Polymorphism Lec
Static Data Member and Functions
Dynamic Memory Allocation
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
Pointers and Pointer-Based Strings
Dynamic Memory.
9-10 Classes: A Deeper Look.
Presentation transcript:

OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena

Size of Empty Class 1. #include using namespace std; class Empty {}; int main() { cout << sizeof(Empty); return 0; } Output: 1 Size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses 2. #include using namespace std; class Empty { }; int main() { Empty a, b; if (&a == &b) cout << "impossible " << endl; else cout << "Fine " << endl; return 0; } Output: Fine

Contd … For the same reason (different objects should have different addresses), “new” always returns pointers to distinct objects #include using namespace std; class Empty { }; int main() { Empty* p1 = new Empty; Empty* p2 = new Empty; if (p1 == p2) cout << "impossible " << endl; else cout << "Fine " << endl; return 0; } Output: Fine

Local Static Object #include class Test { public: Test() { std::cout << "Constructor is executed\n"; } ~Test() { std::cout << "Destructor is executed\n"; } }; void myfunc() { static Test obj; } // Object obj is still not destroyed because it is static int main() { std::cout << "main() starts\n"; myfunc(); // Destructor will not be called here std::cout << "main() terminates\n"; return 0; } Output: main() starts Constructor is executed main() terminates Destructor is executed

Global Static Object #include class Test { public: int a; Test() { a = 10; std::cout << "Constructor is executed\n"; } ~Test() { std::cout << "Destructor is executed\n"; } }; static Test obj; int main() { std::cout << "main() starts\n"; std::cout << obj.a; std::cout << "\nmain() terminates\n"; return 0; } Output: Constructor is executed main() starts 10 main() terminates Destructor is executed

To explicitly call Constructor & Destructor 1. #include using namespace std; class Test { public: Test() { cout << "Constructor is executed\n"; } ~Test() { cout << "Destructor is executed\n"; } }; int main() { Test(); // Explicit call to constructor Test t; // local object t.~Test(); // Explicit call to destructor return 0; } 2. Can be called from the member function of the class #include using namespace std; class Test { public: Test() { cout << "Constructor is executed\n"; } ~Test() { cout << "Destructor is executed\n"; } void show() { Test(); this->Test::~Test(); } }; int main() { Test t; t.show(); return 0; }

Constant Member Function When a function is declared as const, it can be called on any type of object. Non- const functions can only be called by non-const objects. #include using namespace std; class Test { int value; public: Test(int v = 0) {value = v;} int getValue() {return value;} }; int main() { const Test t; cout << t.getValue(); return 0; } This program has compiler errors

Can we use function on left side of expression in C++ ??? #include using namespace std; /* such a function will not be safe if x is non static variable of it */ int &fun() { static int x; return x; } int main() { fun() = 10; /* this line prints 10 on screen */ cout<<fun(); return 0; } Output: 10

Create a class whose objects can only be dynamically allocated #include using namespace std; // A class whose object can only be dynamically created class Test { private: ~Test() { cout << "Destroying Object\n"; } public: Test() { cout << "Object Created\n"; } friend void destructTest(Test* ); }; // Only this function can destruct objects of Test void destructTest(Test* ptr) { delete ptr; cout << "Object Destroyed\n"; } int main() { /* Uncommenting following following line would cause compiler error */ // Test t1; // create an object Test *ptr = new Test; // destruct the object to avoid memory leak destructTest(ptr); return 0; } Output: Object Created Destroying Object Object Destroyed