Skip to main content

Function and Objects

C++ Programming 
 Introducing to Functions

Functions are a component of a programming language that permit you to break down the behavior of an application into discreet pieces of functionality, hence the name function.  A function is essentially a block of C++ code that you give a name and then call from other locations in your application, when you want the computer to perform the instructions contained in that function.
You create a function by defining it.  The function definition may contain a return type, a function name, parameters to accept incoming arguments, and finally a body which contains the code that will execute as part of that function.  Functions may or may not return a value back to the caller and they may or may not accept arguments passed in to the function.
When you move into more advanced C++ programming, you can also overload functions.  This refers to the practice of using the same function name to refer to multiple functions that perform different actions.  Why would you do this?  Consider a simple scenario where you want to perform addition on some values so you create a function called Sum().  You could overload Sum() by creating variants such as:
int Sum(int x, int y)
{
     return x + y;
}

int Sum(int x, int y, int z)
{
     return x + y + z;
}
The compiler will know which function to call, based on the number of arguments passed in.
In this video you will understand briefly about FUNCTIONS.

 Function Prototypes


When declaring a function, you specify its storage class, return type, name, and parameters.  Some refer to this as the function signature.  In C++ it is also called a function prototype. 
In C++, function prototypes belong in header files.  Recall that the header file is what gets imported into other source code files so that the compiler can track proper use of functions and other aspects of the included files.  The function prototype only contains the function's signature with no implementation details.  The implementation details along with the function signature, define the function.  The function definition exists in the source code file (.cpp).
In this video you will briefly understand Functions prototypes.

 Function Parameters

A function can accept values that will be used in the statements in the function body.  These values are known as arguments when passed to a function.  The arguments are passed into parameters.  Parameters are the placeholders that are found inside the parentheses of a function declaration, as shown in the following example where a and b are the parameters.  Note that the data types for these are specified and that the parameters are separated by a comma.
int Sum(int a, int b)
{
     return a + b;
}
Your function can specify as many parameters as necessary for the function to complete its work but for obvious reasons, you don't want to create a function signature that is so long, your code line wraps.  If your function takes that many parameters, it's worth refactoring it to reduce the number of parameters.
When calling the function, you use the function name, followed by an open parenthesis, the arguments that will be passed into the parameters, and then a closing parenthesis, as shown here:
int result = Sum(2, 3);
Note that you don't have to include the data type specifiers for the arguments being passed in but you must know the data type the function expects.  This is one of the reasons for declaring a function prototype prior to using it in code.
In this video you will briefly understand about Function parameters.

  Inline Functions

One of the goals for using functions in your code is to create discrete pieces of functionality in your code that is easier to test and maintain.  However, functions also have an impact on application performance.  The reason for this impact results from the operations that must be performed when a function is called.  For example, registers in the CPU need to be reset, stack pointers are created, memory is consumed for these pointers and the values that are passed into and out of the function.    For simple functions that may perform only a single operation or have only a single, simple statement, you might wonder why creating a function for it is worth the effort.
You can still make use of a function to perform the necessary computation but it makes more sense to create the function as an inline function.  Inline functions avoid the overhead associated with traditional function calls.  You create an inline function by prefixing it with the inline keyword.  A common function found in applications for a sorting algorithm such as bubble sort, is a swap function.  Swap takes two variables and swaps their values as shown here:
inline void swap(int & a, int & b)
{
     int temp = a;
     a = b;

     b = temp;
}
Using this mechanism, each time that a call to the swap() method is encountered in your code, the compiler will insert the body of the function in that location as opposed to making a function call.  This example demonstrates what that would look like.
// Traditional method that results in a function call
swap(5, 6);

// Using an inline function call, the compiler converts the previous line to this
int temp = a;
a = b;

b = temp;
This avoids the overhead of making a function call because the contents of the function body are now located at the point where the functionality is required.   Note a couple of points about inline functions:
  • the inline keyword is a compiler directive that is a recommendation only.  The compiler may ignore your request and compile the function normally resulting in function calls anyway.
  • if you are using inline functions and change the function in anyway, the code needs to be recompiled because the code for that function will need to be updated in each location it was used.
  • use inline functions only for small functions that are used frequently, not for large functions.
Int this video you will briefly understand Inline functions.

 Storage Classes and Scope

MSDN defines storage class as, "A storage class in the context of C++ variable declarations is a type specifier that governs the lifetime, linkage, and memory location of objects."
Lifetime refers to how long the variable "hangs around" in memory from the point at which it is declared and the point at which it is destroyed (the memory it used is released).  For the most part, once a variable goes out of scope, its memory will be released back to the operating system for reuse.
Linkage refers to the visibility of a variable outside of the file that contains it. 
Memory location refers to the place in which the variable is found in memory.  This doesn't refer to the physical memory address as you might expect but more to the logical division of memory that applies to a running application.  There are two logical memory areas known as the stack and the heap.  The stack is a location in memory where intrinsic data is stored as well as memory addresses (pointers).  It operates in the form of data structure known as a stack.  Like a cafeteria stack of plates, items are pushed on top of the stack and other items are pushed further down.  To remove an item from the stack, it is popped off, used, and discarded.
The heap, or free store, is a pool of memory that is used to store objects that dynamically allocated at run time by your application.  An object is what you will learn about in the next topic on object-oriented programming.  You create and destroy objects on the heap by using specific instructions in your program code.
Scope is the term used describe where an identifier is visible in a program.  An identifier is a variable, constant, class, etc.  Your identifier is visible from the point in which you have declared it until the end of its scope.  The following code sample displays different scope for the identifiers used.
1. #include <iostream>
2. int main()
3. {
4.     int total = 0;
5.     for(int i = 1; i <= 10; i++)
6.     {
7.          total += i;
8.     }
9.     std::cout << "The sum of the numbers 1 to 10 is " << total << std::endl;
10.    std::cout << "Current value of i is " << i << std::cout;

11. return 0;
12. }
In the previous code, the variable total is declared inside main() but outside of the for loop.  This means that total is visible (in scope) for the entire main() method, which also includes inside the for loop.  However, the variable i is declared inside the for loop's initialization section and is therefore constrained to the scope of the for loop.   The code at line 10 will result in an error in C++ that indicates the variable is undefined.    Anyplace other than inside the for loop is out of scope for the variable i.
C++ makes use of the following keywords that apply to storage classes:
  • static - identifiers declared with static are allocated when the program starts and deallocated when the program execution ends.  Declaring a variable as static in a function means that the variable will retain its value between calls to the function.
  • extern - used to declare an object that is defined in another translation unit of within the enclosing scope but has an external linkage.
  • thread_local - declares that the identifier is only accessible on the thread in which it is created.  This prevents sharing of the identifier across multiple threads in the same application.   This is part of the C++11 standard.

 Introducing Objects 

Classes enable you to create your own custom, self-contained, and reusable types. A class file is often considered a blueprint for objects that you use in your code.  Typically you will create class files to help model real-world objects in your code. 
An example of this might be the software that manages a bank ATM.  The software would need to understand the concept of customers, accounts, transactions, etc.   It's far easier to implement the software application by modelling these real world objects as software objects (classes) in your code.
In this module, you will gain an introduction to classes in C++ that will cover the basics only.  The second installment of the C++ course will delve deeper into object oriented concepts in C++.

 Creating Classes and Members

In C++, a class is a programming construct that you can use to define your own custom types. When you create a class, you are effectively creating a blueprint for the type. The class defines the behaviors and characteristics, or class members, which are shared by all instances of the class. You represent these behaviors and characteristics by defining methods and fields within your class.

Suppose you create a class to represent a rectangle shape in your program.  You use the class keyword to declare a class, as shown in the following example:

//Declaring a Class
class Rectangle
{
public:
    int _width;
    int _height;

};

Here we have declared a class called Rectangle and given it two public member variables called _width and _height, that will be used to represent the width and height of our rectangle.   Note that they are accessible directly because they are public, as a result of the public: modifier.

 Using a Class

Now that we have a class created to represent a rectangle, we can use that in our code to create instances of a rectangle in our program.  When we create a new rectangle from this class, it is known as a rectangle object and will be given a unique name.  That way ,we can refer to it in our program directly and distinguish it from other rectangle instances that we might create, should our program require more than one.
void main()
{
     Rectangle outer;
     Rectangle inner;     

     outer._width = 10;
     outer._height = 10;

     inner._width = 5;
     inner._height = 5;
}
In this sample code, we have created two rectangle objects called outer and inner.   Then, using what is known as "dot notation" or the dot operator, we provide values for the width and height of each rectangle.  The outer rectangle is 10 x 10 and the inner rectangle is 5x5.
In this video you will understand briefly classes.

 Class Initialization

Initialization is an important part of working with your classes in C++.  Even when using intrinsic data types, if you do not initialize the variable for that type and you access it in your code, you will end up with whatever values are stored in the memory location that the variable refers to.  This is something you want to avoid doing.   You should always initialize your types in C++;
C++ offers a couple of options for initializing your classes.  You can initialize the member variables by using the dot operator and setting the values explicitly or you can include a constructor in your class that is responsible for initialization the member variables.   You'll see this in the demo video next as well as more information in the topic on encapsulation.

 Encapsulation 

Often considered the first pillar of object-oriented programming, encapsulation can be used to describe the accessibility of the members belonging to a class.  C++ provides access modifiers and properties to help implement encapsulation in your classes.  While some consider this accessibility configuration to be the only aspect of encapsulation, others also define encapsulation as the act of including all data and behavior required of the class, within the class definition.
We use encapsulation to prevent changing the member variables directly.  This is considered a poor practice because it presents the opportunity for potentially incorrect values to be assigned to these member variables.  This can result in unexpected behavior or more serious problems with your executing code.  It also helps with debugging of your code.

 Const Objects 

Recall that the keyword const was used to indicate that a data type you use in your code is a constant and cannot have its value changed during application runtime.  Objects in your code can also make use of the const keyword to indicate that the objects are immutable.  Immutable simply means that they cannot change.
In the next couple of video segments, you will be introduced to const objects and see a demo of how to use them in code.
In this video you will briefly understand const objects.

Comments

Popular posts from this blog

C++ Classes

C++ Programming  Introduction to Splitting Class Files Class structure  If an aspect of object-oriented programming is encapsulation, then why would we split our classes into separate files?  Why not keep it all in one if we wish to "encapsulate" all there is about the class? Recall that a part of encapsulation is also data hiding.  Think about your car for a bit as we use it in an analogous way.  A car is a complex device that contains many different components.  You are able to operate your car without the need to understand the complexities of the internal combustion engine or the radio electronics.  You don't need that knowledge to be able to effectively operate the car. Likewise, a programmer using your class files does not need to know how you have implemented your methods to achieve the functionality.  In fact, perhaps you have a proprietary algorithm for a spell checker that is really fast and you don't want any...

Control Statements

C++  Programming  # C++ operators  Because you will start to learn about control statements in C++, it's important to understand the C++ operators that exist in the language first, as they play an important role in control statements. You will work with comparison operators to determine if values are equal, greater, or less than each other.  C++ also allows you to use mathematical operators for incrementing values to help control the number of iterations in a loop.  You can also make use of bitwise operators to speed up some operations in your code.  Operator Description + addition - subtraction * multiplication / division % modulo += (y += x) same as y = y + x -= (y -= x) same as y = y - x *= (y *= x) same as y = y * x ++ increment by 1 -- decrement by 1 == equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to && logical AND || logical OR ! logical NOT ...