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 

Questions Number 3

Creates two classes DM and DB to sore the value of distance. DM stores distance in meters and centimeters and DB in feet and inches. Write a program in c++/c that can read values for the class objects and add one object of DM with another object of DB.  Hint: Use friend function.

#include<iostream>
#include<conio.h>
using namespace std;

class DB;
class DM
{
	float mt;
	int cm;
	public:
		void input();
		void output();
		friend DM add(DM, DB);
};
	class DB
	{
		int feet;
		float inches;
		public:
			void input();
		void output();
		friend DM add(DM, DB);
	};
	void DM::input()
	{
		cout<<"Enter meters: ";
		cin>>mt;
		cout<<"Enter centimeters: ";
		cin>>cm;
	}
	void DM::output()
	{
		cout<<"Distance in meters: "<<mt<<endl;
		cout<<"Distance in centimeters: "<<cm<<endl;
	}
	void DB::input()
	{
		cout<<"Enter feet: ";
		cin>>feet;
		cout<<"Enter inches: ";
		cin>>inches;
	}
	 void DB::output()
	{
	cout<<"Distance: "<<feet<<"feet"<<inches<<"inches"<<endl;
	}
	DM add(DM a, DB b)
	{
		DM t;
		t.cm = a.cm + (b.feet * 12 * 2.54) + ((b.inches * 2.54));
		t.mt = a.mt + (t.cm / 100);
		t.cm = t.cm % 100;
		return t;
	}
  main()
	{
		DM x, y;
		DB z;
		x.input();
		z.input();
		cout<<endl;
		y = add(x, z);
		y.output();
	}

Leave a Reply