Second and Third Week of Programming
|
During last week we went over how to use the pointer functions and arrays. However i do not quite understand just how to use them or the exact definition, according to the book an array is data-type with a set amount of elements which we can define the value it holds. This works as a mean to save space and is currently being used in loops to search through the elements and displaying them onto the screen. I will shortly present an example explaining this. Pointers are variables which contains memory addresses and it is used to create a reference between similar data-types outputs and displaying either the address for the referenced data-type or the values which it is pointing towards. I will need to gather more information about this and read up more on how these functions work and how I shall execute them within my code. Below i will present a couple of examples i made using arrays and pointers. Pointer: I have initialized a value to int which then i refereed to using a pointer pointing(i know it is confusing) towards it and given the new variable a new value. The console output will be both values and the memory address for the first data-type value(7). int value = 7; int* p = &value; *p = 10; std::cout << "addr value: " << &value << std::endl; std::cout << "addr pointer: " << &p << std::endl; std::cout << "value pointer: " << *p << std::endl;
Array: I have declared and array with 5 elements and not defined it´s value since i have used a randomized function which will define the value for each element. It will output a random number between ”1” and ”7” until all five elements have been given a value and then canceling the for loop.
int ValueDe[5];
srand((int)time(0));
for (int i = 0; i < 5; i++)
{
ValueDe[i] = 1 + rand()% 7;
std::cout << "arrays: " << i << ": " << ValueDe[i] << std::endl;
}
For our current week we have started working with classes. We want to let our main contain as little as possible and split the code into different classes and call them to our main where they shall be executed. Each class consists of two parts, one which declares the functions and variables and the second tells them how to function. So basically we want our main will only call the classes and execute the functions each class contains. A class can also inherit from a previous class and all it contains. Below i have a small example we made during live coding in class. Within class Base an output is declared and variables have been given a value. Class Derived now inherit all that is within the base class even those that are being made private which cannot be used outside its original class. Lastly in my Main.cpp they have been declared and i have called the output functions, this is all that it consists of.
//class Base
class Base
{
public:
Base() { std::cout << "Base class ctor" << std::endl; }
~Base() { std::cout << "Base class dtor" << std::endl; }
void BaseMethod()
{
int m_public = 1;
std::cout << m_public << std::endl;
}
protected:
int m_hej2 = 1;
private:
int m_health;
};
// Derived inherit from class Base
class Derived : public Base Base == Base.
{
public:
Derived() { std::cout << "Derived class ctor" << std::endl; }
~Derived() { std::cout << "Derived class dtor" << std::endl; }
void DerivedMethod()
{
m_hej2 = 20;
std::cout << m_hej2 << std::endl;
}
};
//Main.cpp
int main(int argc, char* argv[])
{
Base b;
Derived d;
d.DerivedMethod();
b.BaseMethod();
std::cin.get();
std::cin.ignore();
return 0;
}
This is a brief example on the different things we have learned these past two weeks, all of these are much more complex then I have explained them and as i mentioned above I need to practice more and read up on how to operate them. Until next time!
|