Skip to content
Related Articles

Related Articles

Improve Article

Why C++ is partially Object Oriented Language?

  • Difficulty Level : Easy
  • Last Updated : 30 May, 2017


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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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.






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

    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 :




    class hello
    {
        public static void main(String args[])
        {
            System.out.println("Hello World");
        }
    }
  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.




    #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;
    }

    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
Recommended Articles
Page :