OOT ( Object Oriented Technology) Lab Practical
1)Call By value
#include<conio.h>
#include<iostream.h>
void swap(int a, int b)
{
int t=a;
a=b;
b=t;
cout<<"after swap the value of no."<<m<<n;
}
void main()
{
clrscr();
int m,n;
cout<<"enter any 2 no.";
cin>>m>>n;
swap(&m,&n);
cout<<"after swap the value of no."<<m<<n;
getch();
}
2)Call by Reference
#include<conio.h>
#include<iostream.h>
void swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
void main()
{
clrscr();
int m,n;
cout<<"enter any 2 no.";
cin>>m>>n;
swap(m,n);
cout<<"after swap the value of no."<<m<<n;
getch();
}
3) Call by Pointer
#include<conio.h>
#include<iostream.h>
void swap(int *a, int *b)
{
int t=*a;
*a=*b;
*b=t;
}
void main()
{
clrscr();
int m,n;
cout<<"enter any 2 no.";
cin>>m>>n;
swap(&m,&n);
cout<<"after swap the value of no."<<m<<n;
getch();
}
4)Addition of time in the Hour and minutes format use of objects as functions arguments
#include<conio.h>
#include<iostream.h>
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void puttime(void)
{
cout<<hours<<"hours and";
cout<<minute<<"minute"<<"\n";
}
void sum(time, time); };
void time::sum( time t1, time t2)
{
minutes= t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
voidmain()
{
clrscr();
time T1, T2,T3;
T1.gettime(2,45);
T2.gettime(3,30);
T3.sum(T1,T2);
cout<<"T1=";T1.puttime();cout<<"\n";
cout<<"T2=";T2.puttime();cout<<"\n";
cout<<"T3=";T3.puttime();
getch();
}
5)Create a class called employee that contain name (an array of character) and an employee number (type long). Include a member function called getdata() to get data from user for insertion into object and another function called putdata () to display the data.
#include<conio.h>
#include<iostream.h>
class employess
{
char name [30];
unsigned long employee_number;
public:
void getdata(void)
{
cout<<"enter name";
cin>>name;
cout<<"enter employee no.";
cin>>employee_number;
}
void putdata(void)
{
cout<<"Name: "<<name<<"\n";
cout<<"Employee_number: "<<employee_number<<"\n";
}
};
const int a=5;
void main()
{
clrscr();
employess ep[a];
for (int i=1;i<=a;i++)
{
cout<<"enter the details of employess: "<<i<<"\n";
ep[i].getdata();}
for ( i=1;i<=a;i++)
{
cout<<"The employess: "<<i<<"\n";
ep[i].putdata();}
getch();
}
6)Overloading using Binary Operators
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<x<<y;
}
complex add(complex C1)
{
complex temp;
temp.x=x+C1.x;
temp.y=y+C1.y;
return temp;
}
};
void main()
{
clrscr();
complex c1,c2,c3;
c1.setdata(3,4);
c2.setdata(4,6);
c3=c1.add(c2);
c3.showdata();
getch();
}
7)Overloading using Unary Operators
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<x<<y;
}
complex operator -()
{
complex temp;
temp.x=-x;
temp.y=-y;
return temp;
}
};
void main()
{
clrscr();
complex c1,c2;
c1.setdata(3,4);
c2=-c1;//c1.operatot -();
c2.showdata();
getch();
}
8)Friend Function
#include <iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
void setdata(int a, int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<x<<y;
}
friend void fun(complex);
};
void fun (complex C1)
{
cout<<"the sum of 2 no."<<C1.x+C1.y<<"\n";
}
void main()
{
clrscr();
complex c1;
c1.setdata(3,4);
fun(c1);
c1.showdata();
getch();
}
10) Constructor
#include<conio.h>
#include<iostream.h>
class complex
{
int x,y;
public:
complex()
{};
complex(int a)
{
x=y=a;}
complex(int a, int b){
x=a;y=b;}
friend complex sum(complex,complex);
friend void show(complex);
};
complex sum (complex c1, complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;
}
void show(complex c)
{
cout<<c.x<<"+j"<<c.y<<"\n";
void main()
{
clrscr();
complex A(3,4);
complex B(3);
complex C;
C=sum(A,B);
cout<<"A=";
show(A);
cout<<"B=";
show(B);
cout<<"C=";
show(C);
//Another way
complex P,Q,R;
P=complex(10,12);
Q=complex(13,15);
R=sum(P,Q);
cout<<"\n";
cout<<"P=";
show(P);
cout<<"Q=";
show(Q);
cout<<"R=";
show(R);
getch();
}
11)Destructor
#include<conio.h>
#include<iostream.h>
int count=0;
class alpha {
public:
alpha()
{
count++;
cout<<"\nNo. of object created"<<count;
}
~alpha()
{
cout<<"\nNo. of object destroyed"<<count;
count--; } };void main()
{
alpha A1, A2, A3, A4;
{
cout<<"\n Enter Block \n";
alpha A5;
}
{
cout<<"\n Enter Block \n";
alpha A6;
}
cout<<"\n\n Re-enter Main\n";
getch();
}
12)Single Inheritance
#include<conio.h>
#include<iostream.h>
class First
{
private:
int prival;
protected:
int proval;
public:
int pubval;
};
class Second:public First
{
int result;
public:
void set()
{
proval=100;
pubval=200;
result=proval+pubval;
}
void display()
{
cout<<"\n The result is:"<<result;
}
}
void main()
{
clrscr();
Second obj1;
obj1.set();
obj1.display();
getch();
}
Comments
Post a Comment