You are on page 1of 6

EXPERIMENT NUMBER –Practical 2

STUDENT’S NAME – YANA SRIVASTAVA


STUDENT’S UID – 20BCS2279
CLASS AND GROUP – 15 “B”
SEMESTER – 2ND

TOPIC OF EXPERIMENT –
WAP to swap private data members of classes named as
class_1,class_2 using friend function.

AIM OF THE EXPERIMENT


Learn the concept of using friend function.
FLOWCHART/ ALGORITHM

Step 1: START
Step 2: Define class with friend function swap.
Step 3: Member function will display values of the data.
Step 4: Declare friend function.
Step 5: Define friend function.
Step 6: Member function displays values.
Step 7: Values swapped by a function.
Step 8: Swapped values displayed.
Step 9: END.

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
PROGRAM CODE
#include<iostream>
using namespace std;
class class_2;
class class_1
{
protected:
int num1;
public:
void setData(int a)
{
num1=a;
}
void show()
{
cout<<"\n Value of Number 1 : "<<num1;
}
friend void swap(class_1 *num1, class_2 *num2);
};
class class_2
{
protected:
int num2;
public:
void setData(int b)
{
num2=b;
}
void show()
{
cout<<"\n Value of Number 2 : "<<num2;
}
friend void swap(class_1 *num1, class_2 *num2);
};
void swap(class_1 *no1, class_2 *no2)
{
int no3;
no3=no1->num1;
no1->num1=no2->num2;
no2->num2=no3;
}
int main()
{
class_1 b;
b.setData(20);
class_2 d;
d.setData(40);

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
swap(&b, &d);
b.show();
d.show();
return 0;
}

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION
(Kindly jot down the compile time errors encountered)
No Error

PROGRAMS’ EXPLANATION (in brief)


In this program by using friend function we have to swap the two
private members of two different classes.

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
LEARNING OUTCOMES
• Identify situations where computational methods would be useful.
• Approach the programming tasks using techniques learnt and write pseudo-code.
• Choose the right data representation formats based on the requirements of the problem.
• Use the comparisons and limitations of the various programming constructs and choose the
right one for the task.

EVALUATION COLUMN (To be filled by concerned faculty only)


Sr. No. Parameters Maximum Marks
Marks Obtained
1. Worksheet Completion including writing 10
learning objective/ Outcome
2. Post Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre Lab Questions
4. Total Marks 20

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152

You might also like