Why C++ is partially Object Oriented Language?


The basic thing which are the essential feature of an object oriented programming are Inheritance, Polymorphism and Encapsulation. Any programming language that supports these feature completely are complete Object-oriented programming language whereas any language that supports all three feature but does not supports all features completely are Partial Object-oriented programming language.

Inheritance is used to provide the concept of code-reusability.
Polymorphism makes a language able to perform different task at different instance.
Encapsulation makes data abstraction (security or privacy to data) possible. In object-oriented programming language, Encapsulation is achieved with the help of a class.

Here are the reasons C++ is called partial or semi Object Oriented Language:



  1. Main function is outside the class : C++ supports object-oriented programming, but OO is not intrinsic to the language. You can write a valid, well-coded, excellently-styled C++ program without using an object even once.
    In C++, main function is mandatory, which executes first but it resides outside the class and from there we create objects. So, here creation of class becomes optional and we can write code without using class.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        cout << "Hello World";
        return 0;
    }

    chevron_right

    
    

    While in JAVA, main function is executed first and it reside in the class which is mandatory. So, we can’t do anything without making Class. For doing the same thing as above, we need to make a class as :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class hello
    {
        public static void main(String args[])
        {
            System.out.println("Hello World");
        }
    }

    chevron_right

    
    

  2. Concept of Global variable : In C++, we can declare a variable globally, which can be accessible from anywhere and hence, it does not provides complete privacy to the data as no one can be restricted to access and modify those data and so, it provides encapsulation partially whereas In JAVA, we can declare variable inside class only and we can provide access specifier to it.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
       
    // Global variable declaration:
    int g = 50;
       
    int main () 
    {
        // global variable g
        cout << g;
          
       // Local variable g
       g = 20;
       cout << g;
       
       return 0;
    }

    chevron_right

    
    

    Output:

    50 20 

    So, in JAVA, basically every data is asked explicitly by user if it should be accessible or not.

  3. Availability of Friend function: Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.
    Therefore, again the Object oriented features can be violated by C++.


Related Article:
Why Java is not a purely Object-Oriented Language?

This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.