C++ Articles - Page 603 of 717

C++ Program to Perform Sorting Using B-Tree

Chandu yadav
Updated on 30-Jul-2019 22:30:26

473 Views

Here we will see how to get the sorted sequence using B-Tree. The B-tree is n-ary tree. To get the sorted sequences, we can create a B-tree, then add the numbers into it. Here the B-tree can hold maximum 5 nodes. If number of nodes increases, split the node and form new level. As the nodes are holding few number of elements like 5 (at most), we are using Bubble sorting techniques to sort them. as the number of elements is very low, then it will not affect too much on its performance.After traversing the tree, we will get all ... Read More

Fesetround() and fegetround() in C++

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

143 Views

Here we will see the fesetround() and fegetround() method in C++. These methods can be found in the cfenv library.The fesetround() method is used to set the specified floating point rounding direction to the current rounding direction. This is used with rint(), nearbyint() and some other rounding functions in C++.The syntax is like below −int fesetround(int round);The round can be among these FE_TONEAREST, FE_DOWNWARD, FE_UPWARD etc. This function returns 0 when rounding direction is successfully applied to the required manner.Example#include #include #include using namespace std; main() {    double x = 4.7, ans;    fesetround(FE_TONEAREST); //round to ... Read More

Hiding of all overloaded methods in base class in C++

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

600 Views

In C++, we can use the function overloading techniques. But if some base class has one method in overloaded form (different function signature with the same name), and the derived class redefines one of the function which is present inside the base, then all of the overloaded version of that function will be hidden from the derived class.Let us see one example to get the clear idea.Example#include using namespace std; class MyBaseClass {    public:       void my_function() {          cout

Inheritance and friendship in C++

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

2K+ Views

In C++, the friendship is not inherited. It means that, if one parent class has some friend functions, then the child class will not get them as friend.In this example it will generate an error because the display() function is friend of MyBaseClass but not the friend of MyDerivedClass. The display() can access the private member of MyBaseClass.Example#include using namespace std; class MyBaseClass {    protected:       int x;    public:       MyBaseClass() {          x = 20;       }       friend void display(); }; class MyDerivedClass : public MyBaseClass {    private:       int y;    public:       MyDerivedClass() {          x = 40;       } }; void display() {    MyDerivedClass derived;    cout

Extending namespace and Unnamed namespace

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

341 Views

Here we will see how we can extend some namespace, and how the unnamed or anonymous name space can be used.Sometimes we can define one namespace. Then we can write the namespace again with the same definition. If the first one has some member, and second one has some other members, then the namespace is extended. We can use all of the members from that namespace.Example#include using namespace std; namespace my_namespace {    int my_var = 10; } namespace my_namespace { //extending namespace    int my_new_var = 40; } main() {    cout

Can main() be overloaded in C++?

Tapas Kumar Ghosh
Updated on 03-Jun-2025 14:17:49

867 Views

In every C/C++ program, execution starts from the main() function. Defining multiple main() functions will result in a compilation error. Can main() be Overloaded in C++? No, we cannot overload the main() function in C++ because main() serves as the entry point of any C++ program and must follow a predefined prototype. While C++ does support function overloading (i.e., multiple functions with the same name but different parameters), this does not apply to the main() function. If you try to create multiple main() functions will result in a compilation error due to invalid overloading. The following are the only two ... Read More

Function overloading and const keyword in C++

Tapas Kumar Ghosh
Updated on 13-Jun-2025 14:02:47

691 Views

In C++, function overloading and const keyword are used for different purposes. Function overloading provides different ways to call a function with different parameter types that make the program more readable. While the const keyword provides the ways of declaration such as variable, member variable, function parameters, member function, and return type. What is Function Overloading? Function overloading is the process of defining multiple functions having the same name but different parameter lists. It is also known as compile-time polymorphism. Here, we have list of three points to describe function overloading in C++: The parameter ... Read More

Private Destructor in C++

Nishtha Thakur
Updated on 19-Nov-2024 18:21:52

1K+ Views

In C++, a private destructor is a destructor that is declared within a private access specifier, which means that the destructor cannot be accessed or called directly outside the class, which is useful for design patterns where the user wants to control how and when an object will destroy. Syntax Here is the following syntax for a private destructor, which is declared in the class like any other destructor, but with the private access specifier. class MyClass {private: // Private destructor ~MyClass() { }public: // Public constructor MyClass() { // Constructor code here }}; Here we will see what will ... Read More

Use of explicit keyword in C++

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

1K+ Views

Here we will see what will be the effect of explicit keyword in C++. Before discussing that, let us see one example code, and try to find out its output.Example#include using namespace std; class Point {    private:       double x, y;    public:       Point(double a = 0.0, double b = 0.0) : x(a), y(b) {          //constructor       }       bool operator==(Point p2) {          if(p2.x == this->x && p2.y == this->y)          return true;          return ... Read More

Simulating final class in C++

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

561 Views

In Java or C#, we can use final classes. The final classes are special type of class. We cannot extend that class to create another class. In C++ there are no such direct way. Here we will see how to simulate the final class in C++.Here we will create one extra class called MakeFinalClass (its default constructor is private). This function is used to solve our purpose. The main Class MyClass can call the constructor of the MakeFinalClass as they are friend classes.One thing we have to notice, that the MakeFinalClass is also a virtual base class. We will make ... Read More

Advertisements