Pages

100 Contemporary Living Room Designs

Sunday, August 12, 2012

FRIEND FUNCTION

Program to create a class car and a class truck, each containing , as private variable, the speed of the vehicle. Initialize the name and speed using constructors. Create one object each for class car and truck. Use a friend function to compare the speeds of the two objects.


#include<iostream.h>
#include<conio.h>
#include<string.h>
class truck;
class car
{
  int speed;
  char name[25];
  public:
  car(char x[],int y)
  {
    strcpy(name,x);
    speed=y;
  }
  friend void find(car c1,truck t1);
};
class truck
{
  int speed;
  char name[25];
  public:
  truck(char x[],int y)
  {
    strcpy(name,x);
    speed=y;
  }
  friend void find(car c1,truck t1);
};
void find(car c1,truck t1)
{
  if(c1.speed>t1.speed)
  {
    cout<<"\n"<<c1.name<<" is faster\n";
  }
  else
  {
    cout<<"\n"<<t1.name<<" is faster\n";
  }
}
void main()
{
  int i;
  clrscr();
  car c1("BMW",200);
  truck t1("TATA",185);
  find(c1,t1);
  getch();
}

output


No comments:

Post a Comment