Posted on Leave a comment

Functions in C++

functions

C++ functions have (Except especial functions)

  • declaration: this is a statement of how the function is to be called.
  • definition: this is the statement(s) of the work the executing program will perform when called

C++ Functions

  • Accept parameters, but they are not required
  • Return values, but a return value is not required
  • Can modify parameters, if given explicit direction to do so

Function declaration in C++

void functionName();

syntax for the definition

void functionName()

{

     statement(s);

}

C++ does not allow arrays to be passed to functions, but, as we have seen, it does allow pointers to be passed.

Functions passing constant argument

int doubleInputFunc(const int input)

Function Declaration and Definition

  • Declaration: the function’s name, return type, and parameters (if any)
  • Definition: the body of the function (code to be executed)

void myFunction() { // declaration

  // the body of the function (definition)

}

Function overloading

  • With function overloading, multiple functions can have the same name with different parameters:

int myFunction(int z)

float myFunction(float z)

double myFunction(double z, double w)

Posted on Leave a comment

C++ Arrays

  • C++ includes arrays as other programming languages.
  • Vectors are more versatile/singular/powerful than arrays and it’s a recommendation to switch to vector as fast as possible.
  • Arrays stores fixed-size sequential collection of elements of the same type.
  • Arrays consists of  contiguous memory locations.
  • Lowest address correspond to the first element and the highest address to the last element of the array.

Declaring arrays

Type arrayName [ arraySize ]; //this is known as single-dimension array

  • Array size must be an integer greater than cero.

Example

int members[7]; //array of type integer with 7 elements

Initializing Arrays

  • Initialize an array single statement

int members[4] = {10, 23, 42, 7}; 

Array Index and Value
Array Index and Value

The number of elements inside the braces { } cannot be longer than the value between the square braces[ ]

#include <iostream>
#include <array>
using namespace std;
 
 
int main () {

   // create an array of 5 integers declaring it

int n[5];
 
   // initialize elements of array n to 10-14  
    // n[0]= 10;
    // n[1]= 11;    
    // n[2]= 12;   
    // n[3]= 13;
    // n[4]= 14; 

    for (int i=0; i< 5; i++){
        n[i] = 10+i;
    }
       

   // output each array element's value   

   //cout << n[3] << endl;    
   for (int o= 0; o<5; o++){
       cout << "array index -> " << o << ", value -> " << n[o] << endl;
   }             
 
 // get the array size
 cout << sizeof(n)/sizeof(n[0]) << " array size" <<endl;
   return 0;
}
Posted on Leave a comment

Intro to C++ Classes

C++ Classes

What is C++?

  • “A programming language primarily for applications that are very demanding on performance, energy consumption or speed… If you are really concerned about reliability, performance, response time C++ becomes a good choice.”
  • Bjarne Stroustrup

Is C++ and Object Oriented Programming Language?

  • Bjarne Stroustrup the creator of C++ never thinked C++ as an Object Oriented Programming Language where all the focus resides on it’s classes, objects and inheritance but is also the reason of it’s Language limitations.
  • C++ burrowed the OOP concepts from  The Simula 67 language

C++ is Multi-paradigm

C++ as a multiparadigm programming language supports single or mixed approaches using Procedural or Object-oriented programming and mixing in utilization of Generic and even Functional programming concepts.

C++ Class intro

C++ can’t provide all types you need for work a telephonist needs a dialogue buffer, a mathematician a triangle matrix, a game engine needs a character type.

Memory Management

  • There is an operation that initializes. 
  • There is an operation that cleans when finishes.
  • Constructor, Destructor.
  • »}
  • »All the magic (destructors)

Constructors

A constructor is special function that is executed whenever we create a new instance of the class. It is used to set initial values of data members of the class.

Constructor

Constructors do not return a value, including void.

Destructors

Destructors are special class functions that are called whenever an object goes out of scope. Just like a constructor, a destructor is called automatically.

~className() //this is a destructor

classname::~classname()
{
//tasks to be completed before going out of scope and no longer exists in memory
}