Write a program to create a shape class. Create an display function as non member function of the class. Declare display function as friend of class.

Display function should display details.

Shape class will have setData and getData Function

Shape class will have Color as Data Member

/*
Objective : demonstrate friend function using Shape Class

*/

#include <iostream.h>
#include <conio.h>
#include <string.h>

class Shape
{
	protected:
	char color[15];
	public:
	void getData()
	{
		cout<<"My color is "<<color<<endl;
	}
	void setData(char *col)
	{
		strcpy(color,col);
	}
	friend void display(Shape &);

};
void display(Shape &s)
{
	cout<<"My color is "<<s.color<<endl;
}

int main()
{
	clrscr();
	Shape obj;
	obj.setData("red");
	obj.getData();
	display(obj);

	Shape obj2;
	obj2.setData("Green");
	obj2.getData();


	getch();
}
Accessing a private member of Shape Class with Friend Function in C++

Leave a Reply

Your email address will not be published. Required fields are marked *