The document provides an overview of object-oriented programming (OOP) concepts in C++, including definitions and explanations of key terms such as classes, objects, inheritance, polymorphism, encapsulation, and data abstraction. It also covers various programming constructs like function overloading, operator overloading, exception handling, and templates, along with their characteristics and examples. Additionally, it discusses the types of inheritance and the properties of constructors and destructors.
The document provides an overview of object-oriented programming (OOP) concepts in C++, including definitions and explanations of key terms such as classes, objects, inheritance, polymorphism, encapsulation, and data abstraction. It also covers various programming constructs like function overloading, operator overloading, exception handling, and templates, along with their characteristics and examples. Additionally, it discusses the types of inheritance and the properties of constructors and destructors.
1. Define object oriented programming. 4. What is inheritance / reusability / derivation?
Object oriented programming is an approach that provides a way of Inheritance is the process by which objects of one class acquire the modularizing programs by creating partitioned memory area for both data properties of objects of another class. The concept of inheritance provides and functions that can be used as templates for creating such modules on the idea of reusability. This means that we can add additional features to an demand. existing class without modifying it . Derivation involves the creation of new 2. List out the features of OOPS. classes ( derived class ) from the existing ones (base class). Classes 1. What is polymorphism? · Objects Polymorphism means the ability to take more than one form. · Data abstraction 2. what is the use of a break statement ? · Data encapsulation A break construct terminates the execution of loop and the control is · Inheritance transferred to the statement immediately following the loop. The term break · Polymorphism refers to the act of breaking out of a block of code. · Message passing 3. What is the use of a continue statement ? · Extensibility The continue statement skips the remainder of the current iteration · Persistence initiates the execution of the next iteration. · Delegation 4. Define function. · Genericity The process of splitting a large program into small manageable tasks is · Dynamic binding. designing them independently is popularly called modular programming or 3. What is an object ? divide and conquer technique. A repeated group of instructions in a Objects are the basic run time entities in an object oriented system. program can be organized as a function. A function is a set of program They may represent a person , a place or any data item. Objects contain data statements that can be proceesed independently. and code to manipulate data . 1. What is a pointer? What are the uses of a pointer? 4. What is a class ? Pointer is defined as a variable used to store memory addresses. The entire set of data and code of an object that can be made a user Uses: defined data type with the help of a class.A class is a collection of objects of · Accessing array elements. type class. · Passing arguments to a function when the function 1. What is data abstraction ? needs to modify the original. The technique of creating new data types that are well suited to an · Passing arrays and strings to functions. application to be programmed is known as data abstraction. · Creating data structures such as linked lists, binary tree 2. What is data encapsulation? etc. The wrapping up of data and functions into a single unit called class is · Obtaining memory from the system dynamically. known as data encapsulation. 2. What is a friend function? 3. What is data hiding ? Friend function is a special type of function which is used to access all The insulation of the data from direct access by the program is called the private and protected members of a class. The functions that are data hiding or information hiding declared with the keyword friend are called friend functions. A function can be a friend to multiple classes.
TWO MARK Q&A 1
PROGRAMMING IN C++ 3. What are the properties of a friend function ? 2. What is function prototype ? A friend function is not in the scope of the class to which it has been Function prototype is otherwise known as function declaration. The declared as friend. prototype describes the function interface to the compiler by giving details · It can be invoked like a normal function without the such as the number and type of arguments and the type of return values. help of any object. 3. What is a class ? How will you define a class ? · Unlike member functions it cannot access the member A class is a way to bind the data and its associated functions together. A names directly and has to use an object name and dot class specification has two parts: membership with each member name. · Class declaration 4. What is the difference between friend function and member · Class function definition function? 4. What are the characteristics of a static data member ? The only difference between a friend function and member function is Static data member is initialized to zero when the first object of its that, the friend function requires the argument to be explicitly passed to the class is created. No other initialization is permitted. function and processes them explicitly, whereas, the member function Only one copy of that member is created for the entire class and is considers the first argument implicitly. shared by all the objects of that class, no matter how many objects 1. What is an inline function? are created. Inline functions are those whose function body is inserted in place of the It is visible only within the class, but its life time is the entire function call statement during the compilation process. With the inline code program. the program will not incur any context switching overhead. 1. What are the properties of a static member function ? 2. What is a recursive function ? A static function can have access to only other static members, A function that contains a function call to itself or a function call to a declared in the same class. second function which eventually calls the first function is known as A static member function can be called using the class name as recursive functions. follows 3. What is data conversion? Classname :: function-name; When we use the assignment operator we assign a value on the right 2. In what way is a private member function different from public hand side to a variable on the left side & if it is of a different data type then member function. we have to perform data conversion or type conversion. A private member function can only be called by another function that 4. What are the methods of data conversion? is a member of its class. Even an object cannot invoke a private function There are two methods for data conversion: using the dot operator. (i) implicit data conversion 3. What is a constructor ? (ii) explicit data conversion A constructor is a special member function whose task is to initialize the 1. Give the structure of a C++ program. objects of its class. It is special because its name is the same as the class a. Include files name. The constructor is invoked whenever an object of its associated class b. Class definition is created. It is called constructor because it constructs the values f data c. Member function definitions members of the class. d. Main function
TWO MARK Q&A 2
PROGRAMMING IN C++ 4. How will you declare and define a constructor } Constructor declaration: Class integer 1. What is a destructor? { A destructor as the name implies is used to destroy the objects that int m,n; have been created by a constructor. Like a constructor the destructor is a public : member function whose name is the same as the class name but is integer ( void ) preceded by a tilde symbol. } ~ integer ( ) constructor definition { integer :: integer (void ) } { 2. what is Dynamic constructors m=0; The constructors can also be used to allocate memory while creating n=0; objects. This will enable the system to allocate the right amount of } memory for each object when the objects are not of the same size thus 1. what are the characteristics of a constructor ? resulting in the saving of memory. Allocation of memory to objects at They should be declared in the public section. the time of their construction is known as dynamic constructors. The They are invoked automatically when the objects are created. memory is allocated with the help of the new operator. They do not have return types, not even void and therefore, they 3. What is function overloading? Give an example. cannot return values. Function overloading means we can use the same function name to They cannot be inherited, though a derived class can call the base create functions that perform a variety of different tasks. class constructor. Eg: An overloaded add ( ) function handles different data types as Constructors cannot be virtual. shown below. 2. what are the types of a constructor ? // Declarations Default constructor int add( int a, int b); //add function with 2 arguments of same type Parameterized constructor. int add( int a, int b, int c); //add function with 3 arguments of same Copy constructor type 3. What is a parameterized constructor? double add( int p, double q); //add function with 2 arguments of Constructors. different type The constructors that can take arguments are called parameterized 4. What is operator overloading? Integer ( int x, int y ) 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 m=x; operator is known as Operator overloading. It provides a flexible option n=y; for the creation of new definitions for C++ operators. } 1. List out the operators that cannot be overloaded. 4. What is a default constructor? _ Class member access operator (. , .*) A constructor that accepts no parameter is called default constructor. _ Scope resolution operator (::) Default ( ) _ Size operator (sizeof) { _ Conditional operator (?:)
TWO MARK Q&A 3
PROGRAMMING IN C++ 2. Write at least four rules for Operator overloading. int hrs,mins; _ Only the existing operators can be overloaded. public: _ The overloaded operator must have at least one operand that is of user …………. defined data type. time ( int t) //constructor _ The basic meaning of the operator should not be changed. { _ Overloaded operators follow the syntax rules of the original operators. hours= t/60 ; //t in minutes mins =t % 60; They cannot be overridden. } 3. How will you overload Unary & Binary operator using member }; functions? Constructor will be called automatically while creating objects so that When unary operators are overloaded using member functions it this conversion is done automatically. takes no explicit arguments and return no explicit values. 4. Explain class to basic type conversion with an example. When binary operators are overloaded using member functions, it Using Type Casting operator, conversion from class to basic type takes one conversion can be done. It is done in the source class itself. explicit argument. Also the left hand side operand must be an object Eg: vector : : operator double( ) of the relevant class. { 4. How will you overload Unary and Binary operator using Friend double sum=0; functions? for(int I=0;I<size;I++) sum=sum+v[ i ] *u[ i ] ; return sqrt When unary operators are overloaded using friend function, it takes one ( sum ) ; reference argument (object of the relevant class) When binary operators are } overloaded using friend function, it takes two explicit arguments. This function converts a vector to the corresponding scalar magnitude. 1. How an overloaded operator can be invoked using Friend 1. What is an exception ? functions? Exception refers to unexpected condition in a program. The unusual In case of unary operators, overloaded operator can be invoked as conditions could be faults, causing an error which in turn causes the Operator op (x); In case of binary operators, overloaded operator can be program to fail. The error handling mechanism of C++ is generally referred invoked as Operator op (x , y) to as exception handling. 2. List out the operators that cannot be overloaded using Friend 2. What are the types of exception ? function. They are classified into synchronous and asynchronous exceptions. _ Assignment operator = Synchronous exception : The exception which occur during program _ Function call operator ( ) execution , due to some fault in the input data or technique that is not _ Subscripting operator [ ] suitable to handle the current class of data, within the program are known as _ Class member access operator synchronous exception. For instance errors such as out-of-range, overflow, 3. Explain basic to class type conversion with an example. underflow and so on. Conversion from basic data type to class type can be done in Asynchronous exception : destination class. Using constructors does it. The exceptions caused by events or faults unrelated to the program and Constructor takes a single argument whose type is to be converted. beyond the control of the program are called asynchronous exception. For Eg: Converting int type to class type class time instance, errors such as keyboard interrupts, hardware malfunctions, disk { failure, and so on.
TWO MARK Q&A 4
PROGRAMMING IN C++ 3. What are the blocks related to exception handling constructs? Runtime exceptions are automatically defined for the programs that we The blocks related to exception handling constructs are write and include things such as division by zero and invalid array indexing. · try 1. Define inheritance ? · throw Inheritance is the process of creating new classes from the existing · catch classes. The new classes are called derived classes. The existing classes are The keyword try is used to preface a block of statements. Which may called base classes. The derived classes inherit all the properties of the base generate exceptions. This block of statements is known as try block. When class plus its own properties. The properties of inheritance do not affect the an exception is detected, it is thrown using throw statement in the try block. base classes. A catch block catches the exception thrown by the throw statement in 2. What are the types of inheritance ? the try block and handles it appropriately. Single inheritance Syntax: Multiple inheritance try Hierarchical inheritance { Multilevel inheritance. ……… Hybrid inheritance ……… Multipath inheritance throw exception; 3. What are the advantages of using a derived class ? } New classes can be derived by the user from the existing classes catch (type arg) without any modification. {} It saves time and money. 4. What are the functions supported by C++ to handle uncaught It reduces program coding time. exceptions? The functions supported by C++ to handle uncaught exceptions are It increases the reliability of the program. terminate ( ) set_terminate ( ) unexpected ( ) set_unexpected ( ) 4. Define single inheritance ? 1. What is a template? Inheritance. If a derived class is derived from a base class then it is Templates support generic programming, which allows developing called single BASE CLASS DERIVED CLASS reusable software components such as functions, classes etc., and 1. Define multiple inheritance ? supporting different data types in a single framework If a derived class is derived from more than one base class, then it is 2. What is function template? called multiple inheritance. The templates declared for functions are called function templates. They 2. Define hierarchical inheritance ? perform appropriate operations depending on the data type of the If two or more derived classes are derived from the same base class then parameters passed to them. it is known as hierarchical inheritance 3. What is a class template ? 3. Define multilevel inheritance? The templates declared for classes are called function templates. They If a derived class is derived from another derived class then it is known perform appropriate operations depending on the data type of the as multilevel inheritance. parameters passed to them. 4. 8.Define hybrid inheritance ? 4. Define runtime exceptions Inheritance. The combination of one or more types of inheritance is called hybrid
TWO MARK Q&A 5
PROGRAMMING IN C++ 1. Define abstract class? It is the property of the same object to behave differently in different A class is said to be an abstract class if it satisfies the following contexts given the same message. A single function name can be used for conditions: various purposes and single operator is used to achieve multiple operations · It should act as a base class and the usage of either the function at the operator depends on the context · It should not be used to create any objects in such cases. 2. What is an virtual function? 3. Define Compile time polymorphism: A member function whose definition can be changed during run time is The compiler while compiling the program resolves the function call or called virtual function. The class which contains virtual function is called the operator call. This is known as compile time polymorphism polymorphic class and it should be a base class. Different versions for the 4. 16. Define Runtime polymorphism virtual function should be present in different derived classes with same During multiple inheritances if the appropriate member function could name as virtual function name be selected while the program is running is known as Runtime 3. Define pure virtual function? polymorphism Pure virtual function is a virtual function with no body. The general 1. Define Run time type information: form is It is a very powerful tool in c++ for finding out the type of an object at Virtual void member-function-name( ) = 0 runtime. Because of runtime functioning, RTTI also impacts the 4. What are the properties of pure virtual function? performance of the system to a notable extent. It has no definition in the base class. 2. What is a stream ? We cannot create object for the class containing pure virtual Stream is a series of bytes, which act either as a source from which function. input data can be extracted or as a destination to which the output can be Pure virtual function can be called through derived class objects. sent. The source stream provides data to the program called te input stream It acts as an empty bucket which has to be filled by its derived and the destination stream that receives data from the program is called the classes. output stream. 1. What are the rules for virtual functions? 3. What are the types of standard streams ? Virtual functions must be members of some class. cin - Standard input corresponding to stdin in C They cannot be static members. cout – Standard output corresponding to stdout in C They are accessed by using object pointers. cout – Standard error output corresponding to Stderr in C A virtual function can be a friend of another class. clog – A fully buffered version of cerr. We can have virtual destructors, but we cannot have virtual 4. How do you classify ios class ? constructors. Istream – input stream does formatted input. Ostream – output stream A virtual function in a base class must be defined even though it does formatted output. may not be used. Iostream – input / output stream does formatted input and output. A pointer to a derived class cannot point an object of base type. 1. What are manipulators? Manipulators are special functions that are specifically designed to When a pointer points to a derived class, incrementing or modify the working of a stream. They can be embedded in the I/O decrementing will not make it point to the next object of the derived statements to modify the form parameters of a stream. class. 2. Storing programs and data permanently in main memory is not preferred 2. Define Polymorphism Give reasons.
TWO MARK Q&A 6
PROGRAMMING IN C++ a. Main memory is usually too small to permanently store all the needed programs and data. b. Main memory is a volatile storage device, which loses its contents when power is turned off. 3. What is a file? A file is a collection of related information defined by its creator. Commonly files represent programs ( boyh source and object forms ) and data. Files may be freeform, such as text files or may be rigidly formatted . In general, a file is a sequence of bits, bytes, lines, or records whose meaning is defined by its creator and user. 4. How many classes are used for handling files? ifstream – for handling input files. ofstream – for handling output files. fstream – for handling files on which both input and output can be performed. 1. What are the steps involved in manipulating a file? · Name the file on the disk · Open the file to get the file pointer. · Process the file. (Read / Write ) · Check for errors while processing. · Close the file after its complete usage. 2. What functions are used for manipulation of file pointers ? · seekg ( ) – Moves get file pointer to a specific location. · seekp ( ) - Moves put file pointer to a specific location. · tellg ( ) – Returns the current position of the get pointer · tellp ( ) - Returns the current position of the put pointer 3. What do you mean by sequential access? A sequential file has to be accessed sequentially; to access the particular data in the file all the preceding data items have to be read and discarded. For example a file on a tape must be accessed sequentially. 4. What do you mean by random access? A random file allows access to the specific data without the need for accessing its preceding data items. However, it can be accessed sequentially. For example, a file on a hard disk or floppy disk can be accessed either sequentially or randomly.