Saturday 31 March 2018

Object as Function Argument

An object can be used as a function argument.This can be done in two ways
 1. Pass by value
 2. Pass by reference

Pass by value :
  In pass by value, a copy of entire object is passed to the function so any change made to the object inside the function do not affect the object used to call the function.

Pass by reference :
  In pass by reference , only the object's address is transferred to the function. When an address of the object is passed, the called function works directly on the actual object used in the call.

So any change made to the object inside the function will affect in actual object.

Write a program to illustrate the use of objects as an function argument

#include<iostream.h>
#include<conio.h>
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void puttime( )
{
cout<<hours<<"Hours And"<<minutes<<"Minutes"<<"\n";
}
void sum(time,time);
};
void time::sum(time t1, time t2)
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
void main( )
{
time t1,t2,t3;
clrscr( );
t1.gettime(2,45);
t2.gettime(3,30);
t3.sum(t1,t2);
cout<<"\n\tT1::";
t1.puttime( );
cout<<"\n\tT2::";
t2.puttime( );
cout<<"\n\tT3::";
t3.puttime( );
getch( );
}


Output :-
  T1::2  Hours And 45 Minutes
  T2::3  Hours And 30 Minutes
  T3::6  Hours And 15 Minutes

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (741) Python Coding Challenge (191) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses