Object Oriented Programming is a programming technique in which programming is based upon objects.

What is Object?

Object is any entity about which data is collected. Object may be a Fan, Person, Car etc.

How to Display Your name on Taskbar in Windows 10

Before Understanding classes you have to practice functions.

What is Function?

Function is a piece of code or a block of code that is used to perform some action. There are two types of function

  • Built in Function
  • User Defined Function

Here we Concerned with User Defined Function

What is Class?

Class is collection of objects having same properties and function.

For example if we have a object like Person then

Person have following properties:

  • Color
  • Name
  • Age
  • Height etc.

Similarly it has following functions:

  • Speaking
  • Weeping
  • Eating
  • Walking etc.

Syntax of class Person in C++

class Person

{
  private:
//class member variables definitions. public:
//class member functions definition }

You can practice other programming questions for C and C++ Exercises.

WE provided a large Variety of solved programming exercises.

Functions Programming Exercises 

Question Number 4

Write a class that contains the following data members:

  • The name of the runner
  • The distance covered by a runner.

The class has the following member functions:

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

The user should be able to show the name of the runner who has covered the longest distance at any point of time.

Hint: Use static data members.

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;
class Run
{
	char name[50];
	float dis;
	static char tname[50];
	static float tdis;
	public:
		void get();
		void show();
		void topper();
};

   void Run::get()
   {
	cout<<"Enter runner name: ";
	gets(name);
	cout<<"Enter distance covered: ";
	cin>>dis;
	if(dis > tdis)
	{
		tdis = dis;
		strcpy(tname, name);
	}
   } 
   void Run::show()
   {
	cout<<"Runner name: "<<name<<endl;
	cout<<"Distance covered: "<<dis<<endl;
   }
   void Run::topper()
   {
	cout<<tname<<"covered the longest distance."<<endl;
	cout<<"The distance is "<<tdis<<"miles"<<endl;
   } 
     float Run::tdis = 0;
     char Run::tname[50] = "Noname";

 main()
 {
	Run x, y, z;
	x.get();
	x.topper();
	y.get();
	z.get();
	z.topper();
 }

Leave a Reply