Você está na página 1de 5

Object Oriented Programming Concepts

OOP CONCEPTS

Object-oriented programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. It utilizes several techniques from previously established paradigms, including inheritance, modularity, polymorphism, and encapsulation. Even though it originated in the 1960s, OOP was not commonly used in mainstream software application development until the 1990s. Today, many popular programming languages (such as Java, JavaScript, C#, C++, Python, PHP, Ruby and Objective-C) support OOP.

Objects C++ introduces some object-oriented (OO) features to C. It offers classes, which provide the four features commonly present in OO (and some nonOO) languages: abstraction, encapsulation, inheritance and polymorphism. Objects are set by classes, which are basically like a set of attributes already defined, and can be created at any time.

Encapsulation C++ implements encapsulation by allowing all members of a class to be declared as either public, private, or protected. A public member of the class will be accessible to any function. A private member will only be accessible to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends"). A protected member will be accessible to members of classes that inherit from the class in addition to the class itself and any friends. The OOP principle is that all and only the functions that can access the internal representation of a type should be encapsulated within the type definition. C++ supports this (via member functions and friend functions), but does not enforce it: the programmer can declare parts or all of the representation of a type to be public, and is also allowed to make public entities that are not part of the representation of the type. Because of this, C+ + supports not just OO programming but other weaker decomposition paradigms, like modular programming. .

It is generally considered good practice to make all data private or protected, and to make public only those functions that are part of a minimal interface for users of the class, that hides implementation details.

Inheritance

Inheritance from a base class may be declared as public, protected, or private. This access specifier determines whether unrelated and derived classes can access the inherited public and protected members of the base class. Only public inheritance corresponds to what is usually meant by "inheritance". The other two forms are much less frequently used. If the access specifier is omitted, inheritance is assumed to be private for a class base and public for a struct base. Base classes may be declared as virtual; this is called virtual inheritance. Virtual inheritance ensures that only one instance of a base class exists in the inheritance graph, avoiding some of the ambiguity problems of multiple inheritance. Multiple inheritance is a C++ feature sometimes considered controversial. Multiple inheritance allows a class to be derived from more than one base class; this can result in a complicated graph of inheritance relationships. For example, a "Flying Cat" class can inherit from both "Cat" and "Flying Mammal". Some other languages, such as C# or Java, accomplish something similar (although more limited) by allowing inheritance of multiple interfaces while restricting the number of base classes to one (interfaces, unlike classes, provide only declarations of member functions, no implementation or member data). Role of inheritance in OOP It has the capability to express the inheritance relationship which makes it ensures the closeness with the real world models. Another is the idea of reusablity. Inheritance allows the addition of additional features to an existing class without modifying it.The third reason is its transitive nature.If a class A inherits properties of another class B then all subclasses of A will automatically inherit the properties of B.

Data abstraction

Abstraction refers to the act of representing essential features without including the background details.To understand abstractoin take the example of a car its essential features are geer handling,steering handling,use of clutch,accelerator,brakes etc.But while driving we do not need to get into the internal details like wiring,motor working etc. Polymorphism

C++ supports several kinds of static (compile-time) and dynamic (runtime) polymorphism. Compile-time polymorphism does not allow for certain runtime decisions, while run-time polymorphism typically incurs more of a performance penalty. Static polymorphism Function overloading Function overloading allows programs to declare multiple functions with the same name. The functions are distinguished by the number and/or types of their formal parameters. Thus, the same function name can refer to different functions depending on the context in which it is used.

Operator overloading

Similarly, operator overloading allows programs to define certain operators (such as +, !=, <, or &) to result in a function call that depends on the types of the operands they are used on.
Class and function templates

Templates in C++ provide a sophisticated mechanism for writing generic, polymorphic code. In particular, through the Curiously Recurring Template Pattern it's possible to implement a form of static polymorphism that closely mimics the syntax for overriding virtual methods (a dynamic polymorphism technique described below). Since C++ templates are type-aware and Turing-complete they can also be used to let the compiler resolve recursive conditionals and generate substantial program

Dynamic polymorphism

Polymorphism through inheritance

Variable pointers (and references) of a base class type in C+ + can refer to objects of any derived classes of that type in addition to objects exactly matching the variable type. This allows arrays or other containers of a given type of object to hold pointers to multiple types of objects, which cannot be done otherwise in C+ +. Because assignment of values to variables usually occurs at run-time, this is necessarily a run-time phenomenon.

C++ also provides a dynamic_cast operator, which allows the program to safely attempt conversion of an object into an object of a more specific object type (as opposed to conversion to a more general type, which is always allowed). This feature relies on run-time type information (RTTI). Objects known to be of a certain specific type can also be cast to that type with static_cast, a purely compile-time construct which is faster and does not require RTTI.

Virtual member functions

Through virtual member functions, different objects that share a common base class may all support an operation in different ways. The member functions implemented by the derived class are said to override the same member functions of the base class. In contrast with function overloading, the parameters for a given member function are always exactly the same number and type. Only the type of the object for which this method is called varies. In addition to standard member functions, operator overloads and destructors can also be virtual.By virtue of inherited objects being polymorphic, it may not be possible for the compiler to determine the type of the object at compile time. The decision is therefore put off until runtime, and is called dynamic dispatch. In this way, the most specific implementation of the function is called, according to the actual run-time type of the object. In C++, this is commonly done using virtual function tables.