Posts

Showing posts from February, 2019

# smart pointer implementation in c++

#include <iostream> using namespace std; template <class T> class smart { protected : T *ptr; public : explicit smart(T *data = NULL) { ptr = data; } ~smart() { delete ptr; } T & operator *() { return *ptr; } }; class test { public : test() { cout << "test - constructor " << endl; } ~test() { cout << "test - destroctor " << endl; } }; int main() { smart<int> obj(new int(20)); smart<char> obj2(new char('A')); smart<test> obj3(new test()); cout << *obj << endl; cout << endl; cout << *obj2 << endl; }

Printing the Prime no (As per the user Input )

#include <iostream> using namespace std; void  primeno(int x) { int k ; for (int i = 2; i < x; i++) { k = 0; for (int j = 1; j <= i; j++) { if (i%j == 0) { k++; } } if (k == 2) { cout << i << " " <<endl; } } } int main() { int primenosize;         std::cout << "prime no \n" <<endl; cout << "input range to print the prime no" <<endl; cin >> primenosize; primeno(primenosize); return 0; }

Printing the odd and even no using Pthread

#include<iostream> #include <pthread.h> using namespace std; void *even(void * size) {             int x = reinterpret_cast<int> (size);         cout << "even no" <<endl;     for(int i = 0; i<x; i++)     {         if(i%2 == 0){                         cout << i << endl;         }             }     cout <<endl;     cout <<"even thread is terminited ";     cout <<endl; } void *odd(void *size ) {     pthread_t tid2;         int x = reinterpret_cast<int> (size);         cout << "odd no" <<endl ;     for(int i = 0; i<x; i++)     {         if(i%2 != 0){                         cout << i <<endl ;         }             }             pthread_create(&tid2, NULL, even, (void*)x);         cout <<endl;     pthread_join(tid2, NULL);         cout <<"odd thread is terminited ";     cout <<endl; } int main() {     int size;         cout <<