c plus plus two marks part i

45
UNIT I 1. What i s C++? C++ is a general –purpose programming language wi th a bias towards system programming that - is a better C. - supports data abstraction - supports object-oriented programming - supports generic programming. 2. Write the applications of C++? * C++ is a versatile language for handling very large programs. * C++ is sui table for vir tua lly any pro gra mmi ng tas k including development of edi tors, compi ler s, databases, communication system and complex real-life application system. * C++ programs are easily maintainable and expandable. 3. Write the strikin g featur es of objec t orient ed progr amming 1. Emphasis is one data rather than procedure. 2. Programs are di vided into what are known as objects 3. Data structures are desig ned such that they characteriz e the objects. 4. Functions that operate on the data of an object ar e ti ed together in the data structure. 5. Data is hidden and cannot be accessed by external functions 6. Objects may communicate with each other through functions. 7. New data and functions can be easily added whenever necessary. 8. Foll ows bottom-u p approach in program design. 4. What you mean b y pr ogram par adigms? Oject oriented programming is a technique for programming – a paradigm for writing “good” programs for a set of Problems . If the term “object-orient ed programming language” means anything.

Upload: tharani-tharan

Post on 10-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 1/45

UNIT I

1. What is C++?

C++ is a general –purpose programming language with abias towards system programming that

- is a better C. -

supports data abstraction - supports

object-oriented programming

- supports generic programming.

2. Write the applications of C++? * C++ is

a versatile language for handling very large programs.

* C++ is suitable for virtually any programming task including

development of editors, compilers, databases, communication

system and complex real-life application system.

* C++ programs are easily

maintainable and expandable.

3. Write the striking features of object oriented programming

1. Emphasis is one data rather than procedure.2. Programs are divided into what are known as objects

3. Data structures are designed such that they characterize

the objects.

4. Functions that operate on the data of an object are tied

together in the data structure.

5. Data is hidden and cannot be accessed by external

functions

6. Objects may communicate with each other through

functions.

7. New data and functions can be easily added whenever

necessary.

8. Follows bottom-up approach in program design.

4. What you mean by program paradigms?

Oject oriented programming is a technique for programming

– a paradigm for writing “good” programs for a set of Problems. If 

the term “object-oriented programming language” means anything.

Page 2: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 2/45

It must mean a programming language that provides mechanisms

that supports the object-oriented style of programming well.

5. What are the characteristics exhibited by procedure-oriented

programming

1. Emphasis is on doing things (algorithms)

2. Most of the functions share global data.

3. Large programs are divided into smaller programs known as

functions.

4. Data move openly around the system from function to function.

5. Functions transforms data from one form to another.

6. Employs top-down approach in program design.

6. What you mean by modular programming

A set of related procedures with the data they manipulate is

often called a module. The programming paradigm becomes:

Decide which module you want: partition the

program so that data is hidden within modules. This paradigm is also

known as the data hiding principle. Where there is no grouping of 

procedures with related date, the programming style suffices. Also ,

the technique for designing “good procedures” are now applied foreach procedure in module. The most common example of module is

the definition of stack.

7. What you mean by separate compilation?

C++ supports C’s notation of separate compilation. This can

be used to organize a program into a set of semi-independent

fragments. Typically , we place the declarations that specify the

interface to a module in a file with a name indicating its intended use.

  Thus, namespace stack{

void push(char); // interface

char pop( ); }

would be place

in a file stack.h , and users will include that file, called a header file.

8. Write note on Exception handling. When a

program is designed as a set of modules, error handling must be

considered in right of these modules. Which module is responsiblefor handling what errors? Often, the module that detects an error

Page 3: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 3/45

doesn’t know what action to take. The recovery action depends on

the module that involved the operation rather than on the module

that found the error while trying to perform the operation.

9. Define user-defined types:

C++ attacks this problem by allowing a user to directly define

types that behave in the same way as built-in types. Such a type is

often called an abstract data type. A more reasonable definition of 

abstract data type would require a mathematical “abstract “

specification, what are called types here would be concrete examples

of such truly abstract entities. The programming paradigm becomes:

Decide which types you want:

provide a full set of operations for each type.

10.Drawbacks of pop: * In pop

Global data are more vulnerable to an inadvertent change by a

function. * In a large program it is very difficult to identify

what data is used by which functions.

* In case we need to revise an external

datastructures, we should also revise all functions that access the

data, this provides an opportunity for bugs to creep in.* It does not model real world problems very well because

functions are action-oriented and do not really correspond to the

elements of the problem.

11. What are the characteristics incorporated by OOPS, write some

languages that support these?

Object-Oriented programming incorporates all of object –

based programming features along with two additional features,

namely, inheritance and dynamic-binding. Object-basedfeatures + inheritance + dynamic binding.

Object based features

* Data encapsulation

* Data hiding and access mechanism *

Automatic initialization & clear-up of objects

* Operator overloading Languages that supports these

features include c++ , smalltalk & object pascal.

12.Depends upon features classify OOPS.1. Object-based programming languages.

Page 4: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 4/45

2. Object oriented programming languages.

13.State the following statements are true or false

i. In pop all data are shared by all functions.

---false.(only global data can be shared by all functions)

ii. Inheritance means the ability to reuse the data values

of one object by other objects ----True

iii. C++ allows us to create hierarchy-related objects---

true d. In C++ , it is very

easy to add to the existing structure of an object

---True.

14.Discuss cascading of I/O operators:

 The multiple use of <<in one statement is called cascading.

When cascading an output operator, we should not ensure

necessary blank spaces between different items.

a=10; b=15;

cout << “A=”<<a<<”\n”<< “B=” <<b<<”\n”;

 This is one statement but provides two lines of output. We can also

cascade input operator >> as

Cin>> number1>>number 2;

 The values are assigned from left to right.

15.State whether the following statements are true or

false

1. In C++ , Identifier name can be start with digit ----false

2. ::* is used to declare a pointer to a member of a class---

true

3. (type –name) expression is type cast operator format in

C++ ----false (It should be type-name

(expression)

16. Discuss automatic conversion in C++.

Whenever data types are mixed in an expression, C++

performs the conversions automatically. This process is known as

implicit or automatic conversion.

Page 5: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 5/45

SHORT

CHAR

Int

Unsigned Long int

Unsigned long int Float

Double

Long Double

Water fall model of type conversion

17.Find the errors, if any, in the following C++ statements

a. enum (green, yellow, red);b. const int array-size;

c. int public =1000;

d. int* p = new; // allocate memory with new.

18.Discuss or compare the evolution of software technology.

1,0

MACHINE LANGUAGE

ASSEMBLY LANGUAGEPROCEDURE PROGRAMMING

OBJECT-ORIENTED

PROGRAMMING Layers of  

software technology.

19.Write a short note on relationship of data and functions in a

procedure- oriented program.

GLOBAL

DATA GLOBAL DATA

FUNCTION – 1

FUNCTION – 2

FUNCTION – 3

 

LOCAL DATA LOCAL DATA LOCAL DATA In

a multi function program, many important data items are placed as

global, so that they may accessed by all function. Each function may its

own local data.

Page 6: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 6/45

20.Draw the program fragments representation:

stack.h

stack interface

user.c

stack.c#include “stack.h” #include

“stack.h”

use stack

define stack 

UNIT II

21. What is meant by pointer to void?

 The address that we put in a pointer must be the same type as the

pointer. we cant assign the address of the float variable to a pointer to

int. But there is an exception to this. There is a sort of general purpose

pointer that can point to any data type called a pointer to a void.

E.g. Void *ptr ;

22. What are the uses of pointers?• Accessing array elements.

• Passing arguments to a function when the function needs to modify

the original arguments.

• Passing arrays and string to functions.

• Obtaining memory from the system.

• Creating data structure such as linked lists.

23. What is the use of NEW and DELETE operator.

 The NEW operator obtains a specified amount of memory from the

system and returns a pointer to the memory. The operator is used to crate

variables and data structure during program execution. The DELETE

operator releases memory obtained by new.

24. Write a note on THIS pointer.

 The member functions of every object have access to a sort magic

pointer named this, which points to the object itself. Thus any member

function can find out the address of the object of which is a member.

Page 7: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 7/45

25. How serious is the breach of data integrity when friend functions are

used?

A friend function must be declared as such within the class whose

data it will access. Thus a programmer who does not have access to the

source code for the class cannot make a function into a friend.

26. Write a note on friend function.

A friend function can access a class private data, even though it is

not a member function of the class. This is useful when one function must

have access to two or more unrelated classes and when an overloaded

operator must use, on its left side, a value of a class other than one of 

which it is a member. Friends are also used to facilitate functionalnotation.

27. Define inline function.

An inline function looks like a normal function in the source file but

inserts the function code directly into the calling program. Inline functions

execute faster but may require more memory than normal functions

unless they are very small.

28. Write a note on automatic variables.Variables that are defined within a function body are called

automatic variables. An automatic variable is not created until the function

in which it is defined is called.

29. Define lifetime of a variable.

 The time period between the creation and destruction of a variable

is called the lifetime of the variable.

30. Define class.

Classes are created using the keyword class. A class declaration

defines a new type that links code and data. This new type is then used to

declare objects of that class an object is an instance of a class.

31. What are the 3 access specifiers and explain it.

The 3 access specifiers are Public, Private and Protected

• Private: It accessed only by other members of the class.

• Public: It allows functions or data to be accessible to other parts

of programs.

Page 8: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 8/45

• Protected: Access specifier is needed only when inheritance is

involved.

32. Define Friend class.It is possible to grant a non member function access to the private

members of a class by using a friend. A friend function has access to all

private and protected members of the class for which it is a friend.

33. How friend function can be declared.

 To declare a friend function, include its prototype within the class

preceding it with the keyword friend.

For eg,

Class myclass

{

int a,b;

public:

friend int sum(my class x);

};

34. What are the situations where inline expansion cannot work?• For function returning values, if a loop, a switch or a go to exists.

• For function not returning values, if a return statements exist.

• If function contain static variables.

• If inline function are recursive.

35. How objects can be passed to function.

Objects may be passed to function in just the same way that any

other type of variable can. Objects are passed to functions thru the use of 

standard call by value mechanism.

36. How objects can be returned in C++.

When an object is returned by a function a temporary object is

automatically created that holds the return value. It is this object that is

actually returned by the function.

37. How pointer can be used to derived type.

Page 9: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 9/45

In general, a pointer of one type cannot point to an object of a

different type.

However there is an important exception to this rule that relates only to

derived class.

 To begin assume two classes called B & D. that is D is derived from baseclass B. In this situation, a pointer of type B* may also point to an object of 

type D.

38. What is static member function and explain its characteristics?

A member function that is declared static has following property,

• A static function can have access to only other static members

declared in the same class.

• A static member function can be called using the same class nameas follows

class_name:: function_name;

39. What is scope resolution operator? Explain how does it work in c++?

 The ::operator links a class name with a member name in order to

tell the complier what class the member belongs to. It has another use

that is it can allow access to a name in an enclosing scope that is hidden

by a local declaration of the same name.

Egname.

Eg:

Void f()

{

int I;

I=10;

………..

……..

}It may do so by preceding the I with the :: as shown here

Void f()

{

int I;

::I=10;

……..

……..

}

40. How the pointer to the objects can be used in c++?

Page 10: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 10/45

  Just as we can have pointers to other types of variables, we can

have pointers to objects. When accessing members of a class given a

pointer to an object, use the arrow

(->) operator instead of the dot operator.

When pointer is incremented, it points to next element of its type.For eg: an integer pointer will point to next integer, in general, all pointer

arithmetic is relative to base type of the pointer.

41. Define class and objects?

Classes are user defined data types and behave like the built in

type of a programming languages. the wrapping up of data and functions

into a single unit called class.

Objects are the basic run_time entities in an object oriented system.

When a program is executed, the objects interact by sending messages toone another.

42. Compare and contrast between structure and class.

  The only difference between a class and a structure is that by

default all members are public in a struct and private in a class.

Comparisons:-

 There is no fundamental reason not to increase the capabilities of a

structure. In c structures already provide a means of grouping data. It also

allows us to include member functions.

43. List the restrictions that are to be followed while using unions in c++

• Union cannot inherit any other classes of any type.

• Union cannot be a base class

• No static variables can be members of a union

• A reference member cannot be used

• A union cannot have as a member any object that overloads =

operator

• No objects can be a member of a union if the object has an explicit

constructor on destructor function.

44. Define friend function?

It’s possible to grant a non-member function access to the private

members of a class by using the keyword friend. A friend function has

Page 11: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 11/45

access to all private and protected members of the class for which it is a

friend.

45. Circumstances in which friend functions are quite valuable.

•Friends can be useful when you are overloading certain typesof operators.

• Friend functions make the creation of some types of i/o

functions easier.

•   Two or more classes may contain members that are

interrelated relative to other parts of our program.

46. Uses of inline functions.

Each time a function is called, a significant amount of overhead is

generated by calling and return mechanism. Therefore it increases theperformance of our program.

47. Static data member:-

A variable with a declaration static, tells the compiler that only one

copy of that variable will exist and that all objects of the class will share

the variable. No matter how many objects of a class are create. Only one

copy of a static data member exists. Thus all objects of that class use that

same variable. All static variables are initialized to zero before the first

object is created.

48. Restrictions in using static member functions.

• Static member functions can only directly refer to the other static

members of the class.

• Global data can also be accessed by static member’s functions.

• Static member functions does not have a this pointer.

• A static member functions may not be virtual.

49. Characteristics of friend function?

• It is not in the scope of the class to which it has been declared as

friend.

• Since it is not in the scope of the class. it cannot be called using the

object of that class.

It cannot be invoked like a normal function without the help of anyobject

Page 12: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 12/45

• Usually it has objects as arguments.

• It can be declared either in the public or private part of a class

without affecting its meaning.

50. Arrays of objects:-The objects that are created for a class can also be an array of 

objects.

Eg:-

Class employee

{

char name[10];

float age;

public:

void get(void);void disp(void);

}ob[10];arrays of objects

accessing:

for(I=1;I<=n;I++)

{

ob[i].get();

ob[i].disp();

}

51.What are the characteristics of member functions?

• Several different classes can use the same function name.

• Member functions can access the private data of the class

except friend function.

• A member function can call another member function

directly, without using the dot operator.

52. What are the characteristics of Static member variable?

• It is initialized to zero, when the first object of its class

is created. No other initialization is permitted.

• Only one copy of that member is created for the entire

class and is shared by all the objects of that class.

• It is visible only within the class, but its lifetime is the

entire program.

Page 13: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 13/45

53. What are the properties of Static member functions?

•A Static function can have access to only other staticmembers declared in the same class.

• A static member function can be called using the class

name instead of its objects.

54. What are the advantages of using inline member functions?

•  The size of the object code is considerably reduced.

• It increases the execution speed.

•  The inline member functions are compact function calls.

55. What are the dynamic allocation operators?

New

•  The new operator is used to create a heap memory space

for an object of a class.

Delete

•  The delete operator is used to destroy the variable space

which has been created by using the new operator

dynamically.

56. What are the restrictions that have been placed on Static member

functions?

• Static member functions may only directly refer to other

static members of the class.

• A static member function may only directly refer to other

static members of the class.

•  There cannot be static and a non-static version of the

same function.

• A static member function may not be virtual.

•  They cannot be declared as const or volatile.

57. What are the two ways to achieve call-by-reference parameter

passing?• Explicitly passing a pointer to the argument.

Page 14: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 14/45

• Using a reference parameter.

58. What are the Restrictions to References?

• A Reference cannot be referenced or the address of areference cannot be obtained.

• Array of reference cannot be created.

• A pointer to a reference cannot be created.

• A bit-field cannot be referenced.

• Null references are prohibited.

59. What are the 3 ways a reference can be used?

• As a function parameter.

• As a function return value.

• As a stand-alone reference.

60. What are the 2 ways in which arguments can be passed to functions?

• Call-by-value.

• Call-by-reference.

61. What is meant by this pointer?

When a member function is called, it is automatically passed

an implicit argument that is a pointer to the invoking object. This

pointer is called this.

62. What do local classes mean?

When a class is declared within a function, it is known only to

that function and unknown outside of it. These types of classes are

called local classes.

63. What are the restrictions to local classes?

• All member functions must be defined within the class

declaration.

•   The local class may not use or access local variables of the

function which in which it is declared.

64. Ex plain what happens when constructors and destructor areexecuted?

Page 15: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 15/45

In general an object’s constructor is called when the object

comes into existence and an object’s destructor is called when the object

is destroyed. Likewise when the object’s constructor function is executed,

when the object’s declaration statement is encountered. The destructor

functions for local objects are executed in the reverse order of theconstructor functions.

65. What is the operator which is used to links the class name with

member?

It is the scope resolution operator (::) which links the class name

with a member name in order to tell the computer what class the member

belongs to. It is also used to access the name in an enclosing scope that is

hidden by a local declaration of same name.

 

Example:int a; // global

void f( )

{

int a; //local

a=10; //local

::a=10; //global

}

66. Write about initialized and un initialized array of objects. 

We can create both initialized and un initialized array of objects. since

it is a constructor it calls automatically by differentiating initialized and un

initialized arrays.

Example:

Class cl

{

int a;

public:cl( ) { a=0; }

cl(int j) { a=j; }\

};

cl a1[3] ={3,4,5}; // initialized

cl a2[10]; // uninitilized

67. How can we use objects as pointers.

Page 16: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 16/45

Like we use pointers to other types of variables, we can have

pointers to objects. when accessing members of class given a pointer to

an object use the arrow(->) operator instead of the dot operator.

 

Example:Class sample

{

int a;

public:

void print( ) { a=10*10 }

};

main( )

{

sample s,*p;p=&s;

p->print( );

}

UNIT IV

68. Explain the concept of reusability in c++?

C++ strongly supports the concept of reusability. The c++ classes

can be reused in several ways .Once a class has been written and tested,

it can be adapted by other programmers to suit their requirement .this

done by creating new class reusing the properties of the existing ones is

called reusability,

69. Explain the concept of inheritance?

Creating a new classes, reusing the properties of the existing ones

,this mechanism of deriving a new class from old one is called inheritance

or derivation. The old class is referred to as the bare class and the new

one is called the derived class or subclass.

70. Explain briefly the different forms of inheritance?

There are 5 forms available, namely

Page 17: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 17/45

• Single inheritance: A derived class with only one base

class

• Multiple inheritance: A derived class with only several

base class

• Hierarchical inheritance: The traits one class may beinherited by more than one class

• Multilevel inheritance:

• Hybrid inheritance: it is a combination of multiple and

hierarchical inheritance.

71. Define Derived class?

A derived class can be defined by specifying its relationship with

the base class in addition to its own details. the general form of 

defining a derived class is

Class derived_class_name

{

--------------

--------------//members of derived class

--------------

};

71. What is meant by visibility mode explain briefly?

 

Visibility mode is optional and may be either private or public. The

default visibility mode is private. Visibility mode specifies whether the

features of the base class are privately derived or publicly derived.

Eg:

Class abc: private Xyz // private derivation

{

members of abc;

};

Eg : 2

Class abc: private Xyz // public derivation

{

Page 18: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 18/45

members of abc;

};

 

72. What is a virtual function?

 

A C++ member function that is declared with the keyword virtual.

 The implementation that is executed when you make a call to a virtual

function depends on the type of the object for which it is called. This is

determined at run time.

73. Why do we need virtual function?

When a function is made virtual, c++ determines which function to

use at run time based on the type of object pointed to by the base pointer,

rather than the type of the pointer. By making the base pointer to point to

different objects, we execute different versions of the virtual function.

74. What is meant by pure virtual function?

 

A virtual function, equated to zero is called a pure virtual function.

It is a function declared in a base class that has no definition related to thebase class.

e.g.: virtual void display () =0;

 

75. What is meant by abstract class specify its objective?

 

A virtual function, equated to zero is called a pure virtual function.

It is a function declared in a base class that has no definition relative to

the base class. A class containing such pure function is called an abstractclass.

It objectives are

• Provide some traits to the derived classes.

•   To create a base pointer required for achieving run time

polymorphism.

76. Write some of the rules for virtual function?

1. Virtual functions must be members of some class.

2. They cannot be static members.

Page 19: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 19/45

3. They are accessed by using object pointers.

4. A virtual function can be a friend of another class.

5. We cannot have virtual constructors but we can have virtual

destructors.

6. If a virtual function is defined in the base class. It need notbe necessarily redefined in the derived class.

77. What does generic programming means?

 

 Templates execute us to define generic class and functions and

thus provides support for generic programming. It is an approach where

generic types are used as parameters in algorithms so that they work for a

veriety of suitable data types and data structures.

78. Why do we call templates as a parameterized class?

Templates are defined with a parameter that would be replaced

by a specified data type at the time of actual use of the class or function,

the template are some times called as parameterized classes or functions.

79. Define template class? 

A class created from a class template is called a template class.

 The syntax for defining an object of a template class is :

Class_name<type>object_name(arglist);

This process of creating a specific class from a class is called

instantiation. The compiler will perform the error analysis only when an

instantiation takes place.

80. Define class templates with multiple parameters?

 

We can use more than one generic data types in a class template.

  They are declared as a comma separated list within the template

specification as show below:

Syntax:

 

 Template<class t1, class t2, ..>Class classname

Page 20: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 20/45

{

----------

---------- ( body of the class)

};

81. Define function templates?

 

Like class template we can also define function templates that

would be used to create a family of function with different argument

types.

Syntax:

Template<class T>

return type function name(argument of type T)

{………..

//body of function

//with type T

//wherever appropriate

}

We must use the template parameter T as and when necessary in the

function body and in its arguments list.

82. Explain function templates with multiple parameters?

Like template class we can use more than one generic data type

in the template statement, using comma separator

Syntax:

Template<class T1,class T2,….>

return type function name(argument of type T1,T2,…)

{

………..//body of function

}

83. How can we overload a template functions?

 

A template function may be overloaded either by template

function or by ordinary function of its name. in such cases the overloading

resolution is accomplished by• Call an ordinary function that has an exact match.

Page 21: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 21/45

• Call a template function that would be created with an

exact match.

•  Try normal overloading resolution to ordinary function and

call the one that match.

84. What is meant by member function template?

 

 The member functions of the template classes themselves are

parameterized by the type argument and therefore there function must be

defined by the function template.

Syntax:

Template<class T>

return type classname<T> : : function name(arglist){

……………(function body)

…………….

}

85. Explain non-type template arguments?

 

It is also possible to use non type arguments that are in

additional argument T, we can also use other arguments such as strings,

function names, constant expressions and built-in types.

Eg:

Template<class T, int size>

Class array

{

T a[size];

……..

……..

};this templates supplies the size of the array as an argument.

86. Benefits of templates?

•   Templates allow implementing the concept of generic

programming.

•  Template allows us to generate a family of classes or a

family of functions to handle different data types.

Page 22: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 22/45

•   Template classes and functions eliminate code

duplication for different types and thus make the

program development easier and more manageable.

• We can use multiple parameters in both the class

templates and function templates.• Like other functions templates can be overloaded.

87. What is meant by exception?

 

Some peculiar problems other than logic or syntax errors, they

are known as exceptions. Exceptions are run time anomalies or unusual

conditions that a program may encounter while executing. Anomalies

might include condition such a division by zero, array outside of its bound

etc.

88. Explain the various types of exceptions?

They are of two kinds:

1. synchronous exception

2. Asynchronous exception.

Synchronous exception:

Errors such as “out of range index” and “over flow”

belong to synchronous type exception.Asynchronous exception:

Errors that are caused by event beyond the control of the

program are called asynchronous exception.

Note:

Proposed exception handling mechanism in c++ is assigned to handle

only synchronous exception.

89. Define exception handling mechanism?

Exception handling mechanism is basically built upon through

keywords namely try, catch, and throw. The keyword try is used to preface

a block of statements which may generate exception. This 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 statement is

defined by the keyword catch “catches” the exception thrown by the

throw statement in the try block.

Diagrammatic representation

Page 23: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 23/45

 

90. Purpose of the exception handling?

 

 The purpose of exception handling mechanism is to provide means to

detect and report an “exception circumstance”. So that appropriate action

can be taken. The mechanism suggests a separate error handling code

that performs the following tasks.

1. Find the problem (hit the exception).

2. Inform than an error has occurred (throw the exception).

3. Receives the error information (catch the exception).

4. Take corrective action (handle the exception).

91. Explain throwing mechanism briefly?

 

When an exception that is desired to be handled is detected, it

is thrown using throw statement in one of the following forms:

 Throw(exception)

 Throw exception;

 Throw; // used for rethrowing an exception.

92. Explain multiple catch statements?

  TRY BLOCK

Detects and throws an

exception

CATCH BLOCK

Catches and handles the

exception

Exception objects

Page 24: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 24/45

It is possible that a program segment has more than one

condition to throw an exception. In such case we can associate more than

one catch statement with a try. For eg:

 Try{

//try block;

}

catch (type 1 arg)

{

// catch block1;

}

catch (type 2 arg)

{// catch block 2;

}

……………….

……………….

Catch (type n arg)

{

// catch block n;

}

93. Explain briefly about rethrowing an exception?

A handler may decide to rethrow the exception caught without

processing it. In such situation, we may simply invoke throw without any

argument.

Eg: throw;

This cause the current exception to be thrown to the next

enclosing try/catch sequence and is caught by a catch statement listed

after that enclosing try block.

94. What is STL? What are its three components?

A collection of generic classes and function is called the standard

template library (STL). STL components are part of c++ standard library.

STL consist of three main components. They are

• Containers

• Algorithms

• Iterators

95. What is meant by container?

Page 25: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 25/45

 

A container is an object that actually stores data. It is a way data

is organized in memory, the STL containers are implemented by template

classes and therefore can be easily customized to hold different types of 

data.

96. What is meant by algorithm?

 

An algorithm is a procedure that is used to process the data

contained in the containers. The STL includes many different kinds.

 

97. Define map?

A map is a sequence of key value pairs that provides for fastretrieval based on the flag. Almost one value is held for each key; in other

words, each key in a map is unique.

98. What is a multimap?

A multimap is a map, except that it allows duplicate keys. Naturally

several values can exist for a single key. A multimap is preferred over a

map. In some ways, a multimap is even cleaner and more elegant than a

map.

99. How is an iteration over a map is carried out?

Iteration over a map is simply iteration over a sequence of pair <

const key, mapped_type >. For e.g. We might print out the entries of a

phone book like this

Void f(map<string, number>& phone_book) {

 Typedef map(string, number)::const_iteration CI;

For(CI =phone_book.begin( ); p!=phone_book.end( );++p)

cout<<p->first<<’\t’<<p->second<<’\n’;}

A map iterator presents the elements in ascending order of its keys.

100. What is a predicate?

A predicate is a function object (or a function) that returns a

bool.

101. Define function object?

Page 26: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 26/45

An object of a class with an application operator is called a function.

Like object, a function or simply functions object. Consequently, function

objects often execute faster than do ordinary function.

(or)

An object that acts like a function is often called function like object(or) simply function. Such function objects are important because they

allow us to write code that takes operations as parameters.

102. What are arithmetic function objects?

When dealing with numeric classes, it sometimes useful to have the

arithmetic function available as function objects. Consequently the

standard library provides arithmetic operations.

<functionalplus binary arg1 + arg2

minus binary arg1 - arg2

multiplies binary arg1 * arg2

divides binary arg1 / arg2

modules binary arg1 % arg2

negate Unary -arg1

103. Define Non-modifying sequence algorithm?

 These algorithms are provided to do the most common forms of 

updates. Some update a sequence while others produce a new sequence

based on information found during a traversal.

Std. algorithm work on data structures thru Iterators. This implies

that inserting a new element into a container (or) deleting one is not easy.

104. Define binder.

A binder allows a two-argument function object to be used as a

single-argument function by binding one argument is a value.

105. What are member function adapter & pointer to function adapter?

A member function adapter allows a member function to beused as an argument to algorithms.

Page 27: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 27/45

A pointer to function adapter allows a pointer to function to

be used as an argument to algorithm.

106. Define container.

A container is an object that actually stores data. It is a way data is

organized in memory .The STL container are implemented by template

classes and therefore can be easily customized to hold different types of 

data .

107. Define an algorithm.

An algorithm is a procedure that is used to process the data

contained in the containers. The STL includes many different kinds of algorithm to provide support to task such as initializing; searching,

copying, sorting and merging. Algorithm is implemented by template

functions.

108. Define Iterators.

 

Iterators are an object that points to an element in a container .We

can use Iterators to move through the contents of containers. Iterators are

handled just like pointers. We can increment or decrement them.Iteratorsconnect algorithm with containers and play a key role in the manipulation

of data stored in the containers.

109. What are the types of Iterators that are supported by each container

class?

There are three major categories of containers:

• Sequence container

1) Vectors2) Dequeue

3) list

• Associative container

1. set

2. multiset

3. map

4. multimap

• Derived container

Page 28: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 28/45

1. stack

2. queue

3. priority queue

110. Define sequence container? 

Sequence container stores elements in a linear sequence like a

line .Each elements are related to other by its position along the line. They

all expand themselves to allow insertion to the elements and all of them

support a number of operations on them.

Element0Element1Element2……..Last element…

Begin() end()

111. Define associative container?

Associative containers are designed to support direct access to

elements using keys. They are not sequential. There are 4 types of 

container:

• set

• multiset

• multimap

• map

All these containers stores data in a structure called tree which facilities

fast searching, deletion and insertion.

112. Define derived container?

The STL provides three derived containers namely:Stack, queue and priority queue. These are known as Container adaptors.

Page 29: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 29/45

 These can be created for different sequence container and it does not

support the Iterators and therefore we cannot use them for data

manipulation.

However, they support two member functions pop() and push() fro

implementing deleting and inserting operation.,

113. What is meant by algorithm?

Algorithms are function that can be used generally across a variety of 

containers for processing their contents.

114.What is meant by STL algorithm?

 

STL algorithm rein force the philosophy of reusability. By usingthese algorithms, programmers can save a lot of time and effort.

  To access STL Algorithm we must included <algorithm> in our

program.

115. What are the categories of STL algorithm?

STL algorithm based on the nature of operations it may be

categorized s follows:

• retrieve or non-mutating algorithm

• Mutating algorithm

• Sorting algorithm

• Set algorithm

• Relational algorithm

116. What is meant by list?

The list is another container that is popularly used. It supports

bidirectional, linear list and provides an efficient implementation for

deletion and insertion operation. Unlike a vector, which supports

random access, a list can be accessed sequentially only.

Write some important member functions of the list class?

 

function TaskBack() Gives a reference to the last

elementBegin() Gives a reference to the first

Page 30: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 30/45

elementClear() Deletes all the elementsEmpty() Decides if the list is empty or notEnd() Gives refernce to the end of the listErase() Deletes elements as specified

merge() Merge two ordered listInsert() Insert elemements as specifiedPop_back() Deletes the last elementPop_front() Deletes the first elementPush_back() Adds an element at the endPush_front() Adds an element at the frontRemove()

UNIT V

117. What are the kinds of classes?• Concrete types

• Abstract types

• Nodes

• Operations

• Interfaces

• Handles

• Application frameworks

  These kinds of classes are design notion and not language

constructs.

Page 31: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 31/45

118. What is meant by concrete types?

Classes such as vector, list, date and complex are concrete

in the sense that each is the representation of a relatively simple

concept with all the operations essential for the support of thatconcept.

Each has a one to one correspondence between its

interface and an implementation and none are intended as a

base for derivation.

119. What are the aims of concrete types?

 The aims of concrete type classes are:

i. To be a close match to a particular concept and

implementation strategy.

ii. To provide run time and space efficiency comparableto hand crafted code through the use of in-lining and of 

operations taking full advantage of the properties of 

the concept and its implementation.

iii. To have only minimal dependency on other classes;

and

iv. To be comprehensible and usable in isolation.

v. Providing a clear and efficient representation of a

single concept.

120. what are the important points to remember while using concrete

types?

i. Concrete types are fitted into a hierarchy of related

classes.

ii. Each concrete type can be understood in isolation with

minimal reference to other classes.

iii. If a concrete type is implemented well, programs using it

are comparable in size and speed to programs a user

would write using a hand-crafted and specialized versionof the concept.

iv. If the implementation changes significantly the interface is

usually modified to reflect the change.

v. Concrete type resembles in the built in type.

121. What are the advantages of concrete type over abstract type?

i. An individual concrete classes is easier to understand and

use then is a more general class.

ii. Concrete type is often used for classes that represent well

known data types such as arrays and list.122. What is meant by abstract type?

Page 32: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 32/45

i. A class is said to be abstract class that represents the

interface to a set of implementations of a common

concept.

ii. The abstract class provides the common interface to the

implementation. This means, we can use a set withoutknowing which kind of implementation is used.

123. Differentiate between concrete type and abstract type.

Concrete type Abstract type

* We require a redesign of the

implementation classes to express

commonality and issued a template

to exploit it.

* Design a common interface but no

commonality beyond re-ability to

implement the interface.

* If the implementation changes

significantly the interface is usually

modified to reflect the change.

Users need not depend on these

declarations and need not be

recompiled or in any way changed if set changes.

124. What are the aims of abstract types?

i. Define a single concept in array that allows several

implementations of it to coexist in a program.

ii. Provide reasonable run-time and space efficiency through

the use of virtual functions .

iii. Each implementation have only minimal dependency on

other classes.

iv. Comprehensible in isolation.

125. Why do we have concrete types when we have abstract type?

i. Efficiency : - We want to have concrete types such as

vector and list without the overheads implied by

decoupling the implementations from the interfaces.

ii. Reuse : - We need a mechanism to fit types designed

elsewhere into a new library or application by giving them

a new interface.

iii. Multiple interfaces : - Using a single common base fro avariety of classes leads to fat interfaces. Often it is better

Page 33: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 33/45

to provide a new interface to a class for new purposes

rather than modify its interface to serve multiple purposes.

126. Define node class.A node class relies on services from base classes to

provides its own services. That is, it calls base class member

functions. Atypical node class provides not just an

implementation of the interface specified by its base class. It also

adds new functions itself, thus providing a wider interface.

127. What are the important functions of node class?

 The important functions are the constructor through which

the programmer specifies the basic properties that are relevantto the simulation and the virtual functions that allow the

simulation routines too manipulate a public variable without

knowing its exact type.

128. What are the aims of node classes.

i. relies on its base classes both for its implementation and

for supplying services to its users.

ii. Provides a wider interface (i.e an interface with more

public members) to its users than do its base classes.

iii. Relies primarily on virtual functions in its public interface.iv. Depends on all of its( direct and indirect) base classes.

v. Can be understood only in the contest of its base classes.

vi. Can be used as a base for further derivation.

vii. Can be used to create objects.

129. What is meant by concrete node types?

A class that does not confirm to point base for further

derivation resembles on concrete type and could be called as

concrete node types.

130. What is meant by leaf classes?A concrete node class can be used to implement an abstract

class and variables of such a class can be allocated statically and on

the stack. Such a class is sometimes called as leaf class.

131. What is meant by abstract node class?

A class that does not confirm to create objects resembles on

abstract type and it is called as abstract node types.

132. What is meant by node classes?

i. A node classes is a part of the class hierarchy.

Page 34: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 34/45

ii. A class hierarchy is built with a view of derivation different

from the interface/implementer view used for abstract

types.

iii. A class is viewed as a foundation on which to build even if 

it is an abstract class, it usually has some representationand provides some services for its derived classes.

Example of node classes are polygon, Ival _slider and

satellite.

133. What are the uses of changing interface?

i. These techniques can be used to invoke a function based

on a string supplied by a user.

ii. To manipulate objects of unknown type through interfaces

discovered through run-time type identification.134. Define changing interfaces.

By definition, a node class is part of a class hierarchy. Not

every class in a hierarchy needs to offer the same interface. In

particular, a derived class can provide more member functions,

and a sibling class can provide a completely different set of 

functions.

135. Define action.

 The simplest and most obvious way to specify an action in

C++ is to write a function. If an action has to be delayed, it hasto be transmitted elsewhere before being performed, it requires

its own data that has to be combined with other actions etc.,

 Then is often becomes attractive to provide the action in

the form of a class that can execute the desired action and

provide other services as well.

136. What is meant by interface classes?

i. An interface class doesn’t do much – if it does then it is not

an interface class.

ii. It simply adjusts the appearance of some service to localneeds.

iii. Interface classes are essential to allow sharing without

forcing all users into a common straitjacket.

137. What are the uses of interface classes?

i. A major use of interface function is to adjust interface to

match users exceptions better, thus moving code that

would have been scattered throughout a users code into

an interface.

ii. An interface class that controls access to another class oradjusts its interface and is sometimes called as wrapper.

Page 35: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 35/45

iii. Interface classes is to provide checked or restricted

interfaces.

138. What is handle classes?

i. A handle providing the user interface and a

representation holding all or most of the object’s state.ii. The connection between the handle and the

representation is typically a pointer in the handle.

iii. Handles have a bit more data than the simple

representation, but not much more.

iv. The layout of a handle is typically stable even when

the representation changes and also that handles are

enough to move around relatively freely so that pointer

and references need not be used by the user.

139. Explain the uses of handling classes.

i. It is useful to extract the representation pointer from a

handle and use it directly.

ii. It works nicely provided the called function does not

destroy the object passed to it.

140. What are the operations in handles?

i. Overloading -> enables a handle to gain control and do

some work on each access to an object.

ii. Handles for which work needs to be done both before andafter access require more elaborate programming.

141. Define application frameworks.

A more ambitious approach to the support of design and

reuse is to provide code that establishes a common framework

into which the application builder fits application – specific code

as building blocks. Such an approach is often called as

application frameworks.

142. What are the uses of application frameworks?

HANDLE

REPRESENTATION

Page 36: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 36/45

i. A framework to be of significant use, it must provide more

structure and many more services than this simple

example does.

ii. A framework will also be supported by a library that

provides classes that are useful for the applicationprogrammer when specifying the action classes.

143. What is wrapper?

An interface class that controls access to another class or

adjusts its interface and is sometimes called as wrapper.

144. What are the two parts to separate a single object?

 These issues is to separate a single object into two parts.

i. A handle providing the user interface.ii. A representation holding all or most of the object’s

state.

145. What is used as a connection between handle and the

representation?

  The connection between the handle and the

representation is typically a pointer in the handle.

146. What is called proxy?

A handle that provides an interface that is close to

identical to the class for which it is a handle often called as

proxy.

147. How a base class is defined in handles?

i. Idea is to have one handle class for a family of classes

defined by a base class.

ii. Derivation from this base class can be a powerfultechnique.

iii. It appears to a node class and abstract types.

148. How does the overloading function performs in handles?

Overloading -> enables a handle to gain control and do some

work on each access to an object.

149. Which approach is called application framework?

An approach to the support of design and reuse is to provide

code that establishes a common framework into which the

Page 37: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 37/45

application builder fits application – specific code as building blocks.

Such an approach is often called as application frameworks.

UNIT III

1. Define function overloading.

 Two or more functions can share the same name that perform a

variety of different tasks as long as their parameter declarations are

different. The functions that share the same name are said to be

overloaded, and the process is referred to as function overloading.

2. When the functions are said to be overloaded?

Functions are said to be overloaded when they have same name

with different number and the type of the arguments. Two functions

differing only in their return types cannot be overloaded.

3. Write a note on default arguments.

C++ allows us to call a function without specifying all its

arguments. In such cases, the function assigns a default value to the

parameter that does not have matching arguments in the function call.

Default values are specified when the function is declared. We must add

the defaults from right to left.

4. Mention the advantages of providing the default arguments.

• We can use default arguments to add new parameters to

the existing functions.

• Default arguments can be used to combine similar

functions into one.

5. Mention the advantages of having default arguments in constructors.

• It prevents us from having to provide an overloaded

constructor that takes no parameters.

• It is convenient by providing the default common initial

values than specifying them each time an object is declared.

6. Explain overloading the new operator.

Page 38: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 38/45

Syntax:

Void *operator new(size_t size)

{

/*performs the allocation operation.

Constructor is called automatically.returns pointer to memory*/ }

size_t - It is a defined type capable of containing the largest single

piece of memory that can be allocated.

Size - It contains the number of bytes needed to hold the object

being allocated.

When we allocate an object using new, the object’s constructor is

automatically called.

7.Explain overloading the delete operator.Syntax:

Void operator delete(void *p)

{

/*free memory pointed to by p.

destructor is called automatically.*/

}

The delete function receives a pointer to the region of memory to be

freed. It then releases the previously allocated memory back to the

system. When an object is deleted, its destructor is automatically called.

8. What is constructor overloading?

When there is more than one constructor in a class with different

parameters and with different data types we say that the constructor is

overloaded.

9. What is constructor?A constructor is a special member function whose task is to

initialize the objects of its class. It is special because its name is the

same as the class name. The constructor is invoked whenever an

object of its associatied class is created.

It is called constructor because it construct the values of data

members of the class.

10. How to declare and define constructor?

A constructor is declared and defined as 

Page 39: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 39/45

Class integer

{

int m,n;

public:

integer(void); //constructor declared……….

………..

};

integer : : integer(void)//constructor defined

{

m=0;

n=0;

}

 

11. what are properties of the constructor function?

The constructor functions have some properties:

• they should be declared in the public section.

•  They are invoked automatically when the objects are created.

•   They do not have return types,not even void and

therefore,they cannot return values.

•  They cannot be inherited, through a derived class can call the

base class constructor.

• Like other c++ functions,they can have default arguments.

• Constructors cannot be virtual.

• We cannot refer to their addresses.

• An object with a constructor cannot be used as a member of 

a union.

•  They make implicit calls to the operators new and delete

when memory allocation is required.

When a constructor is declared for a class, initialization of theclass objects becomes mandatory.

12. what are characteristics of the constructor function?

 The constructor functions have some characteristics:

• they should be declared in the public section.

•  They are invoked automatically when the objects are created.

•   They do not have return types,not even void and

therefore,they cannot return values.

Page 40: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 40/45

•  They cannot be inherited, through a derived class can call the

base class constructor.

• Like other c++ functions,they can have default arguments.

• Constructors cannot be virtual.

• We cannot refer to their addresses.

• An object with a constructor cannot be used as a member of 

a union.

•  They make implicit calls to the operators new and delete

when memory allocation is required.

When a constructor is declared for a class, initialization of the class

objects becomes mandatory.

13. list types of constructor?

5 types of constructor

a. default constructor(or)empty constructor

b. default argument constructor

c. constructor polymorphism(or)multiple

constructor(or)constructor overloading

d. copy constructor

e. parameterized constructor.

14.what is default constructor.give example?

A constructor that accepts no parameters is called the default

constructor. The default constructor for class A is A::A( ). If no such

constructor is defined, then the compiler supplies a default

constructor. Therefore a statement such as

A a;

Invokes the default constructor of the compiler to create the

object a.

15.what is parameterized constructors,give example?

It may be necessary to initialize the various data elements of 

different objects with different values when they are created.

C++ permits us to achieve this objective by passing

argument to the constructor function when the objects are created.

 The constructors that can take arguments are called parameterized

constructors.

 

Eg:

Class A

{

Page 41: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 41/45

int m,n;

public:

A(int x,int y); //parameterized constructor

………..

………..};

A : : A(int x,int y)

{

m=x;

n=y;

}

16.what are the two ways to pass values as arguments to the

constructor?

 Two ways to pass values as arguments to the constructorfunction when an object is declared.

By calling the constructor explicitly.

By calling the constructor implicitly.

Eg :

Explicit call:

Integer int1=integer(0,200);

Implicit call:Integer int1(0,100);

17.How to pass values as arguments to the constructor when an object is

declared?

 To pass values as arguments to the

constructor function when an object is declared. This done in two ways.

1. By calling the constructor explicitly.2. By calling the constructor implicitly.

Eg :

Explicit call:

Integer int1=integer(0,200);

Implicit call:

Integer int1(0,100);

18.which method is called as shorthand method? To pass values as arguments to the

Page 42: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 42/45

constructor function when an object is declared.

By calling the constructor implicitly

Eg:

Implicit call:

Integer int1(0,100); This method is called as shorthand method,is used very often

as it is shorter,looks better and is easy to implement.

19.what is default argument constructor?

It is possible to define constructors with default arguments . forexample,the constructor

complex( ) can be declared as follows:

 

Complex(float real,float imag=0)

The default value of the argument imag is zero.then the statement

Complex c(5.0);

Assigns the value 5.0 to the real variable and 0.0 to imag (by default).

However, the statement

Complex c(2.0,3.0);Assigns 2.0 to real and 3.0 to imag. The actual parameter, when

specified, overrides the default value. As pointed out earlier, the missing

arguments must be the trailing ones.

20.Distinguish between default constructor and default argument

constructor?

Default constructor 

Default argument construtor

1. the constructor that accepts

no parameter is called is default

constructor.

2.it is used in class by

A :: A( )

1.the default argument

constructor can be called

with either one argument or

no arguments.

2.it is used in class by

A:: A(int =0)

 21.Discuss on copy constructor?

Page 43: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 43/45

Copy constructor is used to declare and initialize an object from

another object.

For example, the statement

Integer I2(I1);Would define the object I2 and at the same time initialize it to the

values of I1. another form of this statement is integer I2=I1;

 The process of initializing through a copy constructor is known as copy

initialization. A copy constructor takes a reference to an object of the

same class as itself as an argument.

22.What is dynamic constructor?

The constructor can also be used to allocate memory while

creating objects.

 This will enable the system to allocate the right amount of memory for

each object when the objects are not of the same size,thus resulting in

the saving of memory;. Allocation of memory to object at the time of 

their construction is known as dynamic construction of objects. The

memory is allocated with the helop of the new operator. The useo of 

new in construcors that are used to construct strings in objects.

23. Discuss on destructors?

A destrucor, as the name implies, is used to destroy the objects

that have been created by a constructor. Like a constructor, the

destructor is a member function whose name is the same as the class

name but is preceded by a tilde. For example, the destructor for the

class integer can be defined

~integer(){}

A destructor never takes any argument nor does it return any value. It

will be invoked implicity by the compiler upon exit from the program to

clean up storage that is no longer accessible. It is a good practice to

declare destrucors in program since it releases memory space for

future use.

Page 44: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 44/45

24.Difference between constructor and destructor?

constructor destructor1.Constructor is a ‘special’ member

function whose task is to initialize

the objects of its class.

2. it construct the values of data

members of the class

1.destructor is a member function

whose name is the same as the

class name but is preceded by a

tilde.

2. it destroy the objects that have

been created by a constructor.

25.How to use delete operator in destructor?

Whenever new is used to allocate memory in the constructors.weshould use delete to free that memory. For example , the destructor for

the matrix class discussed above may; be defined as follows

 

Matrix :: ~matrix( )

{

for(int i=0;i<d1;i++)

delete p[i];

delete p;

}

26.what is multiple constructor?

 The sharing the same name by wo or more functions is referred to as

function overloading. Similarly when more than one constructor function is

defined in a class. We say that the constructor is overloaded. It is also

called multiple constructor.

Example :

Complex() { } //constructor with no arg.

Complex (float a) { x=y=a;} //constructor with one arg.Complex (float real, float imag) //constructor with two arg.

27.What is overloading constructor?

 The sharing the same name by wo or more functions is referred to as

function overloading. Similarly when more than one constructor function is

defined in a class. We say that the constructor is overloaded. It is also

called multiple constructor.

Example :

Page 45: c Plus Plus Two Marks Part i

8/8/2019 c Plus Plus Two Marks Part i

http://slidepdf.com/reader/full/c-plus-plus-two-marks-part-i 45/45

Complex() { } //constructor with no arg.

Complex (float a) { x=y=a;} //constructor with one arg.

Complex (float real, float imag) //constructor with two arg.

28.How to initialize object dynamically?

Class objects can be initialized dynamically too. That is, the initial

value of an object may; be provided during run time.

Advantages:

We can provide various initialization formats, using overloaded

constructor. This provides the flexibility of using different format of data at

run time depending upon the situation.