Tuesday, 23 December 2014

Character arrays and String arrays in C++ with examples

Character arrays and String arrays in Cpp with examples:

Creation of the character array syntax:

See the bleow sample that we can declare the names or words in the character array
char szExample[] = {‘F’, ‘i’, ‘r’, ‘s’, ‘t’, ‘s’, ‘m’, ‘p’, ‘l’, ‘e’};


Storing Sequences in Arrays

#include <iostream>
using namespace std;
void ShowArray(char stringArray[], int iSize);
int main(int nArg, char* pszArgs[])
{
  char szExample[] = {'F', 'i', 'r', 's', 't', 's', 'm', 'p', 'l', 'e'};
  ShowArray(szExample, 10);
  cout << "\n";
  return 0;
}
// ShowArray - display an array of characters
// by outputing one character at
// a time
void ShowArray(char stringArray[],int iSize) {
for(int i = 0; i< iSize; i++)
{
cout << stringArray[i];
}
}

Wednesday, 10 December 2014

Macros in c++ with example

Macros in C++ with Examples: 

Macros can be implemented using #define. When the call to the macro occurs, the code of the macro will substitute in the source code before compilation.
Syntax of the macro is
#define macroname(arg1, arg2…) expression

NOTE: Do not leave the space between the function and the parenthesis. The parenthesis should start immediately after the macro name.
Below is the small example:
#define MIN(i,j) (i < j ? i : j)


Wednesday, 3 December 2014

Calculate the time taken by a function in C and explain with example

How to calculate the time taken by a function in C? Please give examples for the same.

We can calculate the time taken by the process using the clock () function. This function is available in
 time.h
header.

By calling the clock function at the function start and the end of the code.
And t be follow the below steps to calculate the time taken by the process, please find the pseudo code.

What is name mangling in C++ and explain with example?

What is name mangling in Cpp?

C++ compilers differentiates the functions when it generates the object code. It adds the additional information to the function names called as name mangling. Name mangling is compiler dependent.

Why name mangling is required in c++?

As we know C++ supports function overloading, i.e., there can be more than one functions with same name and differences in parameters. To differentiate the functions name mangling should be used.
Let us see consider following declarations of function fun()

Possibility to overload main function in C++

Is it possible to overload main function in Cpp?

Yes, it is possible by declaring the main function as the member function in a class.
See the below example
#include <iostream>
using namespace std;
class MyTestClass

{

public:
    int main(int iVal)
    {
        cout << iVal << endl;
        return 0;
    }
    int main(char *pcCh)
    {
        cout << pcCh << endl;
        return 0;
    }
    int main(int iVal ,int iVal2)
    {
        cout << iVal << " " << iVal2 << endl;
        return 0;
    }
};
int main()
{
    MyTestClass obj;
    obj.main (12);
    obj.main ("Testin main function");
    obj.main (30, 300);
    getchar ();
    return 0;
}

How is it possible to restrict dynamic allocation of objects in C++?

How is it possible to restrict dynamic allocation of objects in C++?

Yes, it is possible only when we implement the new operator function in the private section.
See the below example
  #include <iostream>
using namespace std;
class MyTestClass
{
private:
    void* operator new(size_t size);
    int iVal;
public:
    MyTestClass ()          { iVal = 100; cout << "Constructor of the class is called\n"; }
    void display ()  { cout << "value is  = " << iVal << "\n";  }
    ~MyTestClass ()         { cout << "Destructor of the class is called\n"; }
};
int main()
{
 //  MyTestClass *ptr = new MyTestClass;
    MyTestClass ocTestCls;     
    ocTestCls.display();
    return 0;
} 


What are the functions that don’t automatically inherited from the base class?

What are the functions that don’t automatically inherited from the base class?

The functions that are not inherited from the base class to the derived class are constructors & destructors. These two are deals with the object creation and deletion, so they have the knowledge about their particular class.

There is another function operator=, which also not inherited by the derived class. Because it is also behaves like constructor to create the object.

Let us see the below example that the functions that are synthesized by the compiler.