In this tutorial, you will learn oops concepts in c++, oops definition in c++, types of oops languages, etc,.
OOPS
- It is a methodology or paradigm to solve tasks using class and objects
- Ex. C++, Java, C#, Python, Ruby, etc, …
TYPES OF OOPS LANGUAGES
- Statically Typed OOPS (Static OOPS)
- It performs type checking at compile time
- Type checking:
- If the source code contains any type related error, then it won’t permit compilation.
- Also, data types must be mentioned in the variable definition / function definition
- Example Languages
- C++, Java, C#
- Dynamically Types OOPS (Dynamic OOPS)
- It performs type checking at runtime
- Type checking:
- If source contains any type errors, then it will permit compilation. Errors will be thrown at run time.
- Also, data types need not be mentioned in the variable definition / function definition
- In dynamic languages, variables need not be initialized (default value) which is a big advantage for many programmers
- Example Languages:
- Ruby, Python, Boo, etc, …
FEATURES OF OOPS
- Class and Object
- Encapsulation
- Inheritance
- Polymorphism
- Message Passing
(A) Class
- Class is a set of objects
- User defined type
- Unlike structure, it contains both data and functions
- It a basic building block for developing applications in c++
- Generic type
- In a class, the variable is called as “data member” and function is called as “member function”
Object
- It is an instance of a class
- Specific Type
- It is used to call / access instance variable and functions
- Object can be local or global
- It is created, when constructor of a same class is created. It will be destroyed, when it reaches out of the scope
- Object is a real world entity
NOTE
- Memory will not be allocated when class is defined, but memory will be allocated only when object is created.
Example 1
Example 2
(B) Encapsulation
- Process of joining data and functions into a single entity (called as class) is called encapsulation
Design Example
Code Example
class Test { public: int id; char city[60]; void disp() { // code } };
(C) Inheritance
- Process of creating new class from old class is called as inheritance
- The old class is called as base class and the new class is called as derived class
- It is mainly used to implement the concept of polymorphism
- It provides the relationship between base class and derived class
- The base class is most general and derived class is most specific.
(D) Polymorphism
- One name, many forms
- If a single function or same function performs different operations, then it is called as polymorphism
Types
1. Compile Time Polymorphism
Ex. Function Overloading
2. Run time Polymorphism
Ex. Virtual Function
Example
(E) Message Passing
- It is nothing but sending and receiving information by the objects / class
- Objects can communicate with each other by sending and receiving information
- In the programming view, sending data from calling function to called function
- Passing data from current function to all other functions
- Passing data from main function to all other functions
In the message passing, object name / class name, function name and information must be mentioned.