Object Oriented Programming

Object Oriented Programming


Basic of Object Oriented Programming

Difference between structure and class:

Structure Class
Members of a structure are public by default Members of a class are private by default
It does not support inheritance Support inheritance
Structure is value type, They are stored as a stack on memory class is reference type. They are stored as heap on memory.
It can have only parameterized constructor It can have all the types of constructor and destructor
Generally used for smaller amounts of data. Generally used for large amounts of data.

Class:

A Class is a user-defined data-type which has data members and member functions. Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behavior of the objects in a Class. Class is a blueprint of data and functions or methods. 
There are two ways to define class member functions:
  • Inside Class Definition
  • OutSide Class Definition
To define a member function outside the class definition we have to use the scope resolution:: operator along with class name and function name.
RETURN-TYPE CLASS-NAME :: FUNCTION-NAME(PARAMETERS)

Why is the size of an empty class not zero in C++?

The size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses.

Object:

Objects are basic run-time entities in an object-oriented system, objects are instances of a class these are defined user-defined data types.

Encapsulation:

Wrapping up(combing) of data and functions into a single unit is known as encapsulation. The data is not accessible to the outside world and only those functions which are wrapping in the class can access it. This insulation of the data from direct access by the program is called data hiding or information hiding.

Encapsulation in c++

Data abstraction:

Data abstraction refers to, providing only needed information to the outside world and hiding implementation details. Using abstract class we can hide the implementation details

Data-abstraction in c++

Characteristics of Abstruct class

  • A class is abstract if it has at least one pure virtual function.
  • We can have pointers and references of abstract class type.
  • If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.
  • An abstract class can have constructors.

Inheritance:

inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. Inheritance provides reusability.

Inheritance in c ++

Polymorphism:

polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation. C++ supports operator overloading and function overloading. The process of making an operator exhibit different behaviors in different instances is known as operator overloading. Function overloading is using a single function name to perform different types of tasks. Polymorphism is extensively used in implementing inheritance.
In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Real-life examples of polymorphism, a person at the same time can have different characteristics. As a man at a same time is a father, a husband, an employee. So the same person posses have different behavior in different situations. This is called polymorphism.

Polymorphism in c++

In C++ polymorphism is mainly divided into two types:
  • Compile-time polymorphism:
    • Function Overloading: When there are multiple functions with the same name but different parameters then these functions are said to be overloaded.
    • Operator Overloading: C++ also provides the option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add to operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.
  • Run time Polymorphism:
    • Function overriding: on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

Dynamic Binding:

In dynamic binding, the code to be executed in response to the function call is decided at runtime. C++ has virtual functions to support this.

Message Passing:

Objects communicate with one another by sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function, and the information to be sent.

Access modifier:

Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
There are three type of access modifiers:
  • Public: All the class members declared under the public will be available to everyone.
  • Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
  • Protected: Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any sub-class (derived class) of that class.

Friend class and function in C++

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.

Friend class in c++

Friend Function: Like a friend class, a friend function can be given special grant to access private and protected members.

Friend function in c++

A friend function can be:
  • A method of another class
  • A global function
Rules for declaring friend function:
  1. Create forward reference for the class, which member variables are accessed.
  2. Create the class which funtion is going to friend function of another class
  3. Create the class which forward reference is being created first
  4. Define the friend function with :: resulation operator outside the class. Not define inline function
Some important points about friend functions and classes:
  1. Friends should be used only for limited purpose. too many functions or external classes are declared as friends of a class with protected or private data, it lessens the value of encapsulation of separate classes in object-oriented programming.
  2. Friendship is not mutual. If a class A is friend of B, then B doesn’t become friend of A automatically.
  3. Friendship is not inherited
  4. The concept of friends is not there in Java.
Share:

0 Comments:

Post a Comment