runner

Write a class Runner that contains the following data members:

  • The name of runner
  • The distance covered by runner

The class has following member functions:

  •  Get function to input runner name and distance
  • Show function to display runner name and distance

 

Create two objects of the class and also display the name of runner who has covered longer distance.

 

#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class runner{
     public:
          string name;
          long dist;
          runner(){                
              name=" ";
              dist=0;
          }
          void get(){                  
              cout<<"Enter Name and Distance:";
              cin>>name>>dist;
          }
          void display(){              
              cout<<"\n Name:"<<name;
              cout<<"\n Distance traveled:"<<dist;
          }
};

int main(){
     runner r1, r2;
     r1.get();
     r2.get();
     cout<<"This runner has covered longer distance:";
     if (r1.dist > r2.dist)
          r1.display();
     else
          r2.display();
     getch();
     return 0;
}

Leave a Reply

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