Showing posts with label cpp programs. Show all posts
Showing posts with label cpp programs. Show all posts

Monday, November 26, 2012

Data Structure Binary Search Tree Interview Question | Problem Solving

Data Structure Binary Search Tree Interview Question | Problem Solving C++ Programming

Data Structure Binary Search Tree Interview Question | Problem Solving

Question

In this problem you have to write a program to check if two given binary trees are structurally equivalent.
Two trees are structurally equivalent if they are both null or if the left and right children of one are structurally equivalent to the RESPECTIVE children of the other. In other words, when you draw the trees on paper, they should LOOK alike (ignoring the values at the nodes).

Thursday, September 27, 2012

Doubly Linked Lists Implementation using oops of c plus plus | Data Structure

Doubly Linked Lists Implementation using oops of c plus plus | Data Structure

Doubly Linked Lists Implementation using oops of c plus plus | Data Structure
By Sanjay Sinalkar
Write a program in c++ using object oriented concept to create Double Linklist which having following functionality 

1)Insert element at Begin
2)Insert element at given position
3)Insert element at End
4)Delete element from Begin
5)Delete element From End
6)Delete element from Given position
 7) Show all element Inserted in Display function()
Program should validate given position before calling given position operation such as Insert or Delete

Monday, September 17, 2012

Stack implementation in c++ code using array | Data Structure

Stack implementation in c++ code using array  | Data Structure

This program is the demonstration for implementing Simple Stack Program using array and Object Oriented Concept with basic functions of Stack push() and pop() with index called top which gives the current status of Stack! Stack has defined size so need two more function to check whether isEmpty() or isFull() to check stack is empty or not or is full or not. In this scenario top is integer type as default to top=-1,if top=-1 Mean Stack is Empty , if top=sizeOfStack-1 then stack is full.

Thursday, September 13, 2012

Function Overloading for Volume Calculation C++ Programming

Function Overloading for Volume Calculation C++ Programming

Function Overloading for Volume Calculation C++ Programming

 



Virtual Function Implementation in C++ Programming

Virtual Function Implementation in C++ Programming

Question:
Write a base class called Employee. Use this class to store values name, salary etc.
Derive three specific classes called Manager, Teamleadand Programmer. Add to the
base class, a member function get_data() to initialize base class data members and
another function display() to compute and display information . Make display() as a
virtual function and redefine this function in the derived classes to suit their
requirements.

Operator Overloading in C++ Programming Hr Min Sec Example

Operator Overloading in C++ Programming Hr Min Sec Example

Question:
Create a class Time with members hr, min and sec. Write all three
constructors, destructor, and make one static member to count the objects of
class.Overload the following operators :
>>,<<,= =,=,+,++,--

Solutions:

/* Program for Operator Overloading */

#include<iostream>
using namespace std;

static int c=0;

class Time
{
public:
int hr,m,s;

Time()
{
hr=0;m=0;s=0;
//cout<<"\nDefault Constructor Called!"<<endl;
c++;
}
Time(int thr,int tm,int ts)
{
hr=thr;
m=tm;
s=ts;
//cout<<"\nParameterized Constructor!"<<endl;
c++;
}

~Time()
{

}
Time operator+(Time);
void operator++();
void operator--();
void operator>>(Time);
void operator<<(Time);
void operator==(Time);
void operator=(Time);
};

Time Time::operator+(Time op)
{Time temp;
temp.s=s+op.s;
if(temp.s>=60)
{
m++;
temp.s=temp.s%60;
}
temp.m=m+op.m;
if(temp.m>=60)
{
hr++;
temp.m=temp.m%60;
}
temp.hr=hr+op.hr;
return temp;
}

void Time::operator>>(Time op)
{cout<<"Enter the Values for Hr Min Sec"<<endl;
cin>>hr>>m>>s;

}

void Time::operator<<(Time op)
{
cout<<"\nTime is:"<<hr<<"hr "<<m<<"m "<<s<<"s "<<endl;
}

void Time::operator==(Time op)
{
if(hr==op.hr && m==op.m && s==op.s)
{
 cout<<"Given Times are Same"<<endl;
}
else
 cout<<"Gives Times are Different!"<<endl;
}

void Time::operator=(Time op)
{
hr=op.hr;
m=op.m;
s=op.s;
}

void Time::operator++()
{
++s;
if(s>=60)
{
m++;
s=s%60;
}
if(m>=60)
{
hr++;
m=m%60;
}

}

void Time::operator--()
{
s--;
if (s<0)
{
s=59;
m--;
}
if (m<0)
{
m=59;
hr--;
}

}


int main()
{
Time hr1(2,30,59);Time hr2(1,20,21);
Time s1,s2,s3;
Time addhr=hr1+hr2;
cout<<"\nThe Addition is:"<<addhr.hr<<"hr "<<addhr.m<<"m "<<addhr.s<<"s "<<endl;
s1>>hr1;
s2>>hr1;
s3=s1+s2;
s3<<hr1;
//s3<<;
++s3;
s3<<hr1;
hr1>>hr1;
--hr1;
hr1<<hr1;
cout<<"\nTotal Object Created:"<<c<<endl;
return 0;
}

Friend Function Implementation in C++ Programming

Friend Function Implementation in C++ Programming

Questions:-
Create two classes DM and DB which store the value of distances. DM stores
distances in meters and centimeters and DB in feet and inches. Write a program
that can read values for the class objects and add one object of DM with another
object of DB. Use a friend function to carry out the addition operation. The object
that stores the results may be a DM or DB object, depending on the units in
which the results are required.
(Object Oriented Programming With C++ By Balaguruswamy)