Create a header of your program c/c++
You can create your own header of your program, to which you can include in any other program by just enclosing in #include<"header">. By doing This you can access the methods of your created header in any programs .
Step 1:
To understand take a program suppose there is a program stack_template.cpp .
This is a stack program where push, pop, reverse, and display method are implemented.
/*
Implementation of stack using array of size capacity
Methods included are
push();
pop();
display();
reverse();
@author: Vikas Bek
*/
#include<iostream>
#include <stdlib.h>
using namespace std;
void inputfunc();
template <class T> /* Using Template for type independent */
class stack{
public:
T *arr;
int capacity;
int tos;
stack(){
tos=-1;
capacity =100;
arr=new T[capacity];
}
void push(stack &s,T value){
if(s.tos==s.capacity){
cout<<"stack is full"<<endl;
}else{
s.arr[++s.tos]=value;
}
}
bool isEmptyStack(stack a){
if(a.tos==-1){
return true;
}else{
return false;
}
}
stack reverse(stack &s){
T data;
if(isEmptyStack(s)){
return s;
}
data=pop(s);
reverse(s);
insert_at_end(s, data);
return s;
}
void insert_at_end(stack &s,T data){
T temp;
if(isEmptyStack(s)){
push(s,data);
return;
}
temp=pop(s);
insert_at_end(s,data);
push(s,temp);
}
T pop(stack &s){
T top=s.arr[s.tos];
if(s.tos==-1){
cout<<"stack is empty"<<endl;
}else{
s.tos--;
}
return top;
}
T display(stack s){
if(s.tos<0){
cout<<"stack is empty: \n";
return -1;
}else{
cout<<"value in stack"<<endl;
for(int i=s.tos;i>=0;i--) {
cout<<"|| "<<s.arr[i]<<" ||"<<endl;
}
return s.arr[tos];
}
}
};
void inputfunc(){
int ch=0;
int value;
stack <int>sa;
stack <int>s1;
do{
cout<<"*******************MENU*******************"<<endl;
cout<<"1. push "<<endl;
cout<<"2. pop "<<endl;
cout<<"3. display "<<endl;
cout<<"4. reverse "<<endl;
cout<<"5. exit "<<endl;
cout<<"enter choice\n";
cin>>ch;
switch(ch){
case 1: cout<<"Enter value to push in stack \n"<<endl;
cin>>value;
sa.push(s1,value);
sa.display(s1);
break;
case 2: cout<<"Pop from stack \n"<<endl;
sa.pop(s1);
sa.display(s1);
break;
case 3: cout<<"display: "<<endl;
sa.display(s1);
break;
case 4: cout<<"reverse \n"<<endl;
s1=sa.reverse(s1);
sa.display(s1);
break;
case 5: cout<<"exit!!!!!!! \n";
exit(0);
default: cout<<"wrong entery\n ";
}
}while(ch!=5);
}
int main(int argc, char** argv){
inputfunc() ;
return 0;
}
Step 2:
Now to create a header. Create a new file and save it with the file name stack_template.h .
Step 3:
Copy the content of stack_template.cpp and paste to stack_template.h .
Step 4:
In stack_template.h
Remove the main method and all other method which are not the member of stack class.
#include<iostream>
#include <stdlib.h>
using namespace std;
template <class T> /* Using Template for type independent */
class stack{
public:
T *arr;
int capacity;
int tos;
stack(){
tos=-1;
capacity =100;
arr=new T[capacity];
}
void push(stack &s,T value){
if(s.tos==s.capacity){
cout<<"stack is full"<<endl;
}else{
s.arr[++s.tos]=value;
}
}
bool isEmptyStack(stack a){
if(a.tos==-1){
return true;
}else{
return false;
}
}
stack reverse(stack &s){
T data;
if(isEmptyStack(s)){
return s;
}
data=pop(s);
reverse(s);
insert_at_end(s, data);
return s;
}
void insert_at_end(stack &s,T data){
T temp;
if(isEmptyStack(s)){
push(s,data);
return;
}
temp=pop(s);
insert_at_end(s,data);
push(s,temp);
}
T pop(stack &s){
T top=s.arr[s.tos];
if(s.tos==-1){
cout<<"stack is empty"<<endl;
}else{
s.tos--;
}
return top;
}
T display(stack s){
if(s.tos<0){
cout<<"stack is empty: \n";
return -1;
}else{
cout<<"value in stack"<<endl;
for(int i=s.tos;i>=0;i--) {
cout<<"|| "<<s.arr[i]<<" ||"<<endl;
}
return s.arr[tos];
}
}
};
Step 5:
Now we put the above code inside
#ifndef stack_template_h
#define stack_template_h
#define stack_template_h
/* above code................. */
#endif
Step 6:
At this point it will look like as follows.
#ifndef stack_template_h
#define stack_template_h
#define stack_template_h
#include<iostream>
#include <stdlib.h>
using namespace std;
template <class T> /* Using Template for type independent */
class stack{
public:
T *arr;
int capacity;
int tos;
stack(){
tos=-1;
capacity =100;
arr=new T[capacity];
}
void push(stack &s,T value){
if(s.tos==s.capacity){
cout<<"stack is full"<<endl;
}else{
s.arr[++s.tos]=value;
}
}
bool isEmptyStack(stack a){
if(a.tos==-1){
return true;
}else{
return false;
}
}
stack reverse(stack &s){
T data;
if(isEmptyStack(s)){
return s;
}
data=pop(s);
reverse(s);
insert_at_end(s, data);
return s;
}
void insert_at_end(stack &s,T data){
T temp;
if(isEmptyStack(s)){
push(s,data);
return;
}
temp=pop(s);
insert_at_end(s,data);
push(s,temp);
}
T pop(stack &s){
T top=s.arr[s.tos];
if(s.tos==-1){
cout<<"stack is empty"<<endl;
}else{
s.tos--;
}
return top;
}
T display(stack s){
if(s.tos<0){
cout<<"stack is empty: \n";
return -1;
}else{
cout<<"value in stack"<<endl;
for(int i=s.tos;i>=0;i--) {
cout<<"|| "<<s.arr[i]<<" ||"<<endl;
}
return s.arr[tos];
}
}
};
#endif
Note:- Make sure that in #ifndef filename_H and #define filename_H are same as the filename.h
Step 7:
Now you can include this new header in any program to access its method.
Put #include "stack_generic.h" in any program and access it.
Note:- Be sure that created header and the program in which you have included this header are in same directory.
Thank You!
No comments :
Post a Comment