abc
#include <iostream.h>
class emp
{
private:
char ID[5];
char name[50];
emp *next;
emp *prev;
public:
friend class empnode;
void accept()
{
cout<<"Enter ID\n";
cin>>ID;
cin.ignore();
cout<<"Enter Name\n";
cin.getline(name,50);
}
void disp()
{
cout<<"ID\t"<<ID
<<"Name\t"<<name<<endl;
}
};
class empnode
{
emp *start;
public:
empnode()
{
start=NULL;
}
void addnode()
{
emp *fresh;
fresh=new emp();
fresh->accept();
fresh->next=start;
start=fresh;
}
void dispnode()
{
for(emp *t=start;t!=NULL;t=t->next)
t->disp();
}
};
void menu()
{
cout<<"\t\tMain Menu\n"
<<"\t1. Add Node\n"
<<"\t2. Display Node\n"<<endl;
cout<<"choice";
}
void main()
{
empnode o;
int ch;
for(;;){
menu();
cin>>ch;
if(ch==1)
o.addnode();
if(ch==2)
o.dispnode();
if(ch<1 || ch>2)
break;
}
}
#include <stdio.h>
struct emp
{
char name[10];
struct contactadd
{
char ph[10];
char email[10];
};
contactadd x;
};
emp var;
void accept()
{
printf("Enter Name of customer\n");
scanf("%s",var.name);
printf("Enter Phone number\n");
scanf("%s",var.x.ph);
printf("Enter Email\n");
scanf("%s",var.x.email);
}
void display()
{
printf("Name:\t%s",var.name);
printf("Phone:\t%s",var.x.ph);
printf("Email:\t%s",var.x.email);
}
void main()
{
accept();
display();
}
//1 static binding- function overloading
//2. dynamic binding- parent pointer to child class
// dynamic binding or dynamic polymorphism
#include <iostream.h>
class emp
{
char ID[10];
public:
virtual void accept() // virtual functions to make the parent silent
when called
{
cout<<"Enter ID";
cin>>ID;
}
virtual void display()
{
cout<<"ID:\t"<<ID<<endl;
}
};
class Grade1:public emp
{
char OFFICERID[10];
public:
void accept()
{
emp::accept();
cout<<"\nEnter Officer ID";
cin>>OFFICERID;
}
void display()
{
emp::display();
cout<<"OFFICERID:\t"<<OFFICERID<<endl;
}
};
void main()
{
emp *p; // base class pointer
Grade1 obj;
p=&obj; // parent pointer can hold childs address
p->accept(); // calling childs function
p->display();
}
#include <iostream.h>
class student
{
char roll[3];
char name[50];
public:
student();
virtual void accept();
virtual void display();
};
student::student()
{
roll[0]='\0';
name[0]='\0';
}
void student::accept()
{
cout<<"Enter ROll No\t"; cin>>roll; cin.ignore();
cout<<"Enter Name\t"; cin.getline(name,50);
}
void student::display()
{
cout<<"\nROLL\t\tName\t\tAddress\n";
cout<<"\n----\t\t------------\t\t-----------\n";
cout<<roll<<"\t\t"<<name<<"\t\t";
}
class res:public student
{
char Resadd[100];
public:
res();
void accept();
void display();
};
res::res()
{
Resadd[0]='\0';
}
void res::accept()
{
student::accept();
cout<<"\nEnter Residential Address\t";
cin.getline(Resadd,100);
}
void res::display()
{
student::display();
cout<<Resadd<<endl;
}
class nres:public student
{
char Hos[100];
public:
nres();
void accept();
void display();
};
nres::nres()
{
Hos[0]='\0';
}
void nres::accept()
{
student::accept();
cout<<"\nEnter Hostel Address\t";
cin.getline(Hos,100);
}
void nres::display()
{
student::display();
cout<<Hos<<endl;
}
void menu()
{
cout<<"\n\t\tMain Menu\n"
<<"\t1. Residential\n"
<<"\t2. Non Residential\n";
}
void submenu()
{
cout<<"\n\n\t\tSub Menu\n"
<<"\t1. Accept Details\n"
<<"\t2. Display Details\n";
}
void main()
{
char ch;
student *p;
do
{
menu();
cout<<"\n\t\t\t\tMain Choice:"; cin>>ch;
switch (ch)
{
case '1':
char ch1;
p=new res();
do
{
submenu();
cout<<"\n\t\t\t\tChoice:"; cin>>ch1;
switch (ch1)
{
case '1':
p->accept();
break;
case '2':
p->display();
break;
}
}while (ch1=='1' || ch1=='2');
break;
case '2':
char ch2;
p=new nres();
do
{
submenu();
cout<<"\n\t\t\t\tChoice:"; cin>>ch2;
switch (ch2)
{
case '1':
p->accept();
break;
case '2':
p->display();
break;
}
}while (ch2=='1' || ch2=='2');
break;
}
}while(ch == '1' || ch=='2');
}
#include <iostream.h>
// friend: private: nobody can access
// a private data can be shared
// friend class,friend function
class emp
{
int empage;
char name[10];
public:
void accept()
{
cin>>name>>empage;
}
void display()
{
cout<<name<<empage;
}
friend class elgiblity_for_prom;
};
class elgiblity_for_prom
{
int exp;
emp o;
public:
void accept()
{
o.accept();
exp=o.empage+10;
}
void display()
{
o.display();
if(exp>40)
cout<<"Eligible";
else
cerr<<"Not Eligible";
}
};
void main()
{
elgiblity_for_prom obj;
obj.accept();
obj.display();
}
#include <iostream.h>
class emp
{
int age;
public:
emp()
{
cout<<"Hi Me is Called";
age=21;
}
void disp() { cout<<age; }
};
void main()
{
emp obj;
emp obj1=obj;
obj1.disp();
}
// + - *
// emp o,o1,o2; o2=o+o1;
#include <iostream.h>
class sal
{
int basic;
public:
sal()
{
basic=0;
}
sal(int s)
{
basic=s;
}
sal operator +(sal x) // copy constructor
{
sal tmp;
tmp.basic=basic+x.basic;
return tmp;
}
sal operator -(sal x) // copy constructor
{
sal tmp;
tmp.basic=basic-x.basic;
return tmp;
}
void disp()
{
cout<<basic;
}
};
void main()
{
sal obj(300),obj1(100),obj4(2);
sal obj3=obj+obj1-obj4; //sal x=obj1
obj3.disp();
}
// Write a program to over load - operator
#include <iostream.h>
#include <string.h>
class string
{
char *x;
public:
string(char y[]='\0')
{
cout<<"constu";
if (y[0]=='\0')
x[0]='\0';
x=new char[strlen(y)+1];
strcpy(x,y);
}
};
void main()
{
string o;
}
// int x; x=9; cout<<x
// Files: variable ( dealing with memory)
// reading from (input ) and writing to(output) HD
// files are stored locations in permanent area
// how to create a file using c++
// how to store the data into files using c++ (output to HD)
// how to read the file from the HD (input from HD)
//
#include <fstream.h>
void main()
{
char name[100];
fstream cfout("bio.txt",ios::out|ios::app);
cout<<"Enter Full Name";
cin.getline(name,100);
cfout<<"\t\t\t\tRESUME\n\n";
cfout<<"NAME\t\t\t:"<<name<<endl;
cout<<"Enter Phone Number";
cin>>name;
cfout<<"PHONE\t\t\t:"<<name<<endl;
cout<<"Enter email address";
cin>>name;
cfout<<"EMAIL\t\t\t:"<<name<<endl;
cout<<"Enter date of birth";
cin>>name;
cin.ignore();
cfout<<"DATE OF BIRTH\t\t:"<<name<<endl;
cout<<"Enter your Qualification";
cin.getline(name,100);
cfout<<"QUALIFICATION\t\t:"<<name<<endl;
while(1){
cout<<"Enter your experience";
cin.getline(name,100);
cfout<<"\nEXPERIENCE\t\t\t:"<<name<<endl;
cout<<"Want to put one mor exp\n";
char ch;
cin>>ch;
cin.ignore();
if(ch!='y')
break;
}
cfout.close();
}
#include <fstream.h>
class emp
{
char name[10];
char qualification[10];
public:
void accept()
{
cout<<"Enter ur name:";
cin.getline(name,10);
cout<<"Enter ur qualification:";
cin.getline(qualification,10);
}
void display()
{
cout<<name;
cout<<qualification;
}
};
class empstore
{
emp obj;
public:
void accept()
{
fstream cfout("emp.txt",ios::out|ios::app);
obj.accept();
cfout.write((char*)&obj,sizeof(obj));
cfout.close();
}
void display()
{
fstream cfin("emp.txt",ios::in);
cfin.read((char*)&obj,sizeof(obj));
obj.display();
cfin.close();
}
};
void main()
{
empstore obj;
obj.accept();
obj.display();
}#include<fstream.h>
class student
{
char name[10];
short int age;
public:
void accept()
{
cout<<"Enter ur name:";
cin>>name;
cout<<"Enter ur age:";
cin>>age;
}
void display()
{
cout<<name<<age;
}
};
class studentstore
{
student obj;
public:
void accept()
{
ofstream cfout("student.txt",ios::out|ios::app);
obj.accept();
cfout.write((char*)&obj,sizeof(obj));
cfout.close();
}
void display()
{
int clt=0;
ifstream cfin("student.txt",ios::in);
cfin.read((char*)&obj,sizeof(obj));
while(!cfin.eof())
{
//..........................
obj.display();
cfin.read((char*)&obj,sizeof(obj));
cltclt+tot;
}
cfin.close();
}
};
void main()
{
studentstore obj;
obj.accept();
obj.display();
}
#include <iostream.h>
class emp
{
private:
char name[10];
char ph[10];
char email[10];
public:
void accept()
{
cout<<"Enter name\n";
cin>>name;
cout<<"Enter phone\n";
cin>>ph;
cout<<"Enter email\n";
cin>>email;
}
void display()
{
cout<<"\n\tName\t\tPhone\t\tEmail\n";
cout<<"\t"<<name<<"\t\t"<<ph<<"\t\t"<<email;
}
};
void main()
{
// pointer to object
emp x;
emp *ptr;
ptr=&x;
ptr->accept(); // -> arrow operator used to access members of a class
using pointer
ptr->display();
}
#include<fstream.h>
#include <string.h>
class reportcard
{
char name[20];
char sub[9][20];
int marks[9];
public:
friend class reportcardstore;
void accept()
{
cout<<"Enter student's name:";
cin.getline(name,20);
for(int i=0;i<9;i++)
{
cout<<"Enter subject name:";
cin>>sub[i];
cout<<"Enter ur marks in the subject:";
cin>>marks[i];
}
}
void change(int i)
{
int tmp;
cout<<"\nsubject Name\t"<<sub[i]
<<"marks\t"<<marks[i]<<endl;
cout<<"\nEnter New marks";
cin>>tmp;
marks[i]=tmp;
}
void display()
{
cout<<"Name\t"<<name<<endl;
for(int i=0;i<9;i++)
{
cout<<"subject name: "<<i+1<<"\t"<<sub[i];
cout<<"subject marks:"<<i+1<<"\t"<<marks[i]<<endl;
}
}
};
class reportcardstore
{
reportcard obj;
public:
void accept()
{
ofstream cfout("reportcard.txt",ios::out|ios::app);
obj.accept();
cfout.write((char*)&obj,sizeof(obj));
cfout.close();
}
void display()
{
int i,n=0;
int sum=0;
int avg;
int clt=0,cla;
ifstream cfin("reportcard.txt",ios::in);
cfin.read((char*)&obj,sizeof(obj));
while(cfin)
{
n++;
for(i=0;i<9;i++)
sum=sum+obj.marks[i];
avg=sum/i;
obj.display();
cout<<"\nTotal Marks: "<<sum
<<"\nAverage: "<<avg<<endl;
clt=clt+sum;
cfin.read((char*)&obj,sizeof(obj));
sum=0;
}
cla=clt/n;
cout<<"\nClass Total: "<<clt
<<"\nClass Average: "<<cla<<endl;
cfin.close();
}
void reportfind()
{
char tmp[20];
cout<<"Enter Name of student whose marks want to display\t";
cin.getline(tmp,20);
ifstream cfin("reportcard.txt",ios::in);
cfin.read((char*)&obj,sizeof(obj));
while(cfin)
{
if(strcmp(tmp,obj.name)==0)
{
obj.display();
}
cfin.read((char*)&obj,sizeof(obj));
}
cfin.close();
}
void modifymarks()
{
char tmp[20];
cout<<"Enter Name of student whose marks want to modify\t";
cin.getline(tmp,20);
fstream cfin("reportcard.txt",ios::out|ios::in);
cfin.read((char*)&obj,sizeof(obj));
while(!cfin.eof())
{
if(strcmp(tmp,obj.name)==0)
{
int subcode;
obj.display();
cout<<"\nEnter new marks for the subcode\n";
cin>>subcode;
obj.change(subcode-1);
int pos=cfin.tellg();
int size=sizeof(obj);
pos=pos-size;
cfin.seekp(pos,ios::beg);
cfin.write((char*)&obj,sizeof(obj));
}
cfin.read((char*)&obj,sizeof(obj));
}
cfin.close();
}
};
void menu()
{
cout<<"Main Menu\t"
<<"\n\t\t1. Add marks"
<<"\n\t\t2. Display marks"
<<"\n\t\t3. Find marks"
<<"\n\t\t4. Modify marks"<<endl;
cout<<"Enter Choice";
}
void main()
{
reportcardstore obj;
char ch;
do
{
menu();
cin>>ch;
cin.ignore();
if(ch=='1') obj.accept ();
if(ch=='2') obj.display ();
if(ch=='3') obj.reportfind();
if(ch=='4') obj.modifymarks();
}while(ch=='1' || ch=='2' || ch=='3' || ch=='4');
}
// exception handling
// Error and Exception
// try throw catch
// try is a block
// throw is a statement
// catch is a function
//
//ex:
#include <iostream.h>
void main()
{
try
{
int i,j;
cin>>i>>j;
if(j==0) throw "Error in denominator";
cout<<i/j;
}
catch(char *er)
{
cerr<<er;
}
}
// templates
// write a program in c++ to accept two variables x and y and sum them show
their average
#include<iostream.h>
class f3
{
int x,y,sum;
public:
void accept()
{
cout<<"Enter 2 nos.";
cin>>x;
cin>>y;
}
void display()
{
cout<<"x="<<x<<endl<<"y="<<y<<endl;
}
void s()
{
sum=x+y;
}
void average()
{
cout<<"the average is="<<sum/2;
}
};
void main()
{
f3 obj;
obj.accept(); // binding
obj.s();
obj.display();
obj.average();
}// write a program for an inn
// cost of AC room is 1500 per night
// cost of Delux is 1400 per night
// cost of Non AC is 900 per night
// you to accept the number of persons, name of one person, no of rooms required
and generate bill
#include <iostream.h>
class inn
{
int x; char name[10]; int y,bill,type,n;
public:
void accept()
{
cout<<"Enter the no. of persons:\t";
cin>>x;
cout<<"Enter the name of person:\t";
cin>>name;
cout<<"Enter the no. of rooms reqd:\t";
cin>>y;
cout<<"Enter the no. of days of stay:\t";
cin>>n;
cout<<"Enter the type of room reqd:\n1.ac room\n2.delux room\n3.non ac
room\n\t";
cin>>type;
}
void b()
{
if(type==1)
{
bill=y*n*1500;
}
if(type==2)
{
bill=y*n*1400;
}
if(type==3)
{
bill=y*n*900;
}}
void display()
{
cout<<"\n\n\n\t\t\tBILL";
cout<<"\nName of person:"<<name;
cout<<"\nNo. of rooms reqd.\t\tNo. of days to be spent\n";
cout<<"\n"<<y<<"\t\t\t\t"<<n<<"\n";
cout<<"total="<<bill<<endl;
}
};
void main()
{
inn obj;
obj.accept();
obj.b();
obj.display();
}
#include <iostream.h>
// inheritance:
class Inn
{
protected:
char name[50]; //raj malhotra
int no_of_per;
int no_of_days;
public:
void accept()
{
cout<<"\nEnter Name of person\t";
cin.getline(name,50);
cout<<"\nEnter Number of Persons\t";
cin>>no_of_per;
cout<<"\nEnter Number of Days\t";
cin>>no_of_days;
}
void display()
{
cout<<"Name of Person\t\tNo of Persons\t\tNo of Days\n";
cout<<name<<"\t\t\t"<<no_of_per<<"\t\t\t"<<no_of_days<<endl;
}
};
class ACInn:public Inn
{
int cost_of_room;
double TAX;
int rooms;
public:
void accept() // function overrrinding
{
Inn::accept(); // calling base class function from child
int ch;
cout<<"Enter number of beds required\n\t"
<<"1. 2 Bed Room\n\t2. 4 Bed Room\n\t3. Single Bed Room\n";
cin>>ch;
if(ch==1)
cost_of_room=1200;
if(ch==2)
cost_of_room=2400;
if(ch==3)
cost_of_room=600;
cout<<"Enter Number of Rooms Required\n";
cin>>rooms;
TAX=(no_of_days*rooms*cost_of_room)*.05;
}
int bill()
{
return cost_of_room*no_of_days*rooms;
}
void display()
{
Inn::display();
cout<<"\nRooms Demanded\t:"<<rooms<<endl;
cout<<"\n Room Opted:";
if(cost_of_room==1200)
cout<<"\n\t\t2 Bed Room AC Inn\t-\t"
<<cost_of_room;
if(cost_of_room==600)
cout<<"\n\t\tSingle Bed Room AC Inn\t-\t"
<<cost_of_room;
if(cost_of_room==2400)
cout<<"\n\t\t4 Bed Room AC Inn\t-\t"
<<cost_of_room;
cout<<"\nTotal Bill is \t\t\t\t:"<<bill()
<<"\nTAX\t\t\t\t\t:"<<TAX
<<"\nTotal\t\t\t\t\t:"<<bill()+TAX<<"\n\t\t\t\t\t"
<<"Thank You\n\t\t\t\t\tVisit Again\n";
}
};
void main()
{
ACInn obj;
obj.accept();
obj.display();
}
// virtual class is used to remove the ambiguity
// ambiguity means same variable from different parents
# include <iostream.h>
class base
{
protected:
int x;
};
class c1:virtual public base
{
protected:
int c1x;
};
class c2:virtual public base
{
protected:
int c2x;
};
class c3:public c1,c2
{
public:
int c3x;
public:
void display()
{
cout<<x;
}
};
void main()
{
c3 obj;
obj.display();
}
// problem is called ambiguity
// enhance the code given in f5 by including DeluxInn and NONACInn
// DeluXInn has 2 bed rooms ( 2000 ) with option AC included ( No Ac 600)
// Other Facilities opted by user ( Extra TV,DVD, Direct Meals etc) TAX .05
// NONAC has 2 bed,4 bed only with NO Tax.// overloading: same function name
with different signatures
// signature means parameters of the function with no of arguments,type of
arguments,arrangement of arguments
// void accept(int x)
// constructors
// destructors
#include <iostream.h>
class sample
{
int x,y;
public:
// constructor is a function with no return type,
//having the name as that of class name
// constructor is used for initialization or to show a initial message
// constructor can also be overloaded
sample() // default constructor
{
x=y=0;
}
sample(int x) // one argument constructor
{
this->x=x;
}
sample(int x,int y)
{
this->x=x;
this->y=y;
}
void accept()
{
cin>>x>>y;
}
// function with default arguments
void accept(int x=10,int y=10)
{
this->x=x;
this->y=y;
}
void disp()
{
cout<<x<<y;
}
};
void main()
{
sample obj; // constructor is called when object is created ( implictly
called)
int a;
cin>>a;
obj.accept(a);
obj.disp();
}
#include <iostream.h>
class sample
{
int x,y;
public:
sample(int x=10,int y=10)
{
this->x=x;
this->y=y;
cout<<x<<y<<"\nconstructor is called";
}
~sample()
{
x=y=0;
cout<<"\ndestructor is called";
}
};
void main()
{
sample obj;
cout<<"\nprogram working";
}
// what is destructor
// destructor is also same as that of constructor but preceded by ~ symbol
// destructos is used for deinitialization
// is called at the end of program
// is not possible to overload#include <iostream.h>
class gbase
{
public:
gbase()
{
cout<<"\ngbase";
}
~gbase()
{
cout<<"\nGBASE";
}
};
class base1:public gbase
{
public:
base1()
{
cout<<"\nbase1";
}
~base1()
{
cout<<"\nBASE1";
}
};
class base2
{
public:
base2()
{
cout<<"\nbase2";
}
~base2()
{
cout<<"\nBASE2";
}
};
class guest
{
public:
guest()
{
cout<<"\nguest";
}
~guest()
{
cout<<"\nGUEST";
}
};
class child:public base1, base2
{
guest o;
public:
child()
{
cout<<"\nchild";
}
~child()
{
cout<<"\nCHILD";
}
};
class grandchild:public child
{
public:
gandchild()
{
cout<<"\ngrandchild";
}
~grandchild()
{
cout<<"\nGRANDCHILD";
}
};
void main()
{
grandchild obj;
}// sequence of constructor
// first parent
//second guest object
//third own object
// destructor is fallowed reverse to constructor sequence
#include <iostream.h>
class base
{
public:
base()
{
cout<<"\nbase";
}
~base()
{
cout<<"\nBASE";
}
};
class base1
{
public:
base1()
{
cout<<"\nbase1";
}
~base1()
{
cout<<"\nBASE1";
}
};
class guest
{
public:
guest()
{
cout<<"\nguest";
}
~guest()
{
cout<<"\nGUEST";
}
};
class child:public base,base1
{
guest obj;
public:
child()
{
cout<<"\nchild";
}
~child()
{
cout<<"\nCHILD";
}
};
void main()
{
child obj;
}
// write a program to check the given tree using constructos ,destructors that
also with arguments
// simple single linked list program
#include <iostream.h>
class emp
{
private:
char ID[5];
char name[50];
emp *next;
public:
friend class empnode;
void accept()
{
cout<<"Enter ID\n";
cin>>ID;
cin.ignore();
cout<<"Enter Name\n";
cin.getline(name,50);
}
void disp()
{
cout<<"ID\t"<<ID
<<"Name\t"<<name<<endl;
}
};
class empnode
{
emp *start;
public:
empnode()
{
start=NULL;
}
void addnode()
{
emp *fresh;
fresh=new emp();
fresh->accept();
fresh->next=start;
start=fresh;
}
void dispnode()
{
for(emp *t=start;t!=NULL;t=t->next)
t->disp();
}
};
void menu()
{
cout<<"\t\tMain Menu\n"
<<"\t1. Add Node\n"
<<"\t2. Display Node\n"<<endl;
cout<<"choice";
}
void main()
{
empnode o;
int ch;
for(;;){
menu();
cin>>ch;
if(ch==1)
o.addnode();
if(ch==2)
o.dispnode();
if(ch<1 || ch>2)
break;
}
}
#include <iostream.h>
namespace IT
{
class emp
{
public:
char ID[4];
};
class soft
{
public:
char nam[10];
};
};
void main()
{
IT::emp obj;
cin>>obj.ID;
}
#include <iostream.h>
#include <stdlib.h>
struct Q
{
int arr[10];
int start;
int end;
};
Q obj;
void init()
{
obj.start=obj.end=0;
}
void push(int x)
{
if(obj.end==10)
{
cout<<"Q is full";
exit(0);
}
obj.arr[obj.end]=x;
obj.end++;
cout<<obj.end;
}
int pop()
{
if((obj.start==0 && obj.end==0 )|| (obj.start>=obj.end))
{
cout<<"Q is MT";
init();
return -1;
}
cout<<obj.start;
int temp= obj.arr[obj.start];
obj.start++;
return temp;
}
void menu()
{
cout<<"\n\t1. push"
<<"\n\t2. pop"
<<"choice ";
}
void main()
{
int ch;
init();
for(;;)
{
menu();
cin>>ch;
if(ch==1)
{
int val;
cout<<"Enter a value"; cin>>val;
push(val);
}
if(ch==2)
cout<<pop();
if(ch<1 || ch>2)
break;
}
}
#include <iostream.h>
void main()
{
int x[]={4,5,60,7,8};
int *p=x;
*(p+4)=*p**p;
cout<<*p; // 4
cout<<*(p+4); // 16
p+=2;
*p-=59;
cout<<*p; // 1
cout<<p; // address of 1
int *p1=p;
cout<<endl<<*p1; // 1
int sum=0;
for(p1=&x[0];p1<=&x[4];p1++)
*p1=*p1+*p1;
cout<<endl<<"the final value is "<<*p1; // error output
}#include <iostream.h>
#include <string.h>
class emp
{
public:
class beings
{
public:
char name[100];
};
char name[10];
emp *ptr;
emp(char *str)
{
strcpy(name,str);
}
void disp()
{
cout<<name;
}
};
emp obj("OM"),obj1("JESUS");
void main()
{
obj.ptr=&obj1;
obj1.ptr=&obj;
obj.disp();
obj.ptr->disp();
obj1.ptr->disp ();
}
/*
linked list with (desc ) sorted insertion
node with value 21 which is 1st node
so is added with start
a node with value is added after the first node so it is second node
there are two chances
1. value of thenode is > the already available nodes
2. value of the fresh node is < the already available nodes
if it is the first option then node goes to the begining
if it is the second option then node is again of two chances
1. somewhere in the middle
2. at the last
the total logic is divided into 3 parts
1. node at the beginging
2. node at the middle
3. node at the end
logic for begining node.
1. if the node is first one
2. node is greater than all the existing nodes
logic for middle nodes
1. the node is less than the first one but greater than the remaining one of
the list
logic for the last node
1. the node is smallest of all available in list
*/
#include <iostream.h>
class emp
{
public:
int age;
emp *next;
void accept()
{
cout<<"Enter age of the person\t"; cin>>age;
}
void disp()
{
cout<<"Age\t"<<age;
}
};
class empnode
{
emp *f;
public:
empnode()
{
f=NULL;
}
void addnode()
{
emp *fresh;
fresh=new emp();
fresh->accept();
//logic for beinging node
if(f==NULL || fresh->age>f->age)
{ fresh->next=f;
f=fresh;
}
else
{
for(emp *t=f,*p;t!=NULL && (t->age > fresh->age) ;p=t,t=t-
>next){}
if(t!=NULL)
{
fresh->next=p->next;
p->next=fresh;
}
else
{
fresh->next=NULL;
p->next=fresh;
}
}
}
void disp()
{
for(emp *t=f;t!=NULL;t=t->next)
t->disp();
}
};
void menu()
{
cout<<"\t\tMain Menu\n"
<<"\t1. Add Node\n"
<<"\t2. Display Node\n"<<endl;
cout<<"choice";
}
void main()
{
empnode o;
int ch;
for(;;){
menu();
cin>>ch;
if(ch==1)
o.addnode();
if(ch==2)
o.disp();
if(ch<1 || ch>2)
break;
}
}
// Data structures
#include <iostream.h>
#include <stdlib.h>
struct stack
{
int arr[10];
int top;
};
stack obj;
void init()
{
obj.top=0;
}
void push(int x)
{
if(obj.top==10)
{
cout<<"Stack is full";
exit(0);
}
obj.arr[obj.top]=x;
obj.top++;
}
int pop()
{
if(obj.top==0)
{
cout<<"Stack is MT";
return -1;
}
obj.top--;
return obj.arr[obj.top];
}
void menu()
{
cout<<"\n\t1. push"
<<"\n\t2. pop"
<<"choice ";
}
void main()
{
int ch;
init();
for(;;)
{
menu();
cin>>ch;
if(ch==1)
{
int val;
cout<<"Enter a value"; cin>>val;
push(val);
}
if(ch==2)
cout<<pop();
if(ch<1 || ch>2)
break;
}
}
__________________________Wish U best of Luck _________________________________
No comments:
Post a Comment