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 1

Write a program in c++/c that creates a class Player that contains attributes for Player’s name, average and team. Write three functions 

  • input()
  • change()
  • display()

values of these attributes. Also write a constructor that asks for input to initialize all the attributes.

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

class Player
{
	private:
		char name[50];
		float avg;
		char team[50];
	public:
		Player()
		{
			cout<<"Enter player name:";
			gets(name);
			cout<<"Enter average:";
			cin>>avg;
			cout<<"Enter team name:";
			gets(team);
		}
		void input()
		{
			cout<<"Enter player name:";
			gets(name);
			cout<<"Enter average:";
			cin>>avg;
			cout<<"Enter team name:";
			gets(team);
		}
		void display()
		{
			cout<<"Player name:"<<name<<endl;
			cout<<"Average:"<<avg<<endl;
			cout<<"Team name:"<<team<<endl;
		}
		void change(char n[],float a, char t[])
		{
			strcpy(name, n);
			avg = a;
			strcpy(team,t);	
		}
};

     main()
        {
        
        	Player p1;
        	p1.display();
        	p1.change("Players",80.59,"Country");
        	p1.display();
        
        }

Leave a Reply