Found 7347 Articles for C++

Can main() be overloaded in C++?

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

557 Views

In C++, we can use the function overloading. Now the question comes in our mind, that, can we overload the main() function also?Let us see one program to get the idea.Example#include using namespace std; int main(int x) {    cout

Function overloading and const keyword in C++

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

400 Views

In C++, we can overload functions. Some functions are normal functions; some are constant type functions. Let us see one program and its output to get the idea about the constant functions and normal functions.Example#include using namespace std; class my_class {    public:       void my_func() const {          cout

Private Destructor in C++

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

592 Views

Here we will see what will be the case if the destructors are private in C++. Let us see some example codes to get the idea.This code has private destructor, but it will not generate any error because no object is created.Example#include using namespace std; class my_class {    private:       ~my_class(){          //private destructor       } }; int main() { }In this program, this will generate compilation error, as we are trying to create one object, but the compiler can notice that the destructor is not accessible. So it cannot be ... Read More

Use of explicit keyword in C++

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

784 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

365 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

492 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

882 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?

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

3K+ Views

The sprint() function is present inside the C and C++ also. This function is used to store something inside a string. The syntax is like the printf() function, the only difference is, we have to specify the string into it.In C++ also, we can do the same by using ostringstream. This ostringstream is basically the output string stream. This is present in the sstrem header file. Let us see how to use this.Example#include #include using namespace std; int main() {    string my_str;    ostringstream os;    os

Advertisements