1

I am currently reading the book Modern C++ for Absolute Beginners by Slobodan Dmitrović that has the following statement in the chapter types:

Every entity has a type. What is a type? A type is a set of possible values and operations. Instances of types are called objects.

So bool, char, int, ... are all type's.

What does he mean by "Instances of types are called objects." ? For example, what would be an instance of int?

  • 5
    5 is an instance of int. `int i;` <- i is an instance of int – user253751 Aug 31 '20 at 13:29
  • 4
    @user253751 Technically `5` and `'5'` are not objects, they are literals. `5` doesn't have a lifetime or storage duration, which are properties an object has. – François Andrieux Aug 31 '20 at 13:32
  • 2
    Definition of an Object - https://en.cppreference.com/w/cpp/language/object Note that the C++ definition of an Object is not the same as what people generally mean when they refer to Objects in OOP sense. – Richard Critten Aug 31 '20 at 13:40
  • This question is quite interesting and can take you deep into the rabbit hole of philosophy - what does it even mean for a piece of data in a computer program to *be*? (As Fred Brooks said, "The programmer, like the poet, works only slightly removed from pure thought-stuff.") – molbdnilo Aug 31 '20 at 14:12
  • Does this answer your question? [What is the difference between an Instance and an Object?](https://stackoverflow.com/questions/2885385/what-is-the-difference-between-an-instance-and-an-object) or [Difference between Object and instance : C++](https://stackoverflow.com/questions/22206044/difference-between-object-and-instance-c) or probably plenty more. – underscore_d Aug 31 '20 at 14:18

1 Answers1

3

In simple language when it says : Instances of types are called objects it means, when you have defined a prototype or a blueprint and you want to use that blueprint you need to declare an object of that type.

You have different types.

  1. Primitive data type like int, long, char, short etc
  2. User defined data types like Class, Structure etc
  3. Derived data types like Function Array Pointers etc

And to use any data type you need to have an object for that type.

For example : Primitive data type , you have int i; When you declare this way, you mean you need a variable/object of 4 bytes of memory. So here i is your object. So you cannot use the object i without the type. Similarly you may require different objects for different types.

For non-primitive types also the same logic apply.

Suppose you have a class as below :

class Student {
public:
    int rollNum;
    String name;

}

This is your blueprint, your type. When you need to use class variables you need to create an object for your type. like below :

Student s;

And you will be able to access the variables and assign them values according to your wish.

s.rollNum = 10;
s.name = "Martin";

So your object is : s which is a type of Student

More from a blog post on "Class vs Object vs Instance":

In short, An object is a software bundle of related state and behavior. A class is a blueprint or prototype from which objects are created. An instance is a single and unique unit of a class.

Object Real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). Car have state (current speed, current gear) and state (applying brake, changing gear). Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields and exposes its behavior through methods.

Class Class is a “template” / “blueprint” that is used to create objects. Basically, a class will consists of field, static field, method, static method and constructor. Field is used to hold the state of the class (eg: name of Student object). Method is used to represent the behavior of the class (eg: how a Student object going to stand-up). Constructor is used to create a new Instance of the Class.

Instance An instance is a unique copy of a Class that representing an Object. When a new instance of a class is created, the Compiler or JVM (for java) will allocate a room of memory for that class instance.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Som
  • 1,522
  • 1
  • 15
  • 48
  • did you copy half of your answer from here: https://codegym.cc/help/1148 ? The similarities are too suspicious, and to be honest I suspect that the other half is copied from elsewhere... – 463035818_is_not_an_ai Aug 31 '20 at 14:51
  • @idclev463035818 No .I have updated my answer from where I have taken the inputs.... other than the definitions all are written by me. – Som Aug 31 '20 at 15:01