You are on page 1of 16

EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Program – 21
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;
}
};

1
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

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:-

2
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Program – 22
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;

3
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Cout<<”\nEnter Minutes :: “;

Cin>>min;

Cout<<”\nEnter Seconds :: “;

Cin>>sec;

Void disp()

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

Void sum(time &,time &);

};

4
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

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;

5
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

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;

6
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

}
Output:

7
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Program -23
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:

8
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

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:-

9
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

10
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Program—24
24 Write a program to demonstrate the default and
parameterized constructor.

Code :-
Parameterized constructor
#include <iostream>
Using namespace std;
Class Wall
{
Private:
Double length;
Double height;

Public:
Wall(double len, double hgt)
{
Length = len;
Height = hgt;
}

Double calculateArea()
{
Return length * height;
}

11
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

};

Int main()
{
Wall wall1(15.5, 6.6);
Wall wall2(5.5, 8.3);

Cout << “Area of Wall 1: “ << wall1.calculateArea() << endl;


Cout << “Area of Wall 2: “ << wall2.calculateArea();

Return 0;
}

Output:-

12
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Default constructor:-
#include <iostream>
Using namespace std;
Class Wall
{
Private:
Double length;

Public:
Wall()
{
Length = 10;
Cout << “Creating a wall.” << endl;
Cout << “Length = “ << length << endl;
}
};

Int main()
{
Wall wall1;
Return 0;
}
Output:-

13
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

14
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Program—25
25 Write a program to demonstrate the constructor
overloading in a class.

Code:-

#include <iostream>
#include <conio.h>
Using namespace std;
Class calarea
{
Public:
Int area;
Calarea(int a)
{
Cout << “\nArea of square : “<<a*a;
}
Calarea(int a,int b)
{
Cout<<”\nArea of rectangle : “<<a*b;
}
};

15
EnrolmentNo :-206170307160 Name:- kush shrikantbhai parikh Division:-c

Int main()
{
Calarea x(15),y(6,9);
Return 0;
}

Output:-

16

You might also like