0

Picture of text

To read something, we need somewhere to read into; that is, we need somewhere in the computer's memory to place what we read. We call such a "place" an object. An object is a region of memory with a type that specifies what kind of information can be placed in it. A named object is called a variable. For example, character strings are put into string variables and integers are put into int variables. You can think of an object as a "box" into which you can put a value of the object's type

An object is a region of memory with a type that specifies what kind of information can be placed in it? Here they give an example with ints and strings which are like built in datatypes, I already know some oop so I was kinda confused because like I thought an object was kinda its own separate entity with its own user created datatype? I get that all objects have their own region of memory but they're basically calling a variable of type int, age, with the value 42 an object. Is this correct?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • In a language like Java, would you not call the `int` an object because it's not user created? One could argue that the book's usage is a tad wide, while yours is a tad narrow. – sweenish Sep 07 '21 at 12:50
  • Please don't use pictures of text. Also, when quoting text, give proper attribution to the actual author! As a new user here, please also take the [tour] and read [ask]. – Ulrich Eckhardt Sep 07 '21 at 12:50
  • Um my bad this is from Bjarne Stroustrup's Programming Principles and Practice using C++ –  Sep 07 '21 at 12:52
  • 7
    Yes, it is correct. In C++, the word "object" does not mean "instance of a class type". This is the definition of what the word "object" means in C++. Words can have different meanings in different contexts. – molbdnilo Sep 07 '21 at 12:55
  • There are no languages that I'm aware of where only instances of user-created types are called "objects". Do you have an example? – molbdnilo Sep 07 '21 at 12:57
  • I believe, this isn't an object like in OOP. This term is used in C++ specific sense. It is referred here and there, e.g. in so-called [aliasing rules](https://stackoverflow.com/a/7005988/7478597) which say that two pointers of different type may not address the same object (with only few exceptions) -- very roughly spoken. – Scheff's Cat Sep 07 '21 at 12:58
  • @eerorika in Java, `String` is not user defined, similarly `list` in Python, or `Array` in JavaScript – Caleth Sep 07 '21 at 13:10
  • @Caleth Hmm, I was wrong about Python and JS. Even classes are objects (not just instances of classes) in those languages. In Java, String is a class. – eerorika Sep 07 '21 at 13:19
  • In C++, "object" is a compiler-writer's term. – Pete Becker Sep 07 '21 at 13:21
  • @eerorika but `String` is not defined by the user, it is defined by the language. And there's the instances of `Class` that denote `String` et al – Caleth Sep 07 '21 at 13:23

2 Answers2

5

This is the wording from the C++ standard [intro.object]:

The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition, by a new-expression ([expr.new]), by an operation that implicitly creates objects (see below), when implicitly changing the active member of a union, or when a temporary object is created ([conv.rval], [class.temporary]). An object occupies a region of storage in its period of construction ([class.cdtor]), throughout its lifetime, and in its period of destruction ([class.cdtor]).

There is more to read about objects, I just picked this paragraph from the intro so you can see that the term "object" in C++ is more general than "instance of a user defined class". Note how the last sentence is quite similar with what Bjarne wrote in the book. What you refer to (instance of a class) is called "class object".

A perhaps more readable introduction to objects in C++ can be found here: https://en.cppreference.com/w/cpp/language/object.

PS: I am not a historian, but I suppose that the term "object" was already in wide-spread use before Object Orientation came into existance. There have been objects before. They were not invented by OOP, just put into the focus. Also note that C++ is not a (pure) OOP language, but rather multi-paradigm.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
2

but they're basically calling a variable of type int, age, with the value 42 an object. Is this correct?

They're correct.

I already know some oop

Modern OOP nomenclature is different from nomenclature of C++.

and strings which are like built in datatypes

The std::string type is a class i.e. a "user defined" data type (although it is provided by the standard library).

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    Modern OOP nomenclature isn't really based on Java. Most Java nomenclature is based on Java designers and/or Java programmers interpretation of OOP nomenclature (it's debatable in a number of cases whether or not Java nomenclature is consistent with modern OOP nomenclature). – Peter Sep 07 '21 at 13:10
  • @Peter Fair enough. I've just noticed that they correlate closely and made an assumption. – eerorika Sep 07 '21 at 13:26