A simple function

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

class name{
//*******************************
private:
string fName;
string mName;
string lName;
int ppl;
//*******************************
public:
int getPpl(){return ppl;}
void setPpl(int ppl){this->ppl=ppl;}
name(){fName;mName;lName;ppl=0;}
string  getfName(){ return fName;}
string  getmName(){ return mName;}
string  getlName(){ return lName;}
void    setfName (string Aname){this->fName=Aname;cout<<"   X.   Please Enter First  Name    "; cin>>fName;}
void    setmName (string Bname){this->mName=Bname;cout<<"   X.   Please Enter Middle Name    "; cin>>mName;}
void    setlName (string Cname){this->lName=Cname;cout<<"   X.   Please Enter Last   Name    "; cin>>lName;}
//-----------------------------------------
void enterName(){

cout<<endl;
cout<<endl;
cout<<"   X.   Please enter the Names of people  \n";
string x;string y;string z;
setfName(x);
setmName(y);
setlName(z);
}
/-----------------------------------------
void showNames(){
for(int x=1;x<2;x++){

cout<<endl;
cout<<"   X.   The Name is:: "<< getfName();
   cout<<" "<<getmName();cout<<" "<<getlName();
//------------------------------------------
}}};
//........................................

int main(){
int ppl;
name nam;
nam.enterName();
nam.showNames();
cout<<endl;
cout<<"\t";
    system("pause");
cout<<endl;
    return 0;
}

Edited 1 Year Ago by bc230201818: spelling mistake

this one is tuff I got is from my book and gave it a little touch
and customised a little.
1. the function of preorder treversal is missing. I tried it but it did not work
so I commented it out.
2. the post order treversal is not there and I did not try
3. If you have an elegant way of writing this code in minimum statements
Will apreciate

#include<iostream>
#include<string>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
using namespace std;

//////////////////


// Binary node and forward declaration
template <class EType>
class BinarySearchTree;
template <class EType>
class BinaryNode
{
EType element;
BinaryNode *left;
BinaryNode *right;
BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt )
: element( theElement ), left( lt ), right( rt ) { }
friend class BinarySearchTree<EType>;
};
// BinarySearchTree class
//
// CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failedfinds
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// EType find( x ) --> Return item that matches x

////////////////////////
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// void printTree( ) --> Print tree in sorted order
template <class EType>
class BinarySearchTree
{
public:

BinarySearchTree( const EType & notFound );
BinarySearchTree( const BinarySearchTree & rhs );
~BinarySearchTree( );
const EType & findMin( ) const;
const EType & findMax( ) const;
const EType & find( const EType & x ) const;
bool isEmpty( ) const;
void printTree( ) const;
void makeEmpty( );
void insert( const EType & x );
void remove( const EType & x );
void preorder(const EType & x);
const BinarySearchTree & operator=( const BinarySearchTree & rhs );
private:
BinaryNode<EType> *root;
const EType ITEM_NOT_FOUND;
const EType & elementAt( BinaryNode<EType> *t ) const;
void insert( const EType & x, BinaryNode<EType> * & t ) const;
void remove( const EType & x, BinaryNode<EType> * & t ) const;
BinaryNode<EType> * findMin( BinaryNode<EType> *t ) const;
BinaryNode<EType> * findMax( BinaryNode<EType> *t ) const;
BinaryNode<EType> * find( const EType & x, BinaryNode<EType> *t )
const;
void makeEmpty( BinaryNode<EType> * & t ) const;
void printTree( BinaryNode<EType> *t ) const;
BinaryNode<EType> * clone( BinaryNode<EType> *t ) const;
};

////////////////

/**
* Construct the tree.
*/
template <class EType>
BinarySearchTree<EType>::BinarySearchTree( const EType & notFound ) :
ITEM_NOT_FOUND( notFound ), root( NULL )
{
}
/**
* Copy constructor.
*/
template <class EType>
BinarySearchTree<EType>::
BinarySearchTree( const BinarySearchTree<EType> & rhs ) :
root( NULL ), ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND )
{
*this = rhs;
}
/**
* Destructor for the tree.
*/
template <class EType>
BinarySearchTree<EType>::~BinarySearchTree( )
{
makeEmpty( );
}
/**
* Insert x into the tree; duplicates are ignored.
*/
template <class EType>
void BinarySearchTree<EType>::insert( const EType & x )
{
insert( x, root );
}
/**
* Remove x from the tree. Nothing is done if x is not found.
*/
template <class EType>
void BinarySearchTree<EType>::remove( const EType & x )
{
remove( x, root );}

///////////////
/**
* Find the smallest item in the tree.
* Return smallest item or ITEM_NOT_FOUND if empty.
*/
template <class EType>
const EType & BinarySearchTree<EType>::findMin( ) const
{
return elementAt( findMin( root ) );
}
/**
* Find the largest item in the tree.
* Return the largest item of ITEM_NOT_FOUND if empty.
*/
template <class EType>
const EType & BinarySearchTree<EType>::findMax( ) const
{
return elementAt( findMax( root ) );
}
/**
* Find item x in the tree.
* Return the matching item or ITEM_NOT_FOUND if not found.
*/
template <class EType>
const EType & BinarySearchTree<EType>::
find( const EType & x ) const
{
return elementAt( find( x, root ) );
}
/**
* Make the tree logically empty.
*/
template <class EType>
void BinarySearchTree<EType>::makeEmpty( )
{
makeEmpty( root );
}
/**
* Test if the tree is logically empty.
* Return true if empty, false otherwise.
*/
template <class EType>
bool BinarySearchTree<EType>::isEmpty( ) const
{
return root == NULL;
}
/**
* Print the tree contents in sorted order.
*/
template <class EType>
void BinarySearchTree<EType>::printTree( ) const
{
if( isEmpty( ) )
cout << "   X.   Empty tree" << endl;
else
printTree( root );
}
/**
* Deep copy.
*/
template <class EType>
const BinarySearchTree<EType> &
BinarySearchTree<EType>::
operator=( const BinarySearchTree<EType> & rhs )
{
if( this != &rhs )
{
makeEmpty( );
root = clone( rhs.root );
}
return *this;
}
/**
* Internal method to get element field in node t.
* Return the element field or ITEM_NOT_FOUND if t is NULL.
*/
template <class EType>
const EType & BinarySearchTree<EType>::
elementAt( BinaryNode<EType> *t ) const
{
if( t == NULL )
return ITEM_NOT_FOUND;
else
return t->element;
}
/**
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the tree.
*/
template <class EType>
void BinarySearchTree<EType>::
insert( const EType & x, BinaryNode<EType> * & t ) const
{
if( t == NULL ){t = new BinaryNode<EType>( x, NULL, NULL );}

else if( x < t->element ){insert( x, t->left );}

else if( t->element < x ){insert( x, t->right );}

else{ cout<<"   X.   Double Entery";
cout<<endl;
Beep(40,500);

}
; // Duplicate; do nothing
}
/**
* Internal method to remove from a subtree.
* x is the item to remove.
* t is the node that roots the tree.
* Set the new root.
*/
template <class EType>
void BinarySearchTree<EType>::
remove( const EType & x, BinaryNode<EType> * & t ) const
{
if( t == NULL )
return; // Item not found; do nothing
if( x < t->element )
remove( x, t->left );
else if( t->element < x )
remove( x, t->right );
else if( t->left != NULL && t->right != NULL ) // Two children
{
t->element = findMin( t->right )->element;
remove( t->element, t->right );
}
else
{
BinaryNode<EType> *nodeToDelete = t;
t = ( t->left != NULL ) ? t->left : t->right;
delete nodeToDelete;
}
}
/**
* Internal method to find the smallest item in a subtree t.
* Return node containing the smallest item.
*/
template <class EType>
BinaryNode<EType> *
BinarySearchTree<EType>::findMin( BinaryNode<EType> *t ) const
{
if( t == NULL )
return NULL;
if( t->left == NULL )
return t;
return findMin( t->left );
}
/**
* Internal method to find the largest item in a subtree t.
* Return node containing the largest item.
*/
template <class EType>
BinaryNode<EType> *
BinarySearchTree<EType>::findMax( BinaryNode<EType> *t ) const
{
if( t != NULL )
while( t->right != NULL )
t = t->right;
return t;
}
/**
* Internal method to find an item in a subtree.
* x is item to search for.
* t is the node that roots the tree.
* Return node containing the matched item.
*/
template <class EType>
BinaryNode<EType> *
BinarySearchTree<EType>::
find( const EType & x, BinaryNode<EType> *t ) const
{
if( t == NULL )
return NULL;
else if( x < t->element )
return find( x, t->left );
else if( t->element < x )
return find( x, t->right );
else
return t; // Match
}

template <class EType>
void preorder(const EType & x, BinaryNode<EType> *t)
{
if( t != NULL )
{
cout << *(t->getInfo())<<" ";
preorder(t->getLeft());
preorder(t->getRight());
}
}
/****** NONRECURSIVE VERSION*************************
template <class EType>
BinaryNode<EType> *
BinarySearchTree<EType>::
find( const EType & x, BinaryNode<EType> *t ) const
{
while( t != NULL )
if( x < t->element )
t = t->left;
else if( t->element < x )
t = t->right;
else
return t; // Match
return NULL; // No match
}
*****************************************************/
/**
* Internal method to make subtree empty.
*/
template <class EType>
void BinarySearchTree<EType>::
makeEmpty( BinaryNode<EType> * & t ) const
{
if( t != NULL )
{
makeEmpty( t->left );
makeEmpty( t->right );
delete t;
}
t = NULL;
}
/**
* Internal method to print a subtree rooted at t in sorted order.
*/
template <class EType>
void BinarySearchTree<EType>::printTree( BinaryNode<EType> *t ) const
{
if( t != NULL )
{
printTree( t->left );
cout <<"   X.   " <<t->element << endl;
printTree( t->right );
}
}
/**
* Internal method to clone subtree.
*/
template <class EType>
BinaryNode<EType> *
BinarySearchTree<EType>::clone( BinaryNode<EType> * t ) const
{
if( t == NULL )
return NULL;
else
return new BinaryNode<EType>( t->element, clone( t->left ), clone( t->right ) );
}
int main( )
{
system("Color 3");
const int ITEM_NOT_FOUND = -9999;
BinarySearchTree<int> t( ITEM_NOT_FOUND );

//////////////////////////////////////////////////
int NUMS;
int i;
cout<<endl;
cout<<endl;
cout<<"   X.   How many entries do you want to make in theis tree";
cin>>NUMS;
int value[NUMS];
cout<<"   X.   The Depth of the tree is  being calculated.";
cout<<endl;
Sleep(1000);
int n;
int d;
int val;
val= NUMS*(2*NUMS-1)/2;
cout<<"   X.   Depth is "<<val;
cout<<endl;


cout<<"   X.   Inserting elements (1 to 30) in the tree .......)" << endl;

for( i = 0; i <= NUMS; i++ )
{
cout<<"   X.   Enter a value";
cin>>value[i];
t.insert( value[i] );
}





//////////////////////////////////////////////////
cout <<"   X.   Printing the values In Order Treversal .......)" << endl;
t.printTree();




/////////////////////////////////////////////////////
cout <<"   X.   Removing the even number elements in the tree .......)" << endl;
for( i = 0; i <= NUMS; i+= 2 )
t.remove( i );
cout <<"   X.   Printing the values of the nodes in tree .......)" << endl;
t.printTree();
cout<<"   X.   Min value of the tree       ->";
cout<<t.findMin();
cout<<endl;
cout<<"   X.   Max value of the tree       ->";
cout<<t.findMax();
cout<<endl;

cout<<"   X.   If you want to remove an elemen please enter";
int remove;
cin>>remove;
t.remove(remove);
t.printTree();
cout<<endl;
string  sam;
cout<<"   X.   If you want to empty the tree please enter Y";
cin>>sam;
if(sam=="y"||sam=="Y"){
t.makeEmpty();t.printTree();
}
else{

cout<<endl;

cout<<"   X.   Type any key and enter to quit";
int abc;
cin >> i;}
return 0;
}
This article has been dead for over six months. Start a new discussion instead.