Lab 2 -classes and objects
A brief comparison with Java
C++ and Java are very similar languages, but they also have differences. One of the most important differences is that C++ programmers are responsible for memory management and have to free up the requested memory themselves, while Java's built-in garbage collection does the job automatically. Modern C++ has memory management, we will look at this. Also, the standard C++ library is smaller than Java's, and it is often the programmer's job to find the right library for the job. See a comparison of Java and C++ here: http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B
Classes
Classes are usually described in two parts. The header contains the class declaration and the cpp files contain the implementations of the class methods. The header of a sample class could look as follows:
/ *! Sample class. * / class ClassName { public: / *! The default class constructor. * / ClassName (); / *! Constructor with class parameters. * / ClassName (bool par1, long par2); / *! Class destructor. * / ~ ClassName (); / *! Public method. * / void method (); / *! Public instance variable - can be read outside the class * / bool public_variable; private: / *! Hidden method. * / int hiddenMethod (); / *! Hidden instance variable. * / long hidden_variable; };
Methods and variables
The main part of the class are the methods (functions) and fields (variables). Visibility can be defined for both:
public
- visible and accessible to all,protected
- visible only for subclass methods,private
- are visible and usable only within this class and for friend methods. See also http://www.cplusplus.com/doc/tutorial/inheritance/
Both methods and variables can be defined as instance methods (and variables), which is the default behavior, or as class methods (and variables) using the static
keyword. See also https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm
A class can have several methods with the same name and different parameters (such as the constructor in the previous example). This is called an overloaded method.
Constructors and destructors
The constructor is a method that is executed when a new object of a class is created. Inside the constructor, fields are initialized and other preparations are made. There can be several constructors as needed. A constructor with parameters can be passed initial values when creating an object. The default constructor must be able to create an object without external values. In addition, there is a copy constructor that can be used to copy objects. If the programmer does not write constructors, the default constructor and copy constructor are created by the compiler itself. Keep in mind that they may not always behave as needed.
ClassName object1; // object creation, the default constructor is used ClassName object2 (true, 100000); // we use a constructor with parameters ClassName object3 (object2); // we use the copy constructor ClassName object4 {false, 5}; // C ++ 11 universal initialization
Read more: http://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization
Destructors are methods that are called at the end of an object's life. Destructors are responsible for releasing all resources used by the class.
Overloading operators
C++ allows you to define operations between objects. It is possible to define operations (assignment, addition, multiplication, etc.) between instances of your classes or operations between your class and integers, for example. One of the operations that is reasonable to overload is the assignment (=) used to assign objects to the class. The compiler again creates some initial version, but you can make your own versions for more complex structures. Note that you can also overload, for example, comparison actions. However, it is generally recommended to use this option with caution, as overloading can confuse the reader.
Additional materials
Read more about classes and activities here:
- https://cplusplus.com/doc/tutorial/classes/
- https://cplusplus.com/doc/tutorial/templates/
- https://cplusplus.com/doc/tutorial/classes2.html
Libraries
A library is a set of object code that performs a specific task and is intended for reuse. For example, you can write a library to help a programmer work with image files, encrypted data, or complex math. The library always comes with a set of headers that describe what is offered in the library. A programmer who starts using a library adds library headers to his program, and when compiling, he also adds the corresponding library file to the compiler (GCC libraries have the .a extension files, from the word archive ). Proper documentation that best describes the use and limitations of the library is very important.
Overloading the output stream insertion operator
For an example, let's look at the output stream insertion operator of Point2
:
Declaration in the header:
friend ostream& operator<<(ostream& out, const Point2& p);
Realization:
ostream& operator<<(ostream& out, const Point2& p) { out << "(" << p.x << ", " << p.y << ")"; return out; }
For me, this only works if I make the method friend in the .h file and have the implementation not as a member of the class (i.e. it does not have a Point2::)
Some keywords and symbols were not used before.
NB! At the moment this is not a class or instance method, i.e. the function is not declared within the class. The latter only needs to be done if the operator needs to be able to use private functions and/or variables. In this case, a we can use the friend
keyword before the function.
Class friends
Methods labeled this way also access the class's private variables. See examples: http://www.cplusplus.com/doc/tutorial/inheritance/
References (ostream &)
We will talk more about references in the fourth practical. Please note that data marked as such will not be copied, but a reference to a specific variable will be passed on. By default, parameters in C++ are passed by value, i.e. as copies (with the exception of arrays). Objects are passed as value as well, using the copy constructor
Read more: http://www.cplusplus.com/doc/tutorial/pointers/
Constant
The compiler does not allow you to change constant expressions. Combining a constant expression with references allows us to quickly transmit data without copying it, while ensuring that the method does not change that data. Unfortunately, we will not be able to deal with constant expressions in this course.