Ans. It is valid statement. The o/p of this statement is 20.
7) What is pragma? what are the advantages of it?
Ans: you typically use a #pragma directive to control the actions of the compiler in a particular portion
of a program without affecting the p rogram as a whole.A pragma is in effect from the point where it is included to the end of the compilation
unit or until another pragma changes its status.
It has two effects on your code. First and most obviously, it has the
special effect that the compiler documents for it. Second and more
subtly, it ties your code to that particular compiler, making it
exceedingly difficult to move your code to another.
/* Example */
#include<stdio.h>
#pragma pack(1)
struct st1
{
int i;
char c;
int j;
}s1;
#pragma pack(4)
struct st2
{
int i;
char ch;
int j;
}s2;
main()
{
printf("size of the structire s1: %d\n",sizeof(s1));
printf("size of structure s2 is: %d\n",sizeof(s2));
}
Use:
Proper use of system memory in embedded system.
8)Differentiate typedef and #define for arr[10]?
Ans:
#include<stdio.h>
#define abc int d[5]
main()
{
typedef int ar[5];
//abc t; //compilation error
ar x ;
printf(" size of array %d",sizeof(x) );
//printf(“size of array of #defien” ,sizeof(t) ) ;
}
To define an array using has define is complex when compared with typedef.
9)How is memory allocation is done for member functions and member variables of class?
Ans: Static fields and methods are allocated memory once only, when they are first referenced, irrespective of
the no. of objects u later create. But, memory is allocated for its data members each time an object is
created. But what about the non-static methods, they don't change for each object.
10)What is reinterpret cast?
Ans: Reinterpret_cast: convert anything to anything without restrictions.Exactly the same as a C cast.
Reinterpret_cast is a binary reinterpretation of the data (no context/brains involved... just a straight
binary copy). There are also plenty of restrictions on what you can reinterpret_cast. For example, You
can't reinterpret a float to an int.
You can convert between objects of unrelated pointer types (cast from A* to B* if A and B are unrelated -- which you can't do with static_cast).
11) what is function hiding in classes and memory stack for function in the derived class?
Ans:
Function Hiding in derived classes:
class B
{
public:
void fun ()
{
cout <<endl<<"in base";
}
};
class D : public B
{
public:
void fun ()
{
cout <<endl<<"in derived";
}
};
int main ()
{
B b;
D d;
cout <<endl<<"in main";
b.fun ();
d.B::fun ();
return 0;
}
12)comment on method of calling for base class and derived class
Ans: a) The methods of the class called depends on the object.
b) If the methods of the derived class are derived from the base class and both class has method
definations, calling to this methodsdepends on object of specific class.(If no vitual keyword is present).
c) If the methods are derived class are derived from the base class, but the defination of method is not
defined in derived class, in such cases if call method form derived class where its definition is not
defined, due to inheritance the base class methoddination is called.
If no virtual keyword is not present: The calling method of funtions is from derived class to the base
class in case defination are not present in the derived classes.
If virtual keyword if present before methods:
#include <iostream.h>
class base
{
public:
virtual void display()
{
cout<<”\nBase”;
}
};
class derived : public base
{
public:
void display()
{
cout<<”\nDerived”;
}
};
void main()
{
base *ptr = new derived();
ptr->display();
}
In the above example, the pointer is of type base but it points to the derived class object. The method
display() is virtual in nature. Hence in order to resolve the virtual method call, the context of the
pointer is considered, i.e., the display method of the derived class is called and not that of the base.
If the method was non virtual in nature, the display() method of the base class would have been called.
13)state the practical applications of virtual functions and memory synchronization.
Ans:http://www.exforsys.com/tutorials/c-plus-plus/c-pure-virtual-function-and-base-class.html
class base
{
}
class der1:virtual public base
{
}
class der2: virtual pubilc base
{
}
class der3:public der1,erv2
{
}
The derived classes(i.e der1,der2) base class by virtual property have single copy of data and member
functions which will share. Further derived (der3) class with two derived classes share the
same copy of the erived classes.