C++ tutorial

C++ Basics


Struct



You can have multiple data members of various types in C Language structures. In C++, you can have both data members and function. Actually there is no difference between strcut and class in C++ except all the members declared in struct are public by default whereas in class it is private.

Three access levels are,

1. Private - Can be accessed only by the members of the struct or class
2. Protected - Can be accessed by the struct and its derived struct  or class
3. public - Can be accessed by any other struct  or class or function.



Sample Code


struct MyClass
{

    int m; // By default variables and functions are public
    
int TestFunc1();

private:
private: 
    int a;
    int b;

public
:
    MyClass() : a(10), b(20), m(30)
    {
    };
};

Class


 
In C++, class is a keyword. Using class, we can implement all object oriented programming language concepts. All the members declared in C++ classes are private by default. We can also have protected access level to for derived classes and public access level for any functions or classes.

Three access levels are,

1. Private - Can be accessed only by the members of the struct or class
2. Protected - Can be accessed by the struct and its derived struct  or class
3. public - Can be accessed by any other struct  or class or function.


Sample Code

In C++, class is a keyword. Using class, we can implement all object oriented programming language concepts. All the members declared in C++ classes are private by default. We can also have protected access level to for derived classes and public access level for any functions or classes.Three access levels are,1. Private - Can be accessed only by the members of the struct or class2. Protected - Can be accessed by the struct and its derived struct  or class3. public - Can be accessed by any other struct  or class or function.
class MyString
{
private:
    char *m_pString;

public:
    MyString()
    {
        std::cout << "Calling Default Constructor\n";
        m_pString = NULL;
    }
    ~MyString()
    {
        if( this->m_pString != NULL)
        {
            std::cout << "Calling Destructor\n";
            delete this->m_pString;
            this->m_pString = NULL;
        }
    }

    MyString(const char *p)
    {
        std::cout << "Calling Parameterized Constructor\n";
        int len = strlen(p);
        m_pString = new char [len + 1];
        strcpy(m_pString, p);
    }

    MyString(const MyString &other)
    {
        std::cout << "Calling Copy Constructor\n";
        m_pString =  other.m_pString;

        //int len = strlen(other.m_pString);
        //m_pString = new char [len + 1];
        //strcpy(m_pString, other.m_pString);
    }

    const MyString& operator = (const MyString &other)
    {
        std::cout << "Calling assignment operator\n";
        int len = strlen(other.m_pString);
        m_pString = new char [len + 1];
        strcpy(m_pString, other.m_pString);
        return *this;
    }


    operator const char*()
    {
        return this->m_pString;
    }
   
};
Struct and Union

All the members in union will share the same memory location unlike struct.
We have a same set of members for MyStruct and MyUnion in the example below. Compiler will allocate 16 bytes for the given structure and allocate just 4 bytes for the union.

struct MyStruct
{
long lValue;
char ch;
char buf[4];
float f;
};

union MyUnion
{
long lValue;
char ch;
char buf[4];
float f;
};


Another typical example in COM is VARIANT. The following is VARIANT definition from OAIDL.H from Visual C++ SDK Library. It can accommodate any kind of data type using VARTYPE and its corresponding variable in the union member.
Loops - display numbers using for loop, while loop and do while loop 


The main difference between for loop, while loop, and do while loop is
  1. While loop checks for the condition first. so it may not even enter into the loop, if the condition is false.
  2. do while loop, execute the statements in the loop first before checks for the condition. At least one iteration takes places, even if the condition is false.
  3. for loop is similar to while loop except that
    • initialization statement, usually the counter variable initialization
    • a statement that will be executed after each and every iteration in the loop, usually counter variable increment or decrement.

The following sample code that can display numbers explains all 3 different loops:

int main()
{

    int num = 0;
    std::cout << "Using while loop\n";
    while(1)
    {
        std::cout << ++num << "\n";
        if(num >= 5)
            break;
    }

    std::cout << "Using do while loop\n";

    num = 0;
    do
    {
        std::cout << ++num << "\n";
    } while(num < 5);

    std::cout << "Using for loop\n";

    for(num = 1; num <= 5; num++)
    {
        std::cout << num << "\n";
    };

    return 0;
   
}



Output


Using while loop
1
2
3
4
5
Using do while loop
1
2
3
4
5
Using for loop
1
2
3
4
5
Press any key to continue . . .



for Next......Please Visit on:: http://softwareandfinance.com/