C++ Articles

Page 570 of 597

C++ map having key as a user defined data type

sudhir sharma
sudhir sharma
Updated on 19-Sep-2019 1K+ Views

A map is a data structure that stores information in the form of key and value pairs. In C++, map is defined in STL (standard template library) and store keys in an ordered form.Syntax to define a map −map map_name;The data type of any of these two data of the map can be any of the data types. We can have any of the primary data types or derived data types as key or value data types in a map.We can use any of the data types as the data type of the key of the map. Even a user-defined ...

Read More

C++ interview questions on virtual function and abstract class

sudhir sharma
sudhir sharma
Updated on 19-Sep-2019 1K+ Views

What is a virtual function?A virtual function is a method that does not have a definition when defined in the base class. This method is left black in the parent class and it is redefined in the child class.What is an abstract class?An abstract class is a class that has abstract members or at least one pure virtual function in its definition. An abstract class can never be instanced (creating an object). It can only be inherited and the methods could be overwritten.Can there be any virtual Destructors?Yes, These are legal in C++, but these are destructors are for base ...

Read More

Print leaf nodes in binary tree from left to right using one stack in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 22-Aug-2019 307 Views

Program should print the leaf nodes of a binary tree from left to right but the challenge involves is using of only one stackThrough push() function insert nodes of a binary tree and through pop() operation display the leaf nodes.Leaf nodes are the end nodes whose left and right pointer is NULL which means that particular node is not a parent node.ExampleInput : 12 21 32 41 59 33 70 Output : 41 59 33 70Stack is a data structure which is a LIFO structure in which top pointer will point to the last elements inserted so the leaf nodes ...

Read More

"static const" vs "#define" vs "enum" ?

sudhir sharma
sudhir sharma
Updated on 20-Aug-2019 5K+ Views

"static const"“static const” is basically a combination of static(a storage specifier) and const(a type qualifier).The static determines the lifetime and visibility/accessibility of the variable. This means if a variable is declared as a static variable, it will remain in the memory the whole time when the program is running, while the normal or auto variables are destroyed when the function (where the variable was defined) is over.The const is a type qualifier. A type qualifier is used to express additional info about a value through type system. When a variable is initialized using the const type qualifier, it will not ...

Read More

"delete this" in C++?

sudhir sharma
sudhir sharma
Updated on 20-Aug-2019 2K+ Views

Delete is an operator that is used to Deallocate storage space of Variable.This pointer is a kind of pointer that can be accessed but only inside nonstatic member function and it points to the address of the object which has called the member function.This pointer holds the address of the current object, in simple words, you can say that this pointer points to the current object of the classWhenever we call a member function through its object, compiler secretly passes the address of calling that object as the first parameter in member function as this pointer.Generally, delete operator should not ...

Read More

Binary Search Tree insert with Parent Pointer in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 932 Views

We can insert new node into the BST in recursive manner. In that case we return the address of the root of each subtree. Here we will see another approach, where parent pointer will need to be maintained. Parent pointer will be helpful to find ancestor of a node etc.The idea is to store the address of the left and right subtrees, we set parent pointers of the returned pointers after recursive call. This confirms that all parent pointers are set during insertion. The parent of root is set to null.Algorithminsert(node, key) −begin    if node is null, then create ...

Read More

Assign weights to edges such that longest path in terms of weights is minimized in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 182 Views

Here we will see one problem, in this problem one edge of a tree and a sum S is given. The task is to assign weights to all other weights, such that longest path in terms of weight is minimized. The sum of weights assigned to it is same as ‘S’.The approach is simple. The property of a tree that a path can have a maximum of two leaf nodes in it. That will be used to get the solution. So if we assign weights only to the edges connecting the leaf nodes, and assign other edges to 0. Then ...

Read More

Array Index with same count of even or odd numbers on both sides in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 215 Views

Here we will see one problem, suppose one array is given. There are n elements. We have to find one index, where frequency of even numbers of its left and the frequency of even numbers of its right side are same, or the frequency of odd numbers of its left is same as the frequency of odd numbers of its right. If no such result is there, return -1.Suppose the array is like {4, 3, 2, 1, 2, 4}. The output is 2. The element at index 2 is 2, there is only one odd number at left of it, ...

Read More

Area of the Largest square that can be inscribed in an ellipse in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 183 Views

Here we will see the area of largest square that can be inscribed in an ellipse. The square in ellipse will be like below −The area of ellipse is −Now, if x and y are same, thenSo area is −Example#include #include using namespace std; float area(float a, float b) {    if (a < 0 || b < 0 ) //if values are is negative it is invalid       return -1;    float area = (4*(a*a + b*b)) / (a*a*b*b);    return area; } int main() {    float a = 4, b = 2;    cout

Read More

Area of the circumcircle of any triangles with sides given in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 212 Views

Here we will see how to get the area of the circumcircle of any triangle whose sides are given. Here the side AB is a, BC is b and CA is c, the radius is ‘r’.The radius r is same as −Example#include #include using namespace std; float area(float a, float b, float c) {    if (a < 0 || b < 0 || c < 0) //if values are is negative it is invalid       return -1;    float s = (a + b + c) /2;    float triangle = sqrt(s * (s - ...

Read More
Showing 5691–5700 of 5,962 articles
« Prev 1 568 569 570 571 572 597 Next »
Advertisements