Recap: Interfaces A class is a logical abstraction, but an object has physical existence. access-specifier can be: public: Allow functions or data to be.

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 Pointers and Strings Section 5.4, , Lecture 12.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Chapter 14 Operator Overloading: What does + mean? Consider the code below. Note the multiple uses of “+”. #include using namespace std; int main() { int.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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];
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 CSC241: Object Oriented Programming Lecture No 07.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
The Queue class //this class implements a Queue class Queue{ int q[100];//an array of integers int sloc, rloc; //start location and rear location public:
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Case Study - Fractions Timothy Budd Oregon State University.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Learners Support Publications Classes and Objects.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
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.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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?
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
1 CSC241: Object Oriented Programming Lecture No 08.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Object-Oriented Programming (OOP) and C++
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Class and Objects UNIT II.
Pointer Data Type and Pointer Variables II
Pointers Psst… over there.
this Pointer Scope Resolution Operator Friend Function
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Pointers Psst… over there.
CS5253 Workshop I Lecturer: Dr. Lusheng Wang
Pointers & Functions.
Anatomy of a Function Part 1
Operator Overloading.
Java Programming Language
Pointers & Functions.
Introduction to Programming
Presentation transcript:

Recap: Interfaces A class is a logical abstraction, but an object has physical existence. access-specifier can be: public: Allow functions or data to be accessible to other parts of the program. private: May be accessed only by other members of the class. protected: Used when inheritance is involved; not discussed yet, will do later today

friends #include using namespace std; class myclass { int a, b; public: friend int sum(myclass x); //declared in the public interface void set_ab(int i, int j); }; void myclass::set_ab (int i, int j) { a = i; b = j; } // Note: sum() is not a member function of any class. int sum (myclass x) { //Because sum() is a friend of myclass, it // can directly access a and b. return x.a + x.b; } main() { myclass n; n.set_ab (3, 4); cout << sum(n); return 0; } //see friend1.cpp

Friends A friend function has access to all private and protected (and public of course) members of the class for which it is a friend. See friend1.cpp example See also friend1b.cpp

//use of friend function to access private data //members of class class Myclass{ private: int a,b; public: Myclass(int i, int j){a=i; b=j;} friend int sum(myclass x); }; //note sum is not a member function of any class int sum (Myclass x) {return x.a+x.b;} int main(){ Myclass n(3,4); cout << sum(n) << "\n"; return 0; }

Friends The friend function negates information hiding This is the whole idea of OO programming i.e. encapsulation We now have a way of destroying this property For this reason many programmers are not keen on the use of friend functions We will return to friends again later in this presentation.

Arrays of Objects We can create arrays of objects just as we can create an array of integers or doubles See ArrayofObjects1.cpp See ArrayofObjects2.cpp //this initialises //an array of // objects See ArrayofObjects3.cpp //2D array of //objects //ignore last program

Pointers to Objects Recall we access a structure directly or indirectly using a pointer to that structure. In like fashion we can access an object either directly or indirectly using pointers again To access an element of an object when using the actual object we use the dot notation To access a specific element of an object using the pointer we use -> (arrow link) See objectpointer1.cpp

Incrementing pointers As we know when a pointer is incremented(or decremented) it is increased (or decreased) in such as way that it will always point to next element of its base type See objectPointer2.cpp

More on friends So it is possible to allow a non-member function of any class to access the private members of a class by declaring it as a friend of the class. To make a function a friend of a class you include its prototype in the public section of a class declaration and precede it with the friend keyword

Demo See friend2.cpp and explain

#include using namespace std; class Myclass { int a, b; public: friend int sum(myclass x); void set_ab(int i, int j); }; void Myclass::set_ab (int i, int j) { a = i; b = j; } // Note: sum() is not a member function of any class. int sum (Myclass x) {//Because sum() is a friend of myclass, it can directly access a and b. return x.a + x.b; } main() { Myclass n; n.set_ab (3, 4); cout << sum(n); return 0; }

Assigning objects If both objects are of the same type (i.e. both objects are of the same class) then one object may be assigned to another. The following program demonstrates this See objectassignment1.cpp

#include using namespace std; class Myclass{ private: int a,b; public: void setab(int i,int j){a=i;b=j;} void showab(); }; void Myclass::showab(){ cout << "a is " << a << endl; cout << "b is " << b << endl; }

int main(){ Myclass ob1,ob2; ob1.setab(10,20); ob2.setab(0,0); cout << "ob1 before assignment" << endl; ob1.showab(); cout << endl; cout << "ob2 before assignment" << endl; ob2.showab(); cout << endl; ob2=ob1; cout << "ob1 after assignment" << endl; ob1.showab();cout << endl; cout << "ob2 after assignment" << endl; ob2.showab();cout << endl; return 0; }

Passing objects to functions An object can be passed to a function In the same way as any other data type. Objects are passed to functions by using the normal C++ call by-value parameter-passing convention. This means that a copy of the objects and not the object itself is passed to the function. Thus any changes made to the object inside the function do NOT affect the object used as argument to the function

Demonstration See passingobjects1.cpp and explain See passingobjectsbyRef.cpp and explain The comments indicate that the modification to x within f() has no affect on the object 0 inside main()