Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.

Slides:



Advertisements
Similar presentations
Storage class in C Topics Automatic variables External variables
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.
 The position of a method in a program is not critical.  Why?
Road Map Introduction to object oriented programming. Classes
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
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 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Storage Classes.
Review of C++ Programming Part II Sheng-Fang Huang.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Pointer Data Type and Pointer Variables
1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z z Room: D104.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Learners Support Publications Classes and Objects.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Object-Oriented Programming in C++
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Dynamic memory allocation and Intraprogram Communication.
Advanced Programming in C
Eine By: Avinash Reddy 09/29/2016.
C Functions -Continue…-.
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.
Storage class in C Topics Automatic variables External variables
Object Lifetime and Dynamic Objects
This pointer, Dynamic memory allocation, Constructors and Destructor
Classes and Objects.
Scope Rules Of Variables
Dynamic Memory.
Submitted By : Veenu Saini Lecturer (IT)
C++ Class Members Class Definition class Name { public: constructor(s)
Object Oriented Programming (OOP) Lecture No. 11
Types of Computer Languages
C Programming Lecture-17 Storage Classes
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

Types of Object  External (Global) Objects  Exist through the program lifetime  Have file scope  Automatic (Local) Objects  Exist throughout the local scope in which they are created  Static Objects  Exist through the program lifetime  Only visible within local scope  Dynamic Objects  Lifetime controlled within a particular scope

External (Global) Objects In C++… BankAccount account1; void main() {…} The declaration of objects is all that we can do outside the scope of {} We cannot call methods of account1 outside {}

External Linkage In a system containing several program modules, such objects may be declared as extern in other modules if their visibility needs to extend beyond the file scope in which they are defined. Prog1.ccp Prog2.cpp Prog3.cpp Manager Joe; extern Manager Joe;extern Manager Joe; void main()…… { … }

Automatic (Local) Objects In C++… Declared inside the body of main. Visible anywhere inside main and persists until main finishes. Example: void main() { BankAccount account1; … }

Static Objects A static object is declared, like an automatic object, within a local scope. However it is only initialised once, regardless of the number of times it falls in and out of scope. In C++ void function() {BankAccount account1; static BankAccount account2; }

Dynamic Objects If Objects are unpredictable they must be represented dynamically. We cannot give dynamic objects unique names =>we must reference them using pointers. In C++ a pointer can be directed to an area of dynamically allocated memory at run time in order to reference a newly created object. In this way the constructor is called via a pointer.

Creating dynamic objects classname *pclassname = new classname; e.g. counter *pcounter = new counter; Pcounter is not the name of an object but the name of a pointer to an object of type counter. Dynamic object do not have names – they simply occupy a memory space which may be referenced by a pointer.

Calling Dynamic Objects’ Methods Calling the methods of a dynamic object: pclassname -> method( parameters ); e.g. pcounter -> increment(); In order to call a method of a dynamic object, the pointer must be dereferenced before the objects methods may be accessed. The ‘-> ‘ operator must be used instead of ‘.’

Delete Keyword delete pclassname; delete pcounter; This cleans up memory by explicitly calling the destructor which destroys the object currently referenced by the pointer. Delete destroys the object NOT the pointer. The pointer is still available to point to other objects of the class.

The MetaClass The class holds the attributes and methods which apply to objects of the class – it is the class of the objects. A class acts as a kind of blueprint or skeleton for objects of the class. The metaclass holds the attributes and methods which apply to the class itself – it is therefore the class of the class. Every class has one ( and only one) metaclass, and the metaclass contains those parts of a class which are not appropriate to be duplicated for every object.

A Metaclass is …? The metaclass is a repository for class attributes which only have one instance regardless of how many objects of the class exist. Class attributes tell us about the class as a whole as opposed to objects of the class. Class attributes are just as visible as object attributes to an object. However, the value of object attributes (an objects state) remain invisible to the class.

public: BankAccount(float start_balance = 0.00); int getAccountNumber(); char* getAccountHolder(); // a class (static) method to return the number of accounts static int getAccountTotal(); };

Using Metaclass Attributes #include #include ”BankAccount.h" void main() { // create a number of bank accounts BankAccount acc1, acc2, acc3, acc4, acc5; // test the class method cout << "Total number of accounts is: " << BankAccount::getAccountTotal() << endl; }