Why C++ is partially Object Oriented Language? - GeeksforGeeks

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.
Here are the reasons C++ is called partial or semi Object Oriented Language:
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");    }}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.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++.
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");    }}
#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");    }}
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.
#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.
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.
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Convert string to char array in C++
Iterators in C++ STL
Inline Functions in C++
List in C++ Standard Template Library (STL)
Multithreading in C++