Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Author Archive admin

Fill up the blank in C++

‘TRUE’ OR ‘FALSE’ in C++ (Set-XI)

SAY ‘TRUE’ OR ‘FALSE’



251. The <iomanip.h> header file contains information for formatted input/output with parameterized stream manipulators.

252. In C++, outputs to object cerr are unbuffered.

253. Member functions tellp and tellg return the current locations of the put and get pointers respectively.

254. C++ provides both unformatted I/O and formatted I/O operations.

255. Inheritance exhibits transitive nature.

256. When many subclasses inherit from a single base class, it is multiple inheritance.

257. The transitive nature of inheritance is reflected by hybrid inheritance.

258. When inheriting publicly, the derived class can access private members of the base class directly.

259. When inheriting privately, the public and protected members of the base class become private members of the derived class.

260. A privately inherited class may be inherited further.

261. Members intended to be inherited but not intended to be public, should be declared as private.

262. When an object of a derived class is created, the program first calls the constructor for base class and then of derived class.

263. You can pass parameters to a base class constructor through a derived class constructor.

264. You can selectively allow access to some of the base class members when deriving privately.

265. Derivation is friendship.

266. We cannot assign a base class pointer to a derived class pointer.

267. A virtual function can be a static member function.

268. A derived class object is converted to a base class object when used as an argument to a base class member function.

269. If you expect to redefine a function in derived classes, declare it as a virtual function in the base class.

270. The ios class represents general properties of a stream.

271. In C++, output to object clog is unbuffered.

272. The endl manipulator flushes the buffer and inserts a newline.

273. unsetf() member function reads current format flag’s value.

274. In C++, references are same as pointers.

275. A virtual function may be declared a function in another class.

276. An overloaded binary operator can be overloaded for ternary operation.

277. showpoint is an enumerated constant defined in the ios class definition.

278. setprecision is a part of <iomanip.h> header file.

279. precision() function has default value as 2.

280. In C++, << is insertion operator and >> is extraction operator.




Solution Set to TRUE or FALSE

(251) T (252) T (253) T (254) T (255) T
(256) F (257) F (258) F (259) T (260) F
(261) F (262) T (263) T (264) T (265) F
(266) F (267) T (268) T (269) T (270) T
(271) F (272) T (273) F (274) F (275) F
(276) T (277) T (278) T (279) F (280) T




Multiple choice questions in C++ (Set-XV)

August 23, 2013 Comments Off on Multiple choice questions in C++ (Set-XV) By admin
Multiple Choice Questions in C++




Multiple choice questions in C++ (With answers)

141. A static automatic variable is used to
a) make a variable visible to several function
b) make a variable visible only to one function
c) converse memory when a function is not executing
d) retain a value when a function is not executing

142. The stacks data structure are based on
a) LIFO
b) FILO
c) FIFO
d) None of the above

143. Queues are based on
a) LIFO
b) FILO
c) FIFO
d) None of the above

144. & operator is
a) indirection operator
b) Logical AND
c) address operator
d) none of the above

145. int *ptr[10]; is
a) an array of 10 int pointers
b) a pointer of 10 int elements
c) an array of 10 elements returning an int value
d) none of the above

146. A structure that refers to itself is a
a) nested structure
b) recursive structure
c) self-referential structure
d) none of the above

147. With in …….scope class member may be referenced simply by their names
a) class’s
b) object’s
c) program
d) none of the above

148. To point to the address of any pointer variable cast the pointer to
a) char *
b) int *
c) void *
d) none of the above

149. Destructors are called
a) in the same order of constructor calls
b) in the reverse order of constructor calls
c) in any random order
d) none of the above

150. The advantage of declaring a virtual function as pure is
a) you can avoid making the class an abstract class
b) you force any derived class to define its own implementation
c) programs runs faster
d) early binding can be achieved




Solution Set to Multiple Choice Questions

(141)      C          (142)      A          (143)      C          (144)      C

(145)      A          (146)      C          (147)      A          (148)      C

(149)      B          (150)      B



Inheritance in C++

August 23, 2013 Comments Off on Inheritance in C++ By admin




Inheritance in C++

Inheritance is the property by virtue of which one class passes over its properties to another class without necessitating modification in the main class.  The class that inherits its properties to other class is called the base class whereas the class that receives the properties of the base class is called derived class.  Inheritance supports extensibility of classes, since new features may be added to existing classes without modifying them, by inheriting them to a new derived class, where derived class is defined to contain new additional features, other than the base class.  Inheritance types may be-

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

1.  Single Inheritance:  In single inheritance, a base class inherits its features to a derived class.

In the above figure the base class inherits its features to the derived class.  C++ code for single inheritance is as follows:

class Base
{int x;
public:
void getx();
void putx();
};

class Derived : public Base
{int y;
public:
void gety();
void puty();
};

void main()
{Derived dobj;
dobj.getx();
dobj.gety();
dobj.putx();
dobj.puty();
}

Inheritance type may be public, private or protected.  Private members of a class are never inherited.  When inheritance type is public, public members of the base class are inherited as public and protected members are inherited as protected.  When inheritance type is private, public members as well as protected members are inherited as private.  When inheritance type is protected, public members as well as protected members are inherited as protected.

2.  Multilevel Inheritance:  If a base class inherits its features to a derived class and the derived class further inherits its own properties and those inherited from base to another derived, the type of inheritance is called multilevel inheritance.

For example: 


class grandfather
{ int x;
public:
void getx();
void putx();
};

class father : public grandfather
{int y;
public:
void gety();
void puty();
};

class son : public father
{int z;
public:
void getz();
void putz();
};

void main()
{son s;
s.getx();
s.gety();
s.getz();
s.putx();
s.puty();
s.putz();
}

3. Multiple Inheritance:  If a derived class attains properties from more than one base class, then the type of inheritance is called multiple inheritance.

For example:


class base1

{ int x;
public:
void getx();
void putx();
};

class base2
{int y;
public:
void gety();
void puty();
};

class derived : public base1, public base2
{int z;
public:
void getz();
void putz();
};

void main()
{derived s;
s.getx();
s.gety();
s.getz();
s.putx();
s.puty();
s.putz();
}

4. Hierarchical Inheritance:  If inheritance forms a hierarchy, such that a class may inherit its properties at many levels as defined in the hierarchy, the type of inheritance is called hierarchical inheritance.

For example:


 

5. Hybrid Inheritance:  Two or more inheritance types combine together to result in hybrid inheritance.  Some situations may arise when two or more types are required together to define solution to a given problem, such a situation results in hybrid inheritance.  An example of hybrid inheritance is demonstrated in the following figure:

The above figure is a combined representation of multiple inheritance and multilevel inheritance.  For the base classes base1 and base2 inheriting to derived1, the inheritance type is multiple inheritance, but for the remaining part is multilevel inheritance.  This is called hybrid inheritance.



Default arguments in C++ and its constraints

August 23, 2013 Comments Off on Default arguments in C++ and its constraints By admin

Default arguments in C++ and its constraints

Default Arguments:  The values given to the arguments of the function may be defaulted by supplying the variables with default values at the time of declaring a function.  If the function is invoked without specifying the argument’s value, the default value is assumed for the variable.  Thus, default arguments prove useful in C++ for applications where default values for values may be considered, as for pie (p), default value is 3.14, that may be supplied at the time of declaring the function as follows:

float volume(float r, float h, float pie=3.14);

After default value is supplied to the argument pie, the following funtion invocation statement will work-

float result;
result = volume(3.5,4.5);

such that r receives 3.5, h receives 4.5 and the variable pie receives the default value given in prototype i.e. 3.14.

The constraint for the default arguments is that argument can only be defaulted from right to left.  Thus, the following function declaration with default argument results in an error:

float volume(float r, float pie=3.14, float h);

Default arguments are useful in situations where default values must be supplied automatically with writing them explicitly.

Exception handling and its classification in C++

May 29, 2013 Comments Off on Exception handling and its classification in C++ By admin

Exception handling and its classification in C++

Exception Handling:  The error handling mechanism of C++ is generally referred to as exception handling.  C++ provides a mechanism of handling errors in a program.

Generally exceptions are classified into synchronous and asynchronous exceptions.

  1. Synchronous Exceptions:  The exceptions which occur during the program execution due to some fault in the input data or technique that is not suitable to handle the current class of data, within the program are known as synchronous exceptions.  For example: errors such as out of range, overflow, underflow and so on belong to the class of synchronous exceptions.
  2. Asynchronous Exceptions:  The exceptions caused by events or faults unrelated (external) to the program and beyond the control of the program are called asynchronous exceptions.  For example: errors such as keyboard interrupts, hardware malfunctions, disk failure and so on belong to the class of asynchronous exceptions.

The exception handling mechanism of C++ is designed to handle only synchronous exceptions within a program.

This is done by throwing an exception.  The exception handling mechanism uses three blocks: try, throw and catch.  The try-block must be followed immediately by a handler, which is a catch block.  If an exception is thrown in the try block, the program control is transferred to the appropriate exception handler.  The program should attempt to catch any exception that is thrown by any function.  Failure to do so may result in abnormal program termination.

Exception Handling Constructs:

(1)  throw:- The keyword throw is used to raise an exception when an error is generated in the computation.

(2)  catch:-  The exception handler is indicated by the catch keyword.  It must be used immediately after the statements marked by the try keyword.

(3)  try:-  The try keyword defines the boundary within which an exception can occur.

Thus, the error handling code must perform the following tasks:-

  1. Detect the problem causing exception. (Will hit the exception).
  2. Inform that an error has occurred.  (Throw the exception).
  3. Receive the error information (Catch the exception).
  4. Take corrective actions. (Handle the exception).

For example:  (Divide operation validation)

int division(int n1, int n2)
{
if(n2==0)
throw n2;
else
return n1/n2;
}

int main()
{
————
————
————
try
{
result=division(n1,n2);
}
catch(int)//exception handler block
{cout<<”Exception raised! Division Error…!”;
return -1;
}
————
————
}

Syntax for creating user defined manipulators in C++

May 29, 2013 Comments Off on Syntax for creating user defined manipulators in C++ By admin

Syntax for creating user defined manipulators in C++

C++ provides a set of predefined manipulators. The header file iomanip.h header file contains these manipulators. Moreover, you can design your own manipulators in C++ to suit specific purpose. The user defined manipulators are defined as follows:

ostream & manipulator(ostream & ostr)
{
set of statements;
return ostr;
}

Consider the following example which creates a user defined manipulator named curr for displaying Rs. and sets the precision to 2.

#include <iostream.h>
#include <iomanip.h>

ostream & curr(ostream & ostr)
{
cout<< setprecision(2);
cout<<“Rs. “;
return ostr;
}

void main()
{
float amt = 4.5476;
cout<<curr<<amt;
}

//Output: Rs. 4.55

Use of manipulators in C++

May 29, 2013 Comments Off on Use of manipulators in C++ By admin

Use of manipulators in C++

Manipulators are stream functions available in iomanip.h header file and are used to change the default formats of input and output.  These are used with stream insertion and extraction operators.  They are used for defining a specified format of input and output.  They provide features similar to that of ios member functions.  Moreover, manipulators may be concatenated to result in a single statement.  Some of the standard manipulators available in iomanip.h header file are endl, setw, setfill, hex, oct, dec, setprecision, flush, setiosflags etc.  Manipulators are different from ios member functions as manipulator does not return the previous format state as is the case with ios member functions.

Consider the following example:

#include <iostream.h>
#include <iomanip.h>

void main()
{
float x = 5.45;
cout<<setw(10);
cout<<setprecision(4);
cout<<setfill(‘*’);
cout<<x<<endl;
}

OUTPUT:

******5.45

Consider another example:

#include <iostream.h>
#include <iomanip.h>

void main()
{
int n=52; //decimal number
cout<<endl<<“Decimal Number=”<<dec<<n;
cout<<endl<<“Hexadecimal Number=”<<hex<<n;
cout<<endl<<“Octal Number=”<<oct<<n;
}

Output:

Decimal Number=52
Hexadecimal Number=34
Octal Number=64

Template class in C++

May 28, 2013 Comments Off on Template class in C++ By admin

Template class in C++

A class which is declared such that it can operate on different data types is referred to as a template class.  It is a generic class defined to support various similar operations on different data types.  For example: a class may be created with a data member, such that it can operate upon any type of data member either int, float or char or any other type.  Whenever we require to generalize the definition of a class for various data types used in C++, we may prefer using class templates, as it gives a more general definition for a class.

To declare a template class, we have to use the keyword template followed by class name i.e. template <class nmofclass>, where template and class are keywords and nmofclass is the userdefined name given to a generic class definition.

An example to illustrate the use of template class is given below:

template <class sample>
class abc
{
sample x;
public:
void getx(){cout<<”Enter x?”;cin>>x;}
void putx(){cout<<”x=”<<x;}
};

The data type is specified when the object of the class is created in the manner specified below:

abc <int> obj1;
// class that contains an int variable.
abc <float> obj2;
// class that contains a float variable.

The complete program is as follows:

#include <iostream.h>
#include <conio.h>

template <class sample>

class abc
{
sample x;
public:
void getx()
{
cout<<“\nEnter x?”;
cin>>x;
}
void putx()
{
cout<<“\nx=”<<x;
}
};

void main()
{
clrscr();
abc <int> obj1;
cout<<“\nWorking on integer…!”;
obj1.getx();
obj1.putx();

abc <float> obj2;
cout<<“\n\nWorking on float…!”;
obj2.getx();
obj2.putx();
getch();
}

Thus class templates are required to create generic classes that can refer to any user-defined data type.

How is polymorphism achieved at compile time in C++

May 28, 2013 Comments Off on How is polymorphism achieved at compile time in C++ By admin

How is polymorphism achieved at compile time in C++

Polymorphism, in C++, is implemented through overloaded functions and overloaded operators. Function Overloading is also referred to as functional polymorphism. The same function can perform a wide variety of tasks. The same function can handle different data types. When many functions with the same name but different argument lists are defined, then the function to be invoked corresponding to a function call is known during compile time. When the source code is compiled, the functions to be invoked are bound to the compiler during compile time, as to invoke which function depending upon the type and number of arguments. Such a phenomenon is referred to early binding, static linking or compile time polymorphism.

For example:

#include <iostream.h>

//function prototype

int multiply(int num1, int num2);
float multiply(float num1, float num2);

void main()
{
//function call statements

int ans1=multiply(4,3);
// first prototype is invoked as arguments
// are of type int

float ans2 = multiply(2.5, 4.5);
//second prototype is invoked
//as arguments are of type float
}

The compiler checks for the correct function to be invoked by matching the type of arguments and the number of arguments including the return type. The errors, if any, are reported at compile time, hence referred to as compile time polymorphism.

x Close

Like Us On Facebook

You cannot copy content of this page.