Objects and Classes

Objects in OOP:

Basically, as the name suggests ‘objects’ are the fundamental part of Object Oriented Programming. It is nothing but instance of a particular class. They can represent a place or a person or anything that a program has to handle. It is a piece of code which represent real life entity. The combination of both data and function into a single unit and these units are called objects. An object not only knows how to perform certain actions but also it can interact with other elements of the program. 

At a time of object creation, we don’t require any keyword. We can directly pass values through the objects. An object consists of data member and member functions. Data members specify object’s state and member functions specify its behaviour. Without a class, object doesn’t exist, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration similarly as we declare variables of basic types. 

Objects not only make the code more efficient but also it becomes easy to understand for everyone. At the same time, it has fast execution and it may have less errors and less memory leakage.

Classes in OOP:

Class is one of the major pillars that are used in Object Oriented Programming. It is basically collection of objects, we can also called it as a blueprint of an object. Once a class has been defined, we can create infinite number of objects belonging to that class. It is similar to structure in C where we can define variables and methods to utilize by objects. Without objects we can use only static functions of class. It doesn’t occupy memory location. It cannot be manipulated as like of objects because it is not available in memory. 

 It is defined with the keyword ‘class’. A class contains data members and it has member functions. A class also defines a namespace so classes offer data abstraction(encapsulation) of Object Oriented Programming. Classes have same format as that of data structures except that they can also include functions and they have ‘access specifiers’. There are three access specifiers namely private, public and protected. 

 By default, functions and data declared within a class are ‘private’, and are accessible inside the definition of the class. While ‘public’ members are accessible everywhere and ‘protected’ members are mostly used only when inheritance is involved and accessible from other members of same class but also from members of their derived class. 
 For example: Let’s take an example of Employee to create our class. 
 class Employee {                    //Class is defined 
 public:                                     //Access specifier
 int id;                                       //Attribute(int variable) 
 string name;                           //Attribute(string variable) 
 float salary;                            //Attribute(float variable) 
 void display ()                       //Member Function 
 { cout<<id<<””<<name<<””<<salary<<endl; } 
 }; 

 A class keyword is used to create a class Employee. Access specifier is public therefore members are accessible outside the class. Variables in the class are id, name and salary which are attributes of Employee and display is a member function.

- Aman Ladkat
  (K 45)

Comments

Post a Comment

Popular posts from this blog

Do you have a ticket?

It's moderator's Office

Dynamic Memory Allocation in C++