You are on page 1of 26
Frequently Asked Questions v 4 3 4g +3 What is Object-Oriented Programming (OOP)? Object oriented programming is a modeV/paradigm of programming language which focuses on the use of classes and objects for problem solution. It is more concemed with data and less concemed with logic. It uses bottom-up approache in program design. What is Procedure-Oriented Programming (POP)? Procedure-Oriented Programming is a standard programming approach, used in traditional computer languages such C, Pascal, FORTRAN and BASIC. A computer program in procedural language can be thought as a set of tasks and sub- tasks. Any complex task in this programming language is broken down into a set of self contained, sufficiently small and easily understandable components (i.e. functions). This programming paradigm uses top-down approach in program design. What are main differences between OOP and POP? ‘See chapter one. Why C++ is called object-oriented programming? C++ is called object-oriented programming because it views a problem in terms of objects involved rather than the procedure for doing. Again, C++ treats data as a critical element during the program development and does not allow the data to flow freely across the program. In C++, data can be accessed only through objects. This supports characteristics of object oriented programming like polymorphism, inheritance, data abstraction and encapsulation. So, it is called object-oriented language. What are differences between C and C++? SNL} c CH 1 |ttis structured programming language | It is object oriented programming language 2 | The complex/large program is divided | The complex/large program is divided into functions/procedures into classes and their objects. 3 It can’t hide code from outside world | It can hide member variables or functions by private or protected keyword 4 | C language does not have ‘C++ has function and operator polymorphism features. overloading/polymorphism features, 5__| Itdoes not support inheritance It supports inheritance ‘Scanned with CamScanner 10) il) what is encapsulation? ww, Encapsulation is the process of combit called class. Using the method of encap: access the data from outside the class. Dat member functions. Data encapsulation provides the concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The programmers have to write specialized function. or methods for performing the operations on hidden members of the class due tdithe concept of restricted access. Thus, it is a way of implementing data abstractiogpand it hides the details of the implementation of an object. What is a class? Class is a common name used to represent objedls of similar characteristics. Actually, a class is a user-defined data type which ha§ a set of states (data) and a set of operations for changing the state (methods). Its also called blue print or template from which we can define objects of similar characteristics. What is an object? An object is an identifiable entity with some charactet Object is an instance of a class, Creating an object of a variable of a data type. What is data abstraction ? Abstraction or Data hiding refers to the act of representing ¢: without including the background details or explanation. A cli members, data and functions which are shared by objects function members of a class can be hidden from outside the cl: private access specifier. What is polymorphism? The word polymorphism is derived from two Greek words pol The poly means many and morphe means forms. Thus, poly the ability to take more than one form i.e. it is mechanism by whi same thing for different purpose. The OOP supports polymorphism through function overloading, operator overloading, function overriding andtemplate. What is inheritance? 4 This is the ability of an existing class to create new classes. the existing class is known as a base class and the newly created classes are derived classes. The derived class inherits all the Zeatures except private ibers from the base class. The use of inheritance provides facility of code reusability and reliability. : How can you write comment in C++? C++ supports two types of comments g a Multi line comment: The text to be written within comment starts with /* and ends with */. : data and functions into a single unit tion, the programmer can’t directly only accessible through the public fics and behavior. An is like defining a tial features only defines common it. The data or ‘using concept of ‘Scanned with CamScanner 338] Textbook of Object Oriented Programming in C++ b. Single line comment: The text to be commented is written after //, It supports only single line. 13) What is the role of define in C++? It is pre-processor directive to define a macro and symbolic constant in a C++ program. For Example : #define PI 3.14 #define AVG(A,B,C) (A+B+C)/3 14) Why the operator >> is called extraction operator? The operator ‘>>* is known as extraction operator as it extracts (i.e. gets) data from input device and stores into a variable 15) How can you read multiple values using single cin object in a single statement? Write syntax. We can read multiple values using single cin object in single statement by cascading the extraction operator as in following syntax. cin>>variablel>>variable2>>variable >variable; 16) How can you define a constant in C++ using const keyword. We can define a constant using const keyword in the same way as we would do with a variable. It syntax is: const data_type constant_name=value; Example:const int size=30; 17) What are the differences between defining constant using #define pre-processor directive and const keyword? SN _| Constant using const Constant using #define 1 | We can define constant of specific type | We can define constant using using const keyword. dde£ ine without specific type. 2 | The constant can be defined in local or | The constant is defined in global scope | global scope. only, 18) What is type conversion in C+¥? ‘When two operands of different data types are encountered in the same expression, the variable of lower data type is automatically converted to the data types of variable with higher data type, and then the expression is evaluated, For example: int a=98; float be5; cout< int main( ) ( enum Shape ( Triangle, <) to modify or ision ()is used to be displayed ides © way for sibility of the list of words, Clrele#20, % Rectangle, é fquare > y ‘ ‘Scanned with CamScanner 340| Textbook of Object Oriented Programming in C++ Shape shl,sh2; shi=Triangle; sh2=Square; cout<<"The value of shl:"<>, cin.get() also leaves behind the delimiter in the buffer. Otherwise, both functions are perfectly identical. 28) What is reference variable? What is its use in C++? A reference variable is just alias (ic. other name) for a variable. It is declared using & operator. Defining a reference variable is a mechanism to define/sct a second name for a variable such that if the value of original variable is changed, che value of the reference variable is also changed. We can use the reference ‘Scanned with CamScanner Appendix [341 variable as function argui function are changed, i - reference variable as functi nt. When the values of reference variables within fect will be in calling program also. The use of gument allows us to dramatically reduce the task. of copying actual arguments tito formal arguments while calling a function that takes place behind the scene. hus, we use reference variable to call function * using “call by reference™pr address” What is function overloading? How.it can be achieved in C++? The function overloading is a procegg of using two or more functions with same name but different in their parameter/atgument lists (i.c. function with same name but different signature). What happens when signature of functiog doesn’t match exactly with any version of overloaded functions in function call? © If no exact match of is found with any version of overloaded functions of function call statement, C+ tries to find a match through promotion of data type using Promotion rule, If there is no possibility of promotion, then it tries to find match through standard conversion using data-t? What are default arguments in C++? _, CH allows a function to assign a parameter Corresponding to that parameter is specified default value is specified in a manner syn initialization. How arguments in function can be made default? The trailing arguments in function can be made defat values for the arguments in function prototypes with fol return_type function_name(......,data_typel= data_type2 =default_value2); = What are the uses of default argument in function? The default arguments are useful in such situations where some arguments have the same value in most of cases and sometimes it may ¢hange. The next use of default argument is to increase the capability of an existing function by adding @ new argument without changing existing function call. Using default arguments means that the existing function call can continue to use the old number of arguments, while new function calls can use more arguments What is inline function? The function, designed to speed up program’s execution is known as inline function. The coding of normal function and inline function is similar except that inline function definition starts with the keyword inline. The distinction between normal functions and inline functions is the different compilation process. When an inline function is called, the compiler will replace the function call with the function code. So in reality, there will be no function call in that place, the code of the function will be appeared there. For inline function, n° a ‘Scanned with CamScanner conversion rule. lefault value when no argument time of function call. The ically similar to a variable by specifying some fixed ring syntax. jefault_valuel, or 342| Textbook of Object Oriented Programming in C++ 35) 36) 37) 38) 39 40) 41) jumping is needed to different addresses such that it doesn’t add extra time for jumping to function definition and returning the result to the calling program. So, the concept of inline function utilizes the advantages of using of a function with no extra overhead execution time in small functions, How does inline function increase execution speed of a program? ‘When an inline function is called, the compiler will replace the function call with the function definition code. Thus, no jumping is needed to different addresses such that it doesn’t add extra tiine for jumping to function definition and returning the result to the calling program. So, the inline function increase execution speed ofa program, Can all functions be made inline? No, only small functions can be defined as inline. The compiler ignores mechanism of inline function if the function definition is too long or too complicated and compiles the function as a normal function. Thus, making a function in] ine is just-a request, not a command, to the compiler. Suppose you have ten lines of coding in a function. Would you treat it as inline function or normal function? Justify your answer with reason. The inline function is suitable just for small function. If the size of the function-is large, the use of inline function increase program’s code size. Thus, we don’t treat such type function as inline. List out advantages of inline functions, The advantages of inline function are: a) It makes a program run faster b) It does not require function calling overhead. ©) _ It saves overhead of variables push/pop on the stack, while function calling. d) It saves overhead of return of result from called function to calling function. When inline function may not work? The inline function may not work for a) Large number of statments b) — Function containing static variables c) Recursive function d) — Function containing a loop, a switch or a goto. What are access specifiers available in C++? There are private, public and protected access specifiers availble in C++ to provide access privileges to members of a class. What is the default access level? The default access level assigned to members of a class is private. Thus, when we don’t use either private or public or protected keyword at the time of definition of members of the class, they are treated as private by default. The private members of a class are accessible only within the class and by friends of the class. What is the difference between a private member and a protected member? ‘Scanned with CamScanner 3 Appendix [343 it can be accessed only from the class for outside the class. The private hiding. The private members can’t members are the class members iey can be inherited by other rembers of the parent class The private members are class member: "where they are defined ile. they are hi members implement the OOP’s concept of be inherited by other classes also. ‘The protect which are same as private with difference is t classes. Thus, the child classes cann’t access privé but they can access protected members of the parent How can you define an object from a class? We can define an object from a class using following sy class_name object_name; Wheat is static member function? The static member function is used to maintain a single various objects of the class. The member function is mi with static keyword. The static member functions following syntax. class_name: :static_member_function y of a class across tatic by preceding be called using What is difference between normal (non-static) member functc member function? SN | static member function Normal (non-static) mem! inction 1 [A static member function can access | A non-static member function can only static member data, static access all of the “above member functions & data and including the static) data functions outside the class member. 2 A static member function can be| A static member function can be called, even when a class is called only after instantiating not instantiated the class as an object 3__| It can’t be declared as virtual It can be declared as virtual 4 It can’t have access to the 'this' | It has access to the ‘this’ poi pointer of the class of the class What is static data member? The static data member of a class is declared by preceding the member variables declaration with the keyword static. Only one copy of static data members exist for all objects of the class and all objects of the class share that single copy — of the variable. Unlike regular data members, individual copies of a static member q variable are not made for each object. a What is ‘this’ pointer? The member functions of every object have access to a pointer named ‘this’, which points to the object itself. When we call a member function, it comes into existence with the values of ‘this’ which stores the address of the object for ‘Scanned with CamScanner 344] Textbook of Object Oriented Programming in C++ which it was called. The ‘this’ pointer is the default hidden and implicit argument of any non-static member function, Using a ‘this? pointer, any member function can find out the address of the object of which it is a member. 48) List out the uses of this pointer. The ‘this’ pointer is used to a) To specify memory address of an object b) To access data members ¢) To retum objects 49) Why ‘this’ pointer is not available in static function? This pointer is available to each member function as a hidden implicit argument. Itis the objects address value as a pointer. For static functions, there is no object address at all because static function can be called even before creation of any object using scope resolution operator. Even calling with an object, pointer will not associate any “this” pointer to it. Thus ‘this’ pointer is not available in any static function. Y What is a friend function? A friend function is a non-member function that grants access to class’s private members. In normal case, private data members of a class can’t be accessed from outside the class. However, situations arise where two classes need to share a particular function. For such situations, C+ has facility of friend functions. These are functions that can be made friendly with the both classes, thus allowing these functions to have an access to the private data of these classes. SY What is a friend class? A friend class is a class whose member functions can access another class’ private members. When a class declares another class as its friend, it is giving complete access to all its data and methods including private and protected data and methods to the friend class member methods. Friendship is one way only, which means if A declares B as its friend it does NOT mean that A can access private data of B. It only means that B can access all data of A 52) What are advantages of friend functions and friend classes? When private data members need to be accessed and used by two classes simultaneously, we can define friend function for both classes such that it can access private data members of both classes. Thus, the advantages of friend function is that single function can access private data members of multiple classes simultaneously. 53) Which function violates the rules of the object oriented programming concept ? The friend function violates rules of the object oriented programming. The OOPs rule states that we can’t access private members of a class from outside the class. But, the friend function of a class can access private members of the class easily even the function is not member function of the class. So, it violates the rules of oop. ‘Scanned with CamScanner eS —~ A destructor can never be overloaded. An overloaded destructor 9) Appendix. [345 iat do you mean by containership ntainership is technique in which o member data. The class which contain: fivate members of the contained class. and the contained object is "has-a" instes What kind of relationship does containersi relationship? The containership possesses has-a relationship. What is constructor? What are its features? A constructor is a special type of member functi characteristics. a) It has same name as the name of the class. b) It has no return type. ¢) _ Itis defined in public visibility mode. d) It is automatically called & executed when an obj What does a destructor do? A destructor is used to destroy the objects that have been when they are no longer required. A destructor de-initi allocates all allocated resources. When an object is dest automatically called. Destructors are mainly useful for doing destructor function has same name as that of a construct preceded by a tilde (‘~’) sign. Can destructor be overloaded? lasses? class contains object of another class as ¢ object can’t acess any protected or \¢ relationship between the container f"is-a" jossess "is-a" or "has-a" yf a class with the following is declared/created. ted by a constructor, an object and de- its destructor is clean up job. The but the name is the destructor should have arguments. Since a destructor does not it can never be overloaded. What do you mean by constructor overloading? Overloading constructors is a process of defining more than one constfuctor for a class, each taking a different set of parameters. Different construct allow creation of objects with desired type of values. In overloaded constrtictor, there are more than one constructor having same name but with diffe jignature. ‘When an object of a class is created, the correct version of the ct called on the basis of supplied arguments. What is copy constructor? A copy constructor is used to copy an object from an existing object. constructor takes one argument or parameter which is the reference to an ok the same class. The default copy constructor copies each data member from the object passed as a parameter to the data member of the new object. = What is operator overloading? ‘The use of an operator for different purposes in different data types is kngm as operator overloading. Most C++ operators can be redefined for user-defined data ‘Scanned with CamScanner 346] Textbook of Object Oriented Programming in C++ 62) 63) 64) 65) 66) 67) 68) 69) types. The process of redefining the existing operator to use for user-defined data type is called operator overloading, Why operator is overloaded? C++ operators are overloaded to make the user-defined data types behave in much the same way as the built in types for the operators. In general, C++ operators are used to act/operate on basic data types like int, float, long , double ete. To use them for user-defined data types like objects of a class, we have to overload the operator. Thus, the operators are overloaded to redefine their meaning to operate on different data types. How many arguments are there in operator function defined as friend function for unary operators? The operator function defined as friend function has one argument. How many arguments are there in operator function defined as member function for unary operators? The operator function defined as member function for unary operator has no argument. What is operator function? How is it defined? An operator overloading is defined using a special function, called operator function, which describes the task to be done by the operator. It is defined with following syntax: return_type operator existing_operator (arguments) ; What are the operators that can’t be overloaded? The operators: sizeOf , : : (scope resolution), ?: (conditional) and . *(pointer to member) can’t be overloaded What are the operators that can’t be overloaded using friend function? Friend function can’t be used to overload operators: =, (), [J and ->. How operator function is declared for increment operator in postfix notation? The operator function declared for increment operator in postfix notation is similar to that of prefix, except that, by convention, it takes a single argument of type int in declaration This int is not really an argument, but a signal to the compiler to inform about postfix notation. How can you convert a class type to a user-defined data type? We can define an overloaded casting operator that can be used to convert a class data type into a basic data type. The general form of an overloaded casting operator function is shown as follow. operator type_name() ‘Scanned with CamScanner Appendix [347 How can you convert a basic type to cl ‘A one-argument constructor can be used data type (i.c. class type), How can you convert an object of a class ii The conversion from object of one class t type? convert basic data type to user defined 1n object of another class? bject of another class can be uctor or a conversion the source class or the jon routine, it will use jion class handles the jor that takes one performed using either a one-argument com! function depending on whether it is specifie destination class. If source class handles the conve conversion function. On the other hand, if de: conversion activity, it will convert through constru argument of type source class. Write syntax to define a child class from an existing class. The syntax of deriving a new class from an already existing cl by: class Child_Class : visibility_mode Base_Cla: { body of child class } What is meant by reusability? Reusability is a feature which is supported by object-oriented programming. allows the use of existing classes (i.c. already written, compiled and tested) Program without redefining again. How does inheritance support reusability of code? Tnheritance enables us to derive a class from an existing class which is previo written, compiled and tested code. The base class is defined and once it compiled, debugged and tested once. Using the concept of inheritance, programmer can create as many derived classes from the ‘base class as ne while adding specific features to each derived class as needed. Thus, inheritance supports reusuability. & is given What are super-class and sub-class? The class which inherits features from other class is known as sub-class. It is also known as child or derived class. Again, the class which in inherited into child classes is known as super class. What happens when a class is inherited in private mode? When a base class is inherited by a derived class in private mode, public and protected members of base class become private members for derived class and the private members of the base class are not inherited in derived class. ‘Scanned with CamScanner 348] Textbook of Object Oriented Programming in C+ 77) Howcan you derive a class from a base class in public mode? Explain with syntax. We can derive a class from a base class in public mode as follows class Derived_Class : public Base_Class ( Specific code for class Derived_Class ie 78) What are the different forms of inheritance in C++ ? ‘There are various types of inheritance, They are a) Single Inheritance b) Multilevel Inheritance ©) Hierarchical Inheritance 4) Multiple Inheritance ©) Hybrid Inheritance 79) Write syntax for defining a child class from two bases classes. We can define a child class from two base classes ( i.e. multiple inheritance) with following syntax: class Child_Class: visibility_mode Base_Class1, visibility_mode Base_Class2 t iF 80) How can a private member be made inheritable? A private member can be made inheritable by declaring the data members as protected. When declared as protected, the data members can be inherited in child classes. 81) What is function overriding? Function Overriding is use of two or more functions having same name with same signature but defined one in base class and other in derived class. 82) What is the difference between function overloading and function overriding? The function overloading uses two or more functions with same name but different signature. But function overriding uses two or more function with same name and same signature, one defined in child class and other in base class. < SH How can programmer resolve ambiguity problem in function overriding? When a function with same name and same signature is defined in base as well as derived class, the base class version of the function is overridden by derived class version. When the function is called using object of derived class, the function of derived class version will be called by default. To call base class version using ‘Scanned with CamScanner 84) | 85) 87) Appendix [349 object of derived class, we use scop@esolution operator using following syntax. derived_class_object..baseljglass_name: ; function_name() ; ‘Thus, scope resolution operator Ives ambiguity problem in function overriding. Is it possible to point derived classes’ o A base class pointer can point to object! For example, if B is a base class and D i declared as a pointer to B can also be a pointe ts by a base class pointer? longing to different derived classes. rived class from B, then a pointer Dii.e. B *bptr; D di,d2; bptr=&d1; //This is valid bptr=&d2; //This is valid How can you access the members of derived classes When a base class pointer points derived classe’s obj members, we can access only those members which ar and not the members that originally belong to derived cl permits a base pointer to point to any object derived pointer can’t be directly used to access all the members access original members of the derived class using base ck cast base class pointer to derived class pointer. ((Derived_Class *)Base_Class_Pointer) -> Is it possible to have parameterized constructor in base class class? No, it is impossible in C++. It is mandatory to have parameteri: 1g base class pointer? and it is used to access ited from base class . Thus, although C++ that base class, the the derived class. To pointer, we have to class (); not in derived mmber_derivé constructor in derived class, either it has data members to be initialized or if there is parameterized constructor in base class. The constructor in derived class must supply initial data to base class constructor. When an object of ved class is created, we must supply the values for derived as well as base class./A base-class initializer can be provided in the derived class constructor to base class constructor explicitly as following syntax, % Derived_Class_Constructor (Argument1, Argument2..—, ArgumentN) : Base_Class_Constructor(Argumentl, Argument? { /fpody of derived constructor } . What is the order of execution of constructors in base class and derived class when an object of derived class is created? When a derived class object is created, we supply data values that are required by all classes (i.e. base class and derived class) together. The constructor of the ‘Scanned with CamScanner 350] Textbook of Object Oriented Programming in C++ 88) 89) 90) derived class receives entire list of values as its arguments and passes some of them on to base constructors in the order in which they are declared in derived class and the remaining values are used to initialize data members of derived 5 if required. Thus, the base class constructor is executed first and then derived constructor. What is the order of execution of destructors in base class and derived class when an object of derived class is created? When an object expires, the program first calls destructor in derived class, if any and then destructor in base class. What is virtual pointer or ptr and vtable? C++ compiler creates a hidden class member called virtual-pointer or in short vptr when there are one or more virtual functions. This vptr is a pointer that points to a table of function pointers. This table is also created by compiler and called virtual function table or vtable. Each row of the vtable is a function pointer pointing to a corresponding virtual function. To accomplish late binding, the compiler creates this vtable table for each class that contains virtual functions and for the class derived from it. The compiler places the addresses of the virtual functions for that particular class in ‘vtable’. When virtual function call is made through a base-class pointer, the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the right function and causing late/dynamic binding to take place. class Base £ functl (void) funct2 (void) ; ae b.vptr = address of b.vtable b.vtable[0]= &base: :funct1() b.vtable[il= &base::funct2() SR ‘vPTR _‘\VTABLE [FUNCTION b.vptr ->\Vtable(0] -> (base: : funct1() \Weable(1] -> [base: :funct2() What is early binding and late binding? When a non virtual class member function is called, compiler places the code to call the function by symbol name. Thus the function call jumps to a location that is decided by compile time or link time. This type of linking process is known as early binding or linking. ‘Scanned with CamScanner Appendix [357 When virtual function is called through a base-class inserts code to fetch the VPTR and look up the functio thus calling the right function and this is called late/dynat What is virtual function? ‘The function which precedes a keyword virtual in its de! virtual function. When a function is declared as virtual, C++ function to call at run time based on the type of object pointed the base pointer, rather than the type of the pointer. What is a pure virtual function? A virtual function, equated to zero is called a pure virtual function. It is a fion declared in a base class that has no definition relative to the base class. 93) +What is virtual base class? ‘When we derive a class 'GrandChild' from ‘Parenti’ and 'Paren| which in tum have been derived from a base class ‘GrandFather’, i member function of 'GrandChild' wants to access data or function in the class, it hits a deadlock. Since ‘Parent1' and 'Parent2° are both derived! from 'GrandFather', each inherits a copy of 'GrandFather' and each copy, called sub-object, contains its own copy of 'GrandFather's data. Now when ‘GrandChild’ refers to the data in the 'GrandFather'' class, it is unclear to the compiler which copy to access and hence reports an error. To get tid of this situation, we make 'Parent1' and 'Parent2' as virtual base classes. Using keyword virtual in the two base classes causes them to share the same sub-object of the base class and then 'Parent1' and 'Parent2' are known as virtual base classes. 4) _ The constructors cannot be declared as virtual, but destructors can be declared “as virtual. Why? The virtual keyword is used in C++ when we need dynamic behavior. It will work only when object exists where constructors are used to create objects. Constructors will be called at the time of object creation. So if we define the constructor as virtual, it should have existing object to use. But the object is created at the time of constructor call. Thus, this is contradictary . Again, we can't override a constructor. When any method is declared as virtual, it should be overridden in its derived class. Since constructor is used for initializing variables of a class and it can be defined only within the class, it can’t be overridden. Thus, the constructor can not be declared as virtual. We can use virtual destructors because, at the time of calling destructor, we have the objects. So we can use virtual keyword for the destructors. 95) What is an abstract class? Abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class. It is a design concept in program development and provides a base upon which other classes may be built. er, the compiler quietly ldress in the VIABLE, ‘Scanned with CamScanner 352| Textbook of Object Oriented Programming in C++ 96) When it is impossible to create the object of a class? Itis impossible to create an object of an abstract class, 97) What are stream in C++? A stream is a sequence of byte. In other word, it can be defined as the Now of data from program to device or device to program. There are two types of streams: input streamand output stream. The input stream is flow of data bytes from a device (e.g. keyboard, disk drive, or a network connection etc) to main memory (i.e. program). Similarly, the output stream is flow of data bytes from main memory (i.e. program) to a device (c.g. display screen, printer, disk drive, or a network connection etc). 98) List the predefined console stream objects. The pre-defined console stream objects are a) cin-standard input b) cout — standard output c) — cerr—standard error output 4) clog- fully buffered version of cerr 99) What is the function of ios class? The ios class provides operations common to both input and output. It has constants and member functions that are useful in handling formatted 1/0 operations in C+. The following are the derived classes of ios class a) — istream— input stream b) ostream — output stream c) iostream — input-output stream 100) What is the flag value and bit field for (a) Left justified output and (b) Decimal conversion? (a) Left justified output: flag value is ios::left and bit field is ios: :adjustfield (0) Decimal conversion: ios: :basefield 101) What is user-defined manipulator? The customized manipulators designed to control the appearance of the output is referred as user-defined/custom manipulator. flag value is ios::dec and bit field is Syntax: ostream & manipulator_name(ostream & output, arguments_if_any) { //manipulator code return output ) 102) What is a file? A file is a collection of related data stored in a particular location on the disk. A file has specific extension like .doc, .ppt, .xls, .txt, .dat etc. ‘Scanned with CamScanner Appendix [353 ay) What are two methods of file opening? * jfile can be opened using, a) member function open () of file stream classes 6) constructor of the file-related stream class 4) List out any five file opening modes with their purpose. ‘there are following file opening modes, a) ios::in Opens file for input operations (i.e. input mode) p) ios::out Opens file for output operations (i.e. output mode) c) ios::app Opens file in append mode. All output operations are performed at the file, appending the content to the current content of the file. This used to open for output-only operations d) dios::binary Opens file in binary mode e) ios::ate Opens a file and set initial position of file pointer at end of the file. If this flag is not set to any value, the initial position is the beginning of the file What are the differences between between ios::out and ios::app? The ios: :out is the default mode of ofstream. With this mode, if the file does not exist, it is created but if the file exists then its existing contents get deleted. The ios: :app is output mode of ofstream. With this mode, if the file does not exist, it is created but if the file exists then its existing contents are retained and new information is appended to it. What are file pointers in C++? a The file pointers available in C++ to set file pointer or cursor in particular location and retrieves are a) seekg() —moves get file pointer to a specific location b) — seekp() —moves put file pointer to a specific location ¢) tel1g() —retums the current position of the get pointer d) — te11p() —returs the current position of the put pointer What is the use of seekg() function? Explain its syntax, The seekg() is a member function of istream class and it moves get pointer to a specified location, The reading operation starts from that location. The particular location is specified by its two arguments. It can be illustrated by its syntax: stream_object .seekg(offset, reference_position) ; ‘Scanned with CamScanner 354] Textbook of Object Oriented Programming in C++ Where of fset specifies the number of bytes the file pointer is to be moved from the location specified by the other argument reference. position. 'The reference_posi tion takes one of following constants defined in 408 class, :beg > beginning of the file :eur ->current position of file ios: rend -> end of the file ios: io’ The ioszbeg , ios:cur and ios:beg are actually built-in constant and we can use 0, 1 and 2 for ios::beg, ios::cur and ios::end respectively. 108) What are the types of file accessing? There are two types of file accessing. They are a) Sequential access This type of file is to be accessed sequentially that is to acces data all the preceding data items have to be read and discarded. b) Random access This type of file allows access to the specific data directly without accessing its preceding data items What are the methods to detect the end of file in C++? We can cither use an object of ifstream class which returns 0 on an end of file or we,can use eof () which is a member function of the ios class. The function eof () retums a non- zero value on reaching the end of the file and zero otherwise $a particular 110) What are the functions to read/write object of a class from/to a file? Whe have two functions: read() and write() . Syntax: infile.read((char *) & V, sizeof (V)) ; //for reading from file outfile.write((char *) & V, sizeof(v)); //for writing to file Here, first argument is address of variable V and the second is length of that variable in bytes. The infile is an object of ifstream and out file is an object of of stream class. 111) What are the functions for reading/writing a character from/to a file? There is a function get()to read a character from a file and put() to write a character to a file. a) get() The get () member function is used to read an alphanumeric characters form a file. Syntax: stream_object.get (character) ‘Scanned with CamScanner 112) 113) Id) 415) 11) a) b) ©) 7) — ae Appondix [355 b) paca ‘The put () member function is used to writ output stream, Syntax: stream_object put (character) ; What is template? ‘The generic family of classes or functions is known as template, we define class or function with general type data me such that we can use these classes or functions for any data type. What are types of templates? ‘There are two types of templates in C++. They are a) Function template b) Class template How can you define a function template? We can define a function template in two steps: a) Defining general data type template b) Defining a function with general data type return_type function_name(arguments_with_template_s ( /Mbody of function with type template_name whenever appropriate ) How can function template overloading be achieved? The overloading of function template can be achieved only with number of parameters but not with types of arguments as it can already handle many data types. How can you define a class template? Write its syntax. We can define class template in following steps: Defining template template Defining class with members of template type class class_name ( haracter to specified file or /Iclass members with data types T whenever appropriate } Defining object class_name object_name What are the advantages of using templates? ‘The advantages of templates are a) — Type safety b) — Reusability ‘Scanned with CamScanner 356| Textbook of Object Oriented Programming in C++ c) Performance 8) What is STL? The C++ Standard Template Library is a powerful set of C++: template classes to provide general-purpose template classes and functions that implement many popular and commonly used algorithms and data structures like vectors, lists, queues, and stacks. 119) What are the components of STL? The components of STL are a) Containers b) Algorithms c) Nerators What is namespace? How can you define it in C+¥? Namespace is the name given to scope or logical grouping of variables, functions, classes in C++. ean be defneds namespace namespace_name { //Seclaration of variables, functions and classes } 121) What is anonymous namespace? The namespace defined without name is known as anonymous or unnamed namespace. The members of such namespaces occupy global scope and are accessible in all scopes following the declaration in the file. 122) How can you access identifiers defined within a namespace in your program? We can access identifiers defined within a namespace in program using a) using keyword b) scope resolution operator 123) How can you use scope resolution operator to access namespace members? We can use namespace identifier with scope resolution operator to qualify a member of a namespace explicitly. Syntax: namespace_name: :member; 124) List out some common types of errors in programming. The common type of errors that may occur in C++ program are a) Compiler/Syntax Error b) Logical Error c) Linker Error d) Runtime Error 125 What is run time error? Run time error is such type of error which occurs during execution of the program. The program having run time error is compiled successfully but error occurs while execution of the program. ‘Scanned with CamScanner : Appendix [357 gy What isan exception? wr © Anexception generally refers to some contradictory or error that is unexpected. In other word, an exception is a arises in a program at run time. It is called exception generates such type of error in abnormal conditions only. 7) What are the keywords in C++ for exception handling? ‘The keywords try, catch and throw are responsible for | What is the use of try block in exception handling? © 4 ey block encloses a block of code for which particular ex analyzed and detected. metry -xpected situation or an normal condition that yiCode to be monitored for exception 19). What is the use of catch block in exception handling? " A program catches/receives an exception with an exception handler at place in _ program where we want to handle the problem. catch (Exception_Type Exception_object) { /folock of statements that handles the exception } 130) May multiple catch blocks exist for a single try block? Yes. Multiple catch blocks may be defined to catch different types of errors. catch(typel argument) { //code } catch(type2 argument) { //code } catch(typen argument) { //code } The corresponding catch block receives error on the basis of type of the error thrown in try block. Write syntax for a catch block which can receive all types of error thrown in try block. We can use following type of catch block to receive all types of error thrown in try block. catch (..) { //statement for processing all exception } ‘Scanned with CamScanner 3298} Textbook of Object Oriented Programming in Crt 132) 133) 134) 135) 136) What are the tasks to be performed by error handling code? The tasks to be performed by error handling code are: a) Hit the exception — detect the problem causing exception b) Throw the exception — inform that an error has occurred ¢) — Catch the exception ~ receive the error information a) Handle the exceptions — take corrective actions List the functions for handling uncaught exceptions, The functions for handling uncaught exceptions are: a) terminate() ~ it is invoked when an exception is raised and the handler is not found, b) set_terminate() . — allows the user to install a function that defines the program’s ‘be taken to terminate the program when a handler for the exception ca be found ito c) unexpected() ~ this function is called when a function throws an exception not listed exception specification d) set_unexpected() ~ it allows the user to install a function that defines the program’s actions tg be taken when a function throws an exception not listed in its exception specification actions tg inits What is OOAD? Object-Oriented Analysis and Design (OOAD) is a software engineering approach that models and designs a system as a group of interacting objects. In process of OOAD, we analyze cach and every objects, their states or attributes and operations and design the system to show cach and every entity of the system. OOAD consists following two parts: a) Object Oriented Analysis (OOA) b) Object Oriented Design (OOD) What is CRC card? The CRC card is used to represent the responsibilities (i.c. knowing and doing activities) of a class and the interaction with other classes, It’s format is: class_name Responsibility Collaborator What is UML? ‘Scanned with CamScanner Appendix [359 ‘The Unified Modeling Language (UML) visualizing, constructing, and documenting # f UML is a standard widely adopted graphical ie of software systems with a focus on conceptual | 137) List out different types of UML diagram. ; ‘The standard UML diagram are: standard language for specifying, nifacts of software systems. The wage that describes the artifacts physical representations. é i, Class Diagram i Diagram Use Case Diagram Component Diagram Deployment Diagram Sequence Diagram Collaboration Diagram . State Diagram ix. Activity Diagram 138) What is class diagram? Write notation of a class in class diagram! ‘The class diagram is a static diagram. It represents the static view o} class is represented by a rectangular box with three compart name at the top, attributes in the middle and operations or] the bottom compartment. Class Name Attributes: Methods, 139) What do you mean by "is-a" and "has-a" class relationships? The generalization or inheritance implies an “is-a" relationship. One class is derived from another, the base class. Generalization is implemented as inheritance in C++. The derived class has more specialization. It may _ cither override the methods of the base, or add new methods. > Similiarly, the aggregation implies a "has-a" relationship. One class is o: constructed from other classes, that is, it contains objects of any component classes. For example, a car class would contain objects such as tires, doors, engine, and seats. 140) What is object diagram? Object diagram represents an instance of a class diagram. It also represents the static view of a system as class diagram but this static view is a snapshot of the ‘Scanned with CamScanner 360] Textbook of Object Oriented Programming in CH system at a particular moment. Thus, the value of data members of the class particular moment is shown in object diagram. What is use case diagram? What are its components? The use case diagram describes what a system does from the view of ah external observer. The use case is made up of a set of scenarios, Each Scenarig is a sequence of steps that encompass an interaction between a user and a system, 141) 142) a) System b) Use cases c) Actors d) _ Relationships What is the objective of drawing use case diagram? The objectives of drawing use case diagram is to model/show the system from the point of view of how the users or other systems interact with this system, when trying to achieve its objectives. ‘Scanned with CamScanner Appendix [361 features. It is menu driven program which allows us to add, @ search record of an employee working in an organization. It sto name, post, department and salary of employee. Initially, it has’ we have to add employee records choosing appropriate option in tl After successful entry of records of some employee, we can searcher the basis of ID or department as our choice. We can search particula from ID choosing option#2 and if we want to see all employee wo department, we have to choose another option ( i.c. option#3 in menu)ay list all employee records also using option#4 in menu. Again, we can up delete existing record. Thus, this project is useful to manage employee an organization. . #include #include #include #include #include #include #include const: char* fileName="Employee. txt"; class Employee { private: int EmpID; char EmpName[50] , Post (50) ,Department [10] : float Salary; public: void ReadData() ; int GetID(); void DisplayRecord() ; char* GetDepartment () ; 8 ye 3 | ‘Scanned with CamScanner

You might also like