0% found this document useful (0 votes)
10 views12 pages

C++ Lecture-21

Uploaded by

Apoorv Malviya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

C++ Lecture-21

Uploaded by

Apoorv Malviya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

01

02

LECTURE 23
04
Today’s Agenda
01 Adding 2 Objects of a Class

02 Adding 2 Objects of a Class using member function

03 Adding 2 Objects of a Class by returning Class object

Adding 2 Objects Using Friend Function by


04 returning class object

05 Adding 2 Objects Using Friend Function passing all 3


05
objects as argument
05
Adding 2 Objects of a Class

We have to create 3 objects, let say D1, D2, and D3 objects of class
Distance having 2 data members named feet and inches and D3 will
store the sum of feet and inches of D1 and D2.

Sample Output:-
D1
feet: 10 .
inches: 8 There are 4 ways to
D2
feet: 9 write a solution to
inches: 7 the given problem
D3
feet: 19 => 20
inches: 15 => 3
First Variety

#include <iostream>
void Distance::add(const Distance & P, const Distance & Q)
using namespace std; D1 P D2 {
feet = P.feet + Q.feet;
class Distance feet 8 feet 7 inches = P.inches + Q.inches;
{ if(inches >= 12)
private:
inches 11 inches
{ D3
int feet; 9 feet = feet + inches / 12;
int inches; inches = inches % 12;
public: } feet 15 16
void get() }
{
cout<<"Enter feet and inches: "; int main()
inches 20 8
cin>>feet>>inches; {
} Distance D1, D2, D3;
void show() D1.get();
{ D2.get();
cout<<feet<<", "<<inches<<endl; D3.add(D1, D2);
} D1.show();
void add(const Distance &, const Distance &); D2.show();
}; D3.show();
return 0;
}.
Output of The Previous Code

Output

Enter feet and inches: 8 11


Enter feet and inches: 7 9
8, 11
7, 9
16, 8

Process returned 0 (0x0) execution time : 4.844 s


Press any key to continue.
Second Variety

#include <iostream> Distance Distance::add(const Distance & Q)

using namespace std; D1 D2


{
Distance Temp;
This line is creating
Temp.feet = feet + Q.feet; a new object
class Distance feet 10 feet 9 Temp.inches = inches + Q.inches;
{ if(Temp.inches >= 12)
private: {
int feet, inches; inches 8 inches 7 Temp.feet = Temp.feet + Temp.inches / 12; Temp
public: Temp.inches = Temp.inches % 12;
void get() } feet 19 20
{ return Temp;
cout<<"Enter feet and inches: "; }
cin>>feet>>inches; int main() inches 15 3
} {
void show() Distance D1, D2, D3;
{ D3 D1.get();
cout<<feet<<", "<<inches<<endl; D2.get();
} D3 = D1.add(D2);
Distance add(const Distance &); feet 20 D1.show();
}; D2.show();
inches 3 D3.show();
return 0;
}
Output of The Previous Code

Output

Enter feet and inches: 10 8


Enter feet and inches: 9 7
10, 8
9, 7
20, 3

Process returned 0 (0x0) execution time : 5.152 s


Press any key to continue.
Third Variety
Adding 2 Objects Using Friend Function
#include <iostream> Distance add(const Distance & P, const Distance & Q)
{
using namespace std; D1 D2 Distance Temp;
Temp.feet = P.feet + Q.feet;
class Distance feet 10 feet 9 Temp.inches = P.inches + Q.inches;
{ if(Temp.inches >= 12)
private: { D3
int feet, inches; inches 8 inches 7 Temp.feet = Temp.feet + Temp.inches / 12;
public: Temp.inches = Temp.inches % 12;
void get() } feet 20
{ return Temp;
cout<<"Enter feet and inches: "; }
cin>>feet>>inches; int main()
inches 3
} {
void show() Distance D1, D2, D3;
{ D1.get();
cout<<feet<<", "<<inches<<endl; D2.get();
} D3 = add(D1, D2);
friend Distance add(const Distance &, const Distance &); D1.show();
}; D2.show();
D3.show();
return 0;
}
Output of The Previous Code

Output

Enter feet and inches: 10 8


Enter feet and inches: 9 7
10, 8
9, 7
20, 3

Process returned 0 (0x0) execution time : 7.052 s


Press any key to continue.
Fourth Variety

#include <iostream> void add(const Distance & P, const Distance & Q, Distance & Temp)
{
using namespace std; D1 D2 Temp.feet = P.feet + Q.feet;
Temp.inches = P.inches + Q.inches;
class Distance feet 10 feet 9 if(Temp.inches >= 12)
{ {
private: Temp.feet = Temp.feet + Temp.inches / 12; D3
int feet, inches; inches 8 inches 7 Temp.inches = Temp.inches % 12;
public: }
void get() } feet 20
{
cout<<"Enter feet and inches: "; int main()
cin>>feet>>inches; {
inches 3
} Distance D1, D2, D3;
void show() D1.get();
{ D2.get();
cout<<feet<<", "<<inches<<endl; add(D1, D2, D3);
} D1.show();
friend void add(const Distance &, const Distance &, Distance &); D2.show();
}; D3.show();
return 0;
}
Output of The Previous Code

Output

Enter feet and inches: 10 8


Enter feet and inches: 9 7
10, 8
9, 7
20, 3

Process returned 0 (0x0) execution time : 4.642 s


Press any key to continue.
End of Lecture 23
For any queries mail us @: [email protected]
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like