Saturday, April 19, 2025

C++ OO Pointer to Class

A pointer to a class is a variable that holds the memory address of an object of that class. It allows us to access the object's properties and methods from the pointer variable. Pointers to classes are useful in situations where we need to pass objects between functions, allocate memory dynamically, or create linked data structures like linked lists, trees, and graphs.

Pointer to a class is declared with the same syntax as a pointer to any other data type. The only difference is that we need to specify the class type in the declaration. 


#include <iostream>

using namespace std;

class Cls{
    public:
        string someStr = "Content";

        void printStr() {
            cout << someStr << endl;
        }
};

int main() {

    Cls *pTr = new Cls();
    pTr->printStr();

    Cls *Blah = new Cls();
    Blah->printStr();

    return 0;
}

This code demonstrates the use of a pointer to a class. In this example, a class named Cls is defined with a string variable someStr and a function printStr() to print the string. In the main() function, two pointers of type Cls are created dynamically using the new keyword.

The first pointer pTr is assigned the address of a new Cls object, and the printStr() function is called on it using the arrow operator ->.

The second pointer Blah is also assigned the address of a new Cls object, and the printStr() function is called on it using the arrow operator.

This code shows how a pointer to a class can be used to create multiple objects of the class dynamically and call their member functions using the arrow operator.

C++ OO Conflicting Objects


#include <iostream>

using namespace std;

class Cls1{
    public:
        void sameName() {
            cout << "I am from Cls 1" << endl;
        }
};

class Cls2{
    public:
        void sameName() {
            cout << "I am from Cls 2" << endl;
        }
};

int main() {

    Cls1 Object;
    Object.sameName();

    Cls2 Object;
    Object.sameName();

    return 0;
}

||=== Build: Debug in Main (compiler: GNU GCC Compiler) ===|
C:\Users\user\Desktop\Main\main.cpp||In function 'int main()':|
C:\Users\user\Desktop\Main\main.cpp|24|error: conflicting declaration 'Cls2 Object'|
C:\Users\user\Desktop\Main\main.cpp|21|note: previous declaration as 'Cls1 Object'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

C++ OO Data Types


#include <iostream>

using namespace std;

class Books {
    public:
        string Title;
        string Author;
        string Topic;
        char intRegister;
        int ID_Book;

        void printAll() {
            cout << "Title:       \t" << Title       << endl;
            cout << "Author:      \t" << Author      << endl;
            cout << "Topic:       \t" << Topic       << endl;
            cout << "intRegister: \t" << intRegister << endl;
            cout << "ID Book:     \t" << ID_Book     << endl;
        }
};

int main() {

    Books UniqBook;
    UniqBook.Title = "Wires in Space";
    UniqBook.Author = "Sheldon";
    UniqBook.Topic = "String Theory";
    UniqBook.intRegister = 'B';
    UniqBook.ID_Book = 254121;

    UniqBook.printAll();

    return 0;
}

The code defines a class called Books with several data types, such as string and char, and a member function called printAll().

The class Books contains the following data members:

  • Title: A string representing the title of the book.
  • Author: A string representing the author of the book.
  • Topic: A string representing the topic of the book.
  • intRegister: A character representing the book's registration status.
  • ID_Book: An integer representing the book's ID number.

The member function printAll() prints out all the data members of the class to the console.

In the main() function, an object of the Books class is created and its data members are initialized with some values. Then, the printAll() function is called on the UniqBook object to print out its data members to the console.

The code demonstrates the use of a class to define a "book" object with various properties such as title, author, topic, and ID.

This code could be useful in a program that manages a library or book inventory, where book objects need to be created and tracked with specific information.

By defining a book class with different data types for each property, it becomes easier to create and manage book objects and their associated properties within the program. The class can also have member functions that perform operations on book objects such as searching, sorting, or printing.

C++ OO String Operations


#include <iostream>

using namespace std;

class Human {
    public:
        string Name;

        void printName() {
            cout << "Name of person: " << Name << endl;
        }
};

int main() {

    Human Object_1;
    Object_1.Name = "John";

    Object_1.printName();

    return 0;
}

This C++ code creates a class called Human. The class has one public attribute called Name which is a string. It also has a public member function printName() that prints the value of Name.

The code then defines the main() function which creates an object of the Human class called Object_1. It then assigns the value "John" to the Name attribute of Object_1 using the dot notation.

Finally, the code calls the printName() function of Object_1 which prints out "Name of person: John" to the console.

In object-oriented programming languages like C++, objects have state and behavior. The state represents the object's properties, while the behavior represents the object's actions or methods. To access an object's state or behavior, we need a way to refer to that object. That's where the dot notation comes in.

The dot notation is used to access an object's methods or properties. It is called the member access operator because it allows us to access the members (properties and methods) of the object. In C++, we use the dot notation to access an object's methods and properties after creating an instance of the class. The dot notation is a way of telling the program which object we want to access and what we want to do with it. 


#include <iostream>

using namespace std;

class Human {
    public:
        string Name;

        void printName() {
            cout << "Name of person: " << Name << endl;
        }
};

int main() {

    Human Object_1;
    Object_1.Name = "John";
    Object_1.printName();

    Human Man_2;
    Man_2.Name = "Mr X";
    Man_2.printName();

    return 0;
}

The main() function creates two Human objects, sets their name using the Name member variable and then calls the printName() function on each object to display their respective names on the console.

C++ OO Run External Apps


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void callCalc() {
            system("calc");
        }
};

int main() {

    External Object;
    Object.callCalc();

    return 0;
}

This C++ code is demonstrating how to use the system() function to execute an external program (in this case, the Windows calculator program). The code defines a class named External with a single public method callCalc() that calls the system() function with the argument "calc", which opens the Windows calculator program.

In the main() function, an object of the External class is created and the callCalc() method is called on this object, which results in the Windows calculator program being opened.

But it's important to note that using the system() function to execute external programs can be a security risk and should be used with caution, so here it's used only as an simple example to demonstrate class blueprint and object creation.


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void callCalc() {
            system("calc");
        }
};

int main() {

    External Object;
    Object.callCalc();
    Object.callCalc();
    Object.callCalc();

    return 0;
}

In the main() function, the callCalc() method is called three times on the Object instance, which will open the calculator application three times. Finally, the program returns 0 to indicate successful execution. 


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void caller() {
            system("calc");
            system("ping google.com");
        }
};

int main() {

    External Object;
    Object.caller();

    return 0;
}

This code defines a class called "External" that has a single public method called "caller". Inside the "caller" method, two system commands are executed: "calc" and "ping google.com".

In the "main" function, an object of the "External" class is created called "Object", and the "caller" method of this object is called. This means that the system commands "calc" and "ping google.com" will be executed in order. 


#include <iostream>
#include <cstdlib>

using namespace std;

class External{
    public:
        void caller() {
            system("cmd");
        }
};

int main() {

    External Object;
    Object.caller();

    return 0;
}

This code is useful in situations where the user needs to execute a specific command in the Windows command prompt, which is accessed by running the "cmd" command.

By calling the "system" function with the "cmd" argument, the program will launch a new instance of the command prompt, giving the user direct access to the command line interface for executing a variety of commands, such as navigating through the file system, running other programs, or executing scripts.

This can be particularly useful in cases where the user needs to perform some advanced system administration tasks, or in situations where the program needs to execute specific command line operations that are not available through other APIs or libraries. 

C++ OO Creating Objects

An object is an instance of a class, which represents a real-world entity that has attributes (data) and behaviors (methods or functions).

Objects are created from classes because they provide a way to create multiple instances of an entity that share the same attributes and behaviors defined by the class. For example, if we have a class named "Car" that defines the attributes and methods for a car, we can create multiple objects of that class, each representing a different car with its own set of attributes.

Objects are useful because they allow us to model real-world entities and interact with them in our programs. By creating objects from classes, we can encapsulate data and behavior, and provide a clear interface for working with the object. This can make our code more organized, easier to read and understand, and easier to maintain over time. 


#include <iostream>

using namespace std;

class specName{

    public:
        void printSomething() {
            cout << "Hack The Planet !" << endl;
        }

};

int main() {

    specName Object;
    Object.printSomething();

    specName ObjectNu2;
    ObjectNu2.printSomething();

    return 0;
}

An object of the class specName is created twice in the main function using the syntax specName Object; and specName ObjectNu2;. These objects are then used to call the printSomething() method using the . operator, which results in the output "Hack The Planet !" being printed twice in the console. 

Dot notation is a way to access the members (variables and functions) of an object through the object name followed by a period (dot) and the name of the member.


#include <iostream>

using namespace std;

class specName{

    public:
        void printSomething() {
            cout << "Hack The Planet !" << endl;
        }

        void test() {
            cout << "Mess with the best, die like the rest." << endl;
        }
		
};

int main() {

    specName Object;
    Object.printSomething();
    Object.test();

    return 0;
}

This code defines a class named specName with two public member functions printSomething() and test(). The printSomething() function prints the message "Hack The Planet!" to the console, and the test() function prints the message "Mess with the best, die like the rest." to the console.

In the main() function, an object of the specName class is created using the specName Object; syntax. The printSomething() and test() member functions of this object are then called using the dot notation, which is denoted by Object.printSomething(); and Object.test();.

When the program is executed, it will print the messages "Hack The Planet!" and "Mess with the best, die like the rest." to the console. 


#include <iostream>

using namespace std;

class specName{

    public:
        void printSomething() {
            cout << "Hack The Planet !" << endl;
        }

        void test() {
            cout << "Mess with the best, die like the rest." << endl;
        }

};

int main() {

    specName Obj3;
    specName Obj4;
    specName Obj5;

    Obj3.printSomething();
    Obj3.printSomething();
    Obj3.printSomething();

    Obj4.test();
    Obj4.test();
    Obj4.test();

    Obj5.printSomething();
    Obj5.printSomething();
    Obj5.printSomething();

    return 0;
}

This code creates three instances of the specName class named Obj3, Obj4, and Obj5.

The printSomething() method is called three times on Obj3, the test() method is called three times on Obj4, and printSomething() is called three times on Obj5.

This demonstrates that each instance of the specName class is independent of the others and can have its own state and behavior.

C++ OO Introduction

Object-oriented programming (OOP) is a programming paradigm that enables developers to create modular, reusable, and extensible software by organizing code into objects that encapsulate data and behavior.

In C++, objects are created by defining classes, which are blueprints for creating objects with specific attributes and methods. These objects can then be used to perform various tasks, such as running external applications, performing string operations, and manipulating different data types.

One of the key benefits of object-oriented programming is that it helps developers manage complex codebases by reducing code duplication, improving code organization, and making it easier to maintain and update code over time. Additionally, OOP provides encapsulation, inheritance, and polymorphism, which are powerful mechanisms for creating modular and reusable code. 


#include <iostream>
#include <cstring>

using namespace std;

void getStuff(struct Books *genField);

struct Books {
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

int main() {

    struct Books Book_1;

    strcpy(Book_1.Title, "Relativity T");
    strcpy(Book_1.Author, "Albert E");
    strcpy(Book_1.Topic, "Relativity");
    Book_1.id_book = 4323;

    getStuff(&Book_1);

    return 0;

}

void getStuff(struct Books *genField) {

    cout << "Book_1 - Title:  \t" << genField->Title   << endl;
    cout << "Book_1 - Author: \t" << genField->Author  << endl;
    cout << "Book_1 - Topic:  \t" << genField->Topic   << endl;
    cout << "Book_1 - ID:     \t" << genField->id_book << endl;

    cout << "-------------------------------------" << endl;

}

This code defines a C++ structure named "Books" that contains four member variables: Title, Author, Topic, and id_book. Then, the main function creates an object of this structure named "Book_1" and initializes its member variables using the "strcpy" function.

After that, the main function calls the "getStuff" function with a pointer to the "Book_1" object. The "getStuff" function takes a pointer to a "Books" structure as its parameter and prints the values of the structure's member variables to the console using the "->" operator.

In summary, this code demonstrates the use of C++ structures to define a blueprint for creating objects with specific properties and methods. It also shows how to initialize and manipulate the member variables of a structure and how to pass a pointer to a structure to a function for further manipulation. Overall, this code provides a good introduction to C++ structures and how they can be used in programming.

Please note, we are using this structure example as nice base for class understanding. Structures are form of blueprint, and we will take that knowledge when thinking about classes. 


#include <iostream>

using namespace std;

class specName{

public:
    void printSomething() {
        cout << "Hack The Planet !" << endl;
    }

};

int main() {

    return 0;
}

Class creation: 


#include <iostream>

using namespace std;

class specName{

    public:
        void printSomething() {
            cout << "Hack The Planet !" << endl;
        }

};

int main() {

    return 0;
}

This code defines a C++ class named "specName" that has one member function named "printSomething". The "printSomething" function simply prints the string "Hack The Planet!" to the console when called.

The "main" function is empty and doesn't do anything. It simply returns 0 at the end, which indicates a successful execution of the program.

Overall, this code demonstrates the basics of defining and using a C++ class, and how to define a member function and call it from the main function. However, it doesn't really show any practical use of classes, and the "printSomething" function doesn't take any arguments or return any values, so it's just a simple example to demonstrate the syntax and structure of a class.

Why we need to use "class" keyword and what is "public:"

The "class" keyword is used to define a user-defined data type that encapsulates data and functions into a single entity. The class provides a blueprint or a template for creating objects of that type. By defining a class, we can encapsulate data and functions together, and hide the implementation details from the user.

The "public:" keyword is used to specify the access level of the member variables and functions in a class. In this code, the "public:" keyword is used to declare that the member function "printSomething" is accessible from outside the class. This means that other parts of the program can call this function and use it to perform some operation.

By using the "public:" keyword, we can explicitly declare that certain members are accessible from outside the class, which can be useful for creating well-structured and modular code.

C++ Struct and Pointers

Other way to extract values from our Book_1 "row" based on Book "database" is to use a pointer.

The situation is similar to a previous tutorial, except we use "->" while extracting data from "columns".

This is our function that will accept a pointer to Book_1 as parameter:

void getStuff(struct Books *genField) {

    cout << "Book_1 - Title:  \t" << genField->Title   << endl;
    cout << "Book_1 - Author: \t" << genField->Author  << endl;
    cout << "Book_1 - Topic:  \t" << genField->Topic   << endl;
    cout << "Book_1 - ID:     \t" << genField->id_book << endl;

    cout << "-------------------------------------" << endl;

}

So, this is full source of our program: 

#include <iostream>
#include <cstring>

using namespace std;

void getStuff(struct Books *genField);

struct Books {
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

int main() {

    struct Books Book_1;

    strcpy(Book_1.Title, "Relativity T");
    strcpy(Book_1.Author, "Albert E");
    strcpy(Book_1.Topic, "Relativity");
    Book_1.id_book = 4323;

    getStuff(&Book_1);

    return 0;

}

void getStuff(struct Books *genField) {

    cout << "Book_1 - Title:  \t" << genField->Title   << endl;
    cout << "Book_1 - Author: \t" << genField->Author  << endl;
    cout << "Book_1 - Topic:  \t" << genField->Topic   << endl;
    cout << "Book_1 - ID:     \t" << genField->id_book << endl;

    cout << "-------------------------------------" << endl;

}

Result:

Book_1 - Title:         Relativity T
Book_1 - Author:        Albert E
Book_1 - Topic:         Relativity
Book_1 - ID:            4323
-------------------------------------

Process returned 0 (0x0)   execution time : 0.059 s
Press any key to continue.

Well done, actually - excellent.

C++ is not easy language to learn. Give yourself some time if you don't understand everything from these tutorials at first reading.

Once where you grasp basic of C++, all other languages will look easy. Take your time.

C++ Passing Struct to Function

To have a generic solution how to get data from our Book structure we will create a function that will accept any concrete structure as parameter.

Data type must be struct, and all those "columns" must be replaced with a generic column, in this case "genField":

void getStuff(struct Books genField) {

    cout << "Book_1 - Title:  \t" << genField.Title   << endl;
    cout << "Book_1 - Author: \t" << genField.Author  << endl;
    cout << "Book_1 - Topic:  \t" << genField.Topic   << endl;
    cout << "Book_1 - ID:     \t" << genField.id_book << endl;

    cout << "-------------------------------------" << endl;

}

Of course, function must be used inside main() section where we will provide Book_1 as argument:

getStuff(Book_1);

Full source: 

#include <iostream>
#include <cstring>

using namespace std;

void getStuff(struct Books genField);

struct Books {
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

int main() {

    struct Books Book_1;

    strcpy(Book_1.Title, "Relativity T");
    strcpy(Book_1.Author, "Albert E");
    strcpy(Book_1.Topic, "Relativity");
    Book_1.id_book = 4323;

    getStuff(Book_1);

    return 0;

}

void getStuff(struct Books genField) {

    cout << "Book_1 - Title:  \t" << genField.Title   << endl;
    cout << "Book_1 - Author: \t" << genField.Author  << endl;
    cout << "Book_1 - Topic:  \t" << genField.Topic   << endl;
    cout << "Book_1 - ID:     \t" << genField.id_book << endl;

    cout << "-------------------------------------" << endl;

}

Result:

Book_1 - Title:         Relativity T
Book_1 - Author:        Albert E
Book_1 - Topic:         Relativity
Book_1 - ID:            4323
-------------------------------------

Process returned 0 (0x0)   execution time : 0.124 s
Press any key to continue.

C++ Struct - Data Extraction

Data extraction from our struct Book_1 will be extremely easy.

To extract value for Title first we will target "row" Book_1, than dot symbol, and than a "column" we need:

cout << "Book_1 - Title:  \t" << Book_1.Title   << endl;

Following same logic, all other values can be extracted:

cout << "Book_1 - Author: \t" << Book_1.Author  << endl;
cout << "Book_1 - Topic:  \t" << Book_1.Topic   << endl;
cout << "Book_1 - ID:     \t" << Book_1.id_book << endl;

Full source: 

#include <iostream>
#include <cstring>

using namespace std;

struct Books {
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

int main() {

    struct Books Book_1;

    strcpy(Book_1.Title, "Relativity T");
    strcpy(Book_1.Author, "Albert E");
    strcpy(Book_1.Topic, "Relativity");
    Book_1.id_book = 4323;

    cout << "Book_1 - Title:  \t" << Book_1.Title   << endl;
    cout << "Book_1 - Author: \t" << Book_1.Author  << endl;
    cout << "Book_1 - Topic:  \t" << Book_1.Topic   << endl;
    cout << "Book_1 - ID:     \t" << Book_1.id_book << endl;

    cout << "-------------------------------------" << endl;

    return 0;

}

Result:

Book_1 - Title:         Relativity T
Book_1 - Author:        Albert E
Book_1 - Topic:         Relativity
Book_1 - ID:            4323
-------------------------------------

Process returned 0 (0x0)   execution time : 0.067 s
Press any key to continue.

C++ Struct - Data Structures

Think about "struct" keyword in C++ as a tool that will help us to create something that look alike small databases.

You probably used Excel or MySQL or any table preparation program where cells are used to store individual values.

Every intersection of a row and column in some table uniquely identify some value there.

So, while using "struct" we are actually defining columns and in those columns we must put only values of specific data type.

For example, we are creating structure that will hold data about Books - that will be name of our structure.

In it we will have Title of the Book, Author, Topic and ID of a book if we have a small library.

All those values are of dedicated data type. When dealing with char data type we will state how big thay can possibly be:

struct Books {
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

All of that will be done outside main() section.

Inside main() section we will create first entry (think about it as table row) based on our struct Book. Name of first entry/row will be Book_1:

struct Books Book_1;

To get data in individual "cells" we will use strcpy method to copy string (concatenated characters) to specific column:

strcpy(Book_1.Title, "Relativity T");
strcpy(Book_1.Author, "Albert E");
strcpy(Book_1.Topic, "Relativity");

We don't need to use strcpy method on integers:

Book_1.id_book = 4323;

To have strcpy() function working we must have this line at the top of our program:

#include <cstring>

Full source:

#include <iostream>
#include <cstring>

using namespace std;

struct Books {
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

int main() {

    struct Books Book_1;

    strcpy(Book_1.Title, "Relativity T");
    strcpy(Book_1.Author, "Albert E");
    strcpy(Book_1.Topic, "Relativity");
    Book_1.id_book = 4323;

    return 0;

}

Result:


Process returned 0 (0x0)   execution time : 0.064 s
Press any key to continue.

Excellent. In next tutorial we will extract and use data from our simple "database". 

C++ References as Arguments

Just as pointers, references can be used as parameters.

Our function will demand one reference in this example. A job will be to multiply original value with itself

void doubleUsingRef(int &xRef) {
    cout << "Address Right Now: " << &xRef << endl;

    xRef = xRef * xRef;
}

Inside main() section first we are establishing a variable:

int some_number = 10;

Than printing of memory address and a value:

cout << "Address BEFORE Operations: " << &some_number << endl;
cout << "Value BEFORE Operations: " << some_number << endl;

Now we are using our function previously created:

doubleUsingRef(some_number);

And this is final value:

cout << "Value AFTER Operations: " << some_number << endl;

Full source:

#include <iostream>

using namespace std;

void doubleUsingRef(int &xRef);

int main() {

    int some_number = 10;
    cout << "Address BEFORE Operations: " << &some_number << endl;
    cout << "Value BEFORE Operations: " << some_number << endl;

    doubleUsingRef(some_number);

    cout << "Value AFTER Operations: " << some_number << endl;

    return 0;

}

void doubleUsingRef(int &xRef) {
    cout << "Address Right Now: " << &xRef << endl;

    xRef = xRef * xRef;
}

Result:

Address BEFORE Operations: 0x6dfeec
Value BEFORE Operations: 10
Address Right Now: 0x6dfeec
Value AFTER Operations: 100

Process returned 0 (0x0)   execution time : 0.060 s
Press any key to continue.

C++ Pointers as Arguments

Pointers can be used as parameters.

In this case function will demand a pointer to a variable whose data type must be an integer.

We will add integer 1000 to value that is at that specific memory address.

Once we create function outside main() section it's easy:

void add_1000(int *xPtr) {
    cout << "Result of add: " << (*xPtr) + 1000 << endl;
}

Inside main() section variable will be created, with value 5 in this example:

int Number = 5;

Then a pointer is created with a dedicated line:

int *ptrNumber = &Number;

And than a call of our function where we provide memory location as argument:

add_1000(ptrNumber);

Full source: 

#include <iostream>

using namespace std;

void add_1000(int *xPtr) {
    cout << "Result of add: " << (*xPtr) + 1000 << endl;
}

int main() {

    int Number = 5;
    int *ptrNumber = &Number;

    add_1000(ptrNumber);

    return 0;

}

Result:

Result of add: 1005

Process returned 0 (0x0)   execution time : 0.072 s
Press any key to continue.

C++ Pointers in Functions

It's possible to use pointers inside functions. In this example for two arguments our function demand we will have two corresponding pointers.

Operation done is simple addition.

Until now we know that functions must be declared/defined outside main() section and call of function must be inside it: 

#include <iostream>

using namespace std;

void simpleAdd(int x, int y) {
    int *ptrX = &x;
    int *ptrY = &y;

    cout << "Result of Add: " << (*ptrX) + (*ptrY) << endl;

}

int main() {

    simpleAdd(5, 8);

    return 0;

}

Result:

Result of Add: 13

Process returned 0 (0x0)   execution time : 0.054 s
Press any key to continue.

 Sure, our function can return a result to the external world:

#include <iostream>

using namespace std;

int simpleAdd(int x, int y) {
    int *ptrX = &x;
    int *ptrY = &y;

    int Result = (*ptrX) + (*ptrY);
    return Result;

}

int main() {

    cout << "Result of Addition: " << simpleAdd(2, 5) << endl;

    return 0;

}

Result:

Result of Addition: 7

Process returned 0 (0x0)   execution time : 0.055 s
Press any key to continue.

C++ Multiple References, Pointers to References

Just as a person can have multiple nicknames, a variable can have multiple references to it.

First we will create a variable with value of 555, for example:

int var = 555;

Then three references to variable, using ampersand symbol in different places (yes, we can do that):

int& ref1 = var;
int & ref2 = var;
int  &ref3 = var;

Now we are printing value directly from a variable:

cout << "Direct value: " << var << endl;

And now using references:

cout << "Value using ref1: " << ref1 << endl;
cout << "Value using ref2: " << ref2 << endl;
cout << "Value using ref3: " << ref3 << endl;

Full script: 

#include <iostream>

using namespace std;

int main() {

    int var = 555;

    int& ref1 = var;
    int & ref2 = var;
    int  &ref3 = var;

    cout << "Direct value: " << var << endl;

    cout << "Value using ref1: " << ref1 << endl;
    cout << "Value using ref2: " << ref2 << endl;
    cout << "Value using ref3: " << ref3 << endl;

    return 0;

}

Result:

Direct value: 555
Value using ref1: 555
Value using ref2: 555
Value using ref3: 555

Process returned 0 (0x0)   execution time : 0.579 s
Press any key to continue.

Pointers to References

C++ is powerful language. We can have pointers to references.

First, variable setup:

int var = 555;

Then 3 references to original variable, using ampersand symbol:

//References
int& ref1 = var;
int & ref2 = var;
int  &ref3 = var;

Then 4 pointers. One for an original variable, three for references individually:

//Pointers
int *ptrVar = &var;
int *ptrRef1 = &ref1;
int *ptrRef2 = &ref2;
int *ptrRef3 = &ref3;

And now we can get memory address. 

cout << ptrVar << endl;
cout << ptrRef1 << endl;
cout << ptrRef2 << endl;
cout << ptrRef3 << endl;

And extract values:

cout << *ptrVar << endl;
cout << *ptrRef1 << endl;
cout << *ptrRef2 << endl;
cout << *ptrRef3 << endl;

Full source: 

#include <iostream>

using namespace std;

int main() {

    int var = 555;
    //References
    int& ref1 = var;
    int & ref2 = var;
    int  &ref3 = var;

    //Pointers
    int *ptrVar = &var;
    int *ptrRef1 = &ref1;
    int *ptrRef2 = &ref2;
    int *ptrRef3 = &ref3;
    
    cout << ptrVar << endl;
    cout << ptrRef1 << endl;
    cout << ptrRef2 << endl;
    cout << ptrRef3 << endl;

    cout << *ptrVar << endl;
    cout << *ptrRef1 << endl;
    cout << *ptrRef2 << endl;
    cout << *ptrRef3 << endl;

    return 0;

}

Result:

0x6dfed0
0x6dfed0
0x6dfed0
0x6dfed0
555
555
555
555

Process returned 0 (0x0)   execution time : 0.106 s
Press any key to continue.

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...