Thursday, 4 May 2017

C++ Programs for Stack Implementation, Array of Objects, Friend Function

Today I am providing Various Important C++ Programs with coding.
AIM:
Write a C++ program to implement a stack with its constructor and two member functions PUSH and POP.

CODING:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define size 4
class stact
{
int st;
int tos;
public:
stact();
~stact();
void push(int x);
pop ();
};
void stact::st()
{
tos=0;
cout<<"stact incremented"<<endl;
}
void stact::~stact()
}
  cout<<"stact is empty"<<endl;
  return ;
}
void stact::push(int x)
{
if(tos==size)
{
cout<<"stack is full"<<endl;
return;

}
stack[tos]=x;
tos++ ;

};
void st::pop()
{
if(t==0)
{
cout<<"stack is empty"<<endl;
}---;
return stack[t];
}
void main()
{
st s1,s2,s3,s4;
s1.push(1);
s2.push(2);
s3.push(3);
s4.push(4);
cout<<" "<<s4.pop();
cout<<" "<<s3.pop();
cout<<" "<<s2.pop();
cout<<" "<<s1.pop();
getch();

}
----------------------------------------------------------------------------------------------------------
AIM:
Write a C++ program for an array of objects.

CODING:

#include<iostream.h>
#include<conio.h>
class Student
{
  public:
            char name[20];
            int enrol;
            char section[20];
            void getdata()
            {
              cout<<"Enter the Name,Enrol & Section"<<endl;
              cin>>name>>enrol>>section;
            }
            void showdata()
            {
              cout<<"Name of Student is : "<<name<<endl;
              cout<<"Enrolment no. is : "<<enrol<<endl;
              cout<<"Section is : "<<section<<endl;
            }
};
main()
{
   Student s[3];
   for(int i=0;i<3;i++)
   {
     s[i].getdata();
   }
   for(i=0;i<3;i++)
   {
     s[i].showdata();
   }
   getch();
   return 0;
}
----------------------------------------------------------------------------------------------------------
AIM:
Write a C++ program for Friend function.


CODING:
#include<iostream.h>
#include<conio.h>
class demo
{
  private:
  int a,b;
  public:
  void setvalue()
  {
    a=10;
    b=15;
  }
   friend int add(demo d);
};
 int add(demo d)
 {
   return int(d.a+d.b);
 }
 main()
 {
  clrscr();
   demo d1;
   d1.setvalue();
   cout<<"addition is="<<add(d1);
   getch();
   return 0;
 }

No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.