C++ Programming

  Home  Computer Programming  C++ Programming


“Learn C++ Programming by C++ Interview Questions and Answers”



120 C++ Programming Questions And Answers

62⟩ What is the difference between an object and a class?

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.

- A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.

- The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.

- An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

 148 views

63⟩ What is a C++ class?

Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class.

 151 views

66⟩ What are virtual functions in C++?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

 115 views

67⟩ What is C++ namespace?

Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.

The form to use namespaces is:

namespace identifier { namespace-body }

Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:

namespace general { int a, b; } In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put:

general::a general::b

The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error.

 128 views

69⟩ What is friend function in C++?

As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.

 114 views

70⟩ What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a method that accepts an object of the same class and copies it’s data members to the object on the left part of assignement:

class Point2D{

int x; int y;

public int color;

protected bool pinned;

public Point2D() : x(0) , y(0) {} //default (no argument) constructor

public Point2D( const Point2D & ) ;

};

Point2D::Point2D( const Point2D & p )

{

this->x = p.x;

this->y = p.y;

this->color = p.color;

this->pinned = p.pinned;

}

main(){

Point2D MyPoint;

MyPoint.color = 345;

Point2D AnotherPoint = Point2D( MyPoint ); // now AnotherPoint has color = 345

 122 views

71⟩ What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.

An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be "attach" to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object.

 148 views

74⟩ What is polymorphism in C++? Explain with an example?

"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.

Example: function overloading, function overriding, virtual functions. Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.

 139 views

75⟩ What do you mean by pure virtual functions?

A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.

class Shape { public: virtual void draw() = 0; };

 167 views

76⟩ What is the output of the following program? Why?

#include <stdio.h>

main()

{

typedef union

{

int a;

char b[10];

float c;

}

Union;

Union x,y = {100};

x.a = 50;

strcpy(x.b,"hello");

x.c = 21.50;

printf("Union x : %d %s %f n",x.a,x.b,x.c );

printf("Union y :%d %s%f n",y.a,y.b,y.c);

}

Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively)

What is output equal to in

output = (X & Y) | (X & Z) | (Y & Z)

 208 views

77⟩ Why are arrays usually processed with for loop?

The real power of arrays comes from their facility of using an index variable to traverse the array, accessing each element with the same expression a[i]. All the is needed to make this work is a iterated statement in which the variable i serves as a counter, incrementing from 0 to a.length -1. That is exactly what a loop does.

 183 views

78⟩ How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array? How can you find the nodes with repetetive data in a linked list?

Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable but aac or bcd are unacceptable.

Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique.

 214 views

79⟩ What is an HTML tag?

An HTML tag is a syntactical construct in the HTML language that abbreviates specific instructions to be executed when the HTML script is loaded into a Web browser. It is like a method in Java, a function in C++, a procedure in Pascal, or a subroutine in FORTRAN.

 179 views

80⟩ You are given a simple code for the class BankCustomer. Write the following functions ...

You’re given a simple code for the class BankCustomer. Write the following functions:

* Copy constructor

* = operator overload

* == operator overload

* + operator overload (customers’ balances should be added up, as an example of joint account between husband and wife)

Note:Anyone confusing assignment and equality operators should be dismissed from the interview. The applicant might make a mistake of passing by value, not by reference. The candidate might also want to return a pointer, not a new object, from the addition operator. Slightly hint that you’d like the value to be changed outside the function, too, in the first case. Ask him whether the statement customer3 = customer1 + customer2 would work in the second case.

 197 views