Passing objects As arguments

Slides:



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

Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
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.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
Learners Support Publications Classes and Objects.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
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++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CONSTRUCTOR AND DESTRUCTORS
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.
Function Overloading and References
Chapter -6 Polymorphism
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Constructors and Destructors
Constructors and Destructors
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Functions + Overloading + Scope
Friend functions.
2 Chapter Classes & Objects.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
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.
Introduction to C++ Systems Programming.
CONSTRUCTORS & DESTRUCTORS
FUNCTIONS In C++.
Review: Two Programming Paradigms
Classes & Objects.
Chapter 5 Functions.
Introduction to C++.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CS212: Object Oriented Analysis and Design
Lecture 6 C++ Programming
Templates.
User-Defined Functions
C Passing arrays to a Function
Contents Introduction to Constructor Characteristics of Constructor
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Dr. Bhargavi Dept of CS CHRIST
Polymorphism Polymorphism
Constructors and Destructors
Classes and Objects.
FUNCTION CSC128.
Chapter 6 Methods.
Dr. Khizar Hayat Associate Prof. of Computer Science
In C Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Dr. Khizar Hayat Associate Prof. of Computer Science
TOPIC: FUNCTION OVERLOADING
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Presentation transcript:

Passing objects As arguments An object can be passed as an argument to a function by the following ways: Pass-by-value, a copy of the entire object is passed to the function . Pass-by-reference, only the address of the object is passed implictly to the function. Pass- by-pointer, the address of the object is passed explicitly to the function.

Passing objects by value A copy of the object is passed to the function and any modifications made to the object inside the function is not reflected in the object used to call the function.

E.g. Pass –By-value #include <iostream.h> void swapnum(int c, int d) { int temp = c; c = d; d = temp; } int main(void) // input a=10 ---- 1000 { b=20--- 2000 int a = 10; //output c=10— 3000 int b = 20; d=20---4000 not reflected swapnum(a, b); cout<<"A is %d and B is %d\n“<< a, b; return 0;

Passing objects by reference An address of the object is passed to the function and any changes made to the object inside the function is reflected in the actual object.

E.g. Pass –By-reference #include <iostream.h> void swapnum(int &c, int &d) { int temp = c; c = d; d = temp; } int main(void) { // Alais name,reflected,value swap int a = 10; //input a=10 ---1000 int b = 20; b=20----2000 //output c=20---1000 swapnum(a, b); d=10---2000 cout<<"A is %d and B is %d\n“<< a, b; return 0;

Passing objects by Pointer The address of the object is passed explicitly to the function.

E.g. Pass –By-Pointer #include <iostream.h> void swapnum(int * c, int *d) { int temp = c; c = d; d = temp; } int main(void) int a = 10; // address swap int b = 20; swapnum(&a, &b); cout<<"A is %d and B is %d\n“<< a, b; return 0;

Friend Function and Friend Classes The concept of encapsulation and data hiding dictate that non-member functions should not be allowed to access an object’s private and protected members. Using friend function or friend class a non-member function can able to access private data members. A function declaration must be prefixed by the keyword friend whereas the function definition must not. A function can be a friend to multiple classes.

Friend function -Special characteristics The scope of a friend function is not limited to the class in which it has been declared as a friend. A friend function cannot be called using the objects of that class; it is not in the scope of the class. It can be invoked like a normal function without the use of any object. It can be declared in the private part or the public part of the class without affecting its meaning.

e.g. //friend function # include <iostream.h> class sample { int a,b; public: void setdata() a=10,b=20; } friend float mean(sample S); }; float mean(sample S) return float(S.a+S.b)/2.0; int main () sample x; x.setdata(); cout<<“Mean value=“<<mean(x)<<“\n”; return 0;

e.g. Normal function accessing object’s private members #include<iostream.h> #include<conio.h> class AC; class AB { int x; public: void setvalue(int i) x=i; } friend void add(AB,AC); };

class AC { int a; public: void setvalue(int i) a=i; } friend void add(AB,AC); }; void add(AB m,Can) cout<<“The total value of distance is:”<<(m.x+n.a)<<“m”; int main() clrscr(); AB ab1; ab1.setvalue(10); AC ac1; ac1.setvalue(20); add(ab1,ac1); getch(); return(0);

Static data member A data member of a class can be qualified as static. Characteristics of Static data members: It is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one copy of that member is created for entire class and is shared by all the objects of that class. It is visible only within the class, but its lifetime is the entire program.

Static Member functions A static function can have access to only other static members declared in the same class. A Static member function can be called using the class name(instead of its objects)as follows: syntax: Class name ::function name

#include<iostream. h> #include<conio #include<iostream.h> #include<conio.h> class stat { int code; static int count; public: stat() code=++count; } void showcode() cout<<"\n\tObject number is :"<<code; static void showcount() cout<<"\n\tCount Objects :"<<count; };

int stat::count; void main() { clrscr(); stat obj1,obj2; stat::showcount(); obj1.showcode(); obj2.showcode(); getch(); } Output: Count Objects: 2 Object Number is: 1 Object Number is: 2

Difference b/w static and Non-static 1) A static member function can access only static member data, static member functions and data and functions outside the class. A non-static member function can access all of the above including the static data member.

2) A static member function can be called, even when a class is not instantiated, A non-static member function can be called only after instantiating the class as an object.

3)A static member function cannot be declared virtual whereas a non-static member functions can be declared as virtual

4) A static member function cannot have access to the 'this' pointer of the class.

Function A function is a set of program statements that can be processed independently. A function can be invoked by a function call. The communication between a caller(calling function) and callee(called function) takes place through parameters.

Advantages of Function Reduction in the amount of work and development time. Program and function debugging is easier. Reduction in size of the program due to code reusability. Library of functions can be implemented by combining well designed, tested and proven functions.

Function components Every function has the following elements associated with it: Function declaration or prototype. Function parameters(formal parameters) Combination of function declaration and its definition. Function definition(function declarator and a function body) Return Statement. Function call

Syntax: Components of a function Void func(int a,int b); // Prototype Void func(int a,int b) // declarator (formal parameters) { //body } func(x,y); //call (actual parameter)

Eg. Maximum of two numbers #include<iostream.h> int max(int x,int y); // prototype void main() // function caller { int a,b,c; cout<<“Enter two integers:”; cin>>a>>b; c=max(a,b); cout<<“max(a,b)”<<c<<endl; Output: int max(int x,int y) // function definition Enter two integers:40,50 { max(a,b):50 if(x>y) return x; else return y; }

Library Function Library functions are shipped along with compliers. They are predefined and precompiled into library files and their prototypes can be found in the files with .h as their extension in the include directory The definitions are available in the form of objects codes in the files with .lib as their extension in the lib directory. In order to make use of a library function, include the corresponding header file. Once the header file is included, any function available in that library can be invoked.

Eg. Sqrt(),pow(),max() –declared and math.h //use of library function calls to round and truncate a result #include<iostream.h> #include<math.h> void main() { float n,n1,n2; cout<<“Enter any fractional number:”; cin>>n; n1=ceil(n); //rounds up n2=floor(n); //rounds down cout<<“ceil(“<<n<<“)=“<<n1<<endl; cout<<“floor(“<<n<<“)=“<<n2<<endl; } Output: Enter any fractional number:2.9 ceil(2.9)=3 floor(2.9)=2

Inline Function Inline functions are those whose function body is inserted in place of the function call statement during the compilation process. Within inline code, the program will not incur any context switching overhead. An inline function definition is similar to an ordinary function except that the keyword inline precedes the function definition. The significant feature of inline function is ,there is no explicit function call and body is substituted at the point of inline function call , therefore, runtime overhead for function linkage mechanism is reduced

//Square of number using inline function #include<iostream.h> inline intsquare(int num) // memory space optimized { return num*num; } void main(){ float n; cout<<“Enter a number:”; cin>>n; cout<<“Square is”<<Square(n)<<endl; Output: Enter a number:6 Square is:36

Function overloading Function overloading is a concept that allows multiple functions to share the same name with different arguments types. Assigning one or more function body to the same name is known as function overloading or function name overloading.

// Multiple swap function ,function overloading #include<iostream // Multiple swap function ,function overloading #include<iostream.h> void swap(char &x,char &y) { char t; t=x; x=y; y=t; } void swap(int &x,int &y) int t; void swap(float &x,float &y) float t;

void main() { char ch1,ch2; cout<<“Enter two charater<ch1,ch2>:”; cin>>ch1>>ch2; swap(ch1,ch2); // complier calls swap(char &a, char &b) cout<<“On swapping<ch1,ch2>:”<<ch1<<“ “<<ch2<<endl; int a,b; cout<<“Enter two intergers<a,b>:”; cin>>a>>b; swap(a,b);// complier calls swap(int &a, int &b) cout<<“On swapping<a,b>:”<<a<<“ “<<b<<endl; float c,d; cout<<“Enter two floats<c,d>:”; cin>>c>>d; swap(c,d);// complier calls swap(float &a, float &b) cout<<“On swapping<c,d>:”<<c<<“ “<<d<<endl; }

Output: Enter two character<ch1,ch2>:A B On Swapping<ch1,ch2>: B A Enter two integer<a,b>: 2 3 On Swapping<a,b>: 3 2 Enter two floats<c,d>:10.5 5.5 On Swapping<c,d>: 5.5 10.5