Learning C++: Part 7: Objects

Learning C++


Objects

While the struct keyword can be used to define a new simple data type, C++ also adds the concept of objects. When there is a need to build more complex data structures using functions to access and change its entries, an object can be created and used.

In C++, objects are created using the keyword class

Like functions, the open and closing curly brackets define everything inside the object. Starting from the keyword, the object then has its name and the content inside the curly brackets.

Public and Private

Everything in a struct is public. That is, by knowing the name of one of its members, its data can be accessed. When using an object, this is not always the case.

In object-oriented programming, complex tasks and processes are sub-divided into objects and their relationships. In understanding objects, they can also be considered in what they consider public and private.

Using the keywords public and private, sections of the object can be divided into what members can be accessed outside the object and what entries only the object can use.

For members that are private, they can be accessed outside of an object through public functions. Through creating new functions on the object, these can serve as a way to express a relationship between the object and others.

Including Other Files

Along with system files, other, more local files can also be included in a project. When a file is local, it can be included through quotation marks. This allows projects to better organize itself through putting code in different parts and “including” them together.

When a system file is needed in another file, it can either be included there or as part of the central, usually “main” file.

Note: As was mentioned in the “Variables and Data Types” section, C++ does not directly support the String data type. However, functionality can be added through part of the standard (STD) files included with C++.

Working with STD Values

The function printf() is part of the C library. Because the STD::String data type is not a normal one, it does not work as well with C functions. Another way to print values (show on output) is to use cout.

In the iostream system library, the cout keyword works on the C-Output (cout) stream. Values are “pushed” into the stream using two less-than signs. The endl keyword also added an “end-line” (new-line) signal to end the output and create a new line.

Namespaces

In C++, the namespace keyword can be used to indicate that objects are in a group. The using namespace combination of keywords paired together can be used to show that objects are part of some named namespace.

When using the STD namespace, this tells C++ that the files are part of a known namespace, “std”. Objects part of the system library will not need to be marked by “std::” (thus showing it is part of another namespace).

Note: Objects and functions that are part of the C++ system libraries are part of the std namespace by default.

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.