Found 33676 Articles for Programming

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

285 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

809 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

632 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

963 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

509 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

Returning multiple values from a function using Tuple and Pair in C++

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

3K+ Views

In C or C++, we cannot return more than one value from a function. To return multiple values, we have to provide output parameter with the function. Here we will see another approach to return multiple value from a function using tuple and pair STL in C++.The Tuple is an object capable to hold a collection of elements, where each element can be of different types.The pair can make a set of two values, which may be of different types. The pair is basically a special type of tuple, where only two values are allowed.Let us see one example, where ... Read More

static keyword in C++ vs Java

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

653 Views

In C++ or Java we can get the static keyword. They are mostly same, but there are some basic differences between these two languages. Let us see the differences between static in C++ and static in Java.The static data members are basically same in Java and C++. The static data members are the property of the class, and it is shared to all of the objects.Examplepublic class Test {    static int ob_count = 0;    Test() {       ob_count++;    }    public static void main(String[] args) {       Test object1 = new Test();   ... Read More

Inheritance in C++ vs Java

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

1K+ Views

In C++ and Java, there are the concept of Inheritance. The inheritance properties are used to reuse the code and also make a relationship between two objects. Here we will see some basic differences between inheritance in C++ and inheritance in Java.In Java, all of the classes are extending the Object class. So there is always a single level inheritance tree of classes. The object class is present at the root of the tree. Let us check this is true or not using a simple code.Example//This is present in the different file named MyClass.java public class MyClass {    MyClass() ... Read More

Advertisements