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

100 Python Programs for Beginner (59) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (938) Python Coding Challenge (373) Python Quiz (31) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

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