Found 7197 Articles for C++

C++ Program to implement Slicker Algorithm that avoids Triangulation to find Area of a Polygon

Aman Kumar
Updated on 16-May-2025 16:58:55

165 Views

In this article, we implement a C++ program to find the area of a polygon using a slicker algorithm that avoids Triangulation to find the area of a polygon. What is Slicker Algorithm The Silcker algorithm is a method for calculating the area of a polygon by performing a series of calculations based on the polygon's vertices. It is an efficient alternative to triangulation, especially for polygons with many sides. How Silcker Algorithm Works? Since slicker is a method used to compute the area of a polygon without triangulation. Instead of breaking the polygon into triangles, it directly computes the ... Read More

C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line

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

551 Views

This is a C++ program to apply Above-Below-on Test to find the position of a point with respect to a Line. For any point t (xt, yt) on the plane, its position with respect to the line L connecting m and n is found by calculating the scalar s −Y = A xt + B yt + CIf Y< 0, t lies in the clockwise halfplane of L; if Y>0, t lies on the counter-clockwise halfplane; if Y= 0, t lies on L.AlgorithmBegin    Take the points as input.    For generating equation of the line, generate random numbers for ... Read More

Template Metaprogramming in C++

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

316 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

370 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

218 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

939 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

Advertisements