C++ questions

Ace your homework & exams now with Quizwiz!

What is the function of the keyword "Volatile"?

"Volatile" is a function that helps in declaring that the particular variable is volatile and thereby directs the compiler to change the variable externally- this way, the compiler optimization on the variable reference can be avoided.

What does a Static member in C++ mean?

Denoted by the static keyword, a static member is allocated storage, in the static storage area, only once during the program lifetime. Some important facts pertaining to the static members are: Any static member function can't be virtual Static member functions don't have 'this' pointer The const, const volatile, and volatile declaration aren't available for static member functions

Define Encapsulation in C++?

Encapsulation is the process of binding together the data and functions in a class. It is applied to prevent direct access to the data for security reasons. The functions of a class are applied for this purpose. For example, the customers' net banking facility allows only the authorized person with the required login id and password to get access. That is too only for his/her part of the information in the bank data source.

Can we have a recursive inline function in C++?

Even though it is possible to call an inline function from within itself in C++, the compiler may not generate the inline code. This is so because the compiler won't determine the depth of the recursion at the compile time. Nonetheless, a compiler with a good optimizer is able to inline recursive calls until some depth is fixed at compile-time and insert non-recursive calls at compile time for the cases when the actual depth exceeds run time.

How is function overloading different from operator overloading?

Function overloading allows two or more functions with different type and number of parameters to have the same name. On the other hand, operator overloading allows for redefining the way an operator works for user-defined types.

Explain the significance of vTable and vptr in C++ and how the compiler deals with them

vTable is a table containing function pointers. Every class has a vTable. vptr is a pointer to vTable. Each object has a vptr. In order to maintain and use vptr and vTable, the C++ compiler adds additional code at two places: In every constructor - This code sets vptr:Of the object being createdTo point to vTable of the class Code with the polymorphic functional call - At every location where a polymorphic call is made, the compiler inserts code in order to first look for vptr using the base class pointer or reference. The vTable of a derived class can be accessed once the vptr is successfully fetched. Address of derived class function show() is accessed and called using the vTable.

Define Block scope variable?

A Block scope variable is the one that is specified as a block using the C++ that can be declared anywhere within the block.

Define a class template?

A class template is a name given to the generic class. The use of the keyword template is made for defining a class template.

What is a destructor?

A destructor is the member function of the class. It has the same name as the class name and also prefixed with a tilde symbol. It can be executed automatically whenever an object loses its scope.

Define the Copy Constructor used in C++ along with its general function prototype. Also, explain the various scenarios in which it is called.

A member function that initializes an object using another object of the same class is known as a copy constructor in C++. Copy Constructor can also be made private. A call to the Copy Constructor can happen in any of the following 4 scenarios when: The compiler generates a temporary object An object is constructed or based on some another object of the same class An object of the class is returned by value An object of the class is passed (i.e., to a function) by value as an argument The general function prototype for the Copy Constructor is: ClassName (const ClassName &old_obj);Point(int x1, int y1) { x=x1; y=y1;}Point(const Point &p2) { x=p2.x; y=p2.y; }

What is a mutable storage class specifier? How can they be used?

A mutable storage class specifier is applied only on the class's non-static and non-constant member variable. It is used for altering the constant class object's member by declaring it. This can be done by using a storage class specifier.

define a namespace

A namespace is used for resolving the name conflict of the identifier, which is accomplished by placing them under various namespaces. This way, it helps in the logical division of the different codes.

Define a token in C++? Give examples?

A token is a name given to the various functions in C++ programs. Examples of tokens include a keyword, symbol, string literal, identifier, constant, etc

Define an Abstract class in C++?

An abstract class in C++ is referred to as the base class, which has at least one pure virtual function. In such a function, a person cannot instantiate an abstract class. This way, an Abstract class a pure virtual function is defined by using a pure specifier which is equal to zero during the declaration of the virtual member function in the class declaration.

What is an abstraction in C++?

An abstraction in C++ is hiding the internal implementations and displaying only the required details. For example, when you send an important message through email, at that time, only writing and clicking the send option is used. This outcome is just the success message that is displayed to confirm that your email has been sent. However, the process followed in transferring the data through email is not displayed because it is of no use to you.

Define access specifier and its various types in C++

An access specifier offers how it is possible to define how the class members, i.e., functions and variables, will be accessed outside the class's scope. There are three types of access specifier in C++: Private - Such class members can't be accessed outside the class in which they are declared and are only accessible within the same class. Even child classes are disabled to access private members of its parent class. Protected - In addition to the class in which they are declared, the child classes can access its parent class's protected members. Public - Class members declared as public can be accessed throughout the program (code)

Explain Virtual Functions and the concept of Runtime Polymorphism in C++ with a code example.

Any function when accompanying the virtual keyword exhibits the behavior of a virtual function. Unlike normal functions that are called in accordance with the type of pointer or reference used, virtual functions are called as per the type of the object pointed or referred. In simple terms, virtual functions resolve at runtime, not anytime sooner. Use of virtual functions could also be understood as writing a C++ program leveraging the concept of runtime polymorphism. Things essential to writing a virtual function in C++ are: A base class A derived class A function with the same name in both the classes i.e. the base class and the derived class A pointer or reference of base class type that points or refers, respectively to an object of the derived class

Briefly explain the concept of Inheritance in C++.

C++ allows classes to inherit some of the commonly used state and behavior from other classes. This process is known as inheritance.

Draw a comparison between C++ and Java

C++ has destructors, which are invoked automatically when an object is destroyed. Java has something called automatic garbage collection C++ supports multiple inheritance, operator overloading, pointers, structures, templates, and unions. Java doesn't have any of them Java has a Thread class that is inherited in order to create a new thread. C++ has no inbuilt support for threads In C++, a goto statement offers a way to jump from a location to some labeled statement in the same function. There is no goto statement in Java C++ run and compile using the compiler, which converts the source code into machine level language. Hence, it is platform-dependent. Java compiler, on the other hand, converts the source code into JVM bytecode, which is platform-independent.

Define C++?

C++ is a computer programming language that is a superset of C wherein additional features are made in the C language.

What are the most important differences between C and C++? Answer:

C++ supports references while C doesn't Features like friend functions, function overloading, inheritance, templates, and virtual functions are inherent to C++. These are not available in the C programming language. In C, exception handling is taken care of in the traditional if-else style. On the other hand, C++ offers support for exception handling at the language level. Mainly used input and output in C are scanf() and printf(), respectively. In C++, cin is the standard input stream while cout serves as the standard output stream. While C is a procedural programming language, C++ provides support for both procedural and object-oriented programming approaches.

Define Class in C++?

Class is referred to as the designing of the user-defined data type. It reflects the different entities, attributes, and actions.

Define an Inline Function in C++? Write its syntax. Is it possible for the C++ compiler to ignore inlining?

In order to reduce the function call overhead, C++ offers inline functions. As the name suggests, an inline function is expanded in line when it is called. As soon as the inline function is called, the whole code of the same gets either inserted or substituted at the particular point of the inline function call. The substitution is complete by the C++ compiler at compile time. Small inline functions might increase program efficiency. As the inlining is a request, not a command, the compiler can ignore it.

Can we overload a destructor?

No, a destructor cannot be overloaded, and it has the only form without the parameters.

Can we have a String primitive data type in C++?

No, we cannot have a String Primitive data type in C++. Instead, we can have a class from the Standard Template Library (STL).

Can we provide one default constructor for our class?

No, we cannot provide one default constructor for our class. When a variable in the class type is set to null, it means that it was never initialized and the outcomes will be zero.

Define Object in C++?

Object is an instance of the class. An object can have fields, methods, constructors, and related. For example, a bike in real life is an object, but it has various features such as brakes, color, size, design, and others, which are instances of its class.

Question: Observe the following code snippet: int i = 5;int j = i++; After execution, what will be the value of I and j? Explain your answer.

Post the execution of the code above, i and j will be 6 and 5, respectively. For understanding the output, it's important to understand how the unary '++' operator and the decrement '--' operator works in C++. When any of these operators precede a variable, the value of the variable is first modified, and then this modified value is used. However, when any of the two operators follow a variable, the value is first used, and then it is modified.

What are the differences between a shallow copy and a deep copy?

Shallow Copy - It allows memory dumping on a bit by bit basis from one object to another. It is achieved by using a copy instructor and an overloading assignment operator. Deep Copy - It allows the copy field, which is done by field from one object to another. It is used for shallow copy purposes.

Why do we need the Friend class and function?

Sometimes, there is a need for allowing a particular class to access private or protected members of a class. The solution is a friend class, which can access the protected and private members of the class in which it is declared as a friend. Similar to the friend class, a friend function is able to access private and protected class members. A friend function can either be a global function or a method of some class. Some important points about friend class and friend function: Friendship is not inherited. Friendship isn't mutual, i.e., if some class called Friend is a friend of some other class called NotAFriend, then it doesn't automatically become a friend of the Friend class. The total number of friend classes and friend functions should be limited in a program as the overabundance of the same might lead to a depreciation of the concept of encapsulation of separate classes, which is an inherent and desirable quality of object-oriented programming.

Define storage class in C++? Name some?

Storage class in C++ specifically resemble life or even the scope of symbols, including the variables, functions, etc. Some of the storage class names in C++ include mutable, auto, static, extern, register, etc.

Explain 'this' pointer?

The 'this' pointer is a constant pointer, and it holds the memory address of the current object. It passes as a hidden argument to all the nonstatic member function calls. Also, it is available as a local variable within the body of all the nonstatic functions. As static member functions can be called even without any object, i.e., with the class name, the 'this' pointer is not available for them.

What is the default constructor?

The compiler provides a constructor to every class in case the provider does not offer the same. This is when the programmer provides the constructor with no specific parameters than it is called a default constructor

What is the 'diamond problem' that occurs with multiple inheritance in C++?

The diamond problem in C++ represents the inability of the programming language to support hybrid inheritance using multiple and hierarchical inheritance.

What are the functions of the scope resolution operator?

The functions of the scope resolution operator include the following. It helps in resolving the scope of various global variables. It helps in associating the function with the class when it is defined outside the class.

What is the function of the keyword "Auto"?

The keyword "Auto" is used by default for various local variables to make function work automatically.

What is the main difference between the keyword struct and class?

The keyword struct is used for resembling public members by default, while the keyword class is used for resembling private members by default

Define the Reference variable?

The reference variable in C++ is the name given to the existing variables. The variable name and reference variable point share the same memory location in C++, which helps in updating the original variable using the reference variable.

What differences separate structure from a class in C++?

There are two important distinctions between a class and a structure in C++. These are: When deriving a structure from a class or some other structure, the default access specifier for the base class or structure is public. On the contrary, default access specifier is private when deriving a class. While the members of a structure are public by default, the members of a class are private by default

Can we call C++ OOPS? and Why?

Yes, C++ can be called OOPS. The full form of OOPS is an Object-Oriented Programming System, which means a paradigm that provides an application of various concepts, including data binding, polymorphism, inheritance, and various others.

Is it possible for a C++ program to be compiled without the main() function?

Yes, it is possible. However, as the main() function is essential for the execution of the program, the program will stop after compiling and will not execute.

Can we use access specifiers to achieve data hiding in C++?

Yes, we can use access specifiers to achieve data hiding in C++. These include Private and Protected.


Related study sets

Cavalieri's Principle and Volume of Composite Figures

View Set

NUR 1020 Fundamentals CH 6 Values, Ethics, and Advocacy

View Set