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.
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.
#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.
#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.
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.
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.
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.
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.
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.
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.
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.
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.
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;