A) Write a program implementing Friend function.
B) Write a program to illustrate this pointer.
C) Write a program to illustrate pointer to a class.
A) AIM: Write a program implementing FriendFunction.
SOURCE CODE:
#include<iostream>
using namespace std;
class Box
{
private:
double length;
protected:
double height;
public:
double width;
void getdata();
friend double volume(Box box);
};
void Box::getdata()
{
cout<<"Enter length,height and width:"<<endl;
cin>>length>>height>>width;
}
double volume(Box box)
{
return box.length*box.height*box.width;
}
int main()
{
Box b;
b.getdata();
cout<<"Volume of box is:"<<volume(b);
}
OUTPUT:
Enter length,height and width:
5
3
2
Volume of box is:30
B) AIM: Write a program to illustrate thispointer.
SOURCE CODE:
#include<iostream>
using namespace std;
class Box
{
private:
double length;
protected:
double height;
public:
double width;
Box(double length,double height,double width)
{
this->length=length;
this->height=height;
this->width=width;
}
double volume()
{
return length*height*width;
}
};
int main()
{
double l,h,w;
cout<<"Enter length,height and width values:"<<endl;
cin>>l>>h>>w;
Box b(l,h,w);
cout<<"Volume of box is:"<<b.volume();
}
OUTPUT:
Enter length,height and width values:
6
4
2
Volume of box is:48
C) AIM: Write a program to illustrate pointer to a class.
SOURCE CODE:
#include<iostream>
using namespace std;
class Box
{
private:
double length;
protected:
double height;
public:
double width;
Box(double l,double h,double w)
{
length=l;
height=h;
width=w;
}
double volume()
{
return length*height*width;
}
};
int main()
{
double l,h,w;
cout<<"Enter length,height and width values:"<<endl;
cin>>l>>h>>w;
Box b(l,h,w),*ptr;
cout<<"Volume of box is:"<<b.volume()<<endl;
ptr=&b;
cout<<"Volume of box using pointer is:"<<ptr->volume();
}
OUTPUT:
Enter length,height and width values:
5
4
3
Volume of box is:60
Volume of box using pointer is:60
B) Write a program to illustrate this pointer.
C) Write a program to illustrate pointer to a class.
A) AIM: Write a program implementing FriendFunction.
SOURCE CODE:
#include<iostream>
using namespace std;
class Box
{
private:
double length;
protected:
double height;
public:
double width;
void getdata();
friend double volume(Box box);
};
void Box::getdata()
{
cout<<"Enter length,height and width:"<<endl;
cin>>length>>height>>width;
}
double volume(Box box)
{
return box.length*box.height*box.width;
}
int main()
{
Box b;
b.getdata();
cout<<"Volume of box is:"<<volume(b);
}
OUTPUT:
Enter length,height and width:
5
3
2
Volume of box is:30
B) AIM: Write a program to illustrate thispointer.
SOURCE CODE:
#include<iostream>
using namespace std;
class Box
{
private:
double length;
protected:
double height;
public:
double width;
Box(double length,double height,double width)
{
this->length=length;
this->height=height;
this->width=width;
}
double volume()
{
return length*height*width;
}
};
int main()
{
double l,h,w;
cout<<"Enter length,height and width values:"<<endl;
cin>>l>>h>>w;
Box b(l,h,w);
cout<<"Volume of box is:"<<b.volume();
}
OUTPUT:
Enter length,height and width values:
6
4
2
Volume of box is:48
C) AIM: Write a program to illustrate pointer to a class.
SOURCE CODE:
#include<iostream>
using namespace std;
class Box
{
private:
double length;
protected:
double height;
public:
double width;
Box(double l,double h,double w)
{
length=l;
height=h;
width=w;
}
double volume()
{
return length*height*width;
}
};
int main()
{
double l,h,w;
cout<<"Enter length,height and width values:"<<endl;
cin>>l>>h>>w;
Box b(l,h,w),*ptr;
cout<<"Volume of box is:"<<b.volume()<<endl;
ptr=&b;
cout<<"Volume of box using pointer is:"<<ptr->volume();
}
OUTPUT:
Enter length,height and width values:
5
4
3
Volume of box is:60
Volume of box using pointer is:60