OOPS Lab Programs by praveena [veenagopi2005@yahoo.co.in]


Contributed by

Praveena

veenagopi2005@yahoo.co.in

==================================================================

1.a) CLASS WITH STATIC DATA MEMBER
Aim
To implement static data member in class.

Objective
Static data member of class is shared by all the instances of the class.

Exercises
1. Create class ITEM with static data member as count.
2. Create a member function to increment the count.
3. Declare the static datamember using scope resolution operator.
4. Display the count value.

1.b) CLASS WITH STATIC MEMBER FUNCTION
Aim
To implement static member function in class.

Objective
Static member function of class is shared by all the instances of the class.A static member functions cannot access auto members of a class.
Exercises
1. Create class TEST with static member function.
2. Invoke the static member function using classname with scope
resolution operator and function name.
3. Display the value

1.c)FUNCTION WITH DEFAULT ARGUMENT

Aim
To implement function with default arguments.

Objective
Function may be defined to take default values for the omitted arguments
by providing the default values in the function prototype.

Exercises
1. Declare the default function.
2. Invoke the default function.
3. Display the result

1.d) CLASS WITH FRIEND FUNCTION

Aim
To implement friend function with class.

Objective
Friend function is used to access the private data member and member
function .

Exercises
1. Create the class and declare the data member as private.
2. Declare the friend function using the keyword friend.
3. Perform the operation
4. Display the result.

2. Implement complex number class with necessary operator overloadings.
Aim
To implement complex number class with necessary operator overloadings.

Objective
Use the template to perform operator overloaded with complex class.

Exercises
1. Create the class name as COMPLEX.
2. Declare the data member and member function.
3. Declare the template for handling various operator overloaded
operation.
4. Display the result.

3. Implement class with dynamic memory allocation.
Aim
To implement class with dynamic memory allocation using constructor,destructor and copy constructor.

Objective
Using the classes with dynamic memory alloction.

Exercises
1. Create the class name as MATRIX.
2. Declare the data member and member function.
3. Declare constructor,destructor and copy constructor
4. Display the result.

4. Overload the new and delete operators to provide custom dynamic allocation of memory.
Aim
To implement class with dynamic memory allocation using new and delete operator to overload.

Objective
New and delete operator is overloaded along with dynamic
memory allocation.

Exercises
1. Create the class.
2. Declare the new and delete operator.
3. Perform the overload operation
4. Display the result.

5. Develope a template of linked list calss and its methods
Aim
To implement the linked list.

Objective
Linked list to perform insert,delete and display the data.
Exercises
1. Create the class with LIST.
2. Create a member function for constructor and to insert the element
in the linked list.
3. Use the pointer to refer the next list.
4. Display the list value.

6. Develope a template of standard sorting algorithms such as bubble sort.
Aim
To implement bubble sort.

Objective
Sort the element using bubble sort technique.
Exercises
1. Create the template to handle for bubble sort.
2. Get the element using for loop.
3. Call the template function for sorting.
4. Display the result.

7. Design stack and queue classes with neccessary exception handling.
Aim
To implement stack and queue using c++.

Objective
To perform LIFO and FIFO operations.
Exercises
1. Create the class for stack .
2. Declare the member function for push and pop operation.
3. Push operation to insert the element.
4. Pop operation to delete the element.

5. Create a class for queue.
6. Declare the member function for front and rear operation.
7. Provide necessary exception handling for stack and queue.
8. Display the result.

Ex:No:8
Implementation of Graph
Aim:
To implement the graph and to obtain a minimum cost spanning tree.
Objective
To know about the concept of Graph along with minimum spanning tree.
Exercise:
1.Create two classes as POINT and ARC.
2.Define a Graph class which represents the collection of Point and Arc objects.
3.Write a method to find a minimum cost spanning tree in a graph.

Ex:no:9
Develop with suitable Hierarchy classes and to implement dynamic polymorphism.

Aim:
To implement Hierarchy classes with Dynamic polymorphism.
Objective
Use Virtual concept along with RTTI

Exercise:
1.Create a class SHAPE with datamember and one member function as Virtual.
2.Create a derived class CIRCLE with SHAPE as base class.
3.Create a derived class DOT with CIRCLE base class.
4. Create a Object in the main function.
5. Invoke the appropriate function using object.
6.Finally display the result.

10.Write a c++ program that randomly generates complex numbers.
Aim:
To implement the program that randomly generates complex numbers .
and write them two per line in a file along with an operator.The numbers are written to file in the format(a+ib).Write another program to read one line at atime from this file,perform the corresponding operation on the two complex numbers read and write the result to another file.

Exercise:
1. Create a two file.
2. One file to read and another file to write.
3. Do the operation in one file and write the result in another file.
4. Display the result.

CODING
© function with default arguments
#include
void printLine(char =’_’,int =70);
void main()
{
printLine();
printLine(‘/’);
printLine(‘*’,40);
printLine(‘R’,55);
}
void printLine(char ch, int Repeatcount)
{
int i;
cout<<endl;
for(i=0;i<Repeatcount;i++)
cout<<ch;
}

OUTPUT:

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
R R R R R R R R R R R R R R R R R R R R R R R

(a) CLASS WITH STATIC DATA MEMBER
Program:
#include
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
cout<<”Number”<<num;
}
void showcount()
{
cout<<”count”;
cout<<count<<”\n”;
}
};
int item::count;
int main()
{
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
a.getdata(20);
b.getdata(30);
c.getdata(40);
a.showcount();
b.showcount();
c.showcount();
}
OUTPUT:
count 0
count 0
count 0
Number 20
Number 30
Number 40
count 3
count 3
count 3

(b) CLASS WITH STATIC MEMBER FUNCTION

Program:
#include
class test
{
int code;
static int count;
public:
void setcode()
{
cout<<”Object Number:”<<code<<”\n”;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcount();
t2.setcount();
test::showcount();
test t3;
t1.showcode();
t2.showcode();
t3.showcode();
return(0);
}

OUTPUT:

count 2
count 3
Object Number 1
Object Number 2
Object Number 3

Ex:no:2
#include
template
class complex
{
private:
T real;
T imag;
Public:
complex()
{
real=imag=0;
}
void getdata()
{
cout<>real;
cout<>imag;
}
complex operator +(complex c2);
void outdata(char *msg)
{
cout<<msg<<”(“<<real;
cout<<”,”<<img<<”)”<<endl;
}
};
Template
complex complex::operator +(complexc2)
{
Complextemp;
temp.real=real+c2.real;
temp.img=imag+c2.imag;
return(temp);
}
void main()
{
complexc1,c2,c3;
cout<<”Addition of integercomplexobjects….”<<endl;
cout<<”Enter complex number c1..”<<endl;
c1.getdata();
cout<<”Enterthecomplexnumberc2”<<endl;
c2.getdata();
c3=c1+c2;
c3.outdata(“c3=c1+c2:”);

complexc4,c5,c6;
cout<<”Additionof float complexobjects….”<<endl;
cout<<”Enter complex number c4..”<<endl;
c4.getdata();
cout<<”Enterthecomplexnumberc5”<<endl;
c5.getdata();
c6=c4+c5;
c6.outdata(“c6=c4+c5:”);

}

Output:
Addition of integer complexobjects…
Enter complex number c1..
Real part?1
Imag part?2
Enter complex number c2..
Real part?3
Imag part?4
C3=c1+c2:(4,6)

Addition of float complexobjects…
Enter complex number c4..
Real part?1.5
Imag part?2.5
Enter complex number c5..
Real part?2.4
Imag part?3.7
C6=c4+c5:(3.9,6.2)
Ex:no:3
#include
#include
const int TRUE=1;
const int FALSE=0;
class matrix
{
private:
int row;
int col;
int **p;
public:
matrix()
{
row=col=0;
p=NULL;
}
matrix(int r,int c);
~matrix();
void read();
void show();
void add(matrix &a,matrix &b);
void sub(matrix &a,matrix &b);

};
matrix::matrix(int r,int c)
{
row=r;
col=c;
p=new int *[row];
for(int i=0;i<row;i++)
p[i]=new int[col];
}
matrix::~matrix()
{
for(int i=0;i<row;i++)
delete p[i];
delete p;
}

void matrix::add(matrix &a,matrix &b)
{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]+b.p[i][j];
}
void matrix::sub(matrix &a,matrix &b)
{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]-b.p[i][j];
}
Void matrix::read()
{
int i,j;
for(inti=0;i<row;i++) for(intj=0;j<col;j++)
{
cout<<”Matrix[“<<i<<”,”<<j<>p[i][j];
}

Void matrix::show()
{
int i,j;
for(inti=0;i<row;i++)
{
cout<<endl;
for(intj=0;j<col;j++)
{
cout<<p[i][j]<<” “;
}

}

void main()
{
int m,n,p,g;
cout<<”Enter the A matrix…”<<endl;
cout<>m;
cout<>n;
matrix a(m,n);
a.read();

cout<<”Enter the B matrix…”<<endl;
cout<>p;
cout<>q;
matrix b(p,q);
b.read();

cout<<”Matrix A is ..”;
a.show();
cout<<endl<<”Matrix B is…”;
b.show();
matrix c(m,n);
c.add(a,b);
cout<<endl<<”c=a+b..”;
c.show();
matrix d(m,n);
d.add(a,b);
cout<<endl<<”d=a-b..”;
d.show();

}

Output:
Enter the A matrix…
Howmany rows?2
How many col?2
Matrix[0,0]=?1
Matrix[0,1]=?1
Matrix[1,0]=?1
Matrix[1,1]=?1

Enter the B matrix…
Howmany rows?2
How many col?2
Matrix[0,0]=?1
Matrix[0,1]=?1
Matrix[1,0]=?1
Matrix[1,1]=?1

Matrix A is
1 1
1 1
Matrix B is
1 1
1 1
c=a+b
2 2
2 2
d=a-b
0 0
0 0
Ex:no:4
#include
const int SIZE=10;
class vector
{
private:
int *array;
public:
void *operator new(size_t s)
{
vector *my_vector;
my_vector=::new vector;
my_vector->array=new int[SIZE];
return my_vector;
}
void operator delete(void *vec)
{
Vector *my_vect;
my_vect=(vector *) vec;
delete(int *)my_vect->array;
::delete vec;
}
void read();
int sum();
};
Void vector::read()
{
for(int i=0;i<SIZE;i++)
{
cout<<”vector[“<<i<>array[i];
}
}
int vector::sum()
{
int sum=0;
for(int i=0;i<SIZE;i++)
sum=sum+array[i];
return sum;
}
void main()
{
vector *my_vector=new vector;
cout<<”enter vector data..”<read();
cout<<”sum of vector=”<sum();
delete my_vector;
}

Output:
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:2
sum of vector=10

Ex:no:5
#include
#include
class list
{
private:
int data;
list *next;
public:
list()
{
data=0;
next=NULL;
}
ist(int dat)
{
data=dat;next=null;
}
~list(){}
int get()
{
return data;
}
void insert(list *node)
friend void display(list *);
};
void list::insert(list *node)
{
list *last=this;
while(last->next)
last=last->next;last->next=node;
}
void display(list *first)
{
list *traverse;
cout<next)
cout<data<<”,”;
cout<<endl;
}
void main()
{
int choice, data;
list *first=null;
list *node;
while(1)
{
cout<<”Linked list…”;
cout<<”1.Insert”<<endl;
cout<<”2.Display”<<endl;
cout<<”3..quit”<<endl;
cout<>choice;
switch(choice)
{
case 1:cout<>data;
node=new list(data);
if(first==null)
first=node;
elsefirst->insert(node)
break;
case 2:
display(first);
break;
case 3:exit(1);

default:cout<<”bad options”<<endl;
continue;
}
}
}

Output:

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:2

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:2
list traversal yields:2

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:3

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:1
Enter the data:4

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:2
list traversal yields:2,3,4

Linked list…
1.Insert
2.Display
3..quit
Enter the choice:3
Ex:no:6
#include
enum Boolean{false,true};
template
void swap(T &x,T&y)
{
T t;
t=x;
x=y;
y=t;
}
template
void bubb(T &s,int size)
{
Boolean swapped=true;
for(int i=0;(i<size-1)&&swapped;i++)
{
swapped=false;
for(int j=0;js[j+1])
{
swapped=true;
swap(s[j],s[j+1]);
}
}
}
void main()
{
int num[25];
float floatnum[25];
int i,size;
cout<<”Program to sort..”<<endl;
cout<>size;
cout<<”Enter the element”<<endl;
for(int i=0;i>num[i];
bubb(num,size);
cout<<”sorted vector:”<<endl;
for(int i=0;i<size;i++)
cout<<num[i]<<” “;

cout<>size;
cout<<”Enter the element”<<endl;
for(int i=0;i>floatnum[i];
bubb(floatnum,size);
cout<<”sorted vector:”<<endl;
for(int i=0;i<size;i++)
cout<<floatnum[i]<<” “;
}

Output:
Program to sort
enter the size of interge 4
Enter the element
4
2
1
3
sorted vector
1 2 3 4

enter the size of float 4
Enter the element
4.2
2.1
1.5
3.3
sorted vector
1.5 2.1 3.3 4.2

Ex:no:7
#include
Const int MAX=5
class stack
{
protected:int stack[MAX]+1;
int stacktop;
public:
stack()
{
stacktop=0;
}
void push(int element)
{
++stacktop;stack[stacktop]=element;
}
void pop(int &element)
{
Element=stack[stacktop];–stacktop;
}
Class mystack:public stack
{
Public:int push(int element){if(stacktop<MAX)
{
Stack::push(element);return 1;
}cout<<”stack overflow”<0)
{
stack::pop(element);return 1;}
cout<<”stack overflow”<<endl;
return 0;
}
};
void main()
{
Mystack stack;
int element;
cout<<”Enter Integer data to put into the stack….”<<endl;
do
{
cout<>element;
}
while(stack.push(element));
cout<<”The stack contains…”<<endl;
while(stack.pop(element))
cout<<”pop:”<<element<<endl;
}

Output:
Enter Integer data to put into the stack…
Element to push?1
Element to push?2
Element to push?3
Element to push?4
Element to push?5
Element to push?6
Stackoverflow
The Stack contains
pop:5
pop:4
pop3
pop:2
pop:1
Stack underflow

Ex:no:9
#include
Class shape
{
int linestyle;
int fillcolor;
public:
virtual void draw( )
{
cout<<”Shape is drawn\n”;
}
};
Class circle:public shape
{
int radius;
int centrex;
int centrey;
public:
void draw( )
{
cout<<”circle is drawn”;
}
};
Class dot:public circle
{
int do;
public:
void sss( )
{
// write some code
}
};
int main()
{
shape some,*ptr;
circle rr;
some.draw();
ring.draw();
ptrshape=&ring;
cout<<”ptrshape is pointing to”<<typeid(*ptrshape).name()<<”\n”;
ptrshape=new do;
cout<<”ptrshape is pointing to”<<typeid(*ptrshape).name()<<”\n”;
}

Output:
Shape is drawn
Circle is drawn
Ptrshape is pointing to class circle
Ptrshape is pointing to class dot
Ex:no:10
#include
#include
#include
#include

fstream f1,f2;
class Complex
{
friend void main();
friend Complex calc(Complex x,Complex y,char op);
float r,im;

Complex(float r1=0,float im1=0)
{
r=r1;im=im1;
}

void out()
{
cout<<“(“<<r<<( (im<0)?”-i”:”+i” )<< (im<0?-im:im)<<“)”;
}

};
Complex calc(Complex x,Complex y,char opr)
{

switch(opr)
{
case’+’:
return Complex((x.r+y.r),(x.im+y.im) );
case’-‘:
return Complex( (x.r -y.r),(x.im -y.im) );
case’*’:
return Complex( (x.r*y.r)-(x.im*y.im), (x.r)*(y.im) +(x.im)*(y.r) );

}

}

void main()
{
clrscr();
f1.open(“C:\\file1.dat”,ios::in | ios::out | ios::trunc);
f2.open(“C:\\file2.dat”,ios::in | ios::out | ios::trunc);
if(f1==NULL || f2==NULL){ cout<<“\nFile Creation error..”;}

f1.seekg(0,ios::end);
char *s1;
time_t t1;
srand((unsigned) time(&t1));

float a=rand()%100,b=rand()%100;

char s,t,op;
f1<<“(“<<a<<( (b<0)?”-i”:”+i” )<< (b<0?-b:b)<<“)”; f1<<“+”; a=rand()%100,b=rand()%100; f1<<“(“<<a<<( (b<0)?”-i”:”+i” )<< (b<0?-b:b)<<“)\n”;
a=rand()%100,b=rand()%100;
f1<<“(“<<a<<( (b<0)?”-i”:”+i” )<< (b<0?-b:b)<<“)”; f1<<“-“; a=rand()%100,b=rand()%100; f1<<“(“<<a<<( (b<0)?”-i”:”+i” )<< (b<0?-b:b)<<“)\n”;
a=rand()%100,b=rand()%100;
f1<<“(“<<a<<( (b<0)?”-i”:”+i” )<< (b<0?-b:b)<<“)”; f1<<“*”; a=rand()%100,b=rand()%100; f1<<“(“<<a<<( (b<0)?”-i”:”+i” )<< (b<0?-b:b)<>t>>A.r>>s>>t>>A.im>>t; if(s==’-‘)A.im=-A.im;
f1>>op;
f1>>t>>B.r>>s>>t>>B.im>>t>>t;if(s==’-‘)B.im=-B.im;
A.out();cout<<op;B.out();
C=calc(A,B,op);cout<<“=”;C.out();
f2<<“(“<<C.r<<( (C.im<0)?”-i”:”+i” )<< (C.im<0?-C.im:C.im)<<“)\n”;
cout<<endl;
}while(!f1.eof());

f1.close();
f2.close();
getch();

}
File 1:
(62+i2)+(47+i68)
(62+i89)-(85+i66)
(63+i94)*(10+i80)

(109+i70)
(-83+i23)
(-7490+i1180)

Ex:no:9
#include
Class shape
{
int linestyle;
int fillcolor;
public:
virtual void draw( )
{
cout<<”Shape is drawn\n”;
}
};
Class circle:public shape
{
int radius;
int centrex;
int centrey;
public:
void draw( )
{
cout<<”circle is drawn”;
}
};
Class dot:public circle
{
int do;
public:
void sss( )
{
// write some code
}
};
int main()
{
shape some,*ptr;
circle rr;
some.draw();
ring.draw();
ptrshape=&ring;
cout<<”ptrshape is pointing to”<<typeid(*ptrshape).name()<<”\n”;
ptrshape=new do;
cout<<”ptrshape is pointing to”<<typeid(*ptrshape).name()<<”\n”;
}

Output:
Shape is drawn
Circle is drawn
Ptrshape is pointing to class circle
Ptrshape is pointing to class dot

1 d)Class with friend function

#include
#include
class vector
{
int a[10][10],b[10][10],c[100][100],m,n,p,q;
public:
void input(void);
void display(void);
friend void mul(vector &);
};
void vector :: input(void)
{
int i,j;
cout<<“enter a matrix values”;
cout<>m>>n;
cout<<” enter the A matrix value”;
for(i=0;i<m;i++)
{
for(j=0;j>a[i][j];
}
cout<<“enter b matrix values”;
cout<>p>>q;
cout<<” enter the A matrix value”;
for(i=0;i<p;i++)
{
for(j=0;j>b[i][j];
}

}

void vector :: display(void)
{
int i,j;
cout<<” the resultant matrix is\n”;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<“\t”;
cout<<“\n”;
}

}

void mul(vector &v )
{
int k,i,j;
for(i=0;i<v.m;i++)
{
for(j=0;j<v.n;j++)
{
v.c[i][j]=0;
for(k=0;k<v.n;k++)
v.c[i][j]=v.c[i][j]+v.a[i][k]*v.b[k][j];
}
}
}

int main()
{
clrscr();
vector v1;
v1.input();
mul(v1);
v1.display();
getch();
return 0;
}

Advertisements

9 thoughts on “OOPS Lab Programs by praveena [veenagopi2005@yahoo.co.in]

  1. 1.b) CLASS WITH STATIC MEMBER FUNCTION
    Aim
    To implement static member function in class.

    Objective
    Static member function of class is shared by all the instances of the class.A static member functions cannot access auto members of a class.
    Exercises
    1. Create class TEST with static member function.
    2. Invoke the static member function using classname with scope
    resolution operator and function name.
    3. Display the value

    The above program had a incomplete coding so kindly display the proper coding

  2. Ex:No:8
    Implementation of Graph
    Aim:
    To implement the graph and to obtain a minimum cost spanning tree.
    Objective
    To know about the concept of Graph along with minimum spanning tree.
    Exercise:
    1.Create two classes as POINT and ARC.
    2.Define a Graph class which represents the collection of Point and Arc objects.
    3.Write a method to find a minimum cost spanning tree in a graph.

    The above program coding is missing in your set of programs., So kindly update it also

  3. hello,

    i try to run 9th and 10th program form oops lab syllabus… But those programs didnt run in turboc++ as well as in dev c++.. pls give correct program…. or what should i do to run that programs…….. pls send it as soon as possible…..

  4. All the programs are good but not very perfect to run!!!!!!
    But it has some typing mistakes!!!
    Better to avoid that!!!!
    Anyway thanks for some good effort guys!!!!!!!

    • all the function overloading programs are examples for polymorphism (static binding or compile time polymorphism)
      all the virtual function programs are examples for dynamic binding (run-time polymorphism)
      ======
      Message Passing:
      A message is simply a method call on an object. …
      all arguments are evaluated before the message is sent….

Comments are closed.