Lecture 8 – 9 Arrays with in a class

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Enumerated Types and Strings Types.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Lecture 3: Getting Started & Input / Output (I/O)
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Lecture 11 Multi-dimensional Arrays
Chapter 7 Pointers and C-Strings
Class and Object Cont’d
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Class and Objects UNIT II.
Motivation and Overview
Command Line Arguments
Concepts and Basics of C++ Programming
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Student Book An Introduction
Multi-dimensional Array
Pointers Psst… over there.
C++ Arrays.
Concepts and Basics of C++ Programming
Lecture 4-7 Classes and Objects
14th September IIT Kanpur
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
C Passing arrays to a Function
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 2 Elementary Programming
Array of objects.
Contents Introduction to Constructor Characteristics of Constructor
Arrays Kingdom of Saudi Arabia
Pointers & Functions.
Lecture 12 Oct 16, 02.
Classes and Objects.
Array of objects.
Pass by Reference.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Object Oriented Programming Using C++
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Arrays of Two-Dimensions
7 Arrays.
CHAPTER 2 Arrays and Vectors.
Glenn Stevenson CSIS 113A MSJC
Pointers and Pointer-Based Strings
CHAPTER 4 File Processing.
Arrays Arrays A few types Structures of related data items
Submitted By : Veenu Saini Lecturer (IT)
CHAPTER 2 Arrays and Vectors.
Array-Based Lists & Pointers
Pointers and dynamic objects
Using string type variables
Pointers & Functions.
CS 144 Advanced C++ Programming February 12 Class Meeting
Structure (i.e. struct) An structure creates a user defined data type
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Dr. Khizar Hayat Associate Prof. of Computer Science
Object Oriented Programming (OOP) Lecture No. 12
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Lecture 8 – 9 Arrays with in a class Memory allocation for objects of class Array of objects Object as function arguments

Arrays within a class Arrays can be used as class member variables. a is an integer array. It is declared as a private data member of the class array. Can be used in class member functions. class array { int a[10]; public: void setVal(); void sort(); void display(); };

Example #include<iostream> using namespace std; class array { int a[10]; public: void setVal() { for (int i = 0; i < 10; i++) cin >> a[i]; } void sort() { for (int i = 0; i < 9; i++) for (int j = 0; j < 10 - i - 1; j++) { if (a[j] > a[j + 1]) { a[j] = a[j] + a[j + 1]; a[j + 1] = a[j] - a[j + 1]; a[j] = a[j] - a[j + 1]; } } }

Contd… void display() { for (int i = 0; i < 10; i++) cout << a[i] << '\t'; } }; int main() { array Arr; Arr.setVal(); Arr.sort(); cout << "Sorted sequence is:\n"; Arr.display(); Output: 10 9 8 7 6 5 4 3 2 1 Sorted sequence is: 1 2 3 4 5 6 7 8 9 10

Memory allocation for objects of class

Contd… During class specification During object creation No memory space is allocated for the class data members. Memory is allocated to class member functions once they are defined as a part of class specification. During object creation No separate space is allocated for class member functions, they remain same for all objects. Memory is allocated for class data members separately for each object as they hold different data values for different objects.

Array of objects It is possible to have arrays of objects. Means array of variables of type class. The syntax for declaring and using an object array is exactly the same as it is for any other type of array. Use usual array-accessing methods to access individual elements, and then the dot member operator to access the member functions. An array of objects is stored inside the memory in the same way as a multi-dimensional array.

Contd… class emp { char name[10]; int age; public: void getData(); void putData(); }; emp manager[3]; // Array of manager emp worker[10]; // Array of worker Accessing member functions. manager[i].getData(); manager[i].putData();

Contd… An array of objects is stored similar to multi-dimensional array. emp manager[3]; name manager[0] age manager[1] manager[2]

Example #include<iostream> using namespace std; class emp { char name[10]; int age; public: void getData(); void putData(); }; void emp :: getData() { cout << "Enter Name: "; cin >> name; cout << "Enter Age: "; cin >> age; } void emp :: putData() { cout << "\tName: " << name << "\tAge: " << age << endl; }

Contd… int main() { emp manager[3]; for (int i = 0; i < 3; i++) { cout << "\nEnter details of manager " << i + 1 << endl; manager[i].getData(); } { cout << "\nManager " << i + 1; manager[i].putData(); return 0;

Contd… Enter details of manager 1 Enter Name: Arun Enter Age: 50 Enter details of manager 2 Enter Name: Amita Enter Age: 35 Enter details of manager 3 Enter Name: Yashika Enter Age: 45 Manager 1 Name: Arun Age: 50 Manager 2 Name: Amita Age: 35 Manager 3 Name: Yashika Age: 45

Objects as function arguments Object can be passed as a function argument, like any other data type to member functions as well as non-member functions. Pass-by-value Pass a copy of the entire object, which destroys when the function terminates. Changes made to the copy of an object within function are not reflected in the actual object. Pass-by-reference Pass only the address of the object, not the entire object. Changes made to an object within the function are also reflected in the actual object.

Contd… When a class object is passed to a member function of the same class. Its data members can be accessed inside the function using the object name and the dot operator. However, data members of the calling object can be accessed directly inside the function without using the object name and the dot operator. When a class object is passed to a non-member function. Its public members can only be accessed inside the function using the object name and the dot operator. No access to private members.

Example (Pass-by-value) #include <iostream> using namespace std; class Convert { public : int i; void increment(Convert obj) { obj.i=obj.i*2; cout << "Value of i in member function : " << obj.i; } }; int main () { Convert obj1; obj1.i=3; obj1.increment(obj1); cout << "\nValue of i in main : " << obj1.i << "\n"; return 0; } Output: Value of i in member function : 6 Value of i in main : 3

Example (Pass-by-reference) #include <iostream> using namespace std; class Convert { public : int i; void increment(Convert &obj) { obj.i=obj.i*2; cout << "Value of i in member function : " << obj.i; } }; int main () { Convert obj1; obj1.i=3; obj1.increment(obj1); cout << "\nValue of i in main : " << obj1.i << "\n"; return 0; } Output: Value of i in member function : 6 Value of i in main : 6

Example #include<iostream> using namespace std; class Time { int h, m, s; public: void getTime(int p, int q, int r) { h = p; m = q; s = r; } void putTime() { cout << h << ":" << m << ":" << s << endl; } void sumTime(Time t1, Time t2) { s = t1.s + t2.s; m = s / 60; s = s % 60; m = m + t1.m + t2.m; h = m / 60; m = m % 60; h = h + t1.h + t2.h; } };

Contd… int main() { Time t1, t2, t3; t1.getTime(1,30,15); t3.sumTime(t1,t2); cout << "T1: "; t1.putTime(); cout << "T2: "; t2.putTime(); cout << "T3: "; t3.putTime(); return 0; } Output: T1: 1:30:15 T2: 4:30:55 T3: 6:1:10