Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program to illustrate template classes

Posted By: Easy Tutor     Category: C++ Programming     Views: 1913

A C++ Program to illustrate template classes.

Code for Program to illustrate template classes in C++ Programming

 #include<iostream.h>
 #include<conio.h>

 constint max=20;

 /*************************************************************************///------------------------------  stack  --------------------------------///*************************************************************************/

 template <class T>

 class stack
    {
       private:
        T stk[max];

        int top;

       public:
        stack()  { top=-1; }
        void push(T);
        T pop();
    };

 /*************************************************************************///---------------------------  complex  ---------------------------------///*************************************************************************/class complex
    {
       private:
        float real_part;
        float imaginary_part;

       public:
        complex()  { real_part=0.0;  imaginary_part=0.0; }
        complex(float r,float i)  { real_part=r; imaginary_part=i; }
        friend ostream &operator<<(ostream &o,complex &c);
    };



 /*************************************************************************///---------------------------   push(T)  --------------------------------///*************************************************************************/

 template <class T>
 void stack<T>::push(T data)
    {
       if(top==max-1)
      cout<<"\n Stack is full "<<endl;

       else
      {
         top++;
         stk[top]=data;
      }
    }

 /*************************************************************************///----------------------------   pop(T)  --------------------------------///*************************************************************************/

 template <class T>
 T stack<T>::pop()
    {
       T item;


       if(top==-1)
         cout<<"\n Stack is empty "<<endl;

       else
      {
         T item=stk[top];

         top--;
      }

       return item;
    }

 /*************************************************************************//*************************************************************************///---------------------------  complex  ---------------------------------///*************************************************************************//*************************************************************************//*************************************************************************///----------------   &operator<<(ostream &,complex &)  ------------------///*************************************************************************/

 ostream &operator<<(ostream &o,complex &c)
    {
       o<<"\n\t Real Part = "<<c.real_part<<"\t Imaginary Part = "<<
                             c.imaginary_part<<endl;
       return o;
    }

 main()
    {
       clrscr();


       cout<<"\n **********  Integers  ********"<<endl;

       stack<int> s_1;

       s_1.push(10);
       s_1.push(20);
       s_1.push(30);

       cout<<s_1.pop()<<endl;
       cout<<s_1.pop()<<endl;
       cout<<s_1.pop()<<endl;

       cout<<"\n **********  Floats  ********"<<endl;

       stack<float> s_2;

       s_2.push(1.1);
       s_2.push(2.2);
       s_2.push(3.3);

       cout<<s_2.pop()<<endl;
       cout<<s_2.pop()<<endl;
       cout<<s_2.pop()<<endl;

       cout<<"\n **********  Complex Numbers  ********"<<endl;

       complex c_1(1.5,2.5);
       complex c_2(2.5,3.5);
       complex c_3(3.5,4.5);

       stack<complex> s_3;

       s_3.push(c_1);
       s_3.push(c_2);
       s_3.push(c_3);

       cout<<s_3.pop()<<endl;
       cout<<s_3.pop()<<endl;
       cout<<s_3.pop()<<endl;

       getch();
       return 0;
    }
  
Share: 


Didn't find what you were looking for? Find more on Program to illustrate template classes Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to illustrate template classes is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!