Home » Important Questions with their Answer of “Introduction to C++”

Important Questions with their Answer of “Introduction to C++”

Short Term Questions & Answer:
1. _________  is essential function for every C++ program.
      Ans: main()
2. When we compile C++ program, then ______ file is created.
       Ans: .obj
3. cin and cout is an input/output stream, which can defined in the ___________ header file.
      Ans: <iostream.h>
4. In C++, a comments can be defined in _____ ways.
       Ans: 2
5. In C++, a program can be saved by _______ extension.
      Ans: .cpp

Other Important Questions & Answer:

Ques 1: Why do we use comments in our program? Also, how many ways comment can be defined in C++ program?
Ans: Comments are an important part of any program. They are used to clear the intention of the programmer. In other words we can say that comments are used in a program to make it meaningful, not for yourself but also for others. Comments have no effect on the executable program because the compiler ignores them.

There are two ways to define a comment in C++ program:

  1. /* ———— */, used for commenting a block of code .
  2. //———— . used for single line comment.

Ques 2: What is preprocessor?
Ans: A preprocessor is a program that processes a source file before the main compilation takes place. It is automatically invoked when we compile the program.

Ques 3: Why main() is essential function?
Ans: Every C++ program consists of one or more functions in which main() is an essential function because the program’s execution starts from this function and terminates with the last statement of this function.

Ques 4: What is the difference between ‘C’ and ‘C++’?
Ans: 
The major difference between C and C++ is that C is a procedural programming language and does not support classes and objects, while C++ object oriented programming language and support the concept of classes and object.

The  differences between C and C++ are shown as below:

Comparison Characteristics C Language C++ Language
Approach
It follows Top Down approach. It follows Bottom Up approach.
Developed by C was developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1980 with C++’s predecessor “C with Classes”.
Programming type It is a Procedural Oriented language. It is an Object-Oriented Programming language
File Extension In C language, file can be saved by .c extension. In C++ language, file can be saved by .cpp extension.
Program Structure (or) Division
In C language, program is divided into small parts called functions. In C++ language, program is divided into parts called objects.
Data Types C does not provide String or Boolean data types. It supports primitive & built-in data types. C++ provides Boolean or String data types. It supports both user-defined and built-in data types.
Compatibility With Functions C does not support functions with default arrangements. C++ supports functions with default arrangements.
Inline function It does not allow inline function. It supports inline function.
Data Security In C language, the data is not secured. Data is secure, so it can’t be accessed by external functions. (Using Encapsulation concept of OOPs)
Namespace Feature C does not have namespace feature. C++ uses NAMESPACE which avoid name collisions.
Input/Output Function C uses functions for input/output.
For example: scanf() and printf().
C++ uses objects for input output.
For example: cin() and cout().
Virtual/friend Function C has no support for virtual and friend functions. C++ supports virtual and friend functions.
Memory Allocation/De-allocation C provides malloc() and calloc() functions for dynamic memory allocation, and free() for memory de-allocation. C++ provides new operator for memory allocation and delete operator for memory de-allocation.
Exception Handling C does not provide direct support for error handling (also called exception handling). C++ provides support for exception handling. Exceptions are used for “hard” errors that make the code incorrect.
Overloading Feature C does not support function and operator overloading. C++ supports both function and operator overloading.
Level of Language It is Mid-level language. It is High-level language.
Pointers and References C supports only Pointers. C++ supports both pointers and references.
Mapping Mapping between function and data is complicated in C. Mapping between function and data can be done easily using ‘Objects’.

Ques 5: Explain the structure of the C++ program?
Ans: C++ is a procedural language with the object oriented extensions. Every C++ program consists of one or more functions and any function can call any function, even a function can call itself i.e. recursion.

In C++, statements are of three main types:

  1. Declaration statements: The statements that declare variables and functions.
  2. Definition statements: The statements that define the instances of variables and functions.
  3. Procedural statements: The statements that include assignments, expressions or control statements.

The structure of C++ program are as shown below:

Comments Section: This section is helpful to understand that what’s going on means that shows the program name or other important things. It have no effect on the executable program because C++ compiler ignores them.
In C++, we can defines comments in two ways:-

  1. /* ———— */, used for commenting a block of code .
  2. //———— . used for single line comment.

Preprocessor Directives: A preprocessor is a program that processes a source file before the main compilation takes place. It is automatically invoked when we compile the program. C++ handles preprocessor directives whose names begins with a sign #. In C++, we use #include<iostream.h> preprocessor directive.
This directive tells the compiler to add the contents of the iostream.h file into source file. The #include directive tells the compiler to include a given source code file in your program. The iostream.h file describes classes, functions and global values for input and output console. The iostream.h file uses the cin and cout for input and output.

External/Global Variable Declaration: The external variable are those variable that are declare here and defined outside the main() function. The global variables that can be used anywhere in the program are declared in global declaration section. This section also declares the user defined functions.

class Definition and Declaration: A class is an organization of data and functions which operate on them. Data types are called data members and the functions are called member functions. The combination of data members and member functions constitute a data object or simply an object.

A class definition is followed by a semicolon(;) as:

class classname
{
// data members
// member functions
};

void main() function: Every C++ program has a main() function. The main() function provides the entry and exit points to the program. The program starts executing with the first statement in main() function and terminates with the last statement in main() function.

The first line of the main() function is called the function heading and the portion enclosed in the braces ( { and } ) is called the function body.


Leave a comment

Your email address will not be published. Required fields are marked *