Inline functions

Generally when a function is called, the control is transferred to that function, along with some values if necessary, and after execution, the control is transferred back to the calling function. Obviously it saves memory space but certainly increases execution time. To reduce this execution time, C++ language provides inline functions.

The syntax of declaring inline functions is:

              inline function_name(list_of_arguments)
              {
                     // Function body
              }

A function is made “inline” by using the keyword inline before the function’s return type of the function definition. Whenever we call inline function, the C++ compiler places the entire code of inline function directly inside the code of the calling function. In other words we can say that inline functions are just like preprocessor macros because the compiler substitutes the entire function body for each inline function call.

If the function code is small, straight-line or when there are relatively few calls to it then use inline function otherwise uses the normal function.
Remember that when we define the function inline, it is up to the C++ compiler whether to insert the code of inline function or not to insert. 

Introduction to C++

C++ (pronounced "see plus plus") is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features.Developed by Bjarne Stroustrup starting in 1979 at Bell Labs, it adds object oriented features, such as classes, and other enhancements to the C programming language. Originally named C with Classes, the language was renamed C++ in 1983.







In 1998, the C++ standards committee (the ISO/IEC JTC1/SC22/WG21 working group) standardized C++ and published the international standard ISO/IEC 14882:1998 (informally known as C++98). For some years after the official release of the standard, the committee processed defect reports, and published a corrected version of the C++ standard, ISO/IEC 14882:2003, in 2003. In 2005, a technical report, called the "Library Technical Report 1" (often known as TR1 for short), was released. While not an official part of the standard, it specified a number of extensions to the standard library, which were expected to be included in the next version of C++. Support for TR1 is growing in almost all currently maintained C++ compilers.

The latest major revision of the C++ standard, C++11, (formerly known as C++0x) was approved by ISO/IEC on 12 August 2011.It has been published as 14882:2011.

C++: kind of and has a relationship

If two classes are related with one another there can be two kinds of relationship between them.

  1. kind of relationship
  2. has a relationship
kind of relationship is supported by inheritance.
has a relationship is supported by composition or containership.

In Inheritance if a class Y is derived from Class X,we can say that "Class Y is a kind of  Class X". This is because class Y has all the characteristics of X, and in addition some of its own.
For this reason inheritance is often called a kind of relationship.

In has a relationship you simply create objects of your existing class inside the new class.
Example 1:
               class company
               {
               };
               class employee
               {
                        company c;
               };

Example 2:
                #include<iostream.h>
                #include<string.h>
                class carburettor
                {
                         private:
                                      char type;
                                      float cost;
                                      char mfr[20];
                         public:
                                      void setdata(char t,float c,char *m)
                                      {
                                               type=t;
                                               cost=c;
                                               strcpy(mfr,m);
                                      }
                                      void displaydata()
                                      {
                                               cout<<endl<<type<<endl<<cost<<endl<<mfr;
                                      }
                 
                }; 
                class car
                {
                       private: 
                                     char model[20];
                                     char drivetype[20];
                       public:
                                     void setdata(char *m,char *d)
                                     {
                                               strcpy(model,m);
                                               strcpy(drivetype,d);
                                     }
                                     void displaydata()
                                     {
                                              cout<<endl<<model<<endl<<drivetype;
                                     }
                                     carburettor c;   //embedded object
                };
               void main()
               {
                       car mycar;
                       mycar.c.setdata('A',6500.00,"Hund");
                       mycar.setdata("sports","4-wheel");

                       mycar.c.displaydata();
                       mycar.displaydata();
               }

C++ has much more sophisticated mechanisms for code reuse in the form of composition and inheritance.

Composition is useful when classes act like a data type.

Constructors and Destructors

Constructor:
Constructors in C++ are special member functions of a class. A Constructor is a function that initilizes the members of object.There can be any number of overloaded constructors inside a class.The Constructor is automatically called whenever an object is created or dynamically allocated using "new" operator.

If no constructor is supplied then a default one is created by the compiler without any parameters.If you supply a constructor with parameters then default will NOT be created.

Some Points about Constructor:
  • Constructors have the same name as the class.
  • Constructors do not return any values.
  • Constructors are invoked first when a class is initialized. Any initialization for the class members,memory allocations are done at the constructor.
  • Constructors are never virtual.
Ex:
       Class Sample
       {
              public:
                      Sample()
                      { 
                           ..
                      }
       };

Destructor:
Destructor s in C++ also have the same name,except that they are preceded by a '~' operator. The destructors are called when the object of a class goes out of scope.The main use of destructors is to release dynamic allocated memory.Destructors are used to free memory,release resources and to perform other cleanup.If the destructor is not declared inside a class,the compiler automatically create a default one.

If the constructor/destructor is declared as private,then the class cannot be instantiated.

Ex:
        ~Sample()

Dangling Pointer

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed. 

The following code snippet shows this:
                     class Sample
                     {
                                public:
                                         int *ptr;
                                Sample(int i)
                                {
                                         ptr = new int(i);
                                }
                               ~Sample()
                                {
                                         delete ptr;
                                 }
                                void PrintVal()
                                 {
                                           cout << "The value is " << *ptr;
                                 }
                     };
                    void SomeFunc(Sample x)
                    {
                                cout << "Say i am in someFunc " << endl;
                    }
                    int main()
                    {
                                Sample s1 = 10;
                                SomeFunc(s1);
                                s1.PrintVal();
                     }
In the above example when PrintVal() function is called it is called by the pointer that has been freed by the destructor in SomeFunc.