Estudo de C++ Flashcards
Aprender C++
Dereference operator
Give access to real valor on the variable
Reference operator
Give access to memory address
Keyword New
Creates and initializes objects with dynamic storage
Keyword Delete
Erase variables after use
Struct
structure for grouping variables, from different types
Classes
Extension of struct concept, can have functions
Data in structs
Atributes
Functions in Classes
Also called methods
Object
Takes attributes and methods from classes
Object
We use it to access what is in classes
Default classes
Everything after a declared class and before a specifier is PRIVATE
Scope operator
::
Scope operator definition
To show that a function is a implementation from a member from a class, not a global function
Constructor
Special method called when a new object is created
Using Constructor
Need to have the same name as the classes and no return valor, even VOID
Destructor
Called when an object is destroyed(when a function ends or when we use DELETE
Using Destructor
Need the same name as the class and a precede “~”
Destructor advantages
Good for memory releasing
Common practices in C++ with classes
Creating two separated files
pragma once
Preprocessor directive to include the header once, regardless how many times he has been imported
Declaring a classes as STATIC
We don’t need to instantiate a classes for using it
Static class example
“static int math::pow();”
Non static class example
"Math math = new Math();" "math.pow();"
Function in static class
Need to be static too
Constructor types
Most time are publics, but i can have private constructor too
How many constructs can I have?
Classes can have multiples constructors
What happens if I don’t create a constructor?
My compiler creates a constructor automatically
How many default constructors a class have?
One, and just one
Default constructor example
class Person { public: Person(); ~Person(); };
From what should I make sure to initialize my own constructor?
Because pointers and arrays can initialize with undefined values.
The complex reason for my own constructor?
When I have a class inside a class, my compiler is unable to initialize that member
Summarizing the ‘this’ operator - pt 1
This serves for refer to attributes and methods from a class
Summarizing the ‘this’ operator - pt 2
When you create an object(instantiate a class), you pass the values in the constructor and we can have methods overloading.
Summarizing the ‘this’ operator - pt 3(when use it)
Use the ‘this’ operator when I need to access anything inside a class
Keyword ‘new’
It’s used for allocate memory for an object in execution time
Keyword ‘delete’
Releases memory after using an object and it’s no more needed
Class Scope
In classes context, refer to member variables and member functions within the class definition
Inside a class
Every variable and function is available for the functions inside the class
Members modifiers
Every member from a class can have access to modifiers like ‘private’, ‘public’ and ‘protected’
Private and public members availability
All the members are available for all members functions inside the class
Accessing members inside a class
When accessing I need to use the dot operator(‘.’) or arrow(‘->’)
Difference between dot and arrow
They have no difference, since they perform the same, but arrows it’s normally used with pointers and dot with variables
Why use the arrow ->?
To replaces the following syntax:
(*a).b
Accessing statics classes
I need to use the class name like “Math::Sum() instead instantiating like “Math myMath” and after this “myMath.Sum()”
The compiler and class scope concept
The compiler knows what function he needs to call, because the object used it’s associated with an specific class. He knows the difference between ‘person.SayHello()’ and ‘dog.SayHello()’
Encapsulation concept in OOP
We can say that it allows to include data and functionalities in a single package.
The second definition of encapsulation
It’s to hide or restrict data. For example we don’t want to a Person class User defines the variables “firstName”, ‘lastName’ and age directly, only using the constructor
A good practice within encapsulation scope
A good practice is to provide public functions to allow classes users to modify values, but indirectly
Using a public function to modify allow me to do…
M, as a programmer, control how the users data are processed
An example of treated data…
I can use a function to check if a user passed a negative value or a character instead a valid value for an age variable
Resuming encapsulation
When using it, we define how attributes and methods are accessed
Limiters…
private, public and protected
The important part of encapsulation?
That the user don’t see how we define values or validates data
The encapsulation analogy
We don’t need to know how a motor inside a car works, I need only to know how to drive him
Namespaces
He acts like a container, we put variables, classes or another identifiers to avoid name conflicts
Namespaces example: cout
He exists only in the std scope, that’s why we use:
std::cout or
using namespace std;
cout «_space;‘Hello world’ «_space;endl;