Programming Articles - Page 2712 of 3366

Template Metaprogramming in C++

Aman Kumar
Updated on 16-May-2025 17:02:33

333 Views

C++ Template metaprogramming (TMP)Template metaprogramming (TMP) is a metaprogramming technique in which a template is used by the compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The best use of template metaprogramming is in C++. The use of templates can be thought of as compile-time polymorphism. The use of templates as a metaprogramming requires two distinct operations: A template must be defined and a defined template must be instantiated. Syntax The syntax of template metaprogramming is not defined by code, but we can explain it and ... Read More

Variadic function templates in C++

Aman Kumar
Updated on 18-Jun-2025 18:43:09

386 Views

What is Variadic Function In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., a function that accepts a variable number of arguments. So, In C++ programming, we can say that a function that accepts a variable number of arguments is variadic function. Why Variadic Function Templates Are Used? In C++, templates can only have a fixed number of parameters, which must be specified at the time of declaration. However, variadic templates can help to solve this issue. Syntax to Create Variadic Function Following is the variadic function template syntax: template return_type function_name(arg var1, ... Read More

Default virtual behavior in C++ vs Java

Aman Kumar
Updated on 16-May-2025 17:05:01

232 Views

The default behaviour of virtual functions in C++ and Java is significantly different, especially in terms of the handling of method overriding and polymorphism. Default Virtual Behavior in C++ C++ methods are non-virtual by default. To enable dynamic polymorphism, the virtual keyword must be explicitly used in the base class when defining a method. If virtual is not given, the method call is handled at compile time using the object of static type. Example In this example, we implement the default behaviour of the virtual function in C++: #include using namespace std; class Base { public: void nonVirtualMethod() { cout

Why do we need a pure virtual destructor in C++?

Aman Kumar
Updated on 16-May-2025 17:09:11

965 Views

Need a pure virtual destructor in C++In C++, the main factor where a pure virtual destructor is needed are with abstract classes and polymorphism. It make sure proper clean-up and avoids memory leaks when working with objects of derived classes through base class pointers. If a class has a virtual function, it's usually meant to be used polymorphically, which means that objects of derived classes can be used the same way as objects of the base class. When destroying instances of a derived class using a base class pointer object, a virtual destructor is used to free up memory space ... Read More

Why is a C++ pure virtual function initialized by 0?

Aman Kumar
Updated on 16-May-2025 17:10:40

3K+ Views

In C++, the pure virtual function is initialized with = 0 to indicate that it must be overridden by the derived class and has no implementation in the base class. A pure virtual function is a virtual function in C++ declared in a base class that has no implementation within that class. Why Initialize by 0? The following are the reasons for initializing by 0: 1. Mark The Function as "Pure Virtual" The = 0 syntax tells the compiler that the function must be overridden by any derived class. The base ... Read More

What is a virtual base class in C++?

Aman Kumar
Updated on 20-May-2025 19:30:46

2K+ Views

Virtual Base ClassVirtual base classes are used in virtual inheritance. It is a way of preventing multiple instances of given classes occurs in the inheritance hierarchy when using multiple inheritance. Why We Use Virtual Base Class? The use of a virtual base class ensures that only one shared instance exists in memory. Virtual base class prevents duplicate base class copies in multiple inheritance. Virtual base classes provide efficient memory management. It avoids redundant copy of base class variables. It ... Read More

Pure Virtual Functions and Abstract Classes in C++

Aman Kumar
Updated on 28-May-2025 16:47:25

25K+ Views

C++ Pure Virtual FunctionsA pure virtual function in C++ is a virtual function that has no definition in the base class and must be overridden in derived classes. It is declared by assigning = 0 in its declaration. Syntax Following is the syntax of the pure virtual function: class Shape { public: virtual double area() = 0; }; C++ Abstract Class A class that contains at least one pure virtual function is called an abstract class. Abstract classes cannot instantiated directly; instead, they work as a blueprint of the derived class. Any class inheriting from an ... Read More

C++ Program to Implement Traveling Salesman Problem using Nearest Neighbour Algorithm

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

1K+ Views

Here is a C++ Program to Implement Traveling Salesman Problem using Nearest Neighbour Algorithm.Required functions and pseudocodesAlgorithmBegin    Initialize c = 0, cost = 1000;    Initialize g[][].    function swap() is used to swap two values x and y.    function cal_sum() to calculate the cost which take array a[] and size of array as input.    Initialize sum = 0.    for i = 0 to n       compute s+= g[a[i %3]][a[(i+ 1) %3]];    if (cost >s)       cost = s    function permute() is used to perform permutation:       if ... Read More

C++ Program to Implement Nearest Neighbour Algorithm

Aman Kumar
Updated on 21-May-2025 14:25:59

2K+ Views

In this article, we will see a C++ program to implement the nearest neighbour algorithm: The nearest neighbour algorithm is a greedy algorithm used to find the approximate solution to the Travelling Salesman Problem (TSP) by computing the minimum cost required to visit all the nodes by traversing across the edges only once. We can implement this algorithm using different data structures like arrays, linked lists, or trees for efficient searching. How Nearest Neighbour Algorithm Works It start at a given point (eg., a city in TSP). Find the nearest unvisited ... Read More

Program to Find Area of an Ellipse using C++

Aman Kumar
Updated on 21-May-2025 14:30:43

568 Views

An ellipse is the locus on all those points in a plane such that the sum of their distance from two fixed points in the plane is constant. Where the fixed point is known as foci, which are sounded by the curve, the fixed line is a directrix, and the constant ratio is the eccentricity of the ellipse. Area of an Ellipse The area of an ellipse is the region enclosed by the ellipse. Which is computed by the following formula: Area = Π∗a∗b Let's discuss the following key points of an ellipse: ... Read More

Advertisements