Subscribe:Posts Comments | Read Articles on Various Topics - Pro Articles

By PDMACpayday loans

You Are Here: Home » Sample Placement Papers, Texas Instruments Test Papers » Texas Instruments Sample Technical & Programming Q Part Eight

Here are the Texas Instruments Written Test Paper sample Technical Questions and C, C++ programming questions. Download free sample placement papers. Theoretical / theory questions for electronics and computer related C , C++ placement test papers.

Accessing a private data member from a different Object…Different objects of the same class can access each other’s members, even if these members are private. For example:
#include < iostream.h >
class sample
{
float f ;
public :
sample ( float ff )
{
f = ff ;
}
void fun ( sample* objptr )
{
objptr -> n = 0 ;
cout << “Value of this objects f is : ” << f << endl ;
cout << “Value of other objects f” << objptr -> n << endl ;
} // another object’s private member!
} ;
void main( )
{
sample s1 ( 6.5f ) , s2 ( 2.5f ) ;
s1.f ( &s2 ) ; // s1 changes s2′s n
}
Typically, this coding style should be avoided. However, you should be aware that private members of an object can be changed by another object of the same type. Therefore, in certain special conditions, this coding style may be useful.

Can you access private data members of a class from out side the class?
Ans: Yes. This program shows how.
#include
class emp
private :
int i ;
public :
emp( )
{
i = 10 ;
}
} ;
void main( )
emp *p = new emp ;
int *pi = (int*) p ;
cout << *pi ;
*pi = 20 ;
cout << *pi ;
}

The pointer to the class is typecasted in an integer pointer. With the help of this pointer private data member ‘i’ is accessed in main( ).

Why creating array of references is not possible?
Ans: The array name always refers or points to the zeroeth element. If array is of references then the array name would point to the zeroeth element which happens to be a reference. Creating pointer to a reference is not valid. So, creating array of references too is not possible.

How do I call a virtual function of a class using a pointer to a function ?
Ans :
#include
class Cvirtual
{
public :
virtual float vfun( )
{
cout << “from vfun” << endl ;
return 7.03f ;
}
} ;
void main( )
{
Cvirtual obj ;
int **p = ( int ** ) &obj ;
float ( *pf1 ) ( ) ;
pf1 = ( float ( * ) ( ) ) **p ;
float f = ( *pf1 ) ( ) ;
cout << “return val = ” << f << endl ;
}

In the above program class Cvirtual consists of a virtual function vfun(). In variable p we have stored the address of an object of class Cvirtual. While doing so, we have type casted the address of obj to int **, because obj holds a hidden data member called vptr, which in turn holds the address of virtual function vfun( ). In pf1, a pointer to a function, we are collecting the address of the virtual function vfun( ). Thus the value returned by vfun( ) would then get collected in f.

Why an overloaded new operator defined in a class is static?
Ans: An overloaded new function is by default static even if it is not declared so. This is because non-static member functions can be called through an object only. But when an overloaded new operator function gets called the object doesn’t stand created. Since new operator function itself is responsible for creating the object. Hence to be able to call a function without an object, the function must be static.

What is a pure virtual destructor?
Ans: Like a pure virtual function we can also have a pure virtual destructor. If a base class contains a pure virtual destructor it becomes necessary for the derived classes to implement the destructor. An ordinary pure virtual function does not have a body but pure virtual destructor must have a body. This is because all the destructors in the hierarchy of inheritance are always called as a part of destruction.

When we are required to find offset of an element within a structure? or, how do we call the function of an outer class from a function in the inner class? (The inner class is nested in the outer class)
Ans:
#include
class outer
{
int i ;
float f ;
public :
class inner
{
public :
infunc( )
{
outer *pout ;
pout = (outer*) this – ( size_t ) &( ( ( outer* ) 0 ) -> in ) ;
pout -> outfunc( ) ;
}
} ;
inner in ;
outfunc( )
{
cout << “in outer class’s function” ;
}
} ;
void main( )
{
outer out ;
out.in.infunc( )
}
In the above example we are calling outer::outfunc( ) from inner::infunc(). To call outfunc( ) we need a pointer to the outer class. To get the pointer we have subtracted offset of the inner class’s object (base address of outer class’s object – address of inner class’s object) from address of inner class’s object.

void f ( float n, int i = 10 ) ;
void f ( float a ) ;
void main( )
{
f ( 12.6 ) ;
}

void f ( float n, int i )
{

}

void f ( float n )
{

}
The above program results in an error (ambiguous call) since without the default argument the two functions have arguments that are matching in number, order and type.

Some programs need to exercise precise control over the memory areas where data is placed. For example, suppose we wish to read the contents of the boot sector into a structure. For this the byte arrangement of the
structure elements must match the arrangement of various fields in the boot sector of the disk.
The #pragma pack directives offer a way to fulfill this requirement. The #pragma pack directive specifies packing alignment for structure and union members. The #pragma takes effect at the first structure or union declaration after the #pragma is seen. Consider the following structure:
#pragma pack (1)
struct emp
{
int a ;
float s ;
char ch ;
} ;
#pragma pack( )
Here, #pragma pack ( 1 ) lets each structure element to begin on a 1-byte boundary. Hence the size of the structure will be 9. (int – 4, float – 4, char – 1). If we use #pragma pack ( 2 ) each structure element can begin on a 2-byte boundary. Hence the size of the structure will be 10. (int – 4, float – 4, char – 2).

How to restrict a friend class’s access to the private data members?
Ans: If we declare a class as a friend of our class the friend class can access the private data members of our class. However, if we want we can restrict this access to some selective functions of the class. Following program shows how to achieve this:
#include
class X
{
public :
void print ( class Z &z ) ;
} ;
class Z
{
private :
int i ;
public :
Z ( int ii )
{
i = ii ;
}
friend X::print ( class Z &z ) ;
} ;
void X::print ( Z &z1 )
{
cout << z1.i ;
}
main( )
{
Z z ( 10 ) ;
X x ;
x.print ( 10 ) ;
}
In the above program only the X::print( ) function can access the private data members of class Z.

What is name mangling?

Ans: C++ enables you to assign the same function name to more than one functions but with different parameter types. This feature is called function overloading. But when we give several functions the same name, how does the compiler decide which particular function is to be called? C++ solves this problem by applying a process called name mangling. Name mangling applies a decorated name to the function. The mangled name includes tokens that identify the functions’ return type and the types of its arguments.
class test
{
public :
void fun ( int a, char b ) ;
void fun ( char *c, float y ) ;
} ;
void main( )
{
test s1 ;
s1.fun ( 65, ‘A’ ) ;
s1.fun ( “Anil”, 5.5f ) ;
}
At the time of resolving the calls to fun( ) function the linker would not be able to find the definition of the overloaded function fun( ) and it would report an error. If you look at these errors you will see the mangled names like, (?fun@test@@QAEXJJ@Z) and (?fun@test@@QAEXMM@Z). Note that different compilers may use different name mangling schemes.

How would you call a C function from C++ code?
Ans: Using extern “C”.
The function prototype must be preceded by extern “C”. More than one C functions can be grouped inside braces as shown below:
extern “C”
{
void f( ) ;
void f1( ) ;
}
// In cfunc.c
#include
void f( )
{
printf ( “in f( )” ) ;
}
// In func.cpp
#include
extern “C” void f( ) ;
void main( )
{
f( ) ;
}
Ensure that both .c and .cpp files are in the same project.

This is the second last part of a series of IT and Electronics Hardware companies Theoretical Placement test paper questions. See other series and MNC latest exam paper questions here.

Leave a Reply

© Copyright 2010 Latestexams. All Rights Reserved | Privacy Policy | Terms & Conditions