Access Functions and Friend Functions

Slides:



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

Operator overloading redefine the operations of operators
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
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.
C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Short C++ Review CS 302 – Data Structures. Call by value/reference.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Structures/Classes CS 308 – Data Structures. What is a structure? It is an aggregate data type built using elements of other types. Declaring a structure.
Constructors & Destructors Review CS 308 – Data Structures.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
CMSC 2021 Stream I/O Operators and Friend Functions.
Definition Class In C++, the class is the construct primarily used to create objects.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Object Oriented Programming (OOP) Lecture No. 11.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
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.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
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.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 Class 19 Chapter 13 – Creating a class definition.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Function Overloading Can enables several function Of same name
Two or more functions can have the same name but different parameters
Dr. Shady Yehia Elmashad
CONSTRUCTORS & DESTRUCTORS
Concepts of Constructors and Its Types
Chapter 5 Classes.
CS1201: Programming Language 2
Constructors & Destructors
Dr. Shady Yehia Elmashad
Dr. Shady Yehia Elmashad
CS150 Introduction to Computer Science 1
Implementing Non-Static Features
Constructor Spl member fn auto ini of object
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
Static in Classes CSCE 121 J. Michael Moore.
CS150 Introduction to Computer Science 1
Introduction to Programming
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
Object Oriented Programming (OOP) Lecture No. 11
Class: Special Topics Overloading (methods) Copy Constructors
Objects as Function Arguments
Classes Member Qualifiers
Presentation transcript:

Access Functions and Friend Functions CS 308 – Data Structures

Access functions To allow clients to read the value of private data, the class can provide a get function. To allow clients to modify private data, the class can provide a set function.   #include <iostream.h> class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function float get_height(); // access function void set_height(float); // access function };

float rectangle::get_height() return (height); { return (height); }   void rectangle::set_height(float h) height = h; void main() rectangle rc(1.0, 3.0); float value; value = rc.get_height(); cout << "height: " << value << endl; rc.set_height(10.0);

Friend functions Friend functions of an object can "see" inside the object and access private member functions and data. To declare a function as a friend of a class, precede the function prototype in the class definition with the keyword friend.

friend void set_value(rectangle&, float); // friend declaration class rectangle {   friend void set_value(rectangle&, float); // friend declaration private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function float get_height(); // access function void set_height(float); // access function };

void set_value(rectangle &rc, float h) { rc.height = h; }   void main() rectangle rc(1.0, 3.0); set_value(rc, 10.0);