Sie sind auf Seite 1von 36

PES INSTITUTE OF TECHNOLOGY AND

MANAGEMENT,SHIVAMOGGA

DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING

OOPS WITH C++ LAB MANUAL


(06CSL47)

PREPARED BY: MRS. AFSHAN KHANUM


OOPS with C++ Lab Manual

1. Given that an EMPLOYEE class contains the following members:


Data Members: Employee_Number, Employee_Name, Basic, DA, IT,Net_Sal
Member Functions: to read data, to calculate Net_Sal and to print data members
Write a C++ program to read data on N employees and compute the Net_Sal of each
employee
(DA = 52% of Basic and Income Tax = 30% of the gross salary)

#include <iostream.h>
#include <conio.h>
class EMPLOYEE //implments the EMPLOYEE class
{
private:
char employee_number[10],employee_name[10];
float basic,DA,IT,net_sal;
public:
void Read_Data(); //reads the employee_number,
// emloyee_name and basic
void Calculate_Net_Salary(); //calculates the net salary
void Display_Data(); //Displays the data
};
void EMPLOYEE::Read_Data()
{
cout << "Enter the Employee Number and Name" << endl;
cin >> employee_number >> employee_name;
cout << "Enter the Basic Salary" << endl;
cin >> basic;
}
void EMPLOYEE::Calculate_Net_Salary()
{
float Gross_Sal;
DA = (52*basic)/100;
Gross_Sal = basic+DA;
IT = (30*Gross_Sal)/100;
net_sal = Gross_Sal-IT;
}
void EMPLOYEE::Display_Data()
{
Dept of IS&E Page 2
OOPS with C++ Lab Manual

cout << "Emp Name:" << employee_name << "\tEmp Number:" <<
employee_number;
cout << "\t Net Salary:" << net_sal << endl;
}
void main()
{
int n,i;
clrscr();
cout << "Enter the number of employees: " ;
cin >> n; cout << endl;
EMPLOYEE Emp[10];
cout << "Enter employee data" << endl;
for(i=0; i<n; i++)
Emp[i].Read_Data();
for(i=0;i<n;i++)
{
Emp[i].Calculate_Net_Salary();
Emp[i].Display_Data();
}
}

2. Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an
array of 10 STUDENT objects. Using appropriate functions, find the average of the two
better marks for each student. Print the USN, Name and the average marks of all the
students.

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class STUDENT
{
private:
char USN[10], Name[10];
float marks1, marks2, marks3; //marks for the three subjects
float average_marks; //Average marks for the best two
public:
void Read_Data();
void Calculate_Average_Marks();
void Display_Data();
};
void STUDENT::Read_Data()
{
cout << "Enter the name and USN : " ;
cin >> Name >> USN; cout << endl;
Dept of IS&E Page 3
OOPS with C++ Lab Manual

cout << "Enter marks1, marks2, marks3 : " ;


cin >> marks1 >> marks2 >> marks3; cout << endl;
}
void STUDENT::Calculate_Average_Marks()
{
int smallest;
if( (marks1 < marks2) && (marks1 < marks3) ) average_marks = (marks2
+ marks3)/2;
else if (marks2 < marks3) average_marks = (marks1 + marks3)/2;
else average_marks = (marks1 + marks2)/2;
}
void STUDENT::Display_Data()
{
cout << "USN:" << USN << "\t Name:" << Name << "\t Average Marks:";
printf("%0.2f\n",average_marks);
}
void main()
{
STUDENT student[10];
clrscr();
for(int i=0;i<10;i++) student[i].Read_Data();
for(i=0;i<10;i++)
{
student[i].Calculate_Average_Marks();

student[i].Display_Data();
}
}

3. Write a C++ program to create a class called COMPLEX and implement the following
overloading functions ADD that return a complex number:
(i) ADD(a, s2) – where ‘a’ is an integer (real part) and s2 is a complex
number
(ii) ADD(s1, s2) – where s1 and s2 are complex numbers

#include <iostream.h>
#include <conio.h>
#include <math.h>
class COMPLEX
{
private:

Dept of IS&E Page 4


OOPS with C++ Lab Manual

int a, imaginary;
public:
void Read_data();
void Add(COMPLEX s1, COMPLEX s2);
void Add(int realnum, COMPLEX s2);
friend ostream& operator << (ostream& out, COMPLEX t);
};
void COMPLEX::Read_data()
{
cout << "Enter the real part and imaginary part: ";
cin >> a >> imaginary; cout << endl;
}
void COMPLEX::Add(COMPLEX s1,COMPLEX s2)
{
a = s1.a + s2.a;
imaginary = s1.imaginary + s2.imaginary;
}
void COMPLEX::Add(int realnum, COMPLEX s2)
{
a = realnum + s2.a;
imaginary =s 2.imaginary;
}
ostream& operator<<(ostream& out, COMPLEX t)
{
out << t.a;
if(t.imaginary < 0)
out << "-i";
else
out << "+i";
out << abs(t.imaginary);
return out;
}
void main()
{
COMPLEX s1, s2, s3, s4;
clrscr();

cout << "Addition of two complex numbers -" << endl;


s1.Read_data();
s2.Read_data();
s3.Add(s1,s2);
cout << "The resultant complex number is: ";
cout << s3 << endl;
cout << "Addition of a real number only-" << endl;
cout << "Enter the real part: ";
Dept of IS&E Page 5
OOPS with C++ Lab Manual

int realnum;
cin >> realnum;
s4.Add(realnum, s2);
cout << "The resultant complex number: ";
cout << s4 << endl;
}

4. Write a C++ program to create a class called LIST (linked list) with member functions
to insert an element at the front as well as to delete an element from the front of the list.
Demonstrate all the functions after creating a list object.

#include <iostream.h>
#include <process.h>
#include <string.h>
#include <conio.h>
class LIST
{
public:
int info;
LIST *next;
};
class LINKED_LIST
{
LIST *head;
public:
LINKED_LIST()
{
head = NULL;
}
void InsertF();
void DeleteF();
void Display_List();
};
void LINKED_LIST::InsertF()
{
LIST *temp;
int item;
cout << "Enter the data: ";
cin >> item; cout << endl;
Dept of IS&E Page 6
OOPS with C++ Lab Manual

temp = new LIST;


temp->info = item;
temp->next = NULL;
if(head == NULL)
head = temp;
else
temp->next = head;
head = temp;
}
void LINKED_LIST::DeleteF()
{
LIST *temp;
if(head == NULL)
cout << "No data is present" << endl;
else
{
temp = head;
head = head->next;
cout << "The deleted data is: " << temp->info << endl;
delete temp;
}
}
void LINKED_LIST::Display_List()
{
if(head == NULL)
cout << "No data is present" << endl;
else
for(LIST *temp = head; temp != NULL; temp =temp->next)
cout << temp->info << "->" << endl;
}
void main()
{
LINKED_LIST s1;
clrscr();
int ch=1;
while(ch)
{
cout << "1 Insert_front
2 Delete_front
3 Display List
0 Exit\n";
cin >> ch;
switch(ch)
{
case 1 : s1.InsertF();
Dept of IS&E Page 7
OOPS with C++ Lab Manual

break;
case 2 : s1.DeleteF();
break;
case 3 : s1.Display_List();
break;
default: cout << "Wrong choice!" << endl;
cout << "Enter the choice again, with 0 to quit" <<
endl;
cin >> ch;
}//end switch
} //end while(ch)
}//end main()

5. Write a C++ program to create a template function for Quicksort and demonstrate
sorting of integers and doubles.

#include <iostream.h>
#include <conio.h>
#include <process.h>
template <class T>
class QUICK_SORT
{
private:
T a[20];
int low, high, size;
public:
QUICK_SORT(int n)
{
size=n;
}
void Get_Data();
void Quick(int low, int high);
int Partition(int low, int high);
void Display_Data();
};
template<class T>
void QUICK_SORT<T>::Get_Data()
{
cout<<"Enter the elements to be inserted" << endl;
for(int i=0; i<size; i++)
cin >> a[i];
}
Dept of IS&E Page 8
OOPS with C++ Lab Manual

template<class T>
void QUICK_SORT<T>::Quick(int low, int high)
{
int j;
if(low <= high)
{
j = Partition(low,high);
Quick(low, j-1);
Quick(j+1, high);
}
}
template<class T>
int QUICK_SORT<T>::Partition(int low, int high)
{
int i, j;
T key;
I = low + 1;
J = high;
Key = a[low];
while(1)
{
while(I < high && key >= a[i]) i++;
while(key <a[j]) j--;
if(I < j)
{
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
T temp = a[j];
a[j] = a[low];
a[low] = temp;
return j;
}
}//end while
}//end QUICK_SORT<T>
template<class T>
void QUICK_SORT<T>::Display_Data()
{
int i;
cout << "The sorted list is";
for(i=0; i<size; i++)
cout << ", "<<a[i];
Dept of IS&E Page 9
OOPS with C++ Lab Manual

cout << endl;


}
void main()
{
int n, ch;
clrscr();
cout<<"Enter number of data: ";
cin>>n; cout << endl;
QUICK_SORT<int>Q1(n);
QUICK_SORT<double>Q2(n);
cout << "1.To sort integer data " << endl;
cout << "2.To sort double data" << endl;
cout << "3.To quit" << endl;
cout << "Enter your choice" << endl;
cin >> ch;
switch(ch)
{
case 1:
Q1.Get_Data();
Q1.Quick(0,n-1);
Q1.Display_Data();
break;
case 2:
Q2.Get_Data();
Q2.Quick(0,n-1);
Q2.Display_Data();
break;
}
getch();
}//end main()

6. Write a C++ program to create a class called STACK using an array of integers.
Implement the following operations by overloading the operators ‘+’ and ‘-‘:
(i) s1 = s1 + element; where s1 is an object of the class STACK and element is an integer to
be pushed on the top of the stack
(ii) s1 = s1- ; where s1 is an object of the class STACK. ‘-‘ operator pops the element.
Handle the STACK empty and full conditions. Also display the contents of the stack after
each operation, by overloading the << operator.

#include <iostream.h>
#include <process.h>
Dept of IS&E Page 10
OOPS with C++ Lab Manual

#include <conio.h>
class STACK
{
private:
int a[10], size, top;
public:
STACK(int n)
{
top = -1;
size = n;
}
void operator +(int x);
void operator -();
friend ostream &operator << (ostream &out, STACK st);
};
void STACK::operator+(int x)
{
if(top == size-1)
cout << "Stack is overflow" << endl;
else
a[++top] = x;
}
void STACK::operator -()
{
if(top == -1)
cout << "Stack is underflow" << endl;
else
cout << "The popped element is" <<a [top--] << endl;
}
ostream &operator<<(ostream &out, STACK st)
{
if(st.top == -1)
cout << "Stack is underflow" << endl;
else
for(int i=st.top; i>=0; i--)
cout << st.a[i] << "->";
cout << endl;
return out;
}
void main()
{
int n,ch,x;
clrscr();
cout << "Enter size of stack:";
cin >> n;
Dept of IS&E Page 11
OOPS with C++ Lab Manual

STACK s1(n);
while(1)
{
cout << endl << ”1 To push
2 To pop
3 To display
4 To exit"; << endl
cout << "Enter your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout << "Enter the data to push:";
cin >> x;
s1+x;
break;
case 2:
-s1;
break;
case 3:
cout << "The content of stack:";
cout << s1;
break;
default:
cout << "Program is terminated\n";
exit(0);
}//end switch
getch();
}//end while
}//end main

7. Write a C++ program to create a class called DATE. Accept two valid dates in the form
dd/mm/yy. Implement the following operations by overloading the operators ‘+’ and ‘-‘.
After every operation display the results by overloading the operator <<.
(i) no_of_days = d1 – d2; where d1 and d2 are DATE objects, and no_of_days is an integer
(ii) d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer

#include <iostream.h>
#include <conio.h>
#include <process.h>
class DATE
{
Dept of IS&E Page 12
OOPS with C++ Lab Manual

private:
int flag, day, month, year;
public:
DATE(int d, int m, int y)
{
day = d, month = m,year = y;
if((year%4) == 0)
flag=1;
else
flag=0;
}
int operator -(DATE D2);
DATE operator +(int n);
friend ostream &operator<<(ostream &out,DATE D1);
int return_integer_date(DATE D1);
};
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int DATE::return_integer_date(DATE D1)
{
int int_value = D1.day;
if((D1.flag == 1) && (D1.month > 2))
{
for(int I = 0; i<D1.month; i++)
int_value+a[i];
int_value++;
}
else
for(int i=0;i<D1.month;i++)
int_value=int_value+a[i];
return int_value;
}
int DATE::operator -(DATE D2)
{
int a1, a2,x = 0;
DATE D1(day, month, year);
if(D1.day == D2.day && D1.month == D2.month && D1.year == D2.year)
return x;
a1 = return_integer_date(D1);
a2 = return_integer_date(D2);
for(int i = D1.year-1; I > D2.year; i--);
{
if(i%4 == 0)
x = x+366;
else
Dept of IS&E Page 13
OOPS with C++ Lab Manual

x = x+365;
}
if(D1.year == D2.year)
x = x+a1-a2;
else
{
x = x+a1;
if(D2.year%4 == 0)
x = x+(366-a2);
else
x = x+(365-a2);
}
return x;
}
DATE DATE::operator+(int n)
{
DATE D(day, month, year);
for(int i=0; i<n; i++)
{
D.day++;
if(D.year%4 == 0)
{
D.flag=1;
if(D.day > b[D.month])
{
D.day = 1;
D.month++;
}
}
else
{
D.flag=0;
if(D.day > a[D.month])
{
D.day=1;
D.month++;
}
}
if(D.month > 12)
{
D.month=1;
D.year++;
}
}
return D;
Dept of IS&E Page 14
OOPS with C++ Lab Manual

}
ostream &operator<<(ostream &out,DATE D1)
{
out << D1.day << "/" << D1.month << "/" << D1.year;
return out;
}
void main()
{
int d, m, y, no_of_days;
clrscr();
cout << "Enter A(VALID)DATE\n";
cout << "ENTER DAY (dd):";
cin >> d;
cout << "ENTER MONTH(mm):";
cin >> m;
cout << "ENTER YEAR(yyyy):";
cin >> y;
cout << endl;
DATE D1(d,m,y);
cout << "Enter another(Valid)DATE(Earlier to that entered above)\n";
cout << "ENTER DAY(dd):";
cin >> d;
cout << "ENTER MONTH(mm):";
cin>>m;
cout<<"ENTER YEAR(yyyy):";
cin >> y;
cout << endl;
DATE D2(d,m,y);
Cout << "First DATE ENTERED:" << D1 << endl << endl;
Cout << "Second DATE ENTERED:" << D2 << endl << endl;
no_of_days = D1-D2;
cout <<"The difference between the 2 days:"<<no_of_days<<endl<<endl;
cout << "Enter a no.:";
cin >> no_of_days;
D1 = D2 + no_of_days;
cout<<"\n The resulting dare from the DATE"<<D2<<"IS:"<<D1<<endl;
getch();
}

8 Write a C++ program to create a class called MATRIX using twodimensional array of
integers. Implement the following operations by overloading the operator ++ whch checks
the compatibility of two matrices to be added and subtracted. Perform the addition and
subtraction by overloading the + and – operators respectively. Display the results by
overloading the operator <<.
Dept of IS&E Page 15
OOPS with C++ Lab Manual

If (m1==m2)
{
m3=m1+m2;
m4=m1-m2;
}
else
display error

#include <iostream.h>
#include <conio.h>
#include <process.h>
class MATRIX
{
int m, n;
int arr[20][20];
public:
MATRIX(int a, int b ) //parametrized constructor
{ m=a;
n=b;
}
friend int operator==(MATRIX,MATRIX);
friend MATRIX operator+(MATRIX,MATRIX);
friend MATRIX operator-(MATRIX,MATRIX);
void get();
friend ostream& operator<<(ostream&,MATRIX);
/* MATRIX operator=(MATRIX r);*/
}; //end of class defn
int operator==(MATRIX M1,MATRIX M2)
{
if((M1.m==M2.m) && (M1.n==M2.n)) return 1;
return 0;
}
MATRIX operator+(MATRIX M1,MATRIX M2)
{
MATRIX M3(M1.m,M1.n);
for(int i=0;i<M1.m;i++)
for(int j=0;j<M1.n;j++)
M3.arr[i][j]=M1.arr[i][j] + M2.arr[i][j];
return M3;
}

MATRIX operator-(MATRIX M1,MATRIX M2)


{
MATRIX M3(M1.m,M1.n);
Dept of IS&E Page 16
OOPS with C++ Lab Manual

for(int i=0;i<M1.m;i++)
for(int j=0;j<M1.n;j++)
M3.arr[i][j]=M1.arr[i][j] - M2.arr[i][j];
return M3;
}
void MATRIX::get()
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
cout<<"Enter the element at row "<<i+1<<" and column
"<<j+1<<" :";
cin>>arr[i][j];
}
}
ostream& operator<<(ostream& out,MATRIX s)
{
for(int i=0;i<s.m;i++)
{
for(int j=0;j<s.n;j++)
out<<s.arr[i][j]<<" ";
out<<endl;
}
return out;
}
int main()
{
int a,b,c,d,ch;
clrscr();
cout<<"Enter the number of rows in matrix A:";
cin>>a;
cout<<"Enter the number of columns in matrix A:";
cin>>b;
cout<<"Enter the number of rows in matrix B:";
cin>>c;
cout<<"Enter the number of columns in matrix B:";
cin>>d;
MATRIX p(a,b),q(c,d),r(a,b);
cout<<"Enter Matrix A:"<<endl;
p.get();
cout<<"Enter Matrix B:"<<endl;
q.get();
lblmenu:
cout<<"1) Add 2) Subtract 3) Quit :";
cin>>ch;
Dept of IS&E Page 17
OOPS with C++ Lab Manual

if(ch<1 || ch>3) goto lblmenu;


if(ch==1)
{
if(p==q)
{
r=p+q;
cout<<r;
}
else
cout<<"Cant add."<<endl;
}
if(ch==2)
{
if(p==q)
{
r=(p-q);
cout<<r;
}
else cout<<"Cant subtract."<<endl;
}
if(ch==3)
{
exit(0);
}
goto lblmenu;
}

9. Write a C++ program to create a class called OCTAL which has the characteristics of an
octal number. Implement the following operations by writing an appropriate constructor
and an overloaded operator +.
(i) OCTAL h = x; where x is an integer.
(ii) int y = h + k; where h is an OCTAL object and k is an integer
Display the OCTAL result by overloading the operator << . Also display the values
of h and y.

#include <iostream.h>
#include <conio.h>
#include <math.h>
class octal
{
private:
int o;
Dept of IS&E Page 18
OOPS with C++ Lab Manual

public:
octal();
octal(int);
~octal();
int dectooct(int x);
int octtodec(int x);
friend ostream &operator<<(ostream &print,octal);
int operator +(int);
};
octal::octal()
{
}
octal::octal(int x)
{
o=dectooct(x);
}
octal::~octal()
{
}
int octal::dectooct(int x)
{
int i=0,sum=0,rem;
while(x!=0)
{
rem=x%8;
sum=sum+rem*pow(10,i);
i++;
x=x/8;
}
return sum;
}
int octal::octtodec(int x)
{
int i=0,sum=0,rem;
while(x!=0)
{
rem=x%10;
sum=sum+rem*pow(8,i);
i++;
x=x/10;
}
return sum;
}
ostream &operator<<(ostream &print,octal x)
{
Dept of IS&E Page 19
OOPS with C++ Lab Manual

print<<x.o;
return print;
}
int octal::operator+(int x)
{
return octtodec(o)+x;
}
main()
{
clrscr();
int x,y,k;
cout<<endl<<"Enter the value of x in decimal notation:";
cin>>x;
octal h(x);
cout<<endl<<"Corresponding value of x in octal notation,h="<<h;
cout<<endl<<"Enter the value of k in decimal notation:";
cin>>k;
cout<<"The value of k="<<k;
y=h+k;
cout<<endl<<"The value of h+k in decimal notation,y="<<y;
getch();
return 0;
}

10. Write a C++ program to create a class called QUEUE with member functions to add an
element and to delete an element from the queue. Using the member functions, implement
a queue of integers and double. Demonstrate the operations by displaying the contents of
the queue after every operation.

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#define size 3
template<class t>
class queue
{
private:
t a[size];
int f,r;
public:
Dept of IS&E Page 20
OOPS with C++ Lab Manual

queue();
~queue();
void add();
void del();
void dis();
};
template<class t>
queue<t>::queue()
{
f=-1;
r=-1;
}
template<class t>
queue<t>::~queue()
{
}
template<class t>
void queue<t>::add()
{
if(r==size-1)
cout<<endl<<"queue is full";
else
{
r++;
cout<<endl<<"Enter the data:";
cin>>a[r];
if(f==-1)
f=0;
}
}
template<class t>
void queue<t>::dis()
{
if(f==-1)
cout<<endl<<"queue is empty";
else
{
cout<<endl<<"Content of queue:"<<endl;
for(int i=f;i<=r;i++)
cout<<setw(5)<<a[i];
}
}
template<class t>
void queue<t>::del()
{
Dept of IS&E Page 21
OOPS with C++ Lab Manual

if(f==-1)
cout<<endl<<"queue is empty";
else
{
cout<<endl<<"Deleted element is"<<a[f];
if(f==r)
f=r=-1;
else
f=f+1;
}
}
main()
{
queue<int>q;
int ch=1;
clrscr();
while(ch)
{
cout<<endl<<"Enter 1 to add a data to queue:";
cout<<endl<<"Enter 2 to delete a data from the queue:";
cout<<endl<<"Enter 0 to quit:";
cout<<endl<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
q.add();
q.dis();
break;
case 2:
q.del();
q.dis();
break;
}
}
getch();
return 0;
}

11. Write a C++ program to create a class called DLIST (doubly Linked List) with member
functions to insert a node at a specified position and delete a node from a specified position
of the list. Demonstrate the operations by displaying the content of the list after every
operation.

Dept of IS&E Page 22


OOPS with C++ Lab Manual

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int data;
node *llink,*rlink;
};
class dlist
{
private:
struct node *head;
public:
dlist();
~dlist();
void create();
void addatpos();
void delatpos();
void dis();
};
dlist::dlist()
{
head=NULL;
}
dlist::~dlist()
{
delete head;
}
void dlist::create()
{
int no,i;
struct node *n;
cout<<endl<<"Enter number of elements:";
cin>>no;
for(i=1;i<=no;i++)
{
n=new node;
n->rlink=NULL;
n->llink=NULL;
cout<<endl<<"Enter the data:";
cin>>n->data;
if(head==NULL)
head=n;
else
Dept of IS&E Page 23
OOPS with C++ Lab Manual

{
n->rlink=head;
head->llink=n;
head=n;
}
}
return;
}
void dlist::addatpos()
{
struct node *n;
int p;
cout<<endl<<"Enter position:";
cin>>p;
n=new node;
n->llink=NULL;
n->rlink=NULL;
cout<<endl<<"Enter data:";
cin>>n->data;
if(p==1)
{
n->rlink=head;
head->llink=n;
head=n;
}
else
{
int i=1;
struct node *t;
t=head;
while(i<=p-2)
{
t=t->rlink;
i++;
}
n->rlink=t->rlink;
t->rlink->llink=n;
n->llink=t;
t->rlink=n;
}
return;
}
void dlist::delatpos()
{
struct node *t;
Dept of IS&E Page 24
OOPS with C++ Lab Manual

int i,p;
cout<<endl<<"Enter position:";
cin>>p;
if(p==1)
{
head=head->rlink;
head->llink=NULL;
return;
}
t=head;
i=1;
while(i<p&&t!=NULL)
{
t=t->rlink;
i++;
}
if(t->rlink==NULL)
t->llink->rlink=NULL;
else
{
t->llink->rlink=t->rlink;
t->rlink->llink=t->llink;
}
return;
}
void dlist::dis()
{
struct node *t;
t=head;
if(t==NULL)
cout<<endl<<"Empty list:";
else
{
cout<<endl<<"Content of the list"<<endl;
while(t!=NULL)
{
cout<<setw(5)<<t->data;
t=t->rlink;
}
}
return;
}
main()
{
dlist d;
Dept of IS&E Page 25
OOPS with C++ Lab Manual

int ch=1;
clrscr();
while(ch)
{
cout<<endl<<"Enter 1 to create double linked list:";
cout<<endl<<"Enter 2 to add element at position:";
cout<<endl<<"Enter 3 to delete an element at a position:";
cout<<endl<<"Enter 0 to quit:";
cout<<endl<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
d.create();
d.dis();
break;
case 2:
d.addatpos();
d.dis();
break;
case 3:
d.delatpos();
d.dis();
break;
}
}
getch();
return 0;
}

12. Write a C++ program to create a class called STUDENT with data members USN,
Name and Age. Using inheritance, create the classes UGSTUDENT and PGSTUDENT
having fields as Semester, Fees and Stipend. Enter the data for at least 5 students. Find the
semester-wise average age for all UG and PG students
separately.

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class student
{
Dept of IS&E Page 26
OOPS with C++ Lab Manual

private:
char name[20];
int reg,age;
public:
student();
~student();
void getsdata();
int giveage();
};
student::student()
{
}
student::~student()
{
}
int student::giveage()
{
return age;
}
void student::getsdata()
{
cout<<endl<<"Name:";
cin>>name;
cout<<"Reg.No.:";
cin>>reg;
cout<<"Age:";
cin>>age;
}
class ugstudent:public student
{
private:
int sem,fee,sti;
public:
ugstudent();
~ugstudent();
void getugdata();
int givesem();
};
ugstudent::ugstudent()
{
}
ugstudent::~ugstudent()
{
}
void ugstudent::getugdata()
Dept of IS&E Page 27
OOPS with C++ Lab Manual

{
getsdata();
cout<<"Semester:";
cin>>sem;
cout<<"Fee:";
cin>>fee;
cout<<"Stipend:";
cin>>sti;
}
int ugstudent::givesem()
{
return sem;
}
class pgstudent:public student
{
private:
int sem,fee,sti;
public:
pgstudent();
~pgstudent();
void getpgdata();
int givesem();
};
pgstudent::pgstudent()
{
}
pgstudent::~pgstudent()
{
}
void pgstudent::getpgdata()
{
student::getsdata();
cout<<"Semester:";
cin>>sem;
cout<<"Fee:";
cin>>fee;
cout<<"Stipend:";
cin>>sti;
}
int pgstudent::givesem()
{
return sem;
}
main()
{
Dept of IS&E Page 28
OOPS with C++ Lab Manual

ugstudent u[10];
pgstudent p[10];
int i,n;
clrscr();
cout<<endl<<"Enter number of students:";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<endl<<"Enter the details of UG student"<<i<<endl;
u[i].getugdata();
}
for(int s=1;s<=8;s++)
{
float sum=0;
int flag=0,cou=0;
for(i=1;i<=n;i++)
if(u[i].givesem()==s)
{
sum=sum+u[i].giveage();
flag=1;
cou++;
}
if(flag==1)
cout<<endl<<s<<"Semester"<<"average age is"<<sum/cou;
}
for(i=1;i<=n;i++)
{
cout<<endl<<"Enter thge details of PG student"<<i<<endl;
p[i].getpgdata();
}
for(s=1;s<=8;s++)
{
float sum=0;
int flag=0,cou=0;
for(i=1;i<=n;i++)
if(p[i].givesem()==s)
{
sum=sum+p[i].giveage();
flag=1;
cou++;
}
if(flag==1)
cout<<endl<<s<<"Semester"<<"average age is"<<sum/cou;
}
getch();
Dept of IS&E Page 29
OOPS with C++ Lab Manual

return 0;
}

13. Write a C++ program to create a class called STRING and implement the following
operations. Display the results after every operation by overloading the operator <<.
(i) STRING s1 = “VTU”
(ii) STRING s2 = “BELGAUM”
(iii) STRING s3 = s1 + s2 (Use copy constructor)

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
private:
char s[20];
public:
string();
~string();
string(char x[]);
string(string & x);
friend string operator+(string s1,string s2);
friend ostream & operator<<(ostream &print,string x);
};
string::string()
{
}
string::string(char x[])
{
strcpy(s,x);
}
string::string(string & x)
{
strcpy(s,x.s);
}
string::~string()
{
}
string operator+(string s1,string s2)
{
string temp(s1);
strcat(temp.s,s2.s);
return temp;
Dept of IS&E Page 30
OOPS with C++ Lab Manual

}
ostream & operator<<(ostream & print,string x)
{
print<<x.s<<endl;
return print;
}
main()
{
clrscr();
string s1="VTU";
cout<<endl<<"First string is"<<s1;
string s2="BELAGAUM";
cout<<endl<<"Second string is"<<s2;
string s3=s1+s2;
cout<<endl<<"Resultant string is"<<s3;
getch();
return 0;
}

14. Write a C++ program to create a class called BIN_TREE (Binary Tree) with member
functions to perform in-order, preorder and post-order traversals. Create a BIN_TREE
object and demonstrate the traversals.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
struct node
{
int data;
node*llink,*rlink;
};
class btree
{
private:
node*root;
public:
btree();
~btree();
struct node*create();
void inorder(node *root);
void preorder(node *root);
void postorder(node *root);
};
btree::btree()
Dept of IS&E Page 31
OOPS with C++ Lab Manual

{
root=NULL;
}
btree::~btree()
{
delete root;
}
node *btree::create()
{
node *n,*f,*c;
int i,no;
cout<<endl<<"Enter member of elements in the tree:";
cin>>no;
for(i=1;i<=no;i++)
{
n=new node;
n->llink=NULL;
n->rlink=NULL;
cout<<endl<<"Enter the data:";
cin>>n->data;
if(root==NULL)
root=n;
else
{
f=root;
c=root;
while(c!=NULL)
{
f=c;
if(n->data<c->data)
c=c->llink;
else
c=c->rlink;
}
if(n->data<f->data)
f->llink=n;
else
f->rlink=n;
}
}
return root;
}
void btree::inorder(struct node *root)
{
if(root!=NULL)
Dept of IS&E Page 32
OOPS with C++ Lab Manual

{
inorder(root->llink);
cout<<root->data<<setw(5);
inorder(root->rlink);
}
}
void btree::preorder(struct node *root)
{
if(root!=NULL)
{
cout<<root->data<<setw(5);
preorder(root->llink);
preorder(root->rlink);
}
}
void btree::postorder(struct node *root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
cout<<root->data<<setw(5);
}
}
main()
{
clrscr();
btree b;
struct node *root;
root=b.create();
cout<<endl<<"Inorder traversal:"<<endl;
b.inorder(root);
cout<<endl<<"Preorder traversal:"<<endl;
b.preorder(root);
cout<<endl<<"Postorder traversal:"<<endl;
b.postorder(root);
getch();
return 0;
}

15. Write a C++ program to create a class called EXPRESSION. Using appropriate
member functions convert a given valid Infix expression into postfix form. Display the infix
and postfix expressions.

#include<iostream.h>
Dept of IS&E Page 33
OOPS with C++ Lab Manual

#include<conio.h>
#include<string.h>
#include<ctype.h>
class expression
{
private:
char infix[20],postfix[20],stack[20];
int top;
public:
expression();
expression(char s[20]);
~expression();
void push(char ch);
char pop();
int priority(char ch);
void infixtopostfix();
void dis();
};
expression::expression()
{
top=-1;
}
expression::expression(char s[20])
{
strcpy(infix,s);
top=-1;
}
expression::~expression()
{
}
void expression::push(char ch)
{
top=top+1;
stack[top]=ch;
}
char expression::pop()
{
char ch;
ch=stack[top];
top=top-1;
return ch;
}
int expression::priority(char ch)
{
int p;
Dept of IS&E Page 34
OOPS with C++ Lab Manual

switch(ch)
{
case'/':
case'*':
p=2;
break;
case'+':
case'_':
p=1;
break;
case'(':
p=0;
break;
case'#':
p=-1;
break;
}
return p;
}
void expression::infixtopostfix()
{
int i,p;
char ch;
i=0;
p=0;
push('#');
while(infix[i]!='\0')
{
ch=infix[i];
switch(ch)
{
case'(':
push(ch);
break;
case')':
while(stack[top]!='(')
{
postfix[p]=pop();
p=p+1;
}
pop();
break;
case'*':
case'/':
case'+':
Dept of IS&E Page 35
OOPS with C++ Lab Manual

case'-':
while(priority(stack[top])>=priority(ch))
{
postfix[p]=pop();
p=p+1;
}
push(ch);
break;
default:
postfix[p]=ch;
p=p+1;
}
i=i+1;
}
while(stack[top]!='#')
{
postfix[p]=pop();
p=p+1;
}
postfix[p]='\0';
return;
}
void expression::dis()
{
cout<<postfix;
}
main()
{
char s[20];
clrscr();
cout<<endl<<"Enter a valid infix expression:";
cin>>s;
expression ex(s);
ex.infixtopostfix();
cout<<"Converted postfix expression:";
ex.dis();
getch();
return 0;
}

Dept of IS&E Page 36

Das könnte Ihnen auch gefallen