C++ Articles - Page 632 of 719

C++ Program to Implement Expression Tree Algorithm

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

465 Views

An expression tree is basically a binary which is used to represent expressions. In expression tree, internal nodes correspond to operators and each leaf node corresponds to an operand. Here is a C++ Program to implement the Expression Tree Algorithm which takes the postfix expression as an input and generates the corresponding expression tree traversed in inorder.AlgorithmBegin    function construct_expression_tree():       Flag = 1 when it is operand.       Flag = -1 when it is operator.       S = suffix[0] means read the first operand from the expression.    For i = 0 and ... Read More

Virtual Constructor in C++

Aman Kumar
Updated on 27-May-2025 16:31:50

23K+ Views

In C++, we cannot create a virtual constructor, this is because C++ is a statically typed language and the constructor is responsible for creating an object. So, the compiler needs to know the exact type of object at compile time. The virtual mechanism works only when we have a base class pointer to a derived class object. The constructor cannot be virtual, because when a constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet. So, the constructor should always be non-virtual. Implementation of Constructor & Destructor In the following ... Read More

Virtual Destructor in C++

Aman Kumar
Updated on 27-May-2025 16:32:51

2K+ Views

A virtual destructor is a destructor declared within the base class with a virtual keyword. In C++, destructors are special members of a class that frees memory occupied by an object when a memory leak occurs. Deleting a derived class object using a pointer to a base class, the base class should be defined with a virtual destructor. When to Use Virtual Destructor? Virtual destructorsare needed in scenarios where polymorphism and inheritance are involved, and instances of derived classes are managed by pointers to base classes. If our class has one or more virtual functions that are inherited from child ... Read More

Virtual functions in derived classes in C++

Aman Kumar
Updated on 26-May-2025 12:49:45

1K+ Views

Virtual Functions in Derived ClassesA virtual function is declared using the virtual keyword in the base class and becomes a member function of the base class overridden by the derived class. It becomes virtual in every class which is derived from the base class. So, the keyword virtual is not necessary in the derived class while declaring redefined versions of the virtual base class function. When we use a pointer or reference to the base class to refer to a derived class object, we can call its virtual function. Syntax Following is the syntax of the virtual function in ... Read More

Default arguments and virtual function in C++

Aman Kumar
Updated on 22-May-2025 18:28:36

337 Views

Default Argument A default argument is a value provided during the function declaration that can be automatically assigned if no argument is provided when the function is called. If a value is passed at the time of the function call, this default value is overridden, and the argument becomes a parametrized Argument. Syntax The syntax below shows how we can define a default argument function: int fun(int x = 0){ // function body } Example of Default Argument The following C++ example shows the working of the default argument: #include using namespace std; //function defined with ... Read More

Virtual Functions and Runtime Polymorphism in C++

Aman Kumar
Updated on 22-May-2025 18:11:33

6K+ Views

In C++, both virtual functions and runtime polymorphism are key features that enable dynamic behavior and code flexibility. Virtual Functions A virtual function is the member function that is declared in the base class using the keyword virtual and is overridden in the derived class. The virtual function enables runtime polymorphism means the function that will be executed is determined at runtime rather than at compile time. Characteristics of Virtual Function Following are the characteristics of the virtual function: Virtual function make sure the correct function is called for an object, regardless of the type ... Read More

C++ Program to Implement Sorted Singly Linked List

Aman Kumar
Updated on 22-May-2025 18:10:45

3K+ Views

A singly linked list is a type of linear data structure where each node contains two items: The data and a link to the next node in the list. Where, first node is called as head node and the last node is called the tail. So, you can traverse the linked list from the head node and continue until the link becomes null. Sorted Singly Linked List A singly linked list is supposed to be sorted when its elements are arranged in specific order (i.e., ascending or descending order), and each node contains data and a pointer (or reference) to ... Read More

C++ Program to Implement Sorted Doubly Linked List

Aman Kumar
Updated on 22-May-2025 18:12:31

2K+ Views

Sorted Doubly Linked List A sorted doubly linked list is a type of doubly linked list in which elements are arranged in a specific order, typically ascending or descending based on the data values. Where, insertion operation makes sure that the new node is placed in its correct sorted position. A doubly linked list is a two-way linked list in which a node is connected with two pointers, i.e., next and previous pointers, which are references to the next node and previous node, respectively. Characteristics of Sorted Doubly Linked List The following are the characteristics of the doubly linked ... Read More

C++ Program to Implement Sorted Circularly Singly Linked List

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

558 Views

In data structure, Linked List is a linear collection of data elements. Each element or node of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. Into a linked list the entry point is called the head of the list.Each node in the list stores the contents and a pointer or reference to the next node in the list, in a singly linked list. Singly linked list does not store any pointer or reference to the previous node.As it is a sorted singly linked ... Read More

C++ Program to Implement Sorted Circularly Doubly Linked List

Aman Kumar
Updated on 21-May-2025 14:37:29

842 Views

Sorted Circular Doubly Linked ListA sorted circular doubly linked list is a type of circular doubly linked list in which elements are arranged in a specific order, typically ascending or descending based on the data values. The insertion operation makes sure that the new node is placed in its correct sorted position. In Circular Doubly Linked List two consecutive elements are linked or connected by previous and next pointer and the last node points to first node by next pointer and the first node also points to last node by previous pointer. Characteristics of Sorted Circular Doubly Linked List ... Read More

Advertisements