Friend function and objects of different classes
Question:1.
Declare a class named first with attribute as “x”. Declare another class named second with attribute as “y”. (Using friend function) calculate and print sum of the private data elements of the objects of the classes first and second.
Sol:
Solution In CodeBlocks/Dev C++
/* Program in codeblocks */ #include <iostream> using namespace std; class second; class first { private: int x; public: void read(); void show(); friend void add(first A,second B); }; class second { private: int y; public: void read(); void show(); friend void add(first A,second B); }; void first::read() { cout<<"Enter value of x "; cin>>x; } void first::show() { cout<<"x = "<<x<<endl; } void second::read() { cout<<"Enter value of y "; cin>>y; } void second::show() { cout<<"y = "<<y<<endl; } void add(first A,second B) { int t; t=A.x+B.y; cout<<"sum = "<<t<<endl; } int main() { first f; second s; f.read(); s.read(); f.show(); s.show(); add(f,s); return 0; }
Solution In Turboc3
/* this program is developed in turboc3 */ #include<iostream.h> #include<conio.h> class second; class first { private: int x; public: void read(); void show(); friend void add(first f,second s); }; class second { private: int y; public: void read(); void show(); friend void add(first f,second s); }; void first::read() { cout<<"Enter x value "; cin>>x; } void first::show() { cout<<"x ="<<x<<endl; } void second::read() { cout<<"Enter y value "; cin>>y; } void second::show() { cout<<"y="<<y<<endl; } void add(first f,second s) { int t; t=f.x+s.y; cout<<"Sum ="<<t<<endl; } void main() { clrscr(); first f1; second s1; f1.read(); s1.read(); f1.show(); s1.show(); add(f1,s1); getch(); }
/* Output */ Enter value of x 10 Enter value of y 20 x = 10 y = 20 sum = 30
Question:2.
Declare a class named student1 with attributes as roll, name and age. Declare another class named student2 with attributes same as student1. Take input for the details calculate and print sum and average of ages of the objects of the classes student1 and student2.
Sol:
Solution In CodeBlocks/Dev C++
#include <iostream> #include<conio.h> using namespace std; class student2; class student1 { private: int roll; char name[20]; float age; public: void read(); void show(); friend void cal(student1 A,student2 B); }; class student2 { private: int roll; char name[20]; float age; public: void read(); void show(); friend void cal(student1 A,student2 B); }; void student1::read() { cout<<"Enter roll,name and age "; cin>>roll>>name>>age; } void student1::show() { cout<<roll<<" "<<name<<" "<<age<<endl; } void student2::read() { cout<<"Enter roll,name and age "; cin>>roll>>name>>age; } void student2::show() { cout<<roll<<" "<<name<<" "<<age<<endl; } void cal(student1 A,student2 B) { float t; t=A.age+B.age; cout<<"Sum of age "<<t<<endl; cout<<"avg age "<<t/2<<endl; } int main() { student1 s1; student2 s2; s1.read();s2.read(); s1.show();s2.show(); cal(s1,s2); getch(); return(0); }
Question:3.
Declare a class employee1 with attributes as empno, name and sal. Declare another class named employee2 with attributes same as employee1. Take input for the details calculate and print sum and average of salaries of the objects of the classes employee1 and employee2.
Sol:
Solution In CodeBlocks/Dev C++
#include <iostream> #include<conio.h> using namespace std; class emp2; class emp1 { private: int empno; char name[20]; float sal; public: void read(); void show(); friend void cal(emp1 A,emp2 B); }; class emp2 { private: int empno; char name[20]; float sal; public: void read(); void show(); friend void cal(emp1 A,emp2 B); }; void emp1::read() { cout<<"Enter empno,name and sal "; cin>>empno>>name>>sal; } void emp1::show() { cout<<"empno "<<empno<<" name "<<name<<" sal "<<sal<<endl; } void emp2::read() { cout<<"Enter empno,name and sal "; cin>>empno>>name>>sal; } void emp2::show() { cout<<"empno "<<empno<<" name "<<name<<" sal "<<sal<<endl; } void cal(emp1 A,emp2 B) { float t; t=A.sal+B.sal; cout<<"Sum of sal "<<t<<endl; cout<<"avg sal "<<t/2<<endl; } int main() { emp1 e1; emp2 e2; e1.read(); e2.read(); e1.show(); e2.show(); cal(e1,e2); getch(); return(0); }
/* Output */ Enter empno,name and sal 1001 abc 45000 Enter empno,name and sal 1002 xyz 55000 empno 1001 name abc sal 45000 empno 1002 name xyz sal 55000 Sum of sal 100000 avg sal 50000
Question:4.
Declare a class named husband with attributes as name and salary. Declare another class named wife with attributes as name and income. Calculate and display the following:
(i). total income of the family.
(ii). Avg income of husband and wife.
Sol:
Solution In CodeBlocks/Dev C++
#include <iostream> #include<conio.h> using namespace std; class husband; class wife { private: char name[15]; float income; public: void read(); void show(); friend void cal(husband h,wife w); }; class husband { private: char name[15]; float salary; public: void read(); void show(); friend void cal(husband h,wife w); }; void husband::read() { cout<<"Enter name and salary "; cin>>name>>salary; } void husband::show() { cout<<name<<" "<<salary<<endl; } void wife::read() { cout<<"Enter name and income "; cin>>name>>income; } void wife::show() { cout<<name<<" "<<income<<endl; } void cal(husband h,wife w) { float t; t=h.salary+w.income; cout<<"Total family income "<<t<<endl; cout<<"Avg family income "<<t/2<<endl; } int main() { husband h1; wife w1; h1.read();w1.read(); h1.show();w1.show(); cal(h1,w1); return(0); }
/* Output */ Enter name and salary Amit 45000 Enter name and income Sunita 54000 Amit 45000 Sunita 54000 Total family income 99000 Avg family income 49500
Solution In Turboc3
/* this program is developed in turboc3 */ #include<iostream.h> #include<conio.h> class husband; class wife { private: char name[15]; float income; public: void read(); void show(); friend void cal(husband h,wife w); }; class husband { private: char name[15]; float salary; public: void read(); void show(); friend void cal(husband h,wife w); }; void husband::read() { cout<<"Enter name and salary "; cin>>name>>salary; } void husband::show() { cout<<name<<" "<<salary<<endl; } void wife::read() { cout<<"Enter name and income "; cin>>name>>income; } void wife::show() { cout<<name<<" "<<income<<endl; } void cal(husband h,wife w) { float t; t=h.salary+w.income; cout<<"Total family income "<<t<<endl; cout<<"Avg family income "<<t/2<<endl; } void main() { clrscr(); husband h1; wife w1; h1.read();w1.read(); h1.show();w1.show(); cal(h1,w1); getch(); }