Monday, June 19, 2017

C++ Multi-Threading program to print odd and even numbers in sequence from two functions

C++ Multi-Threading program to print odd and even numbers in sequence from two functions.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int MAX = 10;
int count = 0;

pthread_mutex_t mutex;
pthread_cond_t cond;

void *even(void *arg)
{
    while(count < MAX) {
        pthread_mutex_lock(&mutex);
        while(count % 2 != 0) {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("%d ", count++);
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
    pthread_exit(0);
}

void *odd(void *arg)
{
    while(count < MAX) {
        pthread_mutex_lock(&mutex);
        while(count % 2 != 1) {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("%d ", count++);
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
    pthread_exit(0);
}

int main()
{
    pthread_t t1;
    pthread_t t2;

    pthread_mutex_init(&mutex, 0);
    pthread_cond_init(&cond, 0);

    pthread_create(&t1, 0, &even, NULL);
    pthread_create(&t2, 0, &odd, NULL);

    pthread_join(t1, 0);
    pthread_join(t2, 0);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return  0;
}

Output::
0 1 2 3 4 5 6 7 8 9 10  

C++ program to play music and compile in GCC compiler

Here is the simplest program to play music in C++.

Here you have to do a setting if you are using and UI based IDE like code block or Dev C++. You have to go to setting and add  lwinmm in linker setting. lwinmm is a linker used while compilation of program containing sound.
with this program only wav music file format can be played. You can convert the music file to wav format online by vising this link -- http://media.io/

The code goes as below::

#include <windows.h>
#include <iostream>
#include <mmsystem.h>
using namespace std;
int main()
{
  system("clear");
  cout<<"Sound playing... enjoy....!!!"<<endl;
  PlaySound("C://music1.wav", NULL, SND_SYNC); //SND_FILENAME or SND_LOOP
  return 0;
}

If you are using gcc compiler and compiling using command line then your life will be easy. you need you write below command for compilation. 


Let say I have saved my program as sound.cpp. So command to compile will be-

$ g++ sound.cpp -lwinmm -o sound

After successful compilation to execute the file, write as below-
$ ./sound

Sunday, June 18, 2017

Forward declaration:: used in friend function of two class.
E Balagurusamy page 128

* Do not confuse between
void fun1(int & a)
{
.....
......
}
and
void fun1(int * a)
{
....
....
}
Both are pass by reference.

Copy constructor::
Program to illustrate the copy constructor.
#include<iostream>
using namespace std;
class ABC
{
    int a,b;
public:
    //int c;
    void show()
    {
        cout<<"a="<<a<<" and b="<<b<<endl;
    }
    ABC(){a=0;b=0;}
    ABC(int x,int y){a=x;b=y;}
    ABC(ABC& obj){ a=obj.a; }
};
int main()
{
    int a=2,b=3;
    ABC obj1,obj2(2,3);
    ABC obj3(obj2);            //Copy constructor called (copy only a, as defined in the constructor)
    ABC obj4 = obj2;          //Copy constructor called (copy only a, as defined in the constructor)
    obj1=obj2;         //default assignment operator of the system called, copy all elements one by one.
    cout<<"obj1(2,3)";
    obj1.show();
    cout<<"\nobj2(2,3)";
    obj2.show();
    cout<<"\nobj3(2,lets see)";
    obj3.show();
    cout<<"\nobj4(2,lets see)";
    obj4.show();

}

Output::
    obj1(2,3) a=2 and b=3
    nobj2(2,3) a=2 and b=3
    obj3(2,lets see) a=2 and b=2293456
    obj4(2,lets see) a=2 and b=0

Shallow Copy and Deep Copy --http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

-------------------------------------------------------------------------------
How to get rid of compiler error-
deprecated conversion from string constant to ‘char*’
I got this compilation error while compiling the below code.

void ABC(char *strng) {};
ABC("Hello World");
or
void ABC(char *strng){};               //This code may compile but the window will crash on execution
char *strng1="Hellow World";
ABC(strng1);

I find two solution for this...
1.
void ABC(char *strng){};
char strng1[] ="Hellow World";
ABC(strng1);

2. define const every where if the sting does no changes.
void ABC(const char *strng){};
const char *strng1 ="Hellow World";
ABC(strng1);


---------------------------------------------------------------------------
Access the private data member variables of the class without friend function.
#include <iostream>
#include <string>
using namespace std;
class ABC
{ private:
   int a,b;
  public:
  void show() {
      cout<<"a="<<a<<"and b="<<b; }
};
int main()
{
    ABC obj;
    int *p_obj=(int*)&obj;
    *p_obj=10;                           // It will set the value of a to 10;
     *(p_obj+1) = 20;                 //It will set the value of b to 20.
    obj.show();
}