Back to Top

Object Oriented Programming

Year Basis Question and Answer Of  C++ 

[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 

Answer

#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

Answer:

#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

Answer:

polymorphism is an important concept of object-oriented programming.it means having many forms. Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

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++

Answer:

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++

Answer:

  • 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.

Answer:

using namespace std;  

int main()  

{  

  int n, i,check=0;  

  cout << "Enter the Number to check Prime: ";  

  cin >> n;  

  for(i = 2; i <=n/2; i++)    {  

      if(n % i == 0)  

      {  

          cout<<"Number is not Prime."<<endl;  

          check=1;  

          break;  

      }  

  }  

  if (Check==1)  {

      cout << "Number is Prime."<<endl;  

  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.

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.

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;

}