Why does C++ have header files and .cpp files?

George John
Published on 28-Feb-2018 11:06:27
C++ has header and .ccp files for separating the interface from the implementation. The header files declare "what" a class (or whatever is being implemented) will do, ie the API of the class, kind of like an interface in Java. The cpp file on the other hand defines "how" it ... Read More

How to call a virtual function inside constructors in C++?

V Jyothi
Published on 28-Feb-2018 11:06:00
Calling virtual functions from a constructor or destructor is dangerous and should be avoided whenever possible. This is because the virtual function you call is called from the Base class and not the derived class.In C++ every class builds its version of the virtual method table prior to entering its ... Read More

What is a segmentation fault in C/C++?

Priya Pallavi
Published on 28-Feb-2018 10:59:51
A segmentation fault occurs when your program attempts to access an area of memory that it is not allowed to access. In other words, when your program tries to access memory that is beyond the limits that the operating system allocated for your program.Seg faults are mostly caused by pointers ... Read More

How to debug a core in C/C++?

Ankith Reddy
Published on 28-Feb-2018 11:01:21
A process dumps core when it is terminated by the operating system due to a fault in the program. The most typical reason this occurs is that the program accessed an invalid pointer value like NULL or some value out of its memory area. As part of that process, the ... Read More

What does the explicit keyword mean in C++?

Nikitha N
Published on 28-Feb-2018 10:54:41
The explicit keyword in C++ is used to mark constructors to not implicitly convert types. For example, if you have a class Foo −class Foo { public:     Foo(int n); // allocates n bytes to the Foo object     Foo(const char *p); // initialize object with char *p ... Read More

How to convert an int to string in C++?

George John
Published on 28-Feb-2018 10:53:42
You can use the itoa function from C to convert an int to string.  example#include<iostream> int main() {    int a = 10;    char *intStr = itoa(a);    string str = string(intStr);    cout << str; }OutputThis will give the output −10This will convert the integer to a string. In ... Read More

How can I get the list of files in a directory using C/C++?

Chandu yadav
Published on 28-Feb-2018 10:42:27
Standard C++ doesn't provide a way to do this. You could use the system command to initialize the ls command as follows −Example#include<iostream> int main () {    char command[50] = "ls -l";    system(command);    return 0; }OutputThis will give the output −-rwxrwxrwx 1 root root  9728 Feb 25 ... Read More

What is the difference between "std::endl" and "\n" in C++?

Srinivas Gorla
Published on 28-Feb-2018 10:49:31
"\n" Outputs a newline (in the appropriate platform-specific representation, so it generates a "\r\n" on Windows), but std::endl does the same AND flushes the stream. Usually, you don't need to flush the stream immediately and it'll just cost you performance, so, for the most part, there's no reason to use ... Read More

Is segmentation fault actual undefined behavior in C++?

Arjun Thakur
Published on 28-Feb-2018 10:33:22
Undefined behavior is a way to give freedom to implementors (e.g. of compilers or of OSes) and to computers to do whatever they "want", in other words, to not care about consequences.The cases in which segmentation fault occurs are transient in nature. They won't always result in a segmentation fault ... Read More

Sorting a vector of custom objects using C++ STL

Ankith Reddy
Published on 28-Feb-2018 10:24:41
You can sort a vector of custom objects using the C++ STL function std::sort. The sort function has an overloaded form that takes as arguments first, last, comparator. The first and last are iterators to first and last elements of the container. The comparator is a predicate function that can ... Read More