Classes and Objects
Tags:
oop
java
cpp
In object-oriented programming (OOP), real world entities are represented as objects. Each entity will have certain properties and behaviour
Class
- A Class is a blueprint design for creating objects of a kind
- It defines the properties (data members denoting state) and behaviour (methods) applicable to all instances of that class. Thus, each object will hold some data and code to manipulate that data
Object
- Objects are runtime entities representing some real-world item
- Each object is an instance of it’s class.It can also be seen as a variable of type as it’s class.
We define the class once and then create as many object of it as we need. Object occupy memory as they are entities that phsically exist, while classes don’t.
Java example
C++ example
In C++, a class
and struct
are technically almost the same with the only difference being that the members are private
by default inside a class
and public
by default inside a struct
. But semantically, structs are used just as a combined block of data and classes are used for OOP features