Saturday, 6 June 2020

copy constructor vs copy assingment operator

#include<iostream>
using namespace std;

class Test
{
public:
Test() {}
Test(const Test &t)
{
cout<<"Copy constructor called "<<endl;
}

Test& operator = (const Test &t)
{
cout<<"Assignment operator called "<<endl;
return *this;
}
};

// Driver code
int main()
{
Test t1, t2;
t2 = t1;
Test t3 = t1;
return 0;
}

Thursday, 4 June 2020

Diff btw strcat and strncat functions in C

// C,C++ program demonstrate difference bewteen
// strncat() and strcat()
#include <stdio.h>
#include <string.h>

int main()
{

// Take any two strings
char src[50] = "ghijklmno";
char dest1[50] = "abcdef";
char dest2[50] = "abcdef";

printf("Before strcat() function execution, ");
printf("destination string : %s\n", dest1);

// Appends the entire string of src to dest1
strcat(dest1, src);

// Prints the string
printf("After strcat() function execution, ");
printf("destination string : %s\n", dest1);

printf("Before strncat() function execution, ");
printf("destination string : %s\n", dest2);

// Appends 3 characters from src to dest2
strncat(dest2, src, 3);

// Prints the string
printf("After strncat() function execution, ");
printf("destination string : %s\n", dest2);

return 0;
}

sorting,reversing,and many more in Algorithm library in STL C++

// A C++ program to demonstrate working of sort(),
// reverse()
#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric> //For accumulate operation
using namespace std;

int main()
{
// Initializing vector with array values
int arr[] = {10, 20, 5, 23 ,42 , 15};
int n = sizeof(arr)/sizeof(arr[0]);
vector<int> vect(arr, arr+n);

cout << "Vector is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";

// Sorting the Vector in Ascending order
sort(vect.begin(), vect.end());

cout << "\nVector after sorting is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";

// Reversing the Vector
reverse(vect.begin(), vect.end());

cout << "\nVector after reversing is: ";
for (int i=0; i<6; i++)
cout << vect[i] << " ";

cout << "\nMaximum element of vector is: ";
cout << *max_element(vect.begin(), vect.end());

cout << "\nMinimum element of vector is: ";
cout << *min_element(vect.begin(), vect.end());

// Starting the summation from 0
cout << "\nThe summation of vector elements is: ";
cout << accumulate(vect.begin(), vect.end(), 0);

return 0;
}

swaping two stacks using STL

// CPP program to illustrate
// Implementation of swap() function
#include <stack>
#include <iostream>
using namespace std;

int main()
{
// stack container declaration
stack<int> mystack1;
stack<int> mystack2;

// pushing elements into first stack
mystack1.push(1);
mystack1.push(2);
mystack1.push(3);
mystack1.push(4);

// pushing elements into 2nd stack
mystack2.push(3);
mystack2.push(5);
mystack2.push(7);
mystack2.push(9);

// using swap() function to swap elements of stacks
mystack1.swap(mystack2);

// printing the first stack
cout<<"mystack1 = ";
while (!mystack1.empty()) {
cout<<mystack1.top()<<" ";
mystack1.pop();
}

// printing the second stack
cout<<endl<<"mystack2 = ";
while (!mystack2.empty()) {
cout<<mystack2.top()<<" ";
mystack2.pop();
}
return 0;
}

stack implementation using standard library reversing the string

#include <iostream>
#include<stack>
#include<string.h>
using namespace std;

int main()
{
    char str[10]="abcdefgh";
    stack<char> s;
    for(int i=0;i<strlen(str);i++)
    {
        s.push(str[i]);
    }
 
    cout<<"reversed string ";
    while(!s.empty())
    {
        cout<<s.top();
        s.pop();
     
    }
    return 0;
}

stack using c++

#include<iostream>
using namespace std;
struct node
{
int data ;
node *next;
};
class stack
{      // struct node*top;
    node *top;
public:

stack()
{
   top=NULL;
}
void push();
void pop();
void show();
~stack();
};
stack::~stack()
{
node * temp;
temp=top;
while(top!=NULL)
{
top=top->next;
delete temp;
}

}
void stack::push()
{
node *temp;
temp=new node;
cout<<" enter the data to insert\n";
cin>>temp->data;
temp->next=top;
top=temp;
}
void stack::pop()
{
     if(top==NULL)
{
  cout<<" stack is empty\n";
}
     else
{
node *temp;
temp=top;
top=top->next;
cout<<" deleted item is \n"<<temp->data;
delete temp;
}
}

 void stack::show()
 {
node *temp2;
temp2=top;
while(temp2!=NULL)
{
cout<<temp2->data<<"-->";
temp2=temp2->next;
}
cout<<"null\n";
 }



int main()
{
stack s;
int choice;
while(1)
{
cout<<"\t\t\t******MENU********";
cout<<"\n 1 PUSH\n 2 POP \n 3 DISPLAY\n 4 EXIT \n";
cin>>choice;
switch(choice)
{
case 1: s.push();
break;
case 2: s.pop();
break;
case 3: s.show();
break;
case 4: exit(0)  ;
break;
default: cout<<" wrong  choice\n:";
break;
}
}

return 0;
}