Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Object Oriented Programming

Object-oriented programming is programming based on the concept of objects.

Take an example,

In Code:Block, program looks like this

In above program, class Student contains the variables id and name.

class Student {    
  public:             
    int id;        
    string name;  
};

To access the variables id and name from the class Student, there is the need of an object for the class Student.

Syntax to create object:

ClassName ObjectName;

Objects for a class are always created in a function. Here object is created in a main function.

int main() {
  Student obj;  
  return 0;
}

Now with the help of object obj, members of class Student are accesible.

Syntax to access class members using objects:

ObjectName.ClassMemberName;

Using object obj, assign a value to variable id and name.

obj.id = 30;
obj.name = "EasyExamNotes.com";

To print the values of variables code is,

cout << obj.id << "\n";
cout << obj.name;

C++ program to implement use of object

#include <iostream>
#include <string>
using namespace std;

class Student {      
  public:            
    int id;        
    string name;  
};

int main() {
  Student obj;  

  obj.id = 30;
  obj.name = "EasyExamNotes.com";

  cout << obj.id << "\n";
  cout << obj.name;
  return 0;
}

Output


Now it will be easy to write some theory points on object oriented programming.

  • Object oriented programming involve use of object.
  • Object is like a key for any class.
  • Class members are accessible, only in the function wehre object is created.
  • A class can have more than one object.
  • Without class there is no existence of object.

What is class in OOP ?

  • A class is like a blueprint for an object.
  • Class is like a container, to store data and functions.
  • Class is a user defined data type.
  • When a class is defined no memory allocated without object.
  • In above program, Student is like a variable whose data type is Class.

We will read more about class in the next article.

What is an Object of a class ?

  • An Object is an instance of a Class.
  • Object makes class members accessible.
  • Objects are runtime entities.
  • When object is created, memory is allocated to the class.

Some other examples of OOP ?

C++, Python, Java, C Sharp dot net, etc.

Categories OOP