What is Object Oriented Programming ?

Object Oriented Programming নামে পরিচিত একটি কম্পিউটার প্রোগ্রামিং যা যুক্তির পরিবর্তে ডেটা বা বস্তুর চারপাশে সফ্টওয়্যারের স্থাপত্যকে সাজায়। একটি বস্তু হল নির্দিষ্ট বৈশিষ্ট্য এবং আচরণ সহ একটি তথ্য ক্ষেত্র।

Object Oriented Programming এ প্রয়োজনীয় যুক্তির চেয়ে প্রোগ্রামাররা যে বস্তুগুলি পরিচালনা করতে চায় তার উপর বেশি জোর দেয়। বড়, পরিশীলিত এবং সক্রিয়ভাবে আপডেট করা বা রক্ষণাবেক্ষণ করা প্রোগ্রামগুলি উন্নয়নের এই পদ্ধতির জন্য উপযুক্ত। এর মধ্যে মোবাইল অ্যাপ্লিকেশনের পাশাপাশি নকশা এবং উৎপাদন সফ্টওয়্যার অন্তর্ভুক্ত রয়েছে। উদাহরণস্বরূপ, ওওপি সিস্টেম সিমুলেশন সফ্টওয়্যার তৈরিতে প্রয়োগ করা যেতে পারে। [A computer programming paradigm known as object-oriented programming (OOP) arranges the architecture of software around data or objects rather than functions and logic. An object is a data field with particular characteristics and behavior.

OOP places more emphasis on the objects that programmers desire to manipulate than on the logic necessary to do so. Large, sophisticated, and actively updated or maintained programs are a good fit for this method of development. This encompasses mobile applications as well as design and production software. OOP, for instance, can be applied to manufacturing system simulation software.]

how is object-oriented programming structured?

The following are some of the components, or building pieces, of object-oriented programming:
Classes: User-defined data types called classes serve as the building blocks for specific objects, attributes, and methods[ব্যবহারকারী-সংজ্ঞায়িত ডেটা প্রকারগুলি যাকে ক্লাস বলা হয় তা নির্দিষ্ট বস্তু, বৈশিষ্ট্য এবং পদ্ধতির জন্য বিল্ডিং ব্লক হিসাবে কাজ করে।]
Objects: Objects are instances of a class that were generated using data that was precisely defined. Objects can be an abstract concept or a real-world thing. The description is the only item that is first defined when a class is created.[অবজেক্টগুলি এমন একটি শ্রেণীর উদাহরণ যা সঠিকভাবে সংজ্ঞায়িত ডেটা ব্যবহার করে তৈরি করা হয়েছিল। বস্তুগুলি একটি বিমূর্ত ধারণা বা বাস্তব-জগতের জিনিস হতে পারে। বর্ণনা হল একমাত্র আইটেম যা প্রথম সংজ্ঞায়িত করা হয় যখন একটি শ্রেণী তৈরি করা হয়।]

Methods: Methods are described as internal class functions that explain an object’s behaviors. The first line of code in every method seen in class declarations is a reference to an instance object. Additionally, an object’s internal functions are referred to as instance methods. Programmers utilize techniques to make code reusable or to keep functionality contained within a single object at a time.[মেথডগুলি অভ্যন্তরীণ শ্রেণী ফাংশন হিসাবে বর্ণনা করা হয় যা কোনও বস্তুর আচরণ ব্যাখ্যা করে। শ্রেণী ঘোষণায় দেখা প্রতিটি পদ্ধতিতে কোডের প্রথম লাইনটি একটি ইনস্ট্যান্স অবজেক্টের একটি রেফারেন্স। উপরন্তু, একটি বস্তুর অভ্যন্তরীণ ফাংশন উদাহরণ পদ্ধতি হিসাবে উল্লেখ করা হয়। প্রোগ্রামাররা কোডকে পুনরায় ব্যবহারযোগ্য করতে বা একবারে একটি বস্তুর মধ্যে কার্যকারিতা রাখতে কৌশলগুলি ব্যবহার করে।]

Attribute: The class template defines attributes, which stand in for an object’s state. Data will be kept in the attributes field of objects. Class characteristics are the property of the class. [ক্লাস টেমপ্লেট বৈশিষ্ট্যগুলি সংজ্ঞায়িত করে, যা একটি বস্তুর অবস্থার জন্য দাঁড়ায়। ডেটা অবজেক্টের অ্যাট্রিবিউট ফিল্ডে রাখা হবে। শ্রেণী বৈশিষ্ট্য হল শ্রেণীটির বৈশিষ্ট্য।]

Main principle Of Object Oriented Programming

Encapsulation

এই ধারণা অনুসারে, সমস্ত প্রয়োজনীয় তথ্য একটি আইটেমের ভিতরে রাখা হলে শুধুমাত্র নির্দিষ্ট তথ্য প্রকাশ করা হয়। প্রতিটি বস্তুর বাস্তবায়ন এবং অবস্থা একটি মনোনীত ক্লাসের ভিতরে গোপনে রাখা হয়। এই ক্লাসটি অন্য বস্তুর কাছে অ্যাক্সেসযোগ্য নয়, বা তাদের এটি পরিবর্তন করার ক্ষমতা নেই। শুধুমাত্র পাবলিক ফাংশন বা পদ্ধতির একটি সীমিত সেট এর দ্বারা অ্যাক্সেস বা পরিবর্তন করা যেতে পারে। এই বৈশিষ্ট্যের সাথে ডেটা গোপন করা অ্যাপ্লিকেশনের নিরাপত্তা বাড়ায় এবং অসাবধানতাবশত ডেটা ক্ষতি প্রতিরোধ করে। [According to this idea, only certain information is exposed while all essential information is kept inside an item. Each object’s implementation and state are kept secretly inside a designated class. This class is not accessible to other objects, nor do they have the power to modify it. Only a limited set of public functions or methods can be called by them. Data concealing with this feature enhances application security and prevents inadvertent data damage.]

Abstraction

অবজেক্টগুলি যে কোনও বহিরাগত বাস্তবায়ন করা কোডকে গোপন করে এবং কেবলমাত্র অভ্যন্তরীণ প্রক্রিয়াগুলি প্রকাশ করে যা অন্যান্য বস্তুর ব্যবহারের জন্য প্রয়োজনীয়। Derived class এর functionality কে বাড়ানো যেতে পারে। এই ধারণাটি ডেভেলপারদের জন্য পরে কিছু যোগ করতে বা আপডেট করতে সাহায্য করে। [Objects conceal any extraneous implementation code and only expose internal mechanisms that are necessary for the use of other objects. The functionality of the derived class can be increased. This idea can make it simpler for developers to add or update something later.]

Inheritance

একটি ক্লাস থেকে অন্য ক্লাসে বৈশিষ্ট্য ও বৈশিষ্ট্য আহরণের ক্ষমতাকে Inheritance বলা হয়। অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিংয়ের (Object Oriented Programming)অন্যতম গুরুত্বপূর্ণ বৈশিষ্ট্য হল Inheritance। Inheritance হল এমন একটি বৈশিষ্ট্য বা প্রক্রিয়া যেখানে বিদ্যমান ক্লাসগুলি(Existing class) থেকে নতুন ক্লাস(New class) তৈরি করা হয়। তৈরি হওয়া নতুন ক্লাসকে “ডিরাইভড ক্লাস(Derived Class)” বা “চাইল্ড ক্লাস(Child class)” বা “সাব ক্লাস(sub class)” বলা হয় এবং বিদ্যমান ক্লাসকে “বেস ক্লাস(Base Class)” বা “প্যারেন্ট ক্লাস(Parent Class)” বা সুপার ক্লাস (Super Class) বলা হয়। [Inheritance is the ability to transfer properties from one class to another. Inheritance is one of the important features of object-oriented programming. Inheritance is a feature or process in which new classes are created from existing classes. The new class created is called “Derived Class” or “Child Class” or “Sub Class” and the existing class is called “Base Class” or “Parent Class” or “Super Class.”]

Polymorphism

“পলিমরফিজম(Polymorphism)” শব্দটি হল “পলি” + “মরফস”-এর সংমিশ্রণ, যার অর্থ অনেক রূপ। এটি একটি গ্রিক শব্দ। অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং-এ আমরা তিনটি প্রধান ধারণা ব্যবহার করিঃ ইনহেরিট্যান্স, এনক্যাপসুলেশন এবং পলিমরফিজম।

Example: এখানে যোগ(add) প্রক্রিয়ার কথা চিন্তা করতে পারি। যেমন দুটি অপারেন্ড যদি সংখ্যা হয় এবং সেক্ষেত্রে যোগ প্রক্রিয়ার ফল হবে একটি সংখ্যা । আবার দুটি অপারেন্ড যদি স্ট্রিং হয় সেক্ষেত্রে দুটি স্ট্রিং এর যোগকে concatenate বলা হয়ে থাকে এবং ফলাফল হিসাবে একটি স্ট্রিং পেয়ে থাকি। i) 4+5=9 ii) poly+morphism=polymorphism এক্ষেত্রে দেখা যাচ্ছে একই “+” operator দুটি ভিন্ন ফলাফল প্রদর্শন করেছে । [The word polymorphism is a combination of poli + morphos, which means many forms. It’s a Greek word. In object-oriented programming, we use three main concepts: inheritance, encapsulation, and polymorphism. Example: Let’s consider the process of adding. For example, if two operands are numbers, the result of the addition process is a number. Again, if two operands are strings, then the sum of the two strings is called a concatenate and the result is a string. i) 4+5 = 9 ii) poly + morphism = polymorphism In this case, the same “+” operator shows two different results.]

Learn C++ Programming

C++ Question and Answer [2015-2019]

2015

1) Do the Following Task using C++ language in OOP

a) Design a Class having Name Circle

b) Declare an integer data for taking value of radius of that circle and take that input through get input() function

c) Find out the area of circle using another member function

#include<iostream>
using namespace std;
class circle{
	public :
		int r;
		float area;
		void input(){
			cout<<"Enter radious of circle:";
			cin>>r;
		}
		void findArea(){
                              area=3.14*r*r;
		}
		void display(){
		   cout<<"Area of circle is:";
		   cout<<area;
		}		
};
int main(){
	circle obj;
	obj.input();
	obj.findArea();
	obj.display();

}

2016

a) Write a program in C++ that accepts two number as input and find the sum using class and object

#include<iostream>
using namespace std;
class Sum_Two_Num{
	public :
		int a,b,sum;
		void inputNumber(){
			cout<<"Enter Two Number :";
			cin>>a>>b;
		}
		void findSum() {
			sum=a+b;
		}
		void display()	{
		   cout<<"Sum of two number is:";
		   cout<<sum;
		}	
};
int main(){
	Sum_Two_Num obj;
	obj.inputNumber();
	obj.findSum();
	obj.display();
}

b) What is Polymorphism? Explain with example

“পলিমরফিজম(Polymorphism)” শব্দটি হল “পলি” + “মরফস”-এর সংমিশ্রণ, যার অর্থ অনেক রূপ। এটি একটি গ্রিক শব্দ। অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং-এ আমরা তিনটি প্রধান ধারণা ব্যবহার করিঃ ইনহেরিট্যান্স, এনক্যাপসুলেশন এবং পলিমরফিজম।

Example :

class MotorBike {
  public:
    void speed() {
    cout << "This is Hero's speed \n" ;
  }
};
// Derived class
class Honda : public MotorBike {
  public:
    void speed() {
    cout << "This is Honda's speed \n" ;
  }
};
// Derived class
class Enfield : public MotorBike {
  public:
    void speed() {
    cout << "This is Enfield's speed\n" ;
  }
};

2017

a) Write a program in C++ to check an integer whether it is odd or even using class and object?

// Answer:
#include<iostream>
using namespace std;
class Odd_Even {
    int num;
public:
    void input() {
        cout << "Enter a number:";
        cin>>num;
    }
    void Check_OddEven() {
        if (num % 2 == 0) {
            cout << "Number is even:" << num;
        } else {
            cout << "Number is odd:" << num;
        }
    }
};
int main() {
    Odd_Even obj;
     obj.input();
     obj.Check_OddEven();
     return 0;
}

b) What do you mean by Data Member? Give example?

Data Members are variable which are declared in any class by using any fundamental data types (like int, char, float etc) or derived data type (like class, structure, pointer etc.).

There are two types of data member  1) Private  2) Public

//Example :
class DataMember {
        private:
                int x;
                float a;
        public:
                 int m;
                float n;		
};

OR

a) Give the difference between public and private member of the class in C++

Public Private
1. All the class members and functions are available to everyone.1.The class members can be accessed only by the functions inside the class.
2. The public member can be accessed from anywhere in the program using the dot operator(.)with the object
of that class.
2.the member functions or the friend functions are allowed to access the private data members of a class
Example:
class Subtraction { 
public:
        int a,b;
        int calculate(){
            return (a-b);
     }
};
int main(){
      Substraction obj;
      // accessing public data member outside class
      obj.a=20;
      obj.b=10;
      cout<<“Substraction is: ” << obj.calculate();
      return 0;
}
Example:
class Subtraction {
     private:
              int a,b;
     private:
              void calculate(int x,int y){
                a=x;
                b=x;
                int result=a-b;
                cout<<“Substraction is=”<<result;
            }
};
int main(){
            Substration obj;
            obj.calculate(20,10);
            return 0;
}

b) Describe the characteristic of destructor in C++

  • The destructor has the same name as that of the class prefixed by the tilde character ‘~’.
  • The destructor cannot have parameters.
  • It has no return type
  • there can be only one destructor in a class
  • The destructor is executed automatically when the control reaches the end of the class scope to destroy the object

c) What do you mean by “get from” Operator in C++? Give example?

Ans: Extraction Operator (>>) sometimes known as “get from” operator which is usually used for input data from the user

2018

a) Write a program in C++ using class and object to check an integer whether is a prime number or not.

#include <iostream>

using namespace std;
class PrimeChecker {
private:
    int num;   //here i have used private 

public:
    // Constructor
    PrimeChecker(int n) {
        num = n;
    }

  // Function to check if the number is prime
    int isPrime() {
        if (num <= 1) {
            cout << "The number is not prime." << endl;
            return 0;
        }
        for (int i = 2; i <= num / 2; ++i) {
            if (num % i == 0) {
                cout << "The number is not prime." << endl;
                return 0;
            }
        }
        cout << "The number is prime." << endl;
        return 1;
    }
};

int main() {
    int number;
    cout << "Enter an integer number: ";
    cin >> number;

    // Create an object of PrimeChecker class
    PrimeChecker primeObj(number);

    // Call the isPrime function
    primeObj.isPrime();

    return 0;
}

b) What is the member function? Give example?

A member function of a class is a function that has its definition or its prototype within or outside the class. member function knows as a method also.

There are two way to define a function

  1) inside the class-defined directly

  2) outside of the class- using scope resolution operator(::) along with class name

// Example:
// Member functions defined within class
class Circle {
   public:
      double r=2.5;         // variable
      double getArea(void) {
        return 3.14*r*r;
      }};
//OR Member functions defined outside of the class
class Circle {
   public:
      double r=2.5;         // variable        
};
class Circle::double getArea(void)    {
        return 3.14*r*r;
      }
int main(){
     Circle obj;
     cout<<"Area of Circle:"<<obj.double getArea();
}

OR

a) Write a program in C++ to find out the factorial of a given integer (N!= 1x2x3x……..N)

Answer:

using namespace std;
class FindFactorial {
public:
    int factorial(int N) {
        int i, f = 1;
        for (i = 1; i <= N; i++) {
            f = f*i;
        }
       return f;
    }
};
int main() {
    FindFactorial obj;
    int N;
    cout << "Enter a number:";
    cin >> N;
    cout << "Factorial is:" << obj.factorial(N);
    return 0;
}

b) How a member function can be declared outside of the class? Give example?

Anwser:

A member function can be declared outside of the class using the scope resolution operator (::) along with the class name(একটি মেম্বার ফাংশনকে ক্লাসের বাইরে স্কোপ রেজল্যুশন অপারেটর (::) এর সাহায্যে ক্লাসের নামের সাথে ডিক্লেয়ার করা যেতে পারে). Example:

class Circle {
   public:
      double r=2.5;         // variable        
};
class Circle::double getArea(void)    {
        return 3.14*r*r;
      }
int main(){
     Circle obj;
     cout<<"Area of Circle:"<<obj.double getArea();
}

2019

a) Write a program in C++ using class and object to find out the sum 1 to N where N is an integer given as input.

Answer:

#include<iostream>
using namespace std;
class Sum_Of_N_Num{
	public :
		int N,i,sum=0;
		void inputNumber(){
			cout<<"Enter Number :";
			cin>>N;
		}
		void findSum()
		{
			for(i=0; i<=N; i++){
				sum=sum+i;
			}
	
		}
		void display()
		{
		   cout<<"Sum of N number is:";
		   cout<<sum;
		}
	
};
int main(){
	Sum_Of_N_Num obj;
	obj.inputNumber();
	obj.findSum();
	obj.display();
}

b)Write a function of setw and endl manipulator.

Answer:

endl : This manipulator has the same functionality as ‘\n’(newline character). But this also flushes the output stream(এই ম্যানিপুলেটরের কার্যকারিতা ‘\n’-এর (New line character) মতোই। কিন্তু এটি আউটপুট স্ট্রিমও ফ্লাশ করে।).

setw():  This manipulator declare in imanip.h . it changes the width of the output field. When used in an expression out << setw(n)output field. (এই ম্যানিপুলেটরটি imanip.h-তে ডিক্লেয়ার করা হয়েছে। এটি আউটপুট ফিল্ডের প্রস্থ পরিবর্তন করে। যখন out << setw(n) এই অভিব্যক্তিতে ব্যবহৃত হয়, আউটপুট ফিল্ডের প্রস্থ n হয়ে যায়।”)

Example:

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
            cout<<"USING setw() ..............\n";
	    cout<< setw(10) <<11<<endl;
	    cout<< setw(10) <<2222<<endl;
	    cout<< setw(10) <<33333<<endl;
	    cout<< setw(10) <<4<<endl;
}

OR

a) Write a program in C++ to print the reverse of given number.

Answer:

#include <iostream>
using namespace std;
int main() {
    int num, revNum = 0, rem;
    cout << "Enter an integer: ";
    cin >> num;
    while(num!= 0) {
        rem = num%10;
        num /= 10;
        revNum = revNum*10 + rem;     
    }
    cout << "Reversed Number = " << revNum;
    return 0;
}

b) what do you mean by reference variable? Give example?

Answer: When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration.(যখন একটি ভেরিয়েবলকে রেফারেন্স হিসেবে ডিক্লেয়ার করা হয়, সেটি কোনো বিদ্যমান ভেরিয়েবলের জন্য একটি বিকল্প নাম হয়ে দাঁড়ায়। ডিক্লেয়ারেশনে ‘&’ সাইন ব্যবহার করে একটি ভেরিয়েবলকে রেফারেন্স হিসেবে ডিক্লেয়ার করা যেতে পারে।)

Example:

#include<iostream>
using namespace std;
int main(){
	int x=10;
	int& ref=x;
	//value of x is now changed
	ref=20;
	cout<<"x="<<x<<endl;
	//value of ref is now changed
	x=30;
	cout<<"ref="<<ref<<endl;
	return 0;
}

Leave a comment