Found 7346 Articles for C++

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

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

699 Views

There are no ill-effects of allowing a pure virtual destructor in C++ program. It is must to provide a function body for pure virtual destructor as derived class’s destructor is called first before the base class destructor, so if we do not provide a function body, it will find out nothing to be called during object destruction and error will occur. We can make easily an abstract class by making a pure virtual destructor with its definition.Example Code Live Demo#include using namespace std; class B {    public: virtual ~B()=0; // Pure virtual destructor }; B::~B() {    cout

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

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

2K+ Views

It’s just a syntax, nothing more than that for saying that “the function is pure virtual”.A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in declaration.Here is an example of pure virtual function in C++ programExample Code Live Demo#include using namespace std; class B {    public: virtual void s() = 0; // Pure Virtual Function }; class D:public B {    public: void s() {       cout s(); }OutputVirtual Function in Derived class

What is a virtual base class in C++?

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

1K+ Views

The virtual base class is used when a derived class has multiple copies of the base class.Example Code#include using namespace std; class B {    public: int b; }; class D1 : public B {    public: int d1; }; class D2 : public B {    public: int d2; }; class D3 : public D1, public D2 {    public: int d3; }; int main() {    D3 obj;    obj.b = 40; //Statement 1, error will occur    obj.b = 30; //statement 2, error will occur    obj.d1 = 60;    obj.d2 = 70;    obj.d3 = 80;    cout

Pure Virtual Functions and Abstract Classes in C++

Nishtha Thakur
Updated on 07-Oct-2023 02:39:22

22K+ Views

A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function. Abstract class can have normal functions and variables along with a pure virtual function. Abstract class cannot be instantiated, but pointers and references of Abstract class type can be created. ... 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

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

2K+ Views

This is a C++ program to implement Nearest Neighbour Algorithm which is used to implement traveling salesman problem to compute the minimum cost required to visit all the nodes by traversing across the edges only once.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]];   ... Read More

Program to find the Area of an Ellipse using C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

420 Views

Here we will see how to get the area of an ellipse using C++. The Ellipse has different parts. These are like below.Key-pointsDescriptionCenterThe center of the ellipse. It is also center of the line segments which links two foci.Major AxisThe longest diameter of an ellipsenmembThis is the number of elements, each one with a size of size bytes.Minor AxisThe smallest diameter of an ellipseChordThe line segment which points tFocusTwo points that are pointed in the diagramLotus RectumThe lotus rectum is a line passes through the focus and perpendicular to the major axis of an ellipseThe area of an ellipse is ... Read More

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

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

729 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

367 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

Advertisements