C++ Apti N OOPS
C++ Apti N OOPS
C++ Apti N OOPS
com
C++ Aptitude
and OOPS
C++ Aptitude and OOPS
Note : All the programs are tested under Turbo C++ 3.0, 4.5 and Microsoft VC++ 6.0
compilers.
It is assumed that,
Programs run under Windows environment,
The underlying machine is an x86 based system,
Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for
example sizeof(int) == 2 may be assumed).
1) class Sample
{
public:
int *ptr;
Sample(int i)
{
ptr = new int(i);
}
~Sample()
{
delete ptr;
}
void PrintVal()
{
cout << "The value is " << *ptr;
}
};
void SomeFunc(Sample x)
{
cout << "Say i am in someFunc " << endl;
}
int main()
{
Sample s1= 10;
SomeFunc(s1);
s1.PrintVal();
}
Answer:
Say i am in someFunc
Null pointer assignment(Run-time error)
Explanation:
Copyright: Students3k Team (http://students3k.com) - All Rights Reserved
void main()
{
int a, *pa, &ra;
pa = &a;
ra = a;
cout <<"a="<<a <<"*pa="<<*pa <<"ra"<<ra ;
}
/*
Answer :
Compiler Error: 'ra',reference must be initialized
Explanation :
Pointers are different from references. One of the main
differences is that the pointers can be both initialized and assigned,
whereas references can only be initialized. So this code issues an error.
*/
const int size = 5;
void print(int *ptr)
{
cout<<ptr[0];
}
void print(int ptr[size])
{
cout<<ptr[0];
}
void main()
{
int a[size] = {1,2,3,4,5};
int *b = new int(size);
print(a);
print(b);
Copyright: Students3k Team (http://students3k.com) - All Rights Reserved
//
}
/*
Answer :
56
Explanation:
In this program, the << operator is overloaded with ostream as argument.
This enables the 'cout' to be present at the right-hand-side. Normally, 'cout'
is implemented as global function, but it doesn't mean that 'cout' is not possible
to be overloaded as member function.
Overloading << as virtual member function becomes handy when the class in which
it is overloaded is inherited, and this becomes available to be overrided. This is as opposed
to global friend functions, where friend's are not inherited.
*/
class opOverload{
public:
bool operator==(opOverload temp);
};
bool opOverload::operator==(opOverload temp){
if(*this == temp ){
cout<<"The both are same objects\n";
return true;
}
else{
cout<<"The both are different\n";
Copyright: Students3k Team (http://students3k.com) - All Rights Reserved
class complex{
double re;
double im;
public:
complex() : re(1),im(0.5) {}
bool operator==(complex &rhs);
operator int(){}
};
bool complex::operator == (complex &rhs){
if((this->re == rhs.re) && (this->im == rhs.im))
return true;
else
return false;
}
int main(){
complex c1;
cout<< c1;
}
Answer : Garbage value
Explanation:
The programmer wishes to print the complex object using output
re-direction operator,which he has not defined for his lass.But the compiler instead of giving
an error sees the conversion function
and converts the user defined object to standard object and prints
some garbage value.
class complex{
double re;
Copyright: Students3k Team (http://students3k.com) - All Rights Reserved
void main()
{
int a, *pa, &ra;
pa = &a;
ra = a;
cout <<"a="<<a <<"*pa="<<*pa <<"ra"<<ra ;
}
Answer :
Compiler Error: 'ra',reference must be initialized
Explanation :
Pointers are different from references. One of the main
differences is that the pointers can be both initialized and assigned,
whereas references can only be initialized. So this code issues an error.
Try it Yourself
1) Determine the output of the 'C++' Codelet.
class base
{
public :
out()
{
cout<<"base ";
}
};
class deri{
Copyright: Students3k Team (http://students3k.com) - All Rights Reserved
A generic definition or a parameterized class not instantiated until the client provides
the needed information. Its jargon for plain templates.
Class template:
A class template specifies how individual classes can be constructed much like the
way a class specifies how individual objects can be constructed. Its jargon for plain classes.
4. When does a name clash occur?
Answer:
A name clash occurs when a name is defined in more than one place. For example.,
two different class libraries could give two different classes the same name. If you try to use
many class libraries at the same time, there is a fair chance that you will be unable to compile
or link the program because of name clashes.
5. Define namespace.
Answer:
It is a feature in c++ to minimize name collisions in the global name space. This
namespace keyword assigns a distinct name to a library that allows other libraries to use the
same identifier names without creating any name collisions. Furthermore, the compiler uses
the namespace signature for differentiating the definitions.
6. What is the use of using declaration.
Answer:
A using declaration makes it possible to use a name from a namespace without the
scope operator.
7. What is an Iterator class?
Answer:
A class that is used to traverse through the objects maintained by a container class.
There are five categories of iterators:
input iterators,
output iterators,
forward iterators,
bidirectional iterators,
random access.
An iterator is an entity that gives access to the contents of a container object without
violating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis
Copyright: Students3k Team (http://students3k.com) - All Rights Reserved
ONTOS of Ontos.
Object Oriented Analysis and Design (OOA/D) (Coad and Yourdon 1991).
X& operator *( );
const X& operator*( ) const;
X* operator->() const;
smart_pointer(const smart_pointer <X> &);
const smart_pointer <X> & operator =(const smart_pointer<X>&);
~smart_pointer();
private:
//...
};
In general, member names are made unique by concatenating the name of the member
with that of the class e.g. given the declaration:
class Bar
{
public:
int ival;
Copyright: Students3k Team (http://students3k.com) - All Rights Reserved