Prepared by : Roy Antony Arnold G/CSE Page 1 INFANT JESUS COLLEGE OF ENGINEERING KAMARAJAR NAGAR, KEELAVALLANADU – 628 851 TWO MARKS SUBJECT : OBJECT ORIENTED PROGRAMMING SEMESER: III SUB CODE : CS35 SECTION: A STAFF NAME : G. ROY ANTONY ARNOLD DEPT : CSE UNIT I 1. List out the characteristics of FOP. 1. Large programs are divided into smaller programs known as functions. 2. Most of the functions share global data 3. Functions transform data from one form to another. 4. It employs top-down approach. 2. List out the characteristics/features of OOP. 1. Emphasis is on data rather than on procedure 2. Programs are divided into objects. 3. Data is hidden. 4. Objects communicate with each other, by sending messages and receiving responses. 5. It follows bottom-up approach. 3. List down the basic concepts of OOP. 1. Objects 2. Classes 3. Data abstraction and encapsulation 4. Inheritance 5. Polymorphism 6. Dynamic binding 7. Message passing 4. Define an object. Objects are the basic run time entities in an object oriented system. They may represent a person, a place or any item that a program has to handle. Each object has the data and code to manipulate the data and theses objects interact with each other. 5. Define Object Oriented Programming. OOP is a method of implementation in which programs are organized as cooperrativ collection of objects, each of which represents an instance of some class and whose classes are all members of a hierarchy of classes united through the property called Inheritance. 6. Define a class. A class is a collection of objects with similar attributes and operations. Eg. Class -Account It will create an object savings_account belonging to the class Account. 7. Define Encapsulation.Prepared by : Roy Antony Arnold G/CSE Page 2 The wrapping up of data and functions into a single unit is known as encapsulation. The data is kept safe from external interference and misuse. 8. Define Data hiding? The isolation of data from direct access by the program is called as data hiding or information hiding. The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it. 9. Define Abstraction. It refers to the act of representing essential features without including the background details. 10. Define ADT? The classes which are using the concept of data abstraction are known as abstract data types (ADT). 11. Define data member and member function? The attributes which are holding the information is known as data members. The functions that operate on data member are sometimes called as member function. 12. Define Inheritance? Inheritance is the process by which objects of one class acquire the properties of objects of another class. The new derived class inherits the members of the base class and also adds its own. 13. Define Polymorphism? It allows a single name/operator to be associated with different operations depending on the type of data passed to it. An operation may exhibit different behaviors in different instances. 14. Define dynamic binding? Dynamic binding means that the code associated with the given procedure call is not known until the time of call at runtime. 15. What is message passing? It is the process of invoking an operation on an object. In response to a message, the corresponding function is executed in the object. 16. List down the benefits of OOP? 1. The principle of data hiding helps to build secure programs. 2. Through inheritance, redundant code is eliminated. 3. Software complexity can be easily managed. 17. List down the applications of OOP. 1. Real time system 2. Simulation and modeling 3. AI and expert system 4. Neural network programming. 5. CAD/CAM systems. 18. What is Implicit Type Conversion? When an expression consists of data items of different types, the compiler performs type conversions automatically. This is referred as Implicit Type Conversion. 19. What are the differences between ‘break’ and ‘continue’ statement. Break continue Break statement takes the control to the Continue statement takes the control to thePrepared by : Roy Antony Arnold G/CSE Page 3 outside of loop beginning of loop It is used in loop and also in switch statement It can be used only in loop statement. 20. Distinguish ‘while’ and ‘do – while’ statements. While do while This is the top tested loop This is the bottom tested loop Loop is not executed if the condition is false Loop is executed at least once even though the condition is false. 21. Give the syntax of Array Initialization with an example. Data-type array-name[size] = {list of values separated by comma}; (eg.) int mark[5] = {96,45,66,74,82}; 22. Give four examples for String Manipulation functions. strlen( ) – finds the length of string strcpy( ) -copies the contents of one string to another strcat( ) – concatenates two strings into one single string strupr( ) – converts a lower case string to upper case 23. List the different types of parameter passing techniques. (i) Pass by value (ii) Pass by Address (iii) Pass by Reference 24. What is Dynamic memory allocation? Allocation of memory space for any data structure during the course of the program execution is called as Dynamic memory allocation. 25. How memory management is performed dynamically in C++? Two operators are available for dynamic memory management. (i) new – for dynamic memory allocation (ii) delete -for dynamic memory deallocation 26. Define class? A class is a way to bind data and its associated functions together. It allows the data to be hidden if necessary. 27. What are the parts of class specification? The class specification has two parts 1. Class declaration – To describe the type and scope of its members 2. Class function definition – To describe how the class functions are implemented. 28. Write the syntax of class declaration class classname { Private:Variable declaration; Function declaration; Public: Variable declaration; Function declaration; }; 29. Where will you define a member function? Member functions can be defined in two places. 1. Outside the class definition. 2. Inside the class definition.Prepared by : Roy Antony Arnold G/CSE Page 4 30. Give the syntax for member function definition outside the class. Return type class name:: function name (argument name declaration) { function body } 31. List the characteristics of member function. 1. Member function can access the private data of class. 2. A member function can call another member function directly without using the dot operator. 32. Define nesting of member function. A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions. 33. List the properties of static members. A data member of a class can be qualified as static properties. 1. It is always initialized to zero when the first object of its class is created. 2. It is visible only within the class. 34. Write the properties of static member function. 1. A static function can have access to only other static members declared in the same class. 2. A static member function can be called using the class name (Instead of objects) as follows: class name:: function name; 35. What are the uses of scope resolution operator? Scope resolution operator is used to uncover the hidden variables. It also allows access to global version of variables. Eg: #include int m=10; //global variable m void main ( ) { int m=20; //local variable m cout<<”m=”< : : Eg: Void x : : getdata() 36. What are reference variable? A reference variable provides an alias(alternative name) for a previously defined variable. For example , if make the variable a reference to the variable , then sum and total can be used interchancheably to represent that variable. Syntax : Data-type &reference-name = variable-name Eg: float total = 100; float &sum = total; 37. What is member-dereferencing operator? C++ permits to access the class members through pointers. It provides three pointer-tomemmbe operators for this purpose, : :* To declare a pointer to a member of a class.Prepared by : Roy Antony Arnold G/CSE Page 5 * To access a member using object name and a pointer to the member ->* To access a member using a pointer to the object and a pointer to that member. 38. What is function prototype ? The function prototype describes function interface to the compiler by giving details such as number ,type of arguments and type of return values. Function prototype is a declaration statement in the calling program and is of the following: type function_name(argument list); Eg: float volume(int x,float y); 39. What is an inline function? An inline function is a function that is expanded in line when it is invoked. That is compiler replaces the function call with the corresponding function code. The inline functions are defined as inline function-header { function body } 40. Write some situations where inline expansion may not work • for functions returning values, if loop, a switch, or a goto exists • for functions not returning values ,if a return statement exists • if function contain static variables • if inline functions are recursive 41. What is a default argument? Default arguments assign a default value to the parameter, which does not have matching argument in the function call. Default values are specified when the function is declared. Always default value should be added from right to left. E.g. : float amount(float principle ,int period, float rate=0. 15) Function call is Value=amount (5000, 7); Here it takes principle=5000 & period=7 and default value for rate=0.15 Value=amount (5000, 7, 0.34) Passes an explicit value 0f 0.34 to rate 42. What are constant arguments? Keyword is const. The qualifier const tells the compiler that the function should not modify the argument. The compiler will generate an error when this condition is violated. This type of declaration is significant only when we pass arguments by reference or pointers. eg: int strlen( const char *p); 43. How the objects are used as function argument? This can be done in two ways • A copy of the entire object is passed to the argument • Only address of the objects is transferred to the f unction 44. Define const member function If a member function does not alter any data in the class, then we may declare it as const member function as e.g.: void mul(int ,int)const; 45. Define pointers to member It is possible to take the address of a member of a class and assign it to a pointer. The address of a member can be obtained by applying the operator &to a “fully qualified” class member name. A class member pointer can be declared using the operator::*with the class name. Eg: class A {int m;Prepared by : Roy Antony Arnold G/CSE Page 6 public: void show( ); }; Pointer to member m is defined as int A::*ip=&A::m; A::*->pointer to member of A class &A::m->address of the m member of A class 46. Define local classes. Classes can be defined and used inside a function or a block. such classes are called local classes. It can use global variables and static variables declared inside the function but cannot use automatic local variables. Eg: void test(int a) { ……. class student { ……… }; student s1(a); ……… } UNIT – II 1. What are the characteristics of constructor? 1. Constructors should be declared in public section. 2. They are involved automatically when the objects are created. 3. They do not have return types. 4. They can not be inherited. 2. Define parameterized constructor. Arguments can be passed to the constructor function when the objects are created. The constructors that can take arguments are called as parameterized constructor. Eg: class integer { int m,n; public: integer(int x,int y) { m=x; n=y; } } ; To invoke parameterized constructor we must pass the initial values as arguments to the constructor function when an object is declared. This is done in two ways 1. By calling the constructor explicitly eg: integer int1=integer(10,10); 2. By calling the constructor implicitly eg: Integer int1(10,10); 3. What is an implicit constructor? C++ compiler has an implicit constructor which creates objects even though it was not defined in the class. 4. What is the use of copy constructor? Copy constructor is used to declare and initialize an object from another object. E.g. class_name object2 (object1); Will define the object2 and at the same time initialize it the values of object1.Prepared by : Roy Antony Arnold G/CSE Page 7 Another form of this statement is Eg: class_name object2=object1; The process of initializing through a copy constructor is known as copy initialization. 5. What do you mean by dynamic construction? Allocation of memory to objects at the time of their contraction Is known as dynamic contraction of objects. 6. What is the use of destructor? It is used to destroy the objects that have been created by a constructor. It releases the memory space for future use. 7. What are the characteristics of destructor? 1. A destructor is a member function whose name is the same as the class name but it is preceded by a tilde. 2. It neither takes any argument nor returns any value. 3. It will be invoked implicitly by the compiler to cleanup the storage. 8. Define operator overloading? The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. 9. Define function overloading? Performing different types of task using single function name is referred as function overloading. 10. Define constructor? A constructor is a special member function whose task is to initialize the object of its class. It is called constructor because it constructs the value of data members of the class. 11. Define Virtual function. When the form of a member function is changed at run time, that member function is referred to as virtual function. 12. Define default constructor The constructor with no arguments is called default constructor Eg: class integer { int m,n; public: integer( ); ……. }; integer::integer( )//default constructor { m=0;n=0; }the statement integer a; invokes the default constructor 13. Define default argument constructor The constructor with default arguments are called default argument constructor. Eg:Prepared by : Roy Antony Arnold G/CSE Page 8 complex(float real,float imag=0); The default value of the argument imag is 0 The statement complex a(6.0); assign real=6.0 and imag=0 the statement complex a(2.3,9.0) ; assign real=2.3 and imag=9.0 14. What is the ambiguity between default constructor and default argument constructor? The default argument constructor can be called with either one argument or no arguments. When called with no arguments, it becomes a default constructor. When both these forms are used in a class, it cause ambiguity for a statement such as A a; The ambiguity is whether to call A::A() or A::A(int i=0) 15. Define dynamic constructor Allocation of memory to objects at time of their construction is known as dynamic constructor. The memory is allocated with the help of the NEW operator. Eg: Class string { char *name; int length; public: string( ) { length=0; name=new char[ length +1]; } }; void main( ) { string name1(“Kavitha”), name2(“Kumar”); }16. Define const object We can create constant object by using const keyword before object declaration. Eg: const matrix x(m,n); 17. Define destructor It is used to destroy the objects that have been created by constructor. Destructor name is same as class name preceded by tilde symbol (~) Eg: ~integer() { } A destructor never takes any arguments nor it does it return any value. The compiler upon exit from the program will invoke it. Whenever new operator is used to allocate memory in the constructor, we should use delete to free that memory. 18. Define multiple constructors (constructor overloading). The class that has different types of constructor is called multiple constructors Eg: #include #include class integer { int m,n; public:Prepared by : Roy Antony Arnold G/CSE Page 9 integer( ) //default constructor { m=0; n=0; }integer(int a, int b) //parameterized constructor { m=a; n=b; }integer(integer &i) //copy constructor { m=i. m; n=i.n; } }; void main() { integer i1; //invokes default constructor integer i2(45,67);//invokes parameterized constructor integer i3(i2); //invokes copy constructor } 19. Write some special characteristics of constructor • T hey 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, and they cannot return values • They cannot be inherited, though a derived class can call the base class • They can have default arguments • Constructors cannot be virtual f unction 20. How the objects are initialized dynamically? To call parameterized constructor we should the pass values to the object ie, for the constructor integer(int a, int b) //parameterized constructor { m=a; n=b; } it is invoked by integer a(10,18) ; this value can be get during run time (dynamic initialization). i.e., for above constructor int p,q; cin>>p>>q; integer a(p,q); 21. What is operator overloading? C++ has the ability to provide the operators with a special meaning for a data type. This mechanism of giving such special meanings to an operator is known as Operator overloading. It provides a flexible option for the creation of new definitions for C++ operators. 21. List out the operators that cannot be overloaded. • Class member access operator (. , .*) • Scope resolution operator (::) • Size operator ( sizeof ) • Conditional operator (?:)Prepared by : Roy Antony Arnold G/CSE Page 10 22) What is the purpose of using operator function? Write its syntax. To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done by Operator function, which describes the task. Operator functions are either member functions or friend functions. The general form is return_type classname :: operator op (op-arglist ) { function body } where return type is the type of value returned by specified operation. Op-operator being overloaded. The op is preceded by a keyword operator. operator op is the function name. 23. Write at least four rules for Operator overloading. • Only the existing operators can be overloaded. • The overloaded operator must have at least one operand that is of user defined data type. • The basic meaning of the operator should not be changed. • Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. 24. How will you overload Unary & Binary operator using member functions? When unary operators are overloaded using member functions it takes no explicit arguments and return no explicit values. When binary operators are overloaded using member functions, it takes one explicit argument. Also the left hand side operand must be an object of the relevant class. 25. How will you overload Unary and Binary operator using Friend functions? When unary operators are overloaded using friend function, it takes one reference argument (object of the relevant class) When binary operators are overloaded using friend function, it takes two explicit arguments. 26. How an overloaded operator can be invoked using member functions? In case of Unary operators, overloaded operator can be invoked as op object_name or object_name op In case of binary operators, it would be invoked as Object . operator op(y) where op is the overloaded operator and y is the argument. 27. How an overloaded operator can be invoked using Friend functions? In case of unary operators, overloaded operator can be invoked as Operator op (x); In case of binary operators, overloaded operator can be invoked as Operator op (x , y) 28. List out the operators that cannot be overloaded using Friend function. • Assignment operator = • Function call operator ( ) • Subscripting operator [ ] • Class member access operators 29. What is meant by casting operator and write the general form of overloaded casting operator? A casting operator is a function that satisfies the following conditions • It must be a class member. • It must not specify a return type. • It must not have any arguments. The general form of overloaded casting operator is operator type name ( ) { ……….. //function statementsPrepared by : Roy Antony Arnold G/CSE Page 11 } It is also known as conversion function. 30. Explain basic to class type conversion with an example. Conversion from basic data type to class type can be done in destination class. Using constructors does it. Constructor takes a single argument whose type is to be converted. Eg: Converting int type to class type class time { int hrs, mins; public: time ( int t) //constructor { hours= t/60 ; //t in minutes mins =t % 60; } };Constructor will be called automatically while creating objects so that this conversion is done automatically. 31. Explain class to basic type conversion with an example. Using Type Casting operator, conversion from class to basic type conversion can be done. It is done in the source class itself. Eg: vector : : operator double( ) { double sum=0; for(int I=0;I ReturnType area(Type b, Type h) { return (0.5*b*h); } 22. What is the need for partial specialization? · Partial specialization is needed to support all type of pointers. · The primary specialization fails to handle the string type effectively. 23. Difference between explicit specialization and partial specialization. Explicit Specialization Partial Specialization It is actually an overloaded function or class which we have defined explicitly. It is actually supports to deal with set of types (all of pointers specially) template <> indicates explicit specialization appearing immediately after the class name indicates partial specialization 24. What are templates? A template can be considered as a kind of macro. It is a new concept which enables us to define generic classes and functions and thus provides support for generic programming. When an object of a specific type is defined for actual use, the template function for that class is substituted with the required data type. Templates are sometimes called parameterized classes or constructors. 25. Define generic programming. Generic programming is an approach where generic types are used as parameters in algorithms so that they work for a variety of suitable data types and data structures. 26. What is the object destroy problem? A destructor is needed for an object which is created with dynamic constructor to smoothly return the resources consumed by it and to close connections. The exit or abort functions do not call the destructor of an object so the objects are not destroyed properly. This is called object destroy problem. Exception handling mechanism may be used here to call the destructor automatically. 27. What is the role of terminate() function in exception handling? The catch block calls the built-in function terminate(), which terminates the program by calling abort() function. It is not possible to call the abort() function directly so we need terminate() function. 28. What is the importance of throwing objects rather than built-in type values?Prepared by : Roy Antony Arnold G/CSE Page 16 Objects may have a member function, which tells the user what to do in that case and it helps to provide some alternate solution. With the object it is possible to provide generic error handling logic irrespective of the situation in which error has occurred using the member functions of the object. This is not possible when we use built-in type values for throwing. 29. When do we need multiple catch blocks for a single try block? With the help of multiple catch block, it is possible to catch all type values which are thrown by the throw block. This is very much needed when we want to treat each catch block differently. The appropriate catch block will be invoked if the match is found. 30. What is the importance of catch all? It may not be able to anticipate all possible types of exceptions and therefore may not be able to design independent catch handlers to catch them. In such cases, it is possible to catch all types of exceptions in a single catch section. Syntax: catch(...) { } Whatever thrown by the throw block, this single catch block will catch them but all the exceptions are treated in the same way. 31. What are exception specifications? It is possible to specify what kind of exceptions can be thrown by functions, using a specific syntax. It is known as exception specification. Ex: void area(int a) throw (int, char) { …… } throw(int,char) is known as exception specification. And this allows only char and int to pass through. Other exceptions are not allowed to be thrown outside the function. The exception specification is used for throwing outside the function. It does not have any effect on exceptions thrown in the function or thrown from any other function called from the current function and caught in the current function. 32. What is rethrow()? What is its use? A handler may decide to rethrow the exception caught without processing it. In such cases, the throw function may be invoked without any argument as below: throw; This causes 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. 33. What is unexpected() function? If a function throws an exception which is not allowed, a function unexpected() is called, which in turn calls abort(). 34. What is uncaught_exception() function and why do we need it? It is not possible in C++ to handle two exceptions simultaneously if so, the abort() function is called to terminate the program. In such situations, uncaught_exception() comes handy. It returns true when an exception is thrown. If the destructor checks and finds that an exception has already been thrown when the destructor is called, the destructor will not throw a new one. 35. What are the disadvantages of the exception handling mechanism? a. Uncertain termination is possible if the exception is not handled. b. Executable code size and execution time are increased. c. Reduces the program performance.Prepared by : Roy Antony Arnold G/CSE Page 17 UNIT – IV 1. What is meant by inheritance? Inheritance is the process by which objects of one class acquire the properties of another class. It supports the concept of hierarchical classification. It provides the idea of reusability. We can add additional features to an existing class without modifying it by deriving a new class from it. 2. What is meant by single inheritance? If a single class is derived from a single base class is called single inheritance. Eg: Base class Derived class Here class A is the base class from which the class D is derived. Class D is the public derivation of class B hence it inherits all the public members of B. But D cannot access private members of B. 3. What is multiple inheritance? If a class is derived from more than one base class, it is called multiple inheritance. Eg: Base classes Derived class Here class C is derived from two base classes A & B. 4. What is hierarchical inheritance? If a number of classes are derived from a single base class then it is called hierarchical inheritance. Eg : Hierarchical classification of students in University ABACB 5. What is multilevel inheritance? If a class is derived from a class, which in turn is derived from another class, is called multilevel inheritance. This process can be extended to any number of levels. Eg: Base class Grandfather Intermediate Base class Father Derived class Child 6. What is hybrid inheritance? It is the combination of one or more types of inheritance. Multilevel inheritance Multiple inheritance The class result will have both the multilevel and multiple inheritances. Student Arts Engineering M e d i c a l CSE ECE Civil ABCStudentPrepared by : Roy Antony Arnold G/CSE Page 18 Test Result Sports 7. What is meant by Abstract base class? A class that serves only as a base class from which derived classes are derived. No objects of an abstract base class are created. A base class that contains pure virtual function is an abstract base class. 8. Write short notes on virtual base class. A base class that is qualified as virtual in the inheritance definition. In case of multiple inheritance, if the base class is not virtual the derived class will inherit more than one copy of members of the base class. For a virtual base class only one copy of members will be inherited regardless of number of inheritance paths between base class and derived class. Eg: Processing of students’ results. Assume that class sports derive the roll number from class student. Class test is derived from class Student. Class result is derived from class Test and sports. As a virtual base class 9. What are virtual functions? A function qualified by the ‘virtual’ keyword is called virtual function. When a virtual function is called through a pointer, class of the object pointed to determine which function definition will be used. 10. Write some of the basic rules for virtual functions · Virtual f unctions must be member of some class. · They cannot be static members and they are accessed by using object pointers · Virtual f unction in a base class must be defined. · Prototypes of base class version of a virtual function and all the derived class versions must be identical. · If a virtual function is defined in the base class, it need not be redefined in the derived class. 11. What are pure virtual functions? Write the syntax. A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases, the compiler requires each derived class to either define the function or redeclare it as a pure virtual function. A class containing pure virtual functions cannot be used to declare any object of its own. It is also known as “donothing” function. The “do-nothing” function is defined as follows: virtual void display ( ) =0; 12. What are the advantages of using Inheritance? a. To extend the functionality of an existing class. b. To avoid the problems of inconsistencies between the common attributes of multiple classes. c. To model a real-world hierarchy in the program in a natural way. 13. Differentiate between object based and object oriented programming. Object based programming Object Oriented Programming Programming without using inheritance. Programming using Inheritance. More Performance. Less Performance compared to object based programming 14. What is the difference between public and private inheritance? Public Inheritance Private Inheritance The public members of base class are copied as public in the derived class. The public members of base class are copied as private in the derived class.Prepared by : Roy Antony Arnold G/CSE Page 19 Base class is visible to all. Base class is accessible only through the public methods of the derived class. 15. What are the disadvantages of multiple inheritance? a. Reduces efficiency of the program. b. Code simplicity is completely removed. 16. What are the issues one must consider when dealing with multiple inheritance? a. In multiple inheritance, the derived class methods can’t be accessed from the base class pointer. b. Conversion of derived class object into base class object is not easy. c. Duplication of an attribute is possible when we use multiple along with multilevel inheritance. 17. What is the advantage of access declaration? Using this public access can be provided to some of the base class members when deriving them as private. All other data will remain as private except the redefined data. 18. What are the restrictions of access declaration? a. The access specifier cannot be modified to raise the status of the original access specifier. b. The member with public access specification can be redefined with public or protected access, but the protected member can only be redefined as protected. 19. What is the need of virtual base class? Having two different copies of the same property is a serious problem in some cases. The virtual base class is a class with only one instance inherited when they are inherited from multiple paths. What is polymorphism? Polymorphism means ‘one name, multiple forms’. Polymorphism is the property of the same object to behave differently in different contexts given the same message. Types: (a) Compile-time polymorphism (b) Run-time polymorphism What is dynamic binding? Linking the function at runtime is known as dynamic binding. Virtual functions are dynamically bound. What is the difference between compile time and runtime polymorphism? Compile-time Polymorphism Run-time Polymorphism Achieved using operator overloading and function overloading. Achieved using virtual functions. Here, the object is bound to its function call at compile time. Here, the object is bound to its function call at run time. What is a subobject concept? Whenever a base class is inherited, the derived class contains the base class subobject. The subobjects are usually stored before the actual data members of the respective class. What is virtual function? A function defined with virtual keyword in the base class is known as virtual function. Compiler will decide the exact class pointed to by the base class pointer and call the respective function of that class if the function is defined as virtual.Prepared by : Roy Antony Arnold G/CSE Page 20 Why is a non-virtual member function of a base class always called even when the base class pointer is pointing to the derived class object? The base class pointer can see only the base class subobject (base class members) embedded in the derived class object and not the additional part of the derived class so it will call non-virtual member function of the base class. What is the difference between a normal member function and a virtual function? Non-Virtual Function (Member function) Virtual Function Can be defined in either base class or derived class. Always defined with the keyword ‘virtual’ in base class. Linked at linking time (static binding) Linked at runtime (dynamic binding) Always invoked through the object. Invoked only when the function with the name is not defined in the derived class. Function defined in the derived class can’t be accessed through the base class pointer. Can be accessed and for that only virtual function concept is introduced. What is the problem of default arguments of the virtual function? The function defined in the derived class invoked but always the default arguments of the base class function is used and not the derived class function. What is the requirement of virtual functions in OO programming? Virtual functions are needed a) Need to deal with different types of objects with unknown proportion. b) Need to deal with objects belonging to same generic class but having specific distinct attributes. Why virtual constructors are not possible but virtual destructor is a good idea? Constructing a derived class object needs specific construction of the base class subobject within. Obviously, their constructors cannot be virtual. It is allowed to provide deletion of the entire derived class object while pointed to by a base class pointer. It is a good idea to have virtual destructors to avoid memory leaks. Differentiate between virtual functions and pure virtual functions. Virtual functions Pure virtual functions Function defined with virtual keyword in the base class A virtual function with ‘=0’ in place of body in the class. Must be defined in the base class. Not mandatory. What is the need for statically invoking pure virtual or virtual functions? Virtual functions are statically invoked if we use, Class_name :: ptr -> virtual_function These are used in C++ to manage the error handling mechanisms. What is RTTI? Run Time Type Information is a mechanism by which we can find the type of an object at runtime. Why do we need RTTI? RTTI is needed because, a. When there is no prior knowledge of usage of all the classes in the hierearchy. b. It can be used to find out the type of the element in the body of a template function or class. c. Enables the user to decide about the type of the object at runtime and take decision at that point of time. d. Needed to do specific job for a specific subset of class hierarchy.Prepared by : Roy Antony Arnold G/CSE Page 21 What are polymorphic objects? If we have a class containing virtual function, it is possible to point to a derived class object using the base class pointer and manipulate the object. Such manipulatable objects are known as polymorphic objects. What is typeinfo object? This is an object associated with every built-in and user defined type, and also with polymorphic type. This object describes the type of the object. This object type for polymorphic type is only available when RTTI is enabled. What is the need for type_info object? What is the role of typeid operator in the RTTI? This operator can be applied to any object or class or built-in and user defined type, and also with polymorphic type. This object describes the type of the object. Differentiate typeid and dynamic_cast. What are the problems with the use of typeid mechanism for solving problem? When working with templates do we need to use RTTI? What is const_cast? There are two types of const data. One is real const and another is contextual const. This C++ operator only casts a contextual const pointer to a non-const variable pointer. What is static_cast? It is a normal non-polymorphic cast. Converting int to double, etc., should be done using this cast. It is an ideal replacement for old C style casting methods. If applied to a polymorphic object results in compile time error. What is reinterpret_cast? A casting operator for abnormal casting cases like int to pointer and pointer to int etc. This saves the programmers from typing errors while casting. What is cross casting? When a derived class object of multiple base classes is pointed to by one of its base class pointers, casting from one base class pointer into another base class pointer is known as cross casting. What is down casting? Casting from a base class pointer to a derived class pointer is known as downcasting. What are the issues of using RTTI? e. Not provides backward compatibility with C and Casting. f. Using RTTI is not efficient. It is because type_info object is added to every polymorphic type. The type_info objects are to be constructed and destroyed like other objects and slow down the execution. g. In some cases, better using virtual functions rather than RTTI.Prepared by : Roy Antony Arnold G/CSE Page 22Prepared by : Roy Antony Arnold G/CSE Page 23 UNIT V 1. What are C++ Streams? The C++ language offers a mechanism, which permits the creation of an extensible and consistent input-output system in the form of streams library. It is a collection of classes and objects which can be used to build a powerful system or it can be modified and extended to handle user defined data types 2. List the predefined console streams. a) cin – standard input b) cout – standard output c) cerr – standard error output d) clog – fully buffered version of cerr 3. Give the usage of ios class. The ios class provides operations common to both input and output. It contains a pointer to a buffer object. It has constants and member functions that are useful in handling formatted I/O operations. Following are the derived classes of ios class, a) istream – input stream b) ostream – output stream c) iostream – input-output stream 4. What are the types of formatted console i/o operations? a) ios stream class member functions and class b) standard manipulators c) userdefined manipulators 5. Give the flag value and bit field for (a) Left justified output and (b) Decimal conversion. Flag value Bit field (a) Left justified output ios::left ios::adjustfield (b) Decimal conversion ios::dec ios::basefield 6. What are the types of manipulators? Give example. Two types of manipulators are available in C++. a) Parameterized manipulators Eg: setw(int width) – sets the field width setprecision(int prec) – sets the floating point precision b) Non-parameterized manipulator Eg: dec – sets the conversion base to 10 Endl – outputs a new line and flushes stream 7. What is a custom manipulator? Give its syntax. Designing of customized manipulators to control the appearance of the output is referred as custom manipulator. Syntax: ostream & manipulator(ostream & output, arguments_if_any) { //manipulator code return output; }8. Write a note on File. A file is a collection of related information normally representing programs, both source and object forms and data. Data may be numeric, alphabetic or alphanumeric. A file can also be defined as a sequence of bits, bytes, lines or records whose meaning is defined by the programmer. Operations such as create, open, read, write and close are performed on filesPrepared by : Roy Antony Arnold G/CSE Page 24 9. Define ifstream & fstream. ifstream: It is used for handling input files. It contains open() with default input mode and inherits get(), getline(), read(), seekg(), tellg() functions from istream. fstream:Used for handling files on which both i/o operations can be performed. It supports simultaneous i/o operations. It contains open() with default input mode and inherits all the functions from istream and ostream classes through iostream. 10. Give the prototypes of file stream class constructors. a) ifstream class constructor ifstream(const char *path, int mode=ios::in, int prot=filebuf::openprot); b) ofstream class constructor ofstream(const char *path, int mode=ios::out int prot=filebuf::openprot); c) fstream class constructor fstream(const char *path, int mode=ios::in/ios::out, int prot=filebuf::openprot); 11. List any four file modes and their purpose a) ios::in – open for reading b) ios::ate – seek to the end of file at opening time c) ios::nocreate – open fails if file does not exist d) ios::binary – opens a binary file 12. List the file pointer control functions. a) seekg() – moves get file pointer to a specific location b) seekp() – moves put file pointer to a specific location c) tellg() – returns the current position of the get pointer d) tellp() – returns the current position of the put pointer 13. What are the types of file accessing? a) Sequential access This type of file is to be accessed sequentially that is to access a particular 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 with out accessing its preceding data items 14. Give any two error handling functions and their purpose a) eof() – TRUE(nonzero) if eof encountered while reading; otherwise FALSE(zero) b) rdstate() – returns the status state data member of the class ios *******