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.

Slides:



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

Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Lecture Note 7- Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Object-Oriented Programming in C++ More examples of Association.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
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++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
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?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Programming Techniques Classes II Important Class Features Spring 2009.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
CSC241: Object Oriented Programming
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.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Review: Two Programming Paradigms
Introduction to Classes
Chapter 5 Classes.
Static Data Member and Functions
Introduction to Classes
10.2 const (Constant) Objects and const Member Functions
Classes Short Review of Topics already covered Constructor
CS150 Introduction to Computer Science 1
Namespaces How Shall I Name Thee?.
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
A Deeper Look at Classes
Lecture 8 Object Oriented Programming (OOP)
9-10 Classes: A Deeper Look.
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

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 when accessing members directly. this – It is used explicitly when using keyword this. this const – The type of the this pointer depends on the type of the object and whether the executing member function is declared const. 2

Systems Programming: Deeper into C++ Classes 3 this this Example

Systems Programming: Deeper into C++ Classes 4 Implicitly using the this pointer to access member x Explicitly using the this pointer to access member x Using the dereferenced this pointer and the dot operator this this Example

Common Programming Error. pointer Attempting to use the member selection operator (. ) with a pointer to an object is a compilation error lvalue such as an object’s name the dot member selection operator may be used only with an lvalue such as an object’s name, a reference to an object or a dereferenced pointer to an object. Systems Programming: Deeper into C++ Classes 5

6 static static Class Members static data member – Only one copy of a variable shared by all objects of a class. – Can be declared public, private or protected. static Fundamental-type static data members Initialized by default to 0. If you want a different initial value, a static data member can be initialized once (and only once). const staticintenum const static data member of int or enum type Can be initialized in its declaration in the class definition. Employee::count;) Exists even when no objects of the class exist (ex:Employee::count;) Also accessible through any object of that class Employee e("Maher","Ahmed",10000); e. Count; Static member function can access only static data, because the function might be called when no objects exist

Function prototype for static member function static data member keeps track of number of Employee objects that currently exist

static data member is defined and initialized at file scope in the.cpp file static member function can access only static data, because the function might be called when no objects exist

Non- static member function (i.e., constructor) can modify the class’s static data members

Calling static member function using class name and binary scope resolution operator Dynamically creating Employee s with new Calling a static member function through a pointer to an object of the class

Arrays of objects An array of class objects is defined in the same manner as build-in types. Employee list [10]; //Classname arrayname [size] Defines an array to hold 10 Student objects, one for each student I own. Which constructor is used? – The default constructor is used because no arguments are specified. The are several ways to use other constructors to form these objects,: – that is, to pass arguments to the constructor. Static array: int primes[5] = {1,2,3,5,7}; Employee list [3] = { Employee(“Saleh", "Mohammed",, 10000); Employee(“Noura”, "Salman",12000); Employee(“Sara","Sadd",8000); } Dynamic Array: int * primes = new int [5]; Employee *list= new Employee [10]; //The default constructor will be used.

C++ Programming: From Problem Analysis to Program Design, Second Edition 13 Arrays of Class Objects-Example Employee list[60]; //call the efault constructor 60 times string f, l; double s; //to fill the objects details from the user, you have to use set function for (int i = 0;i < 60; i++){ cout<<"Enter the first name, last name, and Salary of the employee:\n"; cin>>f>>l>>s; list [i].setFirstName(f); list [i].setLastName(l); List[i].setSalary(); } //to print the data into the screen cout <<left<<setw(15)<<"First Name"<<setw(15)<<"Last Name"<<"Salary\n";//header for (int i = 0;i < 60; i++){ cout<<left<<setw(15)<<list[i].getFirstName()<<setw(15)<<list[i].getLastName()<<list[i. getSalary ()<<endl; }

#include using namespace std; class Rectangle { int width; int height; public: Rectangle(int w=0, int h=0) { width = w; height = h; cout << "Constructing " << width << " by " << height << " rectangle.\n"; } ~Rectangle() { cout << "Destructing " << width << " by " << height << " rectangle.\n"; } void set(int w, int h) { width = w; height = h; } int area() { return width * height; } }; int main() { int size, w, h; Rectangle *p; cout << "How many rectangles?"; cin>>size; p = new Rectangle [size]; cout << "\n"; for(int i=0; i < size; i++) { cout<<"Provide the dimensions of a rectangle:"; cin>>w; cin>>h; p[i].set(w,h); } for(int i=0; i < size; i++) cout << "Area is " << p[i].area() << endl; delete [] p; return 0; } Rectangle -width:int -hieght:int +Rectangle ( int=0,int=0) +set ( int, int):void +area():int +~Rectangle() Example

Most Common Error