Professional Documents
Culture Documents
Presents Presents Presents Presents : Solved World
Presents Presents Presents Presents : Solved World
FAQ in C++ by
0
PRESENTS…
PRESENTS…
01.01.2009
P.T.O.
SOLVED WORLD
FAQ in C++ by
1
P.T.O.
SOLVED WORLD
FAQ in C++ by
2
1. What is a class?
A class is a way to bind data and associated functions together. Actually, class is
an extension of the data type structure which can hold data and functions both as
its members with providing data hiding property. Class is a new kind of data type.
A class is usually declared as follows:-
class A {
int number;
float cost;
public :
void get_data(int a, float b);
void put_data( void);
};
Generally, variables are declared as private by default and functions are made
public.
2. What is an object?
Class variables are called objects. There are two types of procedures to create
objects. Most common procedure is to create them at the place of their use.
Creation of object at the place of their Creation of object at the start of the
use program
class A { class A {
int number; int number;
float cost; float cost;
public : public :
void get_data(int a, float b); void get_data(int a, float b);
void put_data(void); void put_data(void);
}; }x,y;
- - - - - - - - -
- - - - - - - - - - - - - - - - - -
item x,y; - - - - - - - - -
- - - - - - - - -
- - - - - - - - -
Here, x & y are the two objects created of the class item. By the creation of
objects, the necessary memory space is allocated. The purest definition of object
may be “a region of storage” (this is a more specific way of saying, “an object
must have a unique identifier,” which in the case of C++ is a unique memory
address). It’s a place where we can store data, and it’s implied that there are also
operations that can be performed on this data.
P.T.O.
SOLVED WORLD
FAQ in C++ by
3
5. What is public, protected, private?
Public, protected & private are the three access specifiers used to select the nature
of the class members.
A member (data or function) declared in a public section of a class can be
accessed by anyone.
A member (data or function) declared in a protected section of a class can
only be accessed by member functions and friends of that class, and by
member functions and friends of derived classes.
A member (data or function) declared in a private section of a class can only
be accessed by member functions and friends of that class.
P.T.O.
SOLVED WORLD
FAQ in C++ by
4
- - - - - - - -
public :
- - - - - - - -
- - - - - - - -
friend void XYZ(void);
- - - - - - - -
- - - - - - - -
};
The friend function XYZ may be defined any where in the program later. But this
type of definition differs from usual member function definition. The difference is
that there is no need for the scope resolution operator. The definition is like that:
void XYZ(void);
Also, the definition does not need the keyword friend again.
P.T.O.
SOLVED WORLD
FAQ in C++ by
5
“Poly” means “many” and “morph” means “form”. Polymorphism is the ability of
an object to behave differently. This is a crucial feature of OO languages that
provides a mean to extend the functionality of an object. The types of
polymorphism are shown in figure.
Compile time polymorphism is implemented by overloaded functions &
operators. The overloaded member functions are selected for invoking by
matching arguments (both type & number). This information is known to the
compiler at the compile time and so the compiler is able to select the particular
function for a particular call at the compile time itself. This is called early binding
or static binding or static linking. Also known as compile time polymorphism, the
early binding simply means that the object is bound to its function call at the
compile time. For example, the function “add( )” can be overloaded as follows:
Again, an example of operator overloading can be the addition ‘+’ operator is used
for adding two integers or to concatenate two strings.
P.T.O.
SOLVED WORLD
FAQ in C++ by
6
13. What do you mean by binding of data and functions?
This is encapsulation. In other words, it is the ability to package data with
functions which allows us to create a new data type, called “class”.
P.T.O.
SOLVED WORLD
FAQ in C++ by
7
defined either as member functions of that class, or as free functions. Binary
operators whose left operands are not class objects must be defined as free
functions. Now, we will verload the operator “+=”. In the statement:
L1 += 10;
the left operand of += is an “IntList”, and its right operand is an integer.
Therefore, this version of += can be defined either as an “IntList” member
function or as a free function. Here is how it might be defined as a member
function:
class IntList {
public:
void operator+=( int n );
- - - -
};
void IntList::operator+=( int n )
{
AddToEnd( n );
}
Note that it is up to the designer of the class to decide what it means to apply an
operator to a class object. This definition of operator+= adds the right-hand
operand (the integer value) to the end of the list represented by the left-hand
operand (the “IntList” whose member function is called). It would also be
reasonable to define operator+= to add the integer to the front of the list.
Consider a case where all types of inheritances occur namely, multiple, multilevel
and hierarchical. The “Child” class inherits both the “Parent” classes, “Parent 1 &
2”. The two “Parents” are having a common base class “Grandparent”. The
“Child” can inherit the traits of “Grandparent” via the two “Parents” or directly
(dotted line) form the “Grandparent”. Now, during inheritance via “Parents”, the
“Child” may have duplicate sets of members from “Grandparent”, as all public
and protected members are inherited twice from the “Parents” by the “Child”. To
avoid this we need to make the “Grandparent” virtual as follows:
class Grandparent {- - -};
class Parent1 : virtual public Grandparent {- - -};
class Parent2 : virtual public Grandparent {- - -};
class Child : public Parent1, public Parent2 {- - -};
So, this is the concept behind virtual classes or virtual base classes.
P.T.O.
SOLVED WORLD
FAQ in C++ by
8
Friend class:- In object-oriented programming to allow access to “private” or
“protected” data of a class in another class, the latter class is declared as a friend
class. A friend class has full access to the private data members of a class without
being a member of that class. A friend class can be declared as shown:
class A {
- - - - -
- - - - -
public:
- - - - -
- - - - -
friend class B;
}
In this example class B has access to private and protected data and member
functions of class A.
P.T.O.
SOLVED WORLD
FAQ in C++ by
9
typically used to initialize the data members of the object to their default state, but
may also be used to allocate memory. When the object is deleted, a destructor is
called automatically. The destructor is typically used to deallocate the memory
allocated for that object.
In the above figure, B1 is the base claas from where the class D1 is derived and
D2 is again derived from D1. The syntax of multilevel inheritance is given below:
class B1 { - - Body of base class B1 - - };
class D1 : public B1 { - - Body of derived class D1 - - };
class D2 : public D1 { - - Body of derived class D2 - - };
realloc() free()
P.T.O.
SOLVED WORLD
FAQ in C++ by
10
i. The syntax is: i. The syntax is:
void * realloc ( void * ptr, void free ( void * ptr );
size_t size );
ii. It reallocates memory block. The ii. It deallocates space in memory. A
size of the memory block pointed to block of memory previously
by the ptr parameter is changed to allocated using a call to malloc(),
the size bytes, expanding or calloc() or realloc() is
reducing the amount of memory deallocated, making it available
available in the block. again for further allocations
iii. It has two parameters. iii. It has a single parameter ptr. It is a
1) ptr:- Pointer to a memory block pointer to a memory block
previously allocated with previously allocated with
malloc(), calloc() or malloc(), calloc() or
realloc() to be reallocated. realloc() to be deallocated. If a
If this is NULL, a new block is null pointer is passed as argument,
allocated and a pointer to it is no action occurs.
returned by the function.
2) size:- New size for the memory
block, in bytes. If it is 0 and ptr
points to an existing block of
memory, the memory block
pointed by ptr is deallocated
and a NULL pointer is returned.
iv. It returns a pointer to the reallocated iv. It returns nothing.
memory block, which may be either
the same as the ptr argument or a
new location. The type of this
pointer is void*, which can be cast
to the desired type of data pointer in
order to be dereferenceable. If the
function failed to allocate the
requested block of memory, a
NULL pointer is returned.
P.T.O.
SOLVED WORLD
FAQ in C++ by
11
22. What are the main differences between procedure oriented languages and
object oriented languages?
23. What is R T T I?
Run Time Type Identification is a mechanism that helps us to determine the type
of an object during program execution. There are three main C++ language
elements for RTTI as follows:
The dynamic_cast operator: Used for conversion of polymorphic types.
The typeid operator: Used for identifying the exact type of an object.
The typeinfo class: Used to hold the type information returned by the typeid
operator.
P.T.O.
SOLVED WORLD
FAQ in C++ by
12
Here, the function swap() is generic because it is used for swapping integers and
floats at a time.
Generic class:- Generic classes are those classes that can have members that use
template parameters as types. For example:
template <class T>
class mypair {
T values [2];
public:
mypair (T first, T second)
{
values[0]=first; values[1]=second;
}
};
The class mypair is used to store two elements of any datatype. For example, if
we wanted to declare an object of this class to store two integers 15 & 14 we’ll
write:
mypair<int> myobject (115, 36);
the same class can also be used to create an object to store floats:
mypair<double> myfloats (3.0, 2.18);
26. What is the difference between pass by reference and pass by value?
Pass by value:- When we pass an argument to a function, a copy of that argument
is made inside the function. This is referred to as pass-by-value. The effect of
pass-by-value is shown bellow:
void f(int a)
{
cout << "a = " << a << endl;
a = 5;
cout << "a = " << a << endl;
}
int main()
{
int x = 47;
cout << "x = " << x << endl;
f(x);
cout << "x = " << x << endl;
P.T.O.
SOLVED WORLD
FAQ in C++ by
13
}
In f( ), a is a local variable, so it exists only for the duration of the function call to
f( ). Because it’s a function argument, the value of a is initialized by the
arguments that are passed when the function is called; in main( ) the argument is
x, which has a value of 47, so this value is copied into a when f( ) is called.
P.T.O.
SOLVED WORLD
FAQ in C++ by
14
So you can see that pass-by-reference allows a function to modify the outside
object, just like passing a pointer does in C. Actually in C, pointer passing is the
only way to pass-by-reference as it does not support a reference to be passed
directly. You can also observe that the reference obscures the fact that an address
is being passed.
P.T.O.
SOLVED WORLD
FAQ in C++ by
15
29. What are virtual classes?
This is also known as virtual base class.
Consider a case where all types of inheritances occur namely, multiple, multilevel
and hierarchical. The “Child” class inherits both the “Parent” classes, “Parent 1 &
2”. The two “Parents” are having a common base class “Grandparent”. The
“Child” can inherit the traits of “Grandparent” via the two “Parents” or directly
(dotted line) form the “Grandparent”. Now, during inheritance via “Parents”, the
“Child” may have duplicate sets of members from “Grandparent”, as all public
and protected members are inherited twice from the “Parents” by the “Child”. To
avoid this we need to make the “Grandparent” virtual as follows:
class Grandparent {- - -};
class Parent1 : virtual public Grandparent {- - -};
class Parent2 : virtual public Grandparent {- - -};
class Child : public Parent1, public Parent2 {- - -};
So, this is the concept behind virtual classes or virtual base classes.
P.T.O.
SOLVED WORLD
FAQ in C++ by
16
that variable is and causes space to be generated in memory to hold the data for
that variable. For a function, the compiler generates code, which ends up
occupying storage in memory. A function declaration and definition is shown
below:
class A {
int number;
float cost;
public :
void get_data(int a, float b); //declaration
void put_data(void);
};
void item :: get_data(int a, float b) //definition
{
number = a;
cost = b;
}
34. What are virtual constructors/destructors?
C++ does not allow virtual constructor. According to OOP basics, the derived
class object can be created after the creation of base class object. Now, virtual
constructor creates object of derived class. When pointer points to derived type
object it fetches derived constructor directly, without its base class object
constructed. This is not supported in C++. So there is no virtual constructor in
C++.
But, virtual destructors are allowed in C++. Virtual destructors are used to call the
destructor of the correct object. For example:
class Base1 {
public:
~Base1() { cout << "~Base1()\n"; }
};
class Derived1 : public Base1 {
public:
~Derived1() { cout << "~Derived1()\n"; }
};
class Base2 {
public:
virtual ~Base2() { cout << "~Base2()\n"; }
};
class Derived2 : public Base2 {
public:
~Derived2() { cout << "~Derived2()\n"; }
};
int main() {
Base1* b1p = new Derived1;
delete b1p; // calls base class destructor
Base2* b2p = new Derived2;
delete b2p; // calls derived class destructor
}
When we run the program, we see that delete b1p only calls the base-class
destructor, while delete b2p calls the derived-class destructor followed by the
base-class destructor, which is the behavior C++ allows.
P.T.O.
SOLVED WORLD
FAQ in C++ by
17
directly, without its base class object constructed. This is not supported in C++.
So there is no virtual constructor in C++.
36. What is late bound function call and early bound function call? Differentiate.
The procedure of connecting a function call to the function body is called binding.
When binding is performed before the program is run (by the compiler and
linker), it’s called early binding. C compilers have only early binding.
The problem by early binding is that the compiler cannot know the correct
function to call. Here late binding is the solution, where binding occurs at
runtime, based on the type of the object. When a language implements late
binding, there must be some mechanism to determine the type of the object at
runtime and call the appropriate member function. In the case of a compiled
language, the compiler still doesn’t know the actual object type, but it inserts code
that finds out and calls the correct function body. The late-binding mechanism
varies from language to language. For example, in C++, mechanism of virtual
table or VTABLE is used.
C++ exception handling is basically built upon three keywords, namely try,
throw & catch. The keyword try is used to preface a block of statements
(surrounded by braces) which may generate exceptions. The block of statements
is known as “try block”. When an exception is detected, it is thrown using a
throw statement in the “try block”. A “catch block”, defined by the keyword
P.T.O.
SOLVED WORLD
FAQ in C++ by
18
catch, catches the exception thrown in the try block and handles it appropriately.
The relationship is shown in figure.
P.T.O.
SOLVED WORLD
FAQ in C++ by
19
would fail to achieve run time polymorphism. This problem is solved by using
virtual functions.
E-mail us at solvedworld@gmail.com
P.T.O.