C++ Expression

Explain new, delete, and scope resolution operator.

In many situations, you are not able to know in advance how much memory you will need to store data in a particular variable. You only know at run time, how much memory would you require? As an example Arrays are used to store homogenous data and you allocate the memory of an array when they are declared. So, you declare the array with maximum possible memory required according to the condition but this may cause the wastage of memory. Suppose that you declare the array of size 100 but you use memory of only 60 elements, then 40 memory spaces will be of no use.

You can dynamically allocate the memory required during runtime using new operator. It will avoid wastage of memory.

Dynamic Memory Allocation for Arrays:
class Test
{
     private:
     int n;
     float * marks;
     public:
     Test()
     {
          cout <<"Enter total number of students:";
          cin >> n;
          marks = newfloat[n];
          cout <<"Enter Total marks of students."<<endl;
          for (int i = 0; i < n; ++i)
          {
               cout <<"Student"<< i+1 <<": ";
               cin >> *(marks + i);
          }
     }
     ~Test()
     {
          delete[ ] marks;
     }
     void ShowMarks()
     {
          cout <<"\nDisplaying marks of students."<< endl;
          for (int i = 0; i < n; ++i)
          {
               cout <<"Student"<< i+1 <<" :"<<*(marks + i) << endl;
          }
     }
}
int main()
{
     Test obj;
     obj.ShowMarks();
     return 0;
}

Once the memory is allocated using new operator, it should release when the object is not in use or program terminated because memory is limited. Destructor is called automatically when object goes out of scope and it will release the memory allocated by new operator previously.
Operator: ‘::’ is known as Scope Resolution Operator.

Use of scope resolution operator
  • It is used to define a function outside a class.
  • You can access global variable and class data member value simultaneously even though both variable names are same.
Example
int x = 100; ----------------------- Global Variable.
class MyClass
{
     int x, y;
     public:
     void show();
}
void MyClass::show()
{
     // Write Function Code here
     cout <<"Function defined outside the class.\n";
     x = 25;
     cout<<"Local variable x =: "<<x<<endl;
     cout<<"Global variable x =: "<<::x<<endl;
}
void main()
{
     MyClass obj;
     obj.show();
}

Write output with explanation.

void main ( )
{
     cout<<setw (10)<<setfill ('#');
     cout.setf (ios :: internal, ios :: adjustfield);
     cout<<setiosflags (ios :: showpoint);
     cout<<setprecision(5);
     cout<<11.432 <<endl;
     cout<<setiosflags (ios :: showpos);
     cout<<15.4;
}

Output:
####11.432
+15.400

Write output with explanation.

void main ( )
{
     int a, *pa, &ra;
     pa = &a ;
     ra = a ;
     cout << "a =" << a <<" *pa =" << *pa << "ra =" << ra;
}

Description:

In the above program, 'ra' is reference variable. Reference variables should be initialized when it is created. Here reference variable 'ra' is not initialized, so it will give error.

4. Write a cpp program that will generate febonacci numbers up to an input limit.

Solution:

For generating febonacci numbers, first you need two numbers. The third number is the summation of first two numbers. Let first two numbers are f0 and f1, then f2 = f0 + f1;

The febonacci numbers are defined recursively by the equation as follows.

F0 = 0;
F1 = 1;
Fn = Fn-1 + Fn-2

For example: let n=2 then the third equation will be

F2= F2-1 + F2-2 = F1 + F0= 1+0 =1

Similarly for n=3
F3 = F3-1 + F3-2 = F2 + F1 = 1 + 1 = 2
void main (void)
{
     int limit;
     cout<<"how many numbers you want in series?"<<endl;
     cin>>limit;
     int f0 = 0,f1 = 1,f2;
     cout<<"Febonacci numbers less than "<<limit<<endl;
     cout<<f0<<" "<<f1;
     while(true)
     {
          f2 = f0 + f1;
          if(f2>limit)
          {
               break;
          }
          cout<<" "<<f2;
          f0 = f1;
          f1 = f2;
     }
}

Output:
how many numbers you want in series?
500
Febonacci numbers less than 500

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Calculate the real roots of quadratic equation ax2 + bx + c = 0 using the formula

x = -b ± √ b2 - 4ac
2a

Solution:
class equation
{
     int a,b,c,d;
     float root1,root2;
public:
     equation()
     {
          cout<<"Enter value of coeficient a,b,c"<<endl;
          cin>>a>>b>>c;
     }
     void calculate()
     {
          d = b * b-4 * a * c;
          if(d==0)
          {
               root1=(-b)/(2*a);
               root2=root1;
               cout<<"Roots are real & equal";
          }
          elseif(d>0)
          {
               root1 = -(b+sqrt (d)) / (2*a);
               root2 = -(b-sqrt (d)) / (2*a);
               cout<<"Roots are real & distinct";
          }
          else
          {
               cout<<"Roots are imaginary";
          }
               cout<<"\nRoot 1 = "<<root1<<"\nRoot 2 = "<<root2;
     }
};
void main()
{
     equation obj;
     obj.calculate();
     getch();
}

Enter the value of coefficient a, b, c
1
-5
6
Roots are real & distinct
Root 1 = 2
Root 2 = 3

Write output with explanation.

void main()
{
     int a = 10;
     while(1)
     {
          switch(a)
          {
               case 10: cout<<a++;
               case 11: cout<<a--;
               case 12: cout<<a++;
          }
     }
}

Description:

When you execute above program, it will go in infinite loop. While loop will always in true condition, so control will not come out of the loop. Another point is that there is no break statement in switch case. If you write break statement in switch case, then still the program will go in infinite loop because program control will come out from switch block not from while loop. The above program will print 10, 11, 10 in infinite loop.