C++ Question and Answer


C++ language is the general programming language, and it is invented in the year 1980 by the Bjarne Stroustrup at the Bell Laboratories. It is similar to the C language, and C++ is safer than the C language. It includes programming techniques like OOPS, i.e., Object Oriented Programming Language. It is released out the world in 1985.

C++ aspects all from the C and also simplify the memory management and add several features:

  • C++ allows the OOPS concept.
  • It is an Object-Oriented Programming.
  • Portable language because it writes a program irrespective of OS and Hardware.
  • Low-Level language.
  • It uses multi-paradigm programming.
  • It is very much useful for the low-level language and very efficient for general purpose.
  • It provides memory efficiency and performance.
  • It provides a high-level abstraction.
  • Compatible with the C language.
  • Uses the reusability of code.
  • It uses Inheritance and Polymorphism.
C language C++ Language
C is a procedural language C++ is non-procedural language, i.e., Object Oriented Programming Language.
No virtual functions are present in C language The virtual functions are used in C++
Data is less secured You can use class members for outside users to make it inaccessible
It follows a top-down approach It follows the Bottom-Up approach
It does not support function overloading It supports function overloading
You can not use the functions of structure You can use the functions of the structure
It does not support reference variable It supports the reference variable.
scanf(), printf() is used for the input and output cin(), cout() is used for standard input and output.
Multiple declarations of Global variables are allowed Multiple Declarations of global variables are not allowed in C++
Inheritance is not possible Inheritance is possible.
Exception Handling is not present Exception handling is present with a try and catch blocks.
It supports built-in and primitive data type It supports both built-in and user-defined data type.
Operator overloading is not possible Operator Overloading is possible.
It is less powerful than the pointer. Once a reference is created it can not refer to the other object It provides powerful facilities than references.
It is safe and easy to use in the pointers. It is comparatively different to use.

It is the abstract data type and similar to the C language structure.

  • It represents the objects and sets of operations that can apply to the objects.
  • It consists of Methods and Data Members.
The purpose of the class is to hold the data, and it is achieved with the attributes and also called as the data members. The member function of the class defines the behavior of the class and provide the definition for various operations on the data that can hold in the form of an object.

The Object is an instance of the class, and it provides the blueprints to create the objects. You can create one or more object in one class. It can declare with the same sort of the declarations that we declare the variables of the basic types. The methods and Functions are combined together as a self-contained unit called as the object.

The access specifiers are used to define the variables and functions can access outside of the class.

Three types of access specifiers in C++:
  • Private: Variables and Functions are declared as the private. We can access them only within the same class, and we can not access outside of the class.
  • Public: Variables and Functions declared under the public, and it can access from anywhere in the program.
  • Protected: Variables and Functions can declare as protected, we cannot access them in outside of the class except the child class. It is generally used in the Inheritance concept.

It is a paradigm that provides many concepts. The basic concept of OOPS concepts are:

Classes and Objects:

Classes are used to declare the structure of the information. It defines the datatype. You can create any number of objects from a class. Objects called an instance of the class.

Encapsulation:

It is the mechanism, and it binds the data and associated with the operations together and hide the data from outside. It is also known as data hiding. It is achieved and used the access specifies Public, Private, and Protected.

Abstraction:

Abstraction in oops concept can use to hide all the internal implementations and displays only the necessary information to the outside world. It is implemented using the interfaces and abstract classes.

Inheritance:

It is used to inherit the properties of one class into another class. The facilities in the parent class you can define in the subclass.

Polymorphism:

Poly means many Morphism means Forms. We can define in many forms.
Array List
The Array is the collection of the elements having the same data types with the same name. It is an ordered collection of elements and connected with the pointers and links.
Elements can access by using the subscript and index value. Elements can not access randomly but accessed sequentially.
Elements are stored in a consecutive manner. Elements can store in any of the available places. The address of the node is stored in the previous node.
Insertion and deletion take more time in array. Insertion and deletion take very fast.
The Memory of the data will be allocated at compile time i.e., static memory allocation Memory is allocated at run time i.e., Dynamic memory allocation
The types of the array are single dimensional array, multiple dimensional arrays, and a two-dimensional array. A linked list can have single, double, or circular linked lists.
In an array each element is independent it is not connected to the previous elements. In the linked list location and stored data will be in the previous list.
New() malloc()
It has specific features in languages like C, C++, and Java. It is the feature of C language.
New is an operator. It is a function.
New operator does not need the size. It only assists and allows enough memory for a specific type. It requires the size of an operator to know the memory size.
It could initialize the object to allocate the memory. The memory allocation does not do here.
New operator can overload. It does not overload.
It throws an exception. It returns null.
The memory allocation can deallocate using delete. It deallocated memory by using the free() function.
It does not reallocate the memory. It reallocates using realloc().
It cuts the execution time. It takes more execution time.

We can export the functions from a DLL by using two methods:

  • you have to create the module definition file (.def) and use this file when we build the DLL we can use this approach and want to export the functions from the DLL by the ordinal rather than by name.
  • Use the keyword declspec, i.e., dllexport in the function's definition.

We can export the functions from a DLL by using two methods:

  • you have to create the module definition file (.def) and use this file when we build the DLL we can use this approach and want to export the functions from the DLL by the ordinal rather than by name.
  • Use the keyword declspec, i.e., dllexport in the function's definition.

In C++ language any function or entire class in the program can declare as a friend of another class or function. It also uses the function overloading. The declaration of the Friend Function can appear anywhere in the class. But a good one has in the class ends. In C++ ordinary function is not the member function of the class that it has no privilege to access the data members as private. The friend function does have the capability to access the data members as private. The Friend function declaration is very simple. The Keyword of the friend function is the friend in the class that should be in prototype inside the class definition precedes it.

Virtual Function is a type of member function which is declared within the base class and is overridden by the derived class. You can refer to the derived class object by using the pointer or a reference to the base class. You can able to call the virtual function of another object and you can execute the derived class of the function.

  • It ensures the correct function is called as the object or a pointer used for the function call.
  • Mainly used to achieve the Runtime Polymorphism.
  • The Function can declare with the virtual keyword in the base class.
  • Resolving the function call can do at Run-Time.

Multiple Inheritance in C++ is the feature where the class inherits from more than one classes. The constructor of the parent class is called in the same order in which they are inherited. For example, A Bat class is derived from the base classes like Mammal and Winged Animal. It makes sense because the bat is a mammal and bat is the winged animal.

The Destructor is a special type of the class function which can destroy the object as soon as the scope of the object that ends. It is called automatically by the compiler at the time of object goes out of scope.

This error will occur when you pass a large number of the approximation. It runs for a few seconds, and it will end it still will print out the correct answer.

C++ language can allow you to specify more than one definition for the function name or the operator in the same scope. It is called the function overloading and operator overloading.

If we inherit the parent class and provide the definition of subclass function again inside the parent class, then the function is said to override, and the method is called the Function Overriding.

The Virtual Inheritance is used to replace or revoke the implementation that provides by the Subclass. The replacement always called the object of the derived class. Even the object is accessed by the pointer rather than the derived pointer.

Every object in C++ language has its own access to the address through a pointer called the 'this' pointer. It is an implicit parameter to all the methods. Inside the methods, 'this ' may use to refer to invoke the object. Friend function does not have the 'this' pointer. Friends are not methods of the class. Only methods have this pointer.

To define the member function outside the class is called the scope resolution operator.

The delete operator used to release the memory of dynamic that is created by the new operator. Delete[] is used to release the allocated memory of array that allocated using

We declared the class as Private, no one can access the private class from outside the class. If you use the private class member you can get the compile-time error. In C++ member functions and class, variables are private. If we declare a class as protected the class members are not accessible outside the class. But the class members can access by using the subclasses. It is the same as a private modifier. If we declare a class as public, the class embers can available in everywhere. The variables and methods can declare as public it can access the other classes. The key methods cannot declare as public.

C++ supports the following inheritances.

  • Single inheritance.
  • Multiple inheritances.
  • Hierarchical inheritance.
  • Hybrid inheritance.
  • Multilevel inheritance.

It is the mechanism, and it binds the data and associated with the operations together and hide the data from outside. It is also known as data hiding. It is achieved and used the access specifiers.

It is used to hide the internal implementations and shows only the necessary information to the outside world. It is implemented using the interfaces and abstract classes.

It is used to inherit the properties of one class into another class. The facilities in the parent class you can define in the subclass.

It defines the scope of the variables and functions within the program. The specifiers can precede the type that they can modify.

The storage class in c++ is:

  • Auto
  • Register
  • Static
  • Extern
  • Mutable.

The mutable storage class specifier defines the constant class object member variable. That can use to alter by declaring it as the mutable storage class modifier. It applies only for non-constant and non-static member variables of the class.

The shallow copy main aim is to the memory dumping as the bit-by-bit from one object to another object. The Deep copy field by field from the one object to the other object. It is achieved by using the copy of the constructor or overloading the assignment operator.

The virtual functions that can consist of no function body and assigned that with a zero value is called the Pure Virtual Function.

A class that can consist of no method body and no method function is called as the Abstract class.

(or)

A class can consist of at least one pure virtual function is called an abstract class and we cannot instantiate an abstract class.

Another name of the reference variable is the existing variable. Both the reference variable point and variable name belongs to the same memory location. Therefore you can update the original variable can achieve by using the reference variable too.

It does not exist for the objects that belong to the respective class that is not created. The static member of the class can be shared the common memory across all the objects or instances that can create the respective class. The static member can refer by using the class name.

It can invoke by using the class name and it exists before the objects of the class that comes into existence. It can access only for the static member functions of the class only.

The character data type can use to store wide characters in C++ language.

The Boolean data type can store Boolean values.

Private and protected are used to achieve the data hiding in c++ language.

The destructor is one of the member function of the class having the same name as the class name and contains the prefixed like ~. It gets executed automatically when the object is no more in our program. It cannot be overloaded because the only form is without the parameters.

Object Class Purpose
cin istream Standard characters for input
wcin wistream Standard wide character input
cout ostream Standard characters for output
wcout wostream Standard wide character for output
cerr ostream Standard error output
wcerr wostream Standard error wide character output
clog ostream Standard log
wclog ostream Standard wide character log
Struct Class
It referred to the user-defined data type that possesses the own data type. It is the process of collection of related variables and functions that can encapsulate in a single function
Keyword we used in a struct in C++ is Struct. The keyword we used in C++ language is Class.
The Public is the default access specifier Private is the default access specifier.
The main purpose of the structure is generally grouping the data. The main purpose is the Data Abstraction and Inheritance.
Type of a Struct is value. Type of a class is a reference.
It generally used for small amounts of data. It generally used for a large amount of data.
Variable Declaration Variable Definition
In this space is not reserved Space is reserved
It identifies the data types. Some of the initial value is assigned
Re-declaration is an error Redefinition is an error

It is one type of constructor that creates an object by initializing the object of the same class, that has created previously. We can use copy constructor in C++ is:

  • It is used to initialize the one object or instance of the same type.
  • Helpful to copy an object to pass an argument to the function.
  • It copies the object to return it from a function.

A scope is nothing but a region of the program and we can use mainly in three places, where variables can declare:

  • If variables are used inside a block called the local variables.
  • Also, in the definition of the variable function of parameters which is called the formal parameters.
  • We can write in outside the function it is called the global variables.

Construction: First base class and after derived class.

Destruction:
  • Derived class
  • Member Derived class
  • Base class
  • Member base class

 

  • You can not use the Null references. You have to assume that the reference is always connected to the storage of pointers.
  • Once a reference in C++ is initialized to any of the instances, it cannot change to refer another object. Pointers can point to another object at any time.
  • Reference must initialize when it is created.
  • Pointers can initialize at any time by the program creation.

A Structure is a class, only the attributes and methods are defined as public by a default in the Struct. Methods and attributes stored in Struct only, we all know that. You can also able to inherit the attributes and classes from the struct. So, yes, we can use the Struct to build an OOP concept.

A namespace is used to organize too many classes to handle the application very easily. For accessing the class in Namespace, we have to use the namespacename::classname.We can use the keywords so we do not have to use the complete name all the time when we are writing the program in C++. Global Namespace is the root namespace. it will always refer the namespace as std of the framework.

Command Line Arguments is possible to pass some values of the command line to the programs in C++ when they are in execution time. These type of values are called the Command Line Arguments and many of the times the program especially want to control from outside instead of hardcoded values inside the code.

It is the foundation of the generic programming and that involves by writing the code in a way and also independent of any of the particular type. It is the formula or a blueprint to create the generic class or a function. The containers like the algorithms and iterators are also an example of generic programming and developed by using the template concept.

A catch block helps you to handle the exceptions. The block must follow the try block it is compulsory. A single try block can have so many catch blocks and you can catch different exceptions in different catch blocks. When an exception occurs in try block it comes to the particular catch block itself.

defines an original and also a revised meaning for this keyword. It declares the variable stored in an automatic storage class. It declares the variable whose type is declared from the initialization expression in its declaration.

By using the Static keyword we can define the class members as static. It means you can create any number of object it does not a matter. There is only one copy of the static member that we can create as the static variable. It is shared by all the objects of the class. It is initialized by the first object is created and after no initialization is present as static.

It is used to give the reference to the global variable that already visible to all the program files. If you use the extern storage specifier, then the variable cannot initialize as the variable name at the storage location that returned previously. It is used in another file to give the reference to the defined variable or a function.

It is the unique location in primary storage or the main memory that can serve as a reference point for the other memory locations are known as the addresses. Base address. The address of the zeroth element of the array and also the point of location to all the elements of the array.

The Automatic variables are always local that are stored on the stack.

If your class is the derived class then the destructor is virtual then after the objects or instances will destroy in order like firstly derived object and then the base class. If your derived class destructor is not virtual then only the base class object will get destroyed. There is the memory leak for the derived class.

If the compiler of the program does not allow the nest multi-line comments. If you want to terminate the code it will terminate the opening of the multiline comment.

The #undef directive tells about the preprocessor to remove the definitions for the specified macro. The macro can redefine after it removes by the #undef directive. Once a micro can undefine it will evaluate the false.

Actual Parameters: The Parameters that are passed in a function call is known as the actual arguments. These parameters are also defined as calling functions.Formal Parameters: The formal parameters are the function definitions. The space or scope of the parameters is local to the function definition. It belongs to the called functions. The arguments are a copy of the actual arguments. If any change in formal parameters will reflect the actual parameters.

Cin is the input in C++ language. This is not a keyword. It is a variable and instance of the class and declare in iostream and written as >>.

Cout is the output stream of the c++ language. It is used together with the insertion operator and written as <<.

STL means Standard Template Library.

std means Standard library in C++ language.

The error is the recoverable condition that occurs at the runtime like out of memory error. JVM error that we can not recover at runtime. Exceptions are the conditions that occur because of the bad input for example FileNotFoundException.

If we inherit the derives class and it contains and defines the definition of subclass function again inside the parent class, then the function is said to override, and the method is called the Function Overriding.

When any of the function is called within the function in same, it is known as the recursion. The function which calls the function as same is called the recursive function. A function does not perform any another task after the function, it calls itself is called the tail recursion. It generally calls the same function with a return statement.

To rename the function parameter so it does not any problem with the global variable and it does not conflict.

The containers are the standard containers that are the holder of the object and stores a collection of the other elements. Implemented as the templates of the class and allows flexibility in the types that support as elements.

A Friend class is defined as the outside of the class scope but it having access to all private and protected members of the class. The prototypes of the friend functions that appear in the class definition. These are not member functions.

You can give the multiple definitions for the same name of the function in the same scope then the function definition must differ from each other by the types or the arguments. You cannot have to overload the function declarations that can differ only by the return type.

Define a new job for the already existing operator of the class objects is called the operator overloading. (or) The functions having the special names of the keyword operator by the symbol of the operator is called as operator overloading. It has the return type and the parameter list.

No, because String is a basic data type. So it is not a primitive data type.

The Constructor is the methods of the class whose name is the same as the class name. It initializes the object of the class. It invoked automatically when the object of the class gets created. The use of the constructor is to construct the value of the data member of the class.

The operator used to release the memory of dynamic that is created by the new operator. It is the usage of the delete operator.




Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.