UCA31 - PROGRAMMING IN C++ Unit-1 Principal of OOP Type: 75% Theory & 25 % Numerical Question and Answer

Size: px
Start display at page:

Download "UCA31 - PROGRAMMING IN C++ Unit-1 Principal of OOP Type: 75% Theory & 25 % Numerical Question and Answer"

Transcription

1 ACADEMIC YEAR: REGULATION CBCS UCA31 - PROGRAMMING IN C++ Unit-1 Principal of OOP Type: 75% Theory & 25 % Numerical Question and Answer PART A QUESTIONS 1. What are the limitations of c? (Limitations) of C Programming Language: It does not provide data security. Does not support reusability of source code. Provides no help for solving real world problems C language has no run time checking mechanism. C does not support Object Oriented Programming (OOP) features, so to overcome C++ language was introduced. C language has no strict type checking. C language does not support exception handling. 2. What is the difference between C & C++? [Nov / Dec 2015] C++ is an object oriented programming but c is a procedure oriented programming. C is super set of C++. C can t support inheritance, function overloading, method overloading etc. but C++ can do this. In c-program the main function could not return a value but in the C++ the main function should return a value. 3. What is C++? C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-oriented computer language used in the development of enterprise and commercial applications. Microsoft s Visual C++ became the premier language of choice among developers and programmers. 4. Give the evolution diagram of OOPS concept. Machine language Procedural language Assembly language Oops RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 1 of 22

2 ACADEMIC YEAR: REGULATION CBCS What is Procedure oriented language? Conventional programming, using high-level language such as COBOL, FORTRAN and C are commonly known as Procedure oriented language (POP). In POP number of functions is written to accomplish the tasks such as reading, calculating and printing. 6. Give some characteristics of procedure-oriented language. Emphasis is on doing things (algorithms). Larger programs are divided into smaller programs known as functions. Most of the functions share global data. Data move openly around the system from function to function. Employs top-down approach in program design. 7. Write any four features of OOPS. Emphasis is on data rather than on procedure. Programs are divided into objects. Data is hidden and cannot be accessed by external functions. Follows bottom -up approach in program design. 8. What are the basic concepts of OOPS? Objects. Classes. Data abstraction and Encapsulation. Inheritance. Polymorphism. Dynamic binding. Message passing. 9. What are objects? [Nov / Dec 2015] Objects are basic run-time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. Each object has the data and code to manipulate the data and theses objects interact with each other. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 2 of 22

3 ACADEMIC YEAR: REGULATION CBCS What is a class? The entire set of data and code of an object can be made a user-defined data type with the help of a class. Once a class has been defined, we can create any number of objects belonging to the classes. Classes are user-defined data types and behave like built-in types of the programming language. 11. What is encapsulation? Wrapping up of data and function within the structure is called as encapsulation. The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it. These functions provide the interface between the objects, data and the program. This insulation of the data from direct access by the program is called data hiding or information hiding. 12. What is data abstraction? The insulation of data from direct access by the program is called as data hiding or information binding. The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it. 13. What are data members and member functions? Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight, and cost and uses functions to operate on these attributes. The attributes are sometimes called as data members because they hold information. The functions that operate on these data are called as methods or member functions. Eg: int a,b; // a,b are data members Void getdata ( ) ; // member function RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 3 of 22

4 ACADEMIC YEAR: REGULATION CBCS What is dynamic binding or late binding? Binding refers to the linking of a procedure to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at the run-time. 15. Write the process of programming in an object-oriented language? Create classes that define objects and their behavior. Creating objects from class definition. Establishing communication among objects. 16. What are the features required for object-based programming Language? Data encapsulation. Data hiding and access mechanisms. Automatic initialization and clear up of objects. Operator overloading. 17. Give any four applications of OOPS Real-time systems. Simulation and modeling. Object-oriented databases. AI and expert systems. 18. What are manipulators? Setw, endl are known as manipulators. Manipulators are operators that are used to format the display. The endl manipulator when used in an output statement causes a linefeed to be inserted and its effect is similar to that of the newline character \n. Eg:Cout<<setw(5)<<sum<<endl; 19. How to create an object? Once the class has been declared, we can create variables of that type by using the Classname Eg: classname x; //memory for x is created RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 4 of 22

5 ACADEMIC YEAR: REGULATION CBCS 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); 21. What are the three access specifiers used C++? These are three access specifiers in C++. Public Here the data members and functions are accessible outside the class. Protected - Data members and functions are available to derived classes only. Private - Data members and functions are not accessible outside the class 22. Define inheritance? (NOV/DEC 2013) The mechanism of deriving a new class (derived) from an old class (base class) is called inheritance. It allows the extension and reuse of existing code without having to rewrite the code from scratch. Inheritance is the process by which objects of one class acquire properties of objects of another class. 23. What is dynamic binding? Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run time. It is associated with polymorphism and inheritance. 24. Define a stream. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 5 of 22

6 ACADEMIC YEAR: REGULATION CBCS A stream is an abstraction that represents a device on which input and output operations are performed. A stream can basically be represented as a source or destination of characters of indefinite length. 25. List out the standard streams used in C++ Include iostream.h instead of stdio.h Standard iostream objects: cout - object providing a connection to the monitor cin - object providing a connection to the keyboard cerr - object providing a connection to error stream 26. What is the role of iostream.h in C++.(NOV/DEC 2013) IOSTREAM FILE:- The header file iostream should be included at the beginning of all programs that uses one output statement. This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively. PART- B 1. Write short notes on Unformatted console I/O operations [Nov / Dec 2015] Unformatted input/output Operations:- Overloaded operators >> and<< Objects cin and cout are used for input and output of data by using the overloading of >> and << operators. The >> operator is overloaded in the istream class and << is overloaded in the ostream class. The following is the format for reading data from keyboard: cin>>variable1>>variable2>>..>>variable n where variable 1 to variable n are valid C++ variable names The input data are separated by white spaces and should match the type of variable in the cin list spaces, newlines and tabs will be skipped. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 6 of 22

7 ACADEMIC YEAR: REGULATION CBCS The operator >> reads the data character by character and assigns it to the indicated location. The reading for a variable will be terminated at the encounter of a white space or a character that does not match the destination type. For example consider the code int code; cin>> code; Suppose the following data is entered as input The operator will read the characters upto 8 and the value 4258 is assigned to code. The general form of displaying data on the screen is cout <<item1<<item2<< <<item n The item item1 through item n may be variables or constants of any basic type. put() and get() functions:- The classes istream and ostream define two member functions get(),put() respectively to handle the single character input/output operations. There are two types of get() functions. Both get(char *) and get(void) prototype can be used to fetch a character including the blank space,tab and newline character. The get(char *) version assigns the input character to its argument and the get(void) version returns the input character. Example Char c; cin.get( c ) //get a character from the keyboard and assigns it to c while( c!= \n ) cout<< c; //display the character on screen cin.get( c ) //get another character RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 7 of 22

8 ACADEMIC YEAR: REGULATION CBCS The function put(), a member of ostream class can be used to output a line of text, character by character. For example cout.put( x ); displays the character x and cout.put(ch); displays the value of variable ch. The variable ch must contain a character value. A number can be used as an argument to function put().for example, cout.put(68); This statement will convert the numeric value 68 to a char value and displays getline() and write() functions:- The getline() function reads a whole line of text that ends with a newline character. This function can be invoked by using the object cin as follows: cin.getline(line,size); The function getline() which reads character input into the variable line. The reading is terminated as soon as either the newline character \n is encountered or size-1 characters are read(whichever occurs first). The write() function displays an entire line and has the following form: cout.write(line,size) The first argument line represents the name of the string to be displayed and the second argument size indicates the number of characters automatically when the null character is encountered. If the size is greater than the length of line, then it displays beyond the bound of line.program5.3 illustrates how write() method displays a string RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 8 of 22

9 ACADEMIC YEAR: REGULATION CBCS Compare procedural programming and object oriented programming. PROCEDURAL PROGRAMMING In POP, program is divided into small parts Called functions. In POP, Importance is not given to data but to functions as well as sequence of actions to be done. POP follows Top Down approach. POP does not have any access specifier. In POP, Data can move freely from function to function in the system. POP does not have any proper way for hiding data so it is less secure. In POP, Overloading is not possible. Example of POP are : C, VB, FORTRAN, Pascal. OBJECT ORIENTED PROGRAMMING In OOP, program is divided into parts Called objects. In OOP, Importance is given to the data rather than procedures or functions because it works as a real world. OOP follows Bottom Up approach. OOP has access specifiers named Public, Private, Protected, etc. In OOP, objects can move and communicate with each other through member functions. OOP provides Data Hiding so provides more security in OOP, overloading is possible in the form of Function Overloading and Operator Overloading. Example of OOP are : C++, JAVA, 3. VB.NET, C#.NET. 3. Write down the Input /output operator in c++. Input Operator cin :- The identifier cin is a predefined object in c++ that corresponds to the standard input stream. Here this stream represents keyboard. Syntax:- cin>>variable; The operator >> is known as extraction or get from operator & assigns it to the variable on its right. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 9 of 22

10 ACADEMIC YEAR: REGULATION CBCS Output operator cout : - The identifier cout is predefined object that represents the standard output stream in c++. Here standard output stream represents the screen. 4. Give any f our advantages of OOPS. Syntax:- cout<<string; The operator << is called the insertion or put to operator. It inserts contents of the variable on its right to the object on its left. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. It is possible to have multiple instances of an object to co-exist without any interference. Object oriented programming can be easily upgraded from small to large systems. Software complexity can be easily managed PART C 1. Give a detailed note on Streams in C++? Input and output (I/O) streams Input and output functionality is not defined as part of the core C++ language, but rather is provided through the C++ standard library (and thus resides in the std namespace). The iostream library When you include the iostream header, you gain access to a whole hierarchy of classes responsible for providing I/O functionality (including one class that is actually named iostream). The class hierarchy for the non-file- I/O classes looks like this: RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 10 of 22

11 ACADEMIC YEAR: REGULATION CBCS Streams The I/O in C++ is implemented with streams. Abstractly, a stream can be thought of as a sequence of bytes of infinite length that is used as a buffer to hold data that is waiting to be processed. Typically we deal with two different types of streams. Input streams are used to hold input from a data producer, such as a keyboard, a file, or a network. For example, The user may press a key on the keyboard while the program is currently not expecting any input. Rather than ignore the users keypress, the data is put into an input stream, where it will wait until the program is ready for it. Output streams are used to hold output for a particular data consumer, such as a monitor, a file, or a printer. When writing data to an output device, the device may not be ready to accept that data RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 11 of 22

12 ACADEMIC YEAR: REGULATION CBCS For example, the printer may still be warming up when the program writes data to it s output stream. The data will sit in the output stream until the printer begins consuming it. Input/output in C++ The ios class is generally derived from ios_base, ios is typically the most base class you will be working directly with. The ios class defines a bunch of stuff that is common to both input and output streams. The istream class is the primary class used when dealing with input streams. With input streams, the extraction operator (>>) is used to remove values from the stream. The ostream class is the primary class used when dealing with output streams. With output Streams, the insertion operator (<<) is used to put values in the stream. The iostream class can handle both input and output, allowing bidirectional I/O. These stream classes are derived from istream, ostream, and iostream (respectively) with an assignment operator defined, allowing you to assign one stream to another. Standard streams in C+ + A standard stream is a pre-connected stream provided to a computer program by it s environment. C++ comes with four predefined standard stream objects that have already been set up for your use. The first two, you have seen before: 1. cin -- an istream_withassign class tied to the standard input (typically the keyboard) 2. cout -- an ostream_withassign class tied to the standard output (typically the monitor) 3. cerr -- an ostream_withassign class tied to the standard error (typically the monitor), providing unbuffered output 4. clog -- an ostream_withassign class tied to the standard error (typically the monitor), providing buffered output RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 12 of 22

13 ACADEMIC YEAR: REGULATION CBCS Explain the basic concepts of Object Oriented Programming [Nov / Dec 2015] Before starting to learn C++ it is essential to have a basic knowledge of the concepts of Object oriented programming. Some of the important object oriented features are namely: Objects Classes Inheritance Data Abstraction Data Encapsulation Polymorphism Overloading Reusability Objects: Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. There can be more than one instance of a class. Each instance of a class can hold its own relevant data. An Object is a collection of data members and associated member functions also known as methods RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 13 of 22

14 ACADEMIC YEAR: REGULATION CBCS Classes: Classes are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represents a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects become functions of the class and are referred to as Methods. For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR represents individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Color, Top Speed, Engine Power etc., which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, and Stop form the Methods of Car Class. No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created. Inheritance: Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class. The new class that is formed is called derived class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming. Data Abstraction: Data Abstraction increases the power of programming language by creating user defined data types. Data Abstraction also represents the needed information in the program without presenting the details. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 14 of 22

15 ACADEMIC YEAR: REGULATION CBCS Data Encapsulation: Data Encapsulation combines data and functions into a single unit called Class. When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data Encapsulation enables the important concept of data hiding possible. Polymorphism: Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways. Overloading: Overloading is one type of Polymorphism. It allows an object to have different meanings, depending on its context. When an existing operator or function begins to operate on new data type, or class, it is understood to be overloaded. Reusability: This term refers to the ability for multiple programmers to use the same written and debugged existing class of data. This is a time saving device and adds code efficiency to the language. Additionally, the programmer can incorporate new features to the existing class, further developing the application and allowing users to achieve increased performance. This time saving feature optimizes code, helps in gaining secured applications and facilitates easier maintenance on the application. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 15 of 22

16 ACADEMIC YEAR: REGULATION CBCS Explain formatted and unformatted I/O Operation in detail FORMATTED CONSOLE I/O OPERATIONS C++ provides various formatted console I/O functions for formatting the output. They are of three types. 1. Ios class function and flags 2. Manipulators 3. User-defined output functions The ios grants operations common to both input and output. The classes (istream, ostream, and iostream) derived from ios are special I/O with high-level formatting operations: 1. istream performs formatted input. 2. ostream performs formatted output. 3. iostream performs formatted input and output. The streambuf class controls the buffer and its related member functions. It allows the ability to fill, flush, and empty the buffer. The streambuf is an object of ios class. The base class of input and output stream classes is iosclass. The istream and ostream classes are derived classes of ios class and control input and output streams. The iostream class is a derived class of istream and ostream. It provides input and output functions to control console operations. Function width() precision() fill() setf() unsetf() Working To set the required field width. The output will be displayed in given width. To set number of decimal point to a float value. To set a character to fill in the blank space of a field. To set various flags for formatting output. To remove the flag setting. IOS CLASS FUNCTION TABLE RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 16 of 22

17 ACADEMIC YEAR: REGULATION CBCS ios::width (member functions) The width() function can be declared in two ways. 1. int width(); 2. int width (int); int width(); This function is declared as given above, it returns the present width setting. int width (int); This function is declared as given above, it sets the width as per the given integer and returns the previous width setting. The setting should be reset for each input or output value if a width other than the default is desired. Write a program to set column width and display the characters at specified position. #include<iostream.h> #include<conio.h> int main() clrscr(); cout. width(5); cout<< A ; cout. width(15); cout<< B ; return 0; OUTPUT A B RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 17 of 22

18 ACADEMIC YEAR: REGULATION CBCS ios::precision This function can be declared in two ways. int precision (int); int precision(); int precision (int); It sets the floating-point precision and returns the previous setting. The precision should be reset for every value being output if we want a precision result other than the default. int precision(); It returns the current setting of floating-point precision Write a program to set precision to two and display the float number #include<iostream.h> #include<conio.h> int main() clrscr(); cout.precision(2); cout<<3.1452; return 0; OUTPUT ios::fill This function can be declared in two ways. char fill(); char fill (char); RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 18 of 22

19 ACADEMIC YEAR: REGULATION CBCS char fill(); It returns the current setting of fill character. char fill (char); It resets the fill character and returns the previous setting. Format Flag (V1) Bit Field (V2) Left justification ios::left ios:adjustfield Right justification ios::right ios:adjustfield Padding after sign and base ios::internal ios:adjustfield Scientific notation Fixed point notation Decimal base Octal base Hexadecimal base ios:: scientific ios:: fixed ios:: dec ios::oct ios::hex Flags and Bits Table ios::floatfield ios::floatfield ios::basefield ios::basefield ios::basefield 4. Describe briefly I/O Stream Classes in OOPS [Nov / Dec 2014] The C++ supports a number of I/O operations to perform read and write operations. These C++ I/O functions help the user to work with different types of devices such as keyboard, disk, tape drivers, etc. Stream is an inter-mediator between I/O devices and the user. The standard C++ library contains the I/O stream functions. The stream is a flow of data, measured in bytes, in sequence. If data is received from input devices in sequence, then it is called as source stream, and when the data is passed to output devices, then it is called as destination stream. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 19 of 22

20 ACADEMIC YEAR: REGULATION CBCS Streams and I/O devices The data in source stream can be used as input data by the program. So, the source stream is also called as input stream. The destination stream that collects output data from the program is known as the output stream. C++ input and output streams RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 20 of 22

21 ACADEMIC YEAR: REGULATION CBCS C++ has a number of pre-defined streams. These pre-defined streams are also called as standard I/O objects. cin It handles input from input devices usually from keyboard. cout clog cerr It passes data to output devices such as monitors and printers. Thus, it controls output. It controls error messages that are passed from buffer to the standard error device. It controls the unbuffered output data. It catches the errors and passes to standard error device monitor. STREAM CLASSES C++ has number of classes that work with console and file operations. These classes are known as stream classes. All these classes are declared in the header file iostream.h. The classes istream and ostream are derived classes of base class ios. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 21 of 22

22 ACADEMIC YEAR: REGULATION CBCS Class Function/Contents Ios 1. It is an input and output stream class. 2. It is used to implement a buffer, i.e. it is pointer to a buffer streambuf. 3. ios maintains the information on the state of streambuf, i.e. good, bad, eof, etc. Istream 1. istream provides formatted input. 2. It is used to handle formatted as well as unformatted conversion of character from a streambuf. 3. The properties of ios are inherited in istreamclass. 4. The instance of class does not carry out the actual input. 5. istream declares functions such as peek(),tellg(), seekg(), getline(), read(), etc. 6. istream class overloads the >> operator. Ostream 1. It is used for general-purpose output. 2. It is used to declare the output functions such astellp(), put(), write(), seekp(), etc. 3. It is the parent of all output stream. 4. ostream overloads the << operator. Iostream 1. It is used to handle both input and output streams. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB / VER 1.0 Unit 1 Answer Page 22 of 22

23 ACADEMIC YEAR: REGULATION CBCS UCS31 - PROGRAMMING IN C++ Unit-2 INTRODUCTION TO C++ Type: 75% Theory & 25 % Numerical Question Bank PART A QUESTIONS 1. Give any four applications of c++? C++ allows us to create hierarchy-related objects, we can build special objectoriented libraries, which can be used later by many programmers. C++ are easily maintainable and expandable. C part of C++ gives the language the ability to get close to the machine-level details. It is expected that C++ will replace C as a general-purpose language in the near future. 2. What are tokens? (NOV/DEC 2013) The smallest individual units in a program are known as tokens. C++ has the following tokens, Keyword Identifiers Constants Strings Operator 3. What are keywords? The keywords implement specific C++ language features. They are explicitly reserved identifiers and cannot be used as names for the program variables or other user defined program elements. Eg: go to, if, struct, else,union etc. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

24 ACADEMIC YEAR: REGULATION CBCS Write down the Rules for naming the identifiers in C++. Only alphabetic characters, digits and underscore are permitted. The name cannot start with a digit. The upper case and lower case letters are distinct. A declared keyword cannot be used as a variable name. 5. What is a scope resolution operator? Scope resolution operator is used to uncover the hidden variables. It also allows access to global version of variables. Eg: #include<iostream. h> int m=10; // global variable m void main ( ) int m=20; // local variable m cout<< m= <<m<< \n ; cout<< : : m= <<: : m<< \n ; output: (: : m access global m) Scope resolution operator is used to define the function outside the class. Syntax: Return type <class name> : : <function name> Eg: Void x : : getdata() 6. What are free store operators (or) Memory management operators? New and Delete operators are called as free store operators since they allocate the memory dynamically. New operator can be used to create objects of any data type. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

25 ACADEMIC YEAR: REGULATION CBCS Pointer-variable = new data type; Initialization of the memory using new operator can be done. This can be done as, Pointer-variable = new data-type(value) Delete operator is used to release the memory space for reuse. The general form of its use is Delete pointer-variable; 7. What do you mean by enumerated datatype? An enumerated datatype is another user-defined datatype, which provides a way for attaching names to numbers, thereby increasing comprehensibility of the code. The syntax of an enum statement is similar to that of the struct statement Eg: enum shape circle, square, triangle enum color red, blue, green, yellow 8. What are symbolic constants? There are two ways for creating symbolic constants in C++: Using the qualifier constant. Defining a set of integer constants using enum keyword. The program in any way cannot modify the value declared as constant in c++. Eg: Const int size =10; Char name [size]; 9. What do you mean by dynamic initialization of variables? C++ permits initialization of the variables at run-time. This is referred to as dynamic initialization of variables. In C++,a variable can be initialized at run-time using expressions at the place of..... declarationas, RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

26 ACADEMIC YEAR: REGULATION CBCS int n =strlen(string);.. float area=3.14*rad*rad; Thus declaration and initialization is done simultaneously at the place where the variable is used for the first time. 10. What are reference variable? A reference variable provides an alias (alternative name) for a previously defined variable. sum total For example, if make the variable a reference to the variable, then sum and total can be used interchangeably to represent that variable. Syntax : Data-type &reference-name = variable-name Eg: float total = 100; float sum = total; 11. What is member-dereferencing operator? C++ permits to access the class members through pointers. It provides three pointer-to-member operators for this purpose, : :* To declare a pointer to a member of a class. * To access a member using object name and a pointer to the member ->* To access a member using a and a pointer to that member. 12. 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); RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

27 ACADEMIC YEAR: REGULATION CBCS What are constant arguments? keyword is constant. 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); 14. What are the types of expression? Constant expression Integral expression Float expression Pointer Expression Relational Expression Logical expression Bitwise expression 15. Write down the manipulator functions. Manipulator functions are special stream functions that change certain characteristics of the input and output. They change the format flags and values for a stream. The main advantage of using manipulator functions is that they facilitate that formatting of input and output streams. 16. What is pointer? A pointer is a variable that is used to store a memory address. The address is the location of the variable in the memory. Pointers help in allocating memory dynamically. Pointers improve execution time and saves space. Pointer points to a particular data type. The general form of declaring pointer is:- type *variable_name; type is the base type of the pointer and variable_name is the name of the variable of the pointer. For example, int *x; (x is the variable name and it is the pointer of type integer. ) RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

28 ACADEMIC YEAR: REGULATION CBCS Define variable. Variables are a way of reserving memory to hold some data and assign names to them so that we don't have to remember the numbers like and instead we can use the memory location by simply referring to the variable. Every variable is mapped to a unique memory address. 18. What is an object? i) Object are the basic run-time entities in an object-oriented system. ii) They may represent a person, a place a bank account, a table of data or any item that the program has to handle. iii) Programming problem is analyzed in terms of objects and the nature of iv) communication between them. Objects take up space in the memory & have an associated address like structure in c. v) When a program executes, the object interacts by sending messages to one another. Ex. If there are two objects customer and account then the customer object may send a message to account object requesting for the bank balance. Thus each object contains data, and code to manipulate data. 19. How to declare a variable in C++. C requires all the variables to be defined at the beginning of a scope. But C++ allows the declaration of variable anywhere in the scope. That means a variable can be declared right at the place of its first use. It makes the program easier to understand. 20. what is the use of sizeof operator? The amount of memory needed to store an object of a certain type can be ascertained using the sizeof operator: sizeof(name) yields the size of an object in bytes, and the parameter name indicates the object type or the object itself. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

29 ACADEMIC YEAR: REGULATION CBCS For example, sizeof(int)represents a value of 2 or 4 depending contrast, sizeof(float) will always equal 4. on the machine. In 21. Write the Block Structure of C++. Include Files Class Declaration Member Function Definitions Main Function Program 22. What are the parameter passing in C++. Pass by value Pass by Address Pass by reference 23. When the deferencing operator ->* and.*is used? It is used to access a member when we use pointer to both the object and the member. It is used to access a member when the object itself is used as pointers. 24. What is the difference between class and structure? By default, the members of structures are public while that or class is private. Structures doesn t provide something like data hiding which is provided by the classes. Structures contains only data while class bind both data and member functions. 25. Write a simple c++ program using HELLO WORLD # include<iostream.h> int main() cout<< Hello Wold ; return 0; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

30 ACADEMIC YEAR: REGULATION CBCS What is an array? (NOV/DEC 2013) C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1,..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. 27. Write the syntax to declare a class. (NOV/DEC 2013) Classes are defined using either keyword class or keyword struct, with the following syntax: class class_name access_specifier_1: member1; access_specifier_2: member2;... object_names; PART -B 1. What are the features required for object oriented language? Data encapsulation. Data hiding and access mechanisms. Automatic initialization and clear up of objects. Operator overloading. Inheritance. Dynamic binding. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

31 ACADEMIC YEAR: REGULATION CBCS How the member functions are defined? Member functions can be defined in two ways Outside the class definition Member function can be defined by using scope resolution operator:: General format is Return type class_ name::function-name(argument declaration) Inside the class definition This method of defining member function is to replace the function declaration by the actual function definition inside the class. It is treated as inline function Eg:class item int a,b ; void getdata(int x,int y) a=x; b=y; ; 3. Mention the advantage of oop.(apr/may 2012) Object-oriented programming offers several major advantages to software development: Reduced susceptibility to errors: an object controls access to its own data. More specifically, an object can reject erroneous access attempts Easy re-use: objects maintain themselves and can therefore be used as building blocks for other programs Low maintenance requirement: an object type can modify its own internal data representation without requiring changes to the application. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

32 ACADEMIC YEAR: REGULATION CBCS Write a note on 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; 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 5. Distinguish while and do while statements.(nov/dec 2013) While This is the top tested loop. Loop is not executed if the condition is false. do while This is the bottom tested loop. Loop is executed at least once even though the condition is false. 6. Write the syntax of class declaration class classname Private: Public: Variable declaration; Function declaration; Variable declaration; Function declaration; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

33 ACADEMIC YEAR: REGULATION CBCS ; 7. Give the difference between break and continue.(nov/dec 2013) Break Causes immediate exit from a while, for, do/while or switch structure Continue Program execution continues with the first statement after the structure Common uses of the break statement: Escape early from a loop Skip the remainder of a switch structure Skips the remaining statements in the body of a while, for or do/while structure and proceeds with the next iteration of the loop In while and do/while, the loop-continuation test is evaluated immediately after the continue statement is executed In the for structure, the increment expression is executed, then the loopcontinuation test is evaluated 8. What are the 2 commonly used manipulators in c++? Manipulators are operators that are used to format the data display. There are two important manipulators. 1) endl 2) setw 1) endl : - This manipulator is used to insert a linefeed into an output. It has same effect as using \n for newline. Ex- cout<< a << a << endl << n= <<n; << endl<< p= <<p<<endl; The output is a = 2568 n = 34 p = 275 2) Setw : With the stew, we can specify a common field width for all the numbers and force them to print with right alignment. EX cout<<stew (5) <<sum<<endl; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

34 ACADEMIC YEAR: REGULATION CBCS The manipulator setw(5) specifies a field width of 5 for printing the value of variable sum the value is right justified. 9. What are the operators available in C++? All operators in C are also used in C++. In addition to insertion operator << and extraction operator >> the other new operators in C++ are, : : Scope resolution operator : : * Pointer-to-member declarator ->* Pointer-to-member operator.* Pointer-to-member operator delete Memory release operator endl Line feed operator new Memory allocation operator setw Field width operator PART -C 1. Explain in detail about Switch Case Statement present in C++?(NOV/DEC 2013) Control Structures in C++: The programs in C++ executes its statements one by one from beginning to end. There are different approaches that can be used to solve same problems. In process of designing and implementing approaches/algorithms, many bugs may occur. The format of program should be such that it is easy to trace the flow of execution of statements. By making the flow of execution traceable, it is easy to develop accurate, error-free and maintainable code. The programs not only store data but also perform manipulative tasks and to perform such tasks c++ has provided tools which help in performing repetitive tasks and other manipulative actions(decision making). The tools comes in the form of Control Structures. So Control Structures in C++ tells about the order in which the statements are executed and helps to perform manipulative, repetitive and decision making actions. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

35 ACADEMIC YEAR: REGULATION CBCS Control Structures in C++ can be used in three ways : Sequence Structure i.e. Straight Line. Selection Structure i.e. Branching. Looping Structure i.e. Iteration. switch Statement This statement is alternative to nested if-else statement. It is multi-way decision making construct which allows you to choose different statements depending upon the conditions. This switch statement successfully tests the expression against the integer or character constant and when there is a match, the statement associated with the constant is executed. The syntax of switch-statementis as follows: switch(test-expression) case constant 1 : statement sequence 1; break; case constant 2 : statement sequence 2; break; case constant 3 : statement sequence 3; break; : : case constant n-1 : statement sequence n-1; break; [default: statement sequence n]; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

36 ACADEMIC YEAR: REGULATION CBCS The following figure explains the flow of switch-statement break statement is very important in a switch statement structure. It causes exit from the switch structure after the case statements are executed. If the break statement is not mentioned then the control will transfer to the next case statement. Example for switch statment switch(character) case 'm' : cout<<"male candidate"; break; case 'f' : cout<<"female candidate"; break; default: cout<<"invalid sex code"; //if none of the above matches RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

37 ACADEMIC YEAR: REGULATION CBCS When the condition in case is true the corresponding statement will get executed. The default part is optional in the switch statement, if none of the case is true then default statement is executed. [section label= Switch vs if-else ] switch vs if-else switch can only test the expression for equality whereas if-else can evaluate logical and relational expressions. if-else statement is more flexible and versatile than switch statement as if-else can handle range of values whereas switch case can have single value. cases in switch statement cannot handle floating-point values whereas if else statements can handle floating point values apart from integer and character. A switch statement is more efficient than nest if-else statement A switch statement can only work for equality comparisons. Its good practice to put a break statement after the last case statement in a switch. A switch statement can also be nested. 2. Describe the types of derived datatypes.(nov/dec 2013) Arrays An array in c++ is similar to that in c, the only difference is the way character arrays are initialized. In c++, the size should be one larger than the number of character in the string where in c, it is exact same as the length of string constant. Ex - char string1[3] = ab ; // in c++ char string1[2] = ab ; // in c. Functions Functions in c++ are different than in c there is lots of modification in functions in c++ due to object orientated concepts in c++. Pointers Pointers are declared & initialized as in c. Ex- int * ip; // int pointer RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

38 ACADEMIC YEAR: REGULATION CBCS ip = &x; //address of x through indirection c++ adds the concept of constant pointer & pointer to a constant pointer. char const *p2 = HELLO ; // constant pointer 3. Explain the following i) if statement ii) if else statement with example. The if statement: In C++, the if statement is the primary selection control structure, It is used when we want to check the value of an expression before selecting a course of action. There are basically two forms of if statements: If statement with One alternative: if (condition) statement ; statement; The if selection structure performs an indicated action (statement ) only when the condition (condition) is true. Otherwise the action (statement ) is skipped. Program: #include <iostream> int main(void) float x, sum; sum = 100.0; cout <<"Please enter a real number \n"; cin>> x; if (x!= 0.0) sum = sum + x; cout<< This is the sum << sum; return 0; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

39 ACADEMIC YEAR: REGULATION CBCS Program Output: Run 1: Please enter a real number 23.0 This is the sum Run 2: Please enter a real number 0.0 This is the sum If else selection structure if (condition) statement; else statement ; The if/else selection structure allows the programmer to specify the actions to be performed when the condition is true and the actions to be performed when the condition is false. When the if condition evaluates to true, the statement statement is executed. When the if condition evaluated to false, the statement statement that is the target of else will be executed. Only the code associated with if condition OR the code associated with else executes, never both. Example Program: #include <iostream> using namespace std; int main(void) int grade; cout << "Please enter the grade \n"; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

40 ACADEMIC YEAR: REGULATION CBCS cin>> grade; if (grade >= 60) cout<< "Passed ; else cout<< Failed ; return 0; 4. Explain While and Do-While Statement in detail. WHILE RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-2 QB /VER 1.0

41 ACADEMIC YEAR REGUL ATION What is a function? (Apr 2012) UCS31 - PROGRAMMING IN C++ Unit-3 FUNCTION Type: 75% Theory & 25 % Numerical Question and Answer PART A QUESTIONS A function is a group of statements that to gether perform a task. C++ program has at least one function, which is main (), and all the most trivial programs can define additional functions. It consists of three entities: 1) The function name - this is simply a unique identifier. 2) The function parameters - this is a set of zero or more typed identifier. 3) The function return type - this specifies the type of value function returns. 2. What is functionprototype? ( Apr 2012) The function prototype describes f unction 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); 3. Write the scope rules of members of a class. Scope rules of members of a class are: - Private - These members can be accessed only by the member functions friend functions of the class. It cannot be inherited. Protected -These members can be accessed only b y the member functions & friend functions of the class. It can be inherited. Public -These members can be directly accessed by any f unction. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

42 ACADEMIC YEAR REGUL ATION What are the Components of Functions? Function Declaration Function Parameters Function Definition Return Statement Function Call 5. What are the types of Function used in C++? Functions with Arguments and No Return Values Functions with No Arguments and No Return Values Functions with Arguments and Return Values 6. What are the Features of Inline Function? Run Faster Function Call & Return is eliminated Improves Performance 7. Differentiate Get () and Put () member functions. Get ()member functions used to read single character from keyboard and Put () member functions used to write single character to screen. 8. What are different types of constructors? The different types of constructors used in C++ are Default constructor, Parameterized constructor Copy constructor. 9. Define constructor (Nov/Dec 2013) A constructor is aspecial member function whose task is to initialize the objects ofits class.it is special because its name is same as class name. The constructor is invoked whenever an object of its associated class is created. It iscalled constructor because it constructs thevalues of data members of the class RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

43 ACADEMIC YEAR REGUL ATION Define copy constructor A copy constructor is used to declare and initialize an object from another object. It takes a reference to an object of the same class as an argument Eg: integer i2 (i1); Would define the object i2 at the same time initialize it to the values of i1. Another form of this statement is Eg: integer i2=i1; The process of initializing through a copy constructor is known as copy initialization. 11. Define destructor (Nov/Dec 2011, May 2013) 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. 12. Write some special characteristics of constructor They should be declared in thepublic section They do not have return types, not even void and therefore, and they cannot returnvalues They cannot be inherited, though a derived class can call the base class Constructors cannot be virtual function 13. Define Inline Function? (Apr 2014) Inline function is defined as a function definitionsuch that each call to the function is ineffect,replaced by the statements that define the function. It is expanded inline when it isinvoked. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

44 ACADEMIC YEAR REGUL ATION 2012 The general form is inline function-header body of the function 14. What is operator overloading? C++ has the ability to provide the operators witha 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. 15. List out the operators that cannot be overloaded. Class member access operator (.,.*) Scope resolution operator (::) Size operator ( sizeof ) Conditional operator (?:) 16. Write any four rules for Operator overloading. Only the existing operators can be overloaded. The overloaded operator must haveat least one operandthat is of user defined datatype. The basic meaning of the operator should not be changed. Overloaded operators follow thesyntax rules of the original operators. They cannotbe overridden 17. Define data members and member functions. (Nov 2012) The attributes are some time called data members because they hold information. The functions that operate on these data are sometimes called methods or member function. Member function is function definedwithin a class that acts on the data members in the class. 18. Define Call By Value. The values of the actual parameters (appearing in the function call) are copied into the formal parameters (appearing in the function definition) is called call by value RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

45 ACADEMIC YEAR REGUL ATION Define Call By Reference. The actual argument(s) in the calling program is passed (only variables). So the called function does not create its own copy of original value(s) but works with the original value(s) with different name. Any change in the original data in the called function gets reflected back to the calling function. 20. List out the benefits of inline function. Better than a macro. Program becomes more readable. Program executes more efficiently. Function call overheads are eliminated. 21. Define Static Member Function. Example: A static member function can access only the static members of a class. We can do so by putting the keyword static before the name of the function while declaring it Class student Static int count; public : static void showcount (void) //static member function Cout<< count= <<count<< \n ; ; int student ::count=0; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

46 ACADEMIC YEAR REGUL ATION How will you define a function? PART B QUESTIONS The function definition contains the code for the function. The general syntax of a function definition Type name_of_the_function (argument list) //body of the function The type specifies the type of the value to be returned by the function. It may be any valid c++ data type. Name of the function is a valid C++ identifier (no reserved word allowed) defined by the user and it can be used by other functions for calling this function. Argument list is a comma separated list of variables of a function through which the function may receive data or send data when called from other function. Body of the function contains a collection of statements that define what the function does. Example: //function definition add() void add(int a, int b) //variable names are must in definition int sum; sum=a+b; cout<< \nthe sum of two numbers is <<sum<<endl; 2. Differentiate between a Call by value and Call by Reference. (Apr/May 2014) Call by Value The called function creates its owncopies of the original values sent to it. Call by Reference The called function accesses and works withthe original values using their references. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

47 ACADEMIC YEAR REGUL ATION 2012 Any changes that are made in thefunction run, changes in the originalvalues are not reflected #include <iostream> using namespace std; // function declaration void swap(int &x, int &y); int main () // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values using variable reference.*/ swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; Any changes that occur in the function run,changes in the original values are reflected. void swap(int *x, int *y) int z; z = *x; *x = *y; *y = z; printf("swapped values are a = %d and b = %d", *x, *y); int main (int argc, char *argv[]) int a = 7, b = 4; printf("original values are a = %d and b = %d", a, b); swap(&a, &b); printf("the values after swap are a = %d and b = %d", a, b); 3. Explain inline function with example? These are the functions designed to speed up program execution. An inline function is expanded (i.e. the function code is replaced when a call to the inline function is made) in the line where it is invoked. The system of inline function is as follows : inlinefunction_header body of the function RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

48 ACADEMIC YEAR REGUL ATION 2012 For example, //function definition min() inline void min (int x, int y) Void main() cout<< (x < Y? x : y); int num1, num2; cout<< \Enter the two intergers\n ; cin>>num1>>num2; min (num1,num2; //function code inserted here An inline function definition must be defined before being invoked as shown in the above example. Here min ( ) being inline will not be called during execution, but its code would be inserted into main ( ) as shown and then it would be compiled. If the size of the inline function is large then heavy memory penalty makes it not so useful and in that case normal function use is more useful. The inlining does not work for the following situations: For functions returning values and having a loop or a switchor a goto statement. For functions that do not return value and having a return statement. For functions having static variable(s). If the inline functions are recursive (i.e. a function defined in terms of itself). The benefits of inline functions are as follows: Better than a macro. Function call overheads are eliminated. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

49 ACADEMIC YEAR REGUL ATION 2012 Program becomes more readable. Program executes more efficiently. 4. Explain member function in detail? The attributes are some time called data members because they hold information. The functions that operate on these data are sometimes called methods or member function. In C++, the member functions can be coded in two ways: (a) Inside class definition (b) Outside class definition using scope resolution operator (::) The code of the function is same in both the cases, but the function header is different Inside Class Definition: A member function is defined inside a class, we do not require to place a membership label along with the function name. We use only small functions inside the class definition and such functions are known as inline functions. Outside Class Definition Using Scope Resolution Operator (::) : In this case the function s full name (qualified name) is written as shown: Name_of_the_class :: function_name The syntax for a member function definition outside the class definition is : return_typename_of_the_class::function_name (argument list) Body of function The operator::known as scope resolution operator helps in defining the member function outside the class. 5. Explain Static data member and static member function in detail. RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

50 ACADEMIC YEAR REGUL ATION 2012 Static Data Member: It is generally used to store value common to the whole class. The static data member differs from an ordinary data member in the following ways : (i) Only a single copy of the static data member is used by all the objects. (ii) It can be used within the class but its lifetime is the whole program. For making a data member static, we require: a) Declare it within the class. (b) Define it outside the class. For example Class student Static int count; //declaration within class ; The static data member is defined outside the class as : Static Member Function: int student :: count; //definition outside class A static member function can access only the static members of a class. We can do so by putting the keyword static before the name of the function while declaring it Example: Class student Static int count; public : RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

51 ACADEMIC YEAR REGUL ATION 2012 static void showcount (void) //static member function Cout<< count= <<count<< \n ; ; int student ::count=0; Here we have put the keyword static before the name of the function shwocount (). In C++, a static member function fifers from the other member functions in the following ways: (i) Only static members (functions or variables) of the same class can be accessed by a static member function. (ii) It is called by using the name of the class rather than an object 6. Explain Friend function with example. A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. Syntax: classclass_name friendreturn_typefunction_name(argument/s); To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows: #include <iostream.h> using namespace std; class Distance private: RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

52 ACADEMIC YEAR REGUL ATION 2012 int meter; public: Distance(): meter(0) friendintfunc(distance); //friend function ; intfunc(distance d) //function definition d.meter=5; //accessing private data from non-member function returnd.meter; int main() Distance D; cout<<"distace: "<<func(d); return 0; 7. Explain Destructor with example program Destructor is a special class function which destroys the object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope. The syntax for destructor is same as that for the constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it. class A public: ~A(); ; Destructors will never have any arguments. Example class A A() RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

53 ACADEMIC YEAR REGUL ATION 2012 cout<< "Constructor called"; ; ~A() cout<< "Destructor called"; int main() A obj1; // Constructor Called int x=1 if(x) A obj2; // Constructor Called // Destructor Called for obj2 // Destructor called for obj1 1. Explain function overloading with example. PART C QUESTIONS Function overloading means two or more functions can have the same name but either the number of arguments or the data type of arguments has to be different. Return type has no role because function will return a value when it is called and at compile time compiler will not be able to determine which function to call. Function overloading is also known as compile time polymorphism. The secret to overloading is that each redefinition of the function must use either- Different types of parameters - void print (int a); Different number of parameters - void area (float r) Example: #include<iostream.h> #include<conio.h> class measure float cone,cylinder; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

54 ACADEMIC YEAR REGUL ATION 2012 double sphere; public: void shape(double ); void shape(float,double); ; void measure::shape(double c) cout<<"\n VOLUME OF SPHERE\n"<<endl; sphere=1.33*3.14*c*c*c; cout<<"the volume of sphere is:"<<sphere<<endl; void measure::shape(float h,double f) cout<<"\n VOLUME OF CYLINDER\n"<<endl; cylinder=3.14*h*h*f; cout<<"\n The voloume of cylinder is:"<<cylinder<<endl; void main() clrscr(); float b; double d,e; measure m; cout<<"\n enter the radius of sphere:\t"; cin>>d; m.shape(d); cout<<"\n\n"; cout<<"\n enter the radius of cylinder:\t"; cin>>b; cout<<"\n enter the height of cylinder:\t"; cin>>e; m.shape(b,e); getch(); 2. Explain the different types of constructor with example. Nov/Dec 2011,2013,May 2014) CONSTRUCTOR: A constructor is a special member function whose task is to initialize the objects of RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

55 ACADEMIC YEAR REGUL ATION 2012 its class. It is special because its name is same as class name. The constructor is invoked whenever an object of its associated class is created Example: class A int x; public: A(); //Constructor. ; Types of Constructors Constructors are of three types: Default Constructor Default Constructor Parameterized Constructor Copy Constructor A constructor that takes no parameters (or has parameters that all have default values) is called a default constructor. The default constructor is called if no user-provided initialization values are provided. Syntax : class_name () Constructor Definition Example : class Cube RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

56 ACADEMIC YEAR REGUL ATION 2012 int side; public: Cube() side=10; ; int main() Cube c; cout << c.side; Output : 10 Parameterized Constructor To create constructor with arguments, such constructors are called parameterized constructor For such constructors, it is necessary to pass values to the constructor when an object is created Example : #include <iostream.h> #include<conio.h> class myclass int a, b; public: myclass(int i, int j) a=i; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

57 ACADEMIC YEAR REGUL ATION 2012 b=j; void show() cout<< a << " " << b; ; void main() clrscr(); myclass ob(3, 5); ob.show(); getch(); Output: 3 5 Copy Constructor In this Constructor we pass the object of class into the Another Object of Same Class. This is used for Copying the values of class object into an another object of class So we call them as Copy Constructor and For Copying the values 3. Write a Program to implement the concept of function overloading to compute the volume of a geometric primitive #include<iostream.h> #include<conio.h> int volume(int); double volume(double,int); double volume(long,int,int); class shape public: RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

58 ACADEMIC YEAR REGUL ATION 2012 int volume(int a) int c; c=a*a*a; return c; double volume(int r,double h) double e; e=3.14*r*r*h; return e; long volume(long l,int b,int h) long f; f=l*b*h; return f; ; void main() int Area,b,Height,Radius,Breadth; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

59 ACADEMIC YEAR REGUL ATION 2012 double c; long Length,rec; clrscr(); shape obj; cout<<"\n\t\t***display the function overloading***"; cout<<"\nenter the value for Area"; cout<<"\narea:"; cin>>area; cout<<"\narea of Circle is:"; b=obj.volume(area); cout<<b; cout<<"\nenter the cylinder Radius and Height"; cout<<"\nradius:"; cin>>radius; cout<<"\nheight:"; cin>>height; cout<<"\nvolume of Cylinder is:"; c=obj.volume(radius,height); cout<<c; cout<<"\nenter the rectangle value Length and Breadth and Height"; cout<<"\nlength:"; cin>>length; cout<<"\nbreadth:"; RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

60 ACADEMIC YEAR REGUL ATION 2012 cin>>breadth; cout<<"\nheight:"; cin>>height; cout<<"\nvolume of Rectangle is:"; rec=obj.volume(length,breadth,height); cout<<rec; getch(); RAAK/ B.SC/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-3 QA /VER 1.0

61 ACADEMIC YEAR REGUL ATION 2012 UCS31 - PROGRAMMING IN C++ Unit-4 (INHERITANCE) Type: 75% Theory & 25 % Numerical Question and Answer 1. What is inheritance? PART A QUESTIONS Inheritance is the process by which objects of one class acquire the properties of objects of another class is called inheritance 2. Different types of inheritance used in C++. Single Level Inheritance Multiple Inheritance Hierarchical inheritance Multilevel Inheritance Hybrid Inheritance. 3. Define single inheritance. A derived class with only one base class is called single inheritance 4. Define multipleinheritances. A class can inherit properties from more than one base class which is known as multiple inheritances. RAAK/ B.SC(CS)/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-4 QA /VER 1.0

62 ACADEMIC YEAR REGUL ATION Define Hierarchical inheritance. The properties of one class are inherited by more than one class, it is called hierarchical inheritance. 6. Define Multilevel inheritance A class is derived from another derived class is called multilevel inheritance. It is implemented by defining at least three classes. In multilevel inheritance, there is one base class and the remaining two is derived class. RAAK/ B.SC(CS)/ R.DEIVARAJA/ IIYEAR/ IIISem/ C++/ UCS31 / UNIT-4 QA /VER 1.0

PROGRAMMING IN C++ COURSE CONTENT

PROGRAMMING IN C++ COURSE CONTENT PROGRAMMING IN C++ 1 COURSE CONTENT UNIT I PRINCIPLES OF OBJECT ORIENTED PROGRAMMING 2 1.1 Procedure oriented Programming 1.2 Object oriented programming paradigm 1.3 Basic concepts of Object Oriented

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE UNIT I 1. State the characteristics of procedure oriented programming. Emphasis is on algorithm. Large

More information

Unit 1 : Principles of object oriented programming

Unit 1 : Principles of object oriented programming Unit 1 : Principles of object oriented programming Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP) Divided Into Importance Procedure Oriented Programming In

More information

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING Unit I : OVERVIEW PART A (2 Marks) 1. Give some characteristics of procedure-oriented

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions. Dr.G.R.Damodaran College of Science (Autonomous, affiliated to the Bharathiar University, recognized by the UGC)Reaccredited at the 'A' Grade Level by the NAAC and ISO 9001:2008 Certified CRISL rated 'A'

More information

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3 Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

More information

Object Oriented Pragramming (22316)

Object Oriented Pragramming (22316) Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data

More information

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

Module C++ I/O System Basics

Module C++ I/O System Basics 1 Module - 36 C++ I/O System Basics Table of Contents 1. Introduction 2. Stream classes of C++ 3. Predefined Standard Input/Output Streams 4. Functions of class 5. Functions of class

More information

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class C++ IO C++ IO All I/O is in essence, done one character at a time For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Concept: I/O operations act on streams

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Introduction to C++ (Extensions to C)

Introduction to C++ (Extensions to C) Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.

More information

UEE1302 (1102) F10: Introduction to Computers and Programming

UEE1302 (1102) F10: Introduction to Computers and Programming Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins,

More information

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

PART I.   Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++. Unit - III CHAPTER - 9 INTRODUCTION TO C++ Choose the correct answer. PART I 1. Who developed C++? (a) Charles Babbage (b) Bjarne Stroustrup (c) Bill Gates (d) Sundar Pichai 2. What was the original name

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

I/O Streams and Standard I/O Devices (cont d.)

I/O Streams and Standard I/O Devices (cont d.) Chapter 3: Input/Output Objectives In this chapter, you will: Learn what a stream is and examine input and output streams Explore how to read data from the standard input device Learn how to use predefined

More information

CS6301 PROGRAMMING AND DATA STRUCTURES II QUESTION BANK UNIT-I 2-marks ) Give some characteristics of procedure-oriented language. Emphasis is on doing things (algorithms). Larger programs are divided

More information

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the

More information

CS2141 Software Development using C/C++ Stream I/O

CS2141 Software Development using C/C++ Stream I/O CS2141 Software Development using C/C++ Stream I/O iostream Two libraries can be used for input and output: stdio and iostream The iostream library is newer and better: It is object oriented It can make

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

CHAPTER-6 GETTING STARTED WITH C++

CHAPTER-6 GETTING STARTED WITH C++ CHAPTER-6 GETTING STARTED WITH C++ TYPE A : VERY SHORT ANSWER QUESTIONS 1. Who was developer of C++? Ans. The C++ programming language was developed at AT&T Bell Laboratories in the early 1980s by Bjarne

More information

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers. cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... today: language basics: identifiers, data types, operators, type conversions, branching and looping, program structure

More information

C++ Input/Output: Streams

C++ Input/Output: Streams C++ Input/Output: Streams Basic I/O 1 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams:

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Programming in C++ 4. The lexical basis of C++

Programming in C++ 4. The lexical basis of C++ Programming in C++ 4. The lexical basis of C++! Characters and tokens! Permissible characters! Comments & white spaces! Identifiers! Keywords! Constants! Operators! Summary 1 Characters and tokens A C++

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

Streams - Object input and output in C++

Streams - Object input and output in C++ Streams - Object input and output in C++ Dr. Donald Davendra Ph.D. Department of Computing Science, FEI VSB-TU Ostrava Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

VALLIAMMAI ENGINEERING COLLEGE

VALLIAMMAI ENGINEERING COLLEGE VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur 603 203 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK B.E. - Electrical and Electronics Engineering IV SEMESTER CS6456 - OBJECT ORIENTED

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information

Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

More information

Department of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List

More information

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1 Chapter -1 1. Object Oriented programming is a way of problem solving by combining data and operation 2.The group of data and operation are termed as object. 3.An object is a group of related function

More information

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING YEAR/SEM:II & III UNIT I 1) Give the evolution diagram of OOPS concept. 2) Give some

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

Why Is Repetition Needed?

Why Is Repetition Needed? Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Get Unique study materials from

Get Unique study materials from Downloaded from www.rejinpaul.com VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Year & Semester : IV Section : EEE - 1 & 2 Subject Code

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani Object Oriented Programming C++ 6 th Sem, A Div 2018-19 Ms. Mouna M. Naravani Object Oriented Programming (OOP) removes some of the flaws encountered in POP. In OOPs, the primary focus is on data rather

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

Lab # 02. Basic Elements of C++ _ Part1

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Spring 2005 Lecture 1 Jan 6, 2005 Course Information 2 Lecture: James B D Joshi Tuesdays/Thursdays: 1:00-2:15 PM Office Hours:

More information

UNIT-2 Introduction to C++

UNIT-2 Introduction to C++ UNIT-2 Introduction to C++ C++ CHARACTER SET Character set is asset of valid characters that a language can recognize. A character can represents any letter, digit, or any other sign. Following are some

More information

UEE1303(1070) S 12 Object-Oriented Programming in C++

UEE1303(1070) S 12 Object-Oriented Programming in C++ Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1303(1070) S 12 Object-Oriented Programming in C++ Lecture 06: Streams and File Input/Output I/O stream istream and ostream member

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Lecture 05 - I/O using the standard library, stl containers, stl algorithms Dan Pomerantz School of Computer Science 5 February 2013 Basic I/O in C++ Recall that in C, we

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 The C++ standard libraries provide an extensive set of input/output capabilities. C++ uses type-safe I/O. Each I/O operation is executed in a manner sensitive to the data type. If an I/O member function

More information

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA Variables: Lab 6 Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA We learned that a variable is a name assigned to the first byte of the necessary memory to store a value.

More information

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI 621213 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Sub code: CS2203 SEM: III Sub Name: Object Oriented Programming Year: II UNIT-I PART-A 1. What is

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please

More information

Unit-V File operations

Unit-V File operations Unit-V File operations What is stream? C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs. A C++ stream is a flow of data into or out of a program, such as the

More information

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams Physics 6720 I/O Methods October 30, 2002 We have been using cin and cout to handle input from the keyboard and output to the screen. In these notes we discuss further useful capabilities of these standard

More information

END TERM EXAMINATION

END TERM EXAMINATION END TERM EXAMINATION THIRD SEMESTER [BCA] DECEMBER 2007 Paper Code: BCA 209 Subject: Object Oriented Programming Time: 3 hours Maximum Marks: 75 Note: Attempt all questions. Internal choice is indicated.

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. 4.1 Introduction CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Chapter 3 - Notes Input/Output

Chapter 3 - Notes Input/Output Chapter 3 - Notes Input/Output I. I/O Streams and Standard I/O Devices A. I/O Background 1. Stream of Bytes: A sequence of bytes from the source to the destination. 2. 2 Types of Streams: i. Input Stream:

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

More information

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Contents. 1 Introduction to Computers, the Internet and the World Wide Web 1. 2 Introduction to C Programming 26

Contents. 1 Introduction to Computers, the Internet and the World Wide Web 1. 2 Introduction to C Programming 26 Preface xix 1 Introduction to Computers, the Internet and the World Wide Web 1 1.1 Introduction 2 1.2 What Is a Computer? 4 1.3 Computer Organization 4 1.4 Evolution of Operating Systems 5 1.5 Personal,

More information

An Object Oriented Programming with C

An Object Oriented Programming with C An Object Oriented Programming with C By Tanmay Kasbe Dr. Ravi Singh Pippal IDEA PUBLISHING WWW.ideapublishing.in i Publishing-in-support-of, IDEA PUBLISHING Block- 9b, Transit Flats, Hudco Place Extension

More information

Introduction to C++ Introduction to C++ 1

Introduction to C++ Introduction to C++ 1 1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 12 Streams and File I/O Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output,

More information

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information