You are on page 1of 11

21 Write a program to demonstrate object as argument.

Code:

#include <iostream>

Using namespace std;

Class student

Private:

Int a;

Public:

Void set(int x)

A = x;

Void sum(student ob1, student ob2)

A = ob1.a + ob2.a;

Void print()
{

Cout<<”Value of A : “<<a<<endl;

};

Int main()

Student s1;

Student s2;

Student s3;

S1.set(10);

S2.set(20);

S3.sum(s1,s2);

S1.print();

S2.print();

S3.print();

Return 0;

}
OUTPUT :-

22 Write a program to find addition of time (hours and minutes) using object as
argument and return the object.

Code:-

#include<iostream>

Using namespace std;

Class time

Int hr,min,sec;
Public:

Void get()

Cout<<”\nEnter Hour :: “;

Cin>>hr;

Cout<<”\nEnter Minutes :: “;

Cin>>min;

Cout<<”\nEnter Seconds :: “;

Cin>>sec;

}
Void disp()

Cout<<”[ “<<hr<<”:”<<min<<”:”<<sec<<” ] \n”;

Void sum(time &,time &);

};

Void time::sum(time &t1,time &t2)

{
Sec=t1.sec+t2.sec;

Min=sec/60;

Sec=sec%60;

Min=min+t1.min+t2.min;

Hr=min/60;

Min=min%60;

Hr=hr+t1.hr+t2.hr;

Int main()
{

Class time t1,t2,t3;

Cout<<”\nEnter 1st time Details :: \n”;

T1.get();

Cout<<”\nEnter 2nd time Details :: \n”;

T2.get();

Cout<<”\nThe 1st time entered is :: “;

T1.disp();

Cout<<”\nThe 2nd time entered is :: “;

T2.disp();

T3.sum(t1,t2);

Cout<<”\nThe Sum of two times are :: “;


T3.disp();

Return 0;

OUTPUT:-
23 Write a program to find maximum number between two numbers, both numbers
are declared in two different class. Create one friend function named max which
takes object of two classes as argument.

Code:-
#include<iostream>

Using namespace std;

Class B;

Class A

Int x;

Public:

A(int i)

X=I;

Friend void max(A,B);

};

Class B

Int y;
Public:

B(int i)

Y=I;

Friend void max(A,B);

};

Void max(A a , B b)

If(a.x>b.y)

Cout<<”class A’s member is greater :-“<<a.x;

Else

Cout<<”class B’s member is greater :-“<<b.y;

Int main()

A a=A(30);

B b=B(20);

Max(a,b);

Return 0;

}
Output:-

You might also like