Bharathiar University
Programming in C++ LabC++ Program 8Write a C++ Program to create two classes each class consists of two private variables, an integer and a float variable. Write member functions to get and display them. Write a FRIEND function common to both classes, which takes the object of above two classes as arguments and the integer and float values of both objects separately and display the result.
In this page
Write a C++ Program to create two classes each class consists of two private variables, an integer and a float variable. Write member functions to get and display them. Write a FRIEND function common to both classes, which takes the object of above two classes as arguments and the integer and float values of both objects separately and display the result.
In this page
- C++ Source Code for MS Dos Turbo C++
Source Code for MS Dos Turbo C++
#include<iostream.h>#include<conio.h>class B;class A{ private: int a; float b; public: void get() { cout<<"\nEnter integer value of class A: "; cin>>a; cout<<"Enter float value of class A: "; cin>>b;
} void display(){ cout<<"\nClass A"; cout<<"\n======="; cout<<"\nInteger value: "<<a; cout<<"\nFloat value: "<<b; } friend void sum(A ob1,B ob2);};class B{ private: int x; float y; public: void get() { cout<<"\nEnter integer value of class B: "; cin>>x; cout<<"Enter float value of class B: "; cin>>y;
} void display(){ cout<<"\nClass B"; cout<<"\n========"; cout<<"\nInteger value: "<<x; cout<<"\nFloat value: "<<y; } friend void sum(A ob1,B ob2);};
void sum(A ob1,B ob2){ cout<<"\n\nFriend Function"; cout<<"\n==============="; cout<<"\nSum of Integer values class A and class B: "<<ob1.a+ob2.x; cout<<"\nSum of Float values of class A and class B: "<<ob1.b+ob2.y;}int main(){ A c1; B c2; clrscr(); c1.get(); c2.get(); c1.display(); c2.display(); sum(c1,c2); getch(); return 0;}
OUTPUT
#include<iostream.h>
#include<conio.h>
class B;
class A
{
private:
int a;
float b;
public:
void get()
{
cout<<"\nEnter integer value of class A: ";
cin>>a;
cout<<"Enter float value of class A: ";
cin>>b;
}
void display(){
cout<<"\nClass A";
cout<<"\n=======";
cout<<"\nInteger value: "<<a;
cout<<"\nFloat value: "<<b;
}
friend void sum(A ob1,B ob2);
};
class B
{
private:
int x;
float y;
public:
void get()
{
cout<<"\nEnter integer value of class B: ";
cin>>x;
cout<<"Enter float value of class B: ";
cin>>y;
}
void display(){
cout<<"\nClass B";
cout<<"\n========";
cout<<"\nInteger value: "<<x;
cout<<"\nFloat value: "<<y;
}
friend void sum(A ob1,B ob2);
};
void sum(A ob1,B ob2)
{
cout<<"\n\nFriend Function";
cout<<"\n===============";
cout<<"\nSum of Integer values class A and class B: "<<ob1.a+ob2.x;
cout<<"\nSum of Float values of class A and class B: "<<ob1.b+ob2.y;
}
int main()
{
A c1;
B c2;
clrscr();
c1.get();
c2.get();
c1.display();
c2.display();
sum(c1,c2);
getch();
return 0;
}
No comments:
Post a Comment