Found 7197 Articles for C++

Use of explicit keyword in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:26

963 Views

Here we will see what will be the effect of explicit keyword in C++. Before discussing that, let us see one example code, and try to find out its output.Example#include using namespace std; class Point {    private:       double x, y;    public:       Point(double a = 0.0, double b = 0.0) : x(a), y(b) {          //constructor       }       bool operator==(Point p2) {          if(p2.x == this->x && p2.y == this->y)          return true;          return ... Read More

Simulating final class in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

509 Views

In Java or C#, we can use final classes. The final classes are special type of class. We cannot extend that class to create another class. In C++ there are no such direct way. Here we will see how to simulate the final class in C++.Here we will create one extra class called MakeFinalClass (its default constructor is private). This function is used to solve our purpose. The main Class MyClass can call the constructor of the MakeFinalClass as they are friend classes.One thing we have to notice, that the MakeFinalClass is also a virtual base class. We will make ... Read More

Returning multiple values from a function using Tuple and Pair in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

3K+ Views

In C or C++, we cannot return more than one value from a function. To return multiple values, we have to provide output parameter with the function. Here we will see another approach to return multiple value from a function using tuple and pair STL in C++.The Tuple is an object capable to hold a collection of elements, where each element can be of different types.The pair can make a set of two values, which may be of different types. The pair is basically a special type of tuple, where only two values are allowed.Let us see one example, where ... Read More

static keyword in C++ vs Java

Smita Kapse
Updated on 30-Jul-2019 22:30:26

653 Views

In C++ or Java we can get the static keyword. They are mostly same, but there are some basic differences between these two languages. Let us see the differences between static in C++ and static in Java.The static data members are basically same in Java and C++. The static data members are the property of the class, and it is shared to all of the objects.Examplepublic class Test {    static int ob_count = 0;    Test() {       ob_count++;    }    public static void main(String[] args) {       Test object1 = new Test();   ... Read More

Inheritance in C++ vs Java

Anvi Jain
Updated on 30-Jul-2019 22:30:26

1K+ Views

In C++ and Java, there are the concept of Inheritance. The inheritance properties are used to reuse the code and also make a relationship between two objects. Here we will see some basic differences between inheritance in C++ and inheritance in Java.In Java, all of the classes are extending the Object class. So there is always a single level inheritance tree of classes. The object class is present at the root of the tree. Let us check this is true or not using a simple code.Example//This is present in the different file named MyClass.java public class MyClass {    MyClass() ... Read More

Name Mangling and extern “C” in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

In C++ we can use the function overloading feature. Using this feature, we can create functions with same name. The only difference is the type of the arguments, and the number of arguments. The return type is not considered here. Now the question comes how the C++ distinguishes overloaded functions in object code?In the object code, it changes the name by adding information about the arguments. The technique which is applied here is called the Name Mangling. C++ has no standardized technique for name mangling. So different compiler uses different techniques.Here is an example of Name Mangling. The overloaded functions ... Read More

What is the C++ equivalent of sprintf?

Tapas Kumar Ghosh
Updated on 16-Jun-2025 16:48:56

3K+ Views

The sprintf() function of C library equivalent to C++ that is used to create strings with a specified format like custom text with numbers or names. In C++, we can perform the same operation as in C with the help of ostringstream.C++ std::ostringstreamThe ostringstream is known for the output string stream and is defined under header file. This is used to build the string by writing some text into it. Syntax i. Following is the basic syntax of sprint() function in C: int sprintf(char *str, const char *format, ...); ii. Here, we show the syntax of C++ function ... Read More

Compiling multiple .cpp files in c++ program

Anvi Jain
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see how to compile multiple cpp file in C++ program. The task is very simple. We can provide the names as a list to the g++ compiler to compile them into one executable fileTo compile multiple files like abc.cpp, and xyz.cpp at once, the syntax will be like this −g++ abc.cpp xyz.cppTo run the program, we can use this −./a.outExamplefloat area(float r){    return (3.1415*r*r); //area of a circle } float area(float l, float w) {    return (l * w); //area of a rectangle }Example#include #include "area.cpp" using namespace std; main() {    cout Read More

How to declare a global variable in C++

Tapas Kumar Ghosh
Updated on 02-Jun-2025 14:02:21

2K+ Views

In C++, a global variable is a variable that is declared outside of any function or class. It can be accessible from any part of the function. Declaration of a Global Variable The global variables are declared after the heading file inclusions section and before starting the main() function. The declaration is simple just like a normal variable declaration. You need to use data type and variable name. You can also initialize them if required. Syntax The following is the basic syntax of declaring global variable: datatype global_var_name = var_value; Example of Global Variable Declaration In this example, we ... Read More

What is the difference between a destructor and a free function in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

607 Views

Here we will see what are the differences between destructor and the free() functions in C++. The destructor is used to perform some action, just before the object is destroyed. This action may not freeing up the memory, but can do some simple action such as displaying one message on screen.The free() function is used in C, in C++, we can do the same thing using delete keyword also. When the object is deleted using free() or delete, the destructor is invoked. The destructor function takes no argument and returns nothing. This function is called when free or delete is ... Read More

Advertisements