Found 33676 Articles for Programming

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

552 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

C++ Program to Construct Transitive Closure Using Warshall’s Algorithm

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

902 Views

If a directed graph is given, determine if a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Reachable mean that there is a path from vertex i to j. This reach-ability matrix is called transitive closure of a graph. Warshall algorithm is commonly used to find the Transitive Closure of a given graph G. Here is a C++ program to implement this algorithm.AlgorithmBegin    1.Take maximum number of nodes as input.    2.For Label the nodes as a, b, c …..    3.To check if there any edge present between ... Read More

C++ Program to Implement Queue

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

3K+ Views

QueueThe queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first.Queue operations are −EnQueue (int data) − Insertion at rear endint DeQueue()− Deletion from front endThis is a C++ program to implement queue using array.AlgorithmBegin    function Enqueue() to insert elements in queue:       If queue is completely filled up then print “Overflow”.       Otherwise insert element at rear.       Update the value of rear End Begin    function Dequeue() to delete elements from ... Read More

fread() function in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

502 Views

The C/C++ library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given stream into the array pointed to, by ptr. Following is the declaration for fread() function.size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)The Following table contains the fread() parameters and description:ParametersDescriptionptrThis is the pointer to a block of memory with a minimum size of size*nmemb bytes.sizeThis is the size in bytes of each element to be read.nmembThis is the number of elements, each one with a size of size bytes.streamThis is the pointer to a FILE object that specifies an input stream.The ... Read More

C++ Program to Implement Dequeue

Aman Kumar
Updated on 22-May-2025 18:14:56

12K+ Views

A deque is a double-ended queue linear data structure where elements can be added or removed from both the front and rear ends. Unlike a standard queue which is FIFO. A dequeue can function in both FIFO and LIFO modes. Application of Dequeue Deques are useful in a situation where you need to add or remove an element from both ends, such as in buffer implementation managing undo/redo functions or implementing certain algorithms. Deque Operations Deque supports the following operations: insert_at_beg(): inserts an item at the front of Dequeue. insert_at_end(): inserts ... Read More

Get and Set the stack size of thread attribute in C

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

1K+ Views

To get and set the stack size of thread attribute in C, we use the following thread attributes:pthread_attr_getstacksize()Use for get threads stack size. The stacksize attribute gives the minimum stack size allocated to threads stack. In case of a successful run, then it gives 0 otherwise gives any value.It takes two arguments −pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize)First one for pthread attribute.Second one for giving the size of the thread attribute.pthread_attr_setstacksize()Used for set new threads stack size. The stacksize attribute gives the minimum stack size allocated to threads stack. In case of a successful run, then it gives 0 otherwise it gives ... Read More

Advertisements