Vous êtes sur la page 1sur 29

OBJECT ORIENTED

PROGRAMING USING C++

Name:- Tanay

Menghani
Roll no:- 08516412814

1.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{char name[10],address[10];
int age;
public:
void getdata()
{
gets(name);
cin>>age;
gets(address);}
void putdata()
{
puts(name);
cout<<""<<age<<"";
puts(address);
}
};
void main()
{
student s[10],*sptr;
sptr=s;
for(int i=0;i<10;i++)
{sptr->getdata();sptr++;}
sptr=s;
for(i=0;i<10;i++)
{sptr->putdata();sptr++;}
getch();
}

2.
#include<iostream.h>
#include<conio.h>
void neg(int *c)
{*c=-*c;
}
void neg(int &a)
{a=-a;
}
void main()
{int a,*b;
cout<<"Enter a integer"<<endl;
cin>>a;
b=&a;
neg(b);
cout<<endl<<"By pointer"<<a<<endl;
cout<<"Enter a integer"<<endl;
cin>>a;
neg(a);
cout<<endl<<"By Reference"<<a<<endl;
getch();
}

3.
#include<iostream.h>
#include<conio.h>
class time
{
private:
int hrs,min,sec;
public:
void gettime(int a,int b,int c)
{
hrs=a;min=b;sec=c;
}
time addtime(time a)
{
time b;
b.hrs=hrs+a.hrs;
b.min=min+a.min;
b.sec=sec+a.sec;
b.min=b.min+(b.sec)/60;
b.sec=(b.sec)%60;
b.hrs=b.hrs+(b.min)/60;
b.min=(b.min)%60;
b.hrs=(b.hrs)%24;
return(b);
}
void disptime()
{
cout<<hrs<<":"<<min<<":"<<sec<<endl;
}

time()
{hrs=0;min=0;sec=0;
}
};
void main()
{clrscr();
time t1,t2,t3;
int a,b,c;
cin>>a>>b>>c;
cout<<"T1 : ";
t1.gettime(a,b,c);
t1.disptime();
cin>>a>>b>>c;
cout<<endl<<"T2 : ";
t2.gettime(a,b,c);
t2.disptime();
t3=t1.addtime(t2);
cout<<endl<<"sum : ";
t3.disptime();
getch();
}

10.BUBBLE SORT
#include<conio.h>
#include<iostream.h>
template<class bubble>
void bubble(bubble a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
bubble element;
element = a[i];
a[i] = a[j];
a[j] = element;

}
}
}
}
void main()
{
int a[6]={1,2,3,4,4,3};
char b[4]={'s','b','d','e'};
clrscr();
bubble(a,6);
cout<<"\nSorted Order Integers: ";
for(int i=0;i<6;i++)
cout<<a[i]<<"\t";
bubble(b,4);
cout<<"\nSorted Order Characters: ";
for(int j=0;j<4;j++)
cout<<b[j]<<"\t";
getch();
}

10 b. SELECTION SORT

#include<stdlib.h>
#include <iostream.h>
void selectsort(int A[],int n)
{
int i,j,min;
for(i=0;i<n;i++){
min=i;
for(j=i+1;j<n;j++)
if(A[j]<A[min]) min=j; //find min value
//swapping
int temp=A[i];
A[i]=A[min];
A[min]=temp;
}
}
int main(int argc, char *argv[])
{
clrscr();

int A[]={23,4,45,22,12,2};

selectsort(A,6);
for(int i=0;i<6;i++)
cout<<A[i]<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

14.#include<fstream.h>
#include<conio.h>
#include<stdio.h>

class PHYSICAL_EDUCATION
{
private:
char name_of_game[30];
float cost;
float duration;
char place[30];
char finals[30];
public:
void get_data();
void putdata();
friend ostream& operator<<(ostream &stream,PHYSICAL_EDUCATION
&OBJ);
friend istream& operator>>(istream &stream,PHYSICAL_EDUCATION
&OBJ);
};
void main()
{
clrscr();
cout.precision(2);
PHYSICAL_EDUCATION *OBJ;
PHYSICAL_EDUCATION *TEMP;
fstream DATA;

fstream BIN;
DATA.open("Data.txt",ios::out|ios::in);
BIN.open("Bin.txt",ios::out|ios::in|ios::binary);
int size;
cout<<"Enter number of records: ";
cin>>size;
if(size <= 0)
cout<<"\nWrong Entry...";
else
{
clrscr();
OBJ = new PHYSICAL_EDUCATION[size];
TEMP = new PHYSICAL_EDUCATION[size];
for(int i=0;i<size;i++)
{
cout<<"Record #"<<i+1<<"\n";
OBJ[i].get_data();
cout<<"\n";
DATA<<OBJ[i];
BIN<<OBJ[i];
}
clrscr();
cout<<"ASCII File:\n";
for(i=0;i<size;i++)
{
cout<<"\n\nRecord #"<<i+1;
DATA>>TEMP[i];
TEMP[i].putdata();
}
getch();
clrscr();
cout<<"Binary File:\n";
for(i=0;i<size;i++)
{
cout<<"\n\nRecord #"<<i+1;
BIN>>TEMP[i];
TEMP[i].putdata();
}
}
getch();
}
void PHYSICAL_EDUCATION::get_data()
{
cout<<"Game
: ";
gets(name_of_game);
cout<<"Cost
: ";
cin>>cost;
cout<<"Duration (hours) : ";
cin>>duration;

cout<<"Place
gets(place);
cout<<"Finals
gets(finals);

: ";
: ";

}
void PHYSICAL_EDUCATION::putdata()
{
cout<<"\nGame
: "<<name_of_game;
cout<<"\nCost
: "<<cost;
cout<<"\nDuration (hours) : "<<duration;
cout<<"\nPlace
: "<<place;
cout<<"\nFinals
: "<<finals;
}
ostream& operator<<(ostream &stream,PHYSICAL_EDUCATION &OBJ)
{
stream<<OBJ.name_of_game<<" ";
stream<<OBJ.cost<<" ";
stream<<OBJ.duration<<" ";
stream<<OBJ.place<<" ";
stream<<OBJ.finals<<" "<<flush;
return stream;

istream& operator>>(istream &stream,PHYSICAL_EDUCATION &OBJ)


{
stream>>OBJ.name_of_game;
stream>>OBJ.cost;
stream>>OBJ.duration;
stream>>OBJ.place;
stream>>OBJ.finals;
return stream;
}

7.#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class _2D
{
private:
int num1;
int num2;
int dec1;
int dec2;
float value;
short int figure;
void area(float radius);
void area(int side);
void area(int length,int breadth);
void area(float base,float height);
public:
friend ostream& operator<<(ostream &stream,_2D &OBJ);
friend istream& operator>>(istream &stream,_2D &OBJ);
void* operator new(size_t size);
void operator delete(void *ptr);
void _2D::setfigure(short int figure);
int getnum1();
int getnum2();
float getdec1();
float getdec2();

};
void main()
{
clrscr();
_2D OBJ;
_2D *PTR;
char ch = 0;
cout.precision(2);
while(ch != '8')
{
cout<<"\t\t\t::::::::::MENU::::::::::";
cout<<"\n\n\n1) Circle";
cout<<"\n2) Square";
cout<<"\n3) Rectangle";
cout<<"\n4) Triangle";
cout<<"\n5) Save to pointer";
cout<<"\n6) Display Pointer";
cout<<"\n7) Delete Pointer";
cout<<"\n8) Exit";
cout<<"\n\nSelect among these: ";
ch = getch();
cout<<ch;
cout<<"\n----------------------\n";
switch(ch)
{
case '1':
case '2':
case '3':
case '4': OBJ.setfigure(ch-48);
cin>>OBJ;
cout<<OBJ;
break;
case '5': PTR = new _2D();
PTR = &OBJ;
break;
case '6': cout<<*PTR;
break;
case '7': delete(PTR);
break;
case '8': cout<<"\nProgram will exit...";
break;
default: cout<<"\nWrong Choice...";
}

cout<<"\n\nPress any key to continue...";


getch();
clrscr();

}
}

void _2D::area(float radius)


{
value = 3.14*radius*radius;
}
void _2D::area(int side)
{
value = side*side;
}
void _2D::area(int length,int breadth)
{
value = length*breadth;
}
void _2D::area(float base,float height)
{
value = 0.5*base*height;
}
void* _2D::operator new(size_t size)
{
void *ptr;
ptr = malloc(size);
if(!ptr)
cout<<"\nMemory not allocated somehow...";
else
cout<<"\nMemory Allocated...";
return ptr;
}
void _2D::operator delete(void *ptr)
{
cout<<"\nMemory Freed...";
free(ptr);
}
ostream& operator<<(ostream &stream,_2D &OBJ)
{
stream<<"Area: "<<OBJ.value<<" square units";
return stream;
}
istream& operator>>(istream &stream,_2D &OBJ)
{
if(OBJ.figure == 1)
{
cout<<"Enter radius: ";
stream>>OBJ.dec1;

OBJ.area(OBJ.getdec1());

if(OBJ.figure == 2)
{
cout<<"Enter side: ";
stream>>OBJ.num1;
OBJ.area(OBJ.getnum1());
}
if(OBJ.figure == 3)
{
cout<<"Enter length: ";
stream>>OBJ.num1;
cout<<"Enter length: ";
stream>>OBJ.num2;
OBJ.area(OBJ.getnum1(),OBJ.getnum2());
}
if(OBJ.figure == 4)
{
cout<<"Enter base: ";
stream>>OBJ.dec1;
cout<<"Enter height: ";
stream>>OBJ.dec2;
OBJ.area(OBJ.getdec1(),OBJ.getdec2());
}
return stream;

void _2D::setfigure(short int figure)


{
this->figure = figure;
}
int _2D::getnum1()
{
return num1;
}
int _2D::getnum2()
{
return num2;
}
float _2D::getdec1()
{
return dec1;
}
float _2D::getdec2()
{
return dec2;
}

12 a.)void main()
{
clrscr();
student *ptr;
char ch='y';
int i=0;
ptr=S;
do
{
ptr->getdata();
cout<<"\n Continue to next entry (y/n) - ";
cin>>ch;
i++;
ptr=&S[i];
}
while(ch=='y');
int j=i;

i=0;
while(j>0)
{
ptr=&S[i];
ptr->putdata();
i++;
j--;
}getch();}

12 B.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
//Question 1
class student
{
char name[20];
int age;
char address[20];
public :
void getdata();
void putdata();

};
void student::getdata()
{
cout<<"\nEnter name : ";
gets(name);
cout<<"\n\Enter age : ";
cin>>age;
cout<<"\nEnter address : ";
gets(address);
}
void student::putdata()
{
cout<<" \n\tDetails of student - \n "<<name<<"\t\t"<<age<<"\t"<<address;
}
student S[10];
#include<conio.h>
#include<iostream.h>
int f1(int*x)
{
int t;
t=(-1)*(*x);
return t;
}
int f2(int &y)
{
int t;
t=-1*y;

return t;
}
void main()
{
clrscr();
int N,m;
cout<<" \n\t Enter any number : ";
cin>>N;
cout <<"\n\t using pointer parameter to negate the number ...." ;
m=f1(&N);
cout<<"\t : "<<m;
cout<<"\n\t using reference parameter to negate the number ....";
m=f2(N);
cout<<"\t : "<<m;
getch();}

12 C.
#include<iostream.h> #include<conio.h>
class Time
{ short int hh,mm,ss;
public:

Time()
{
hh=mm=ss=0;
}
void gettime(int i,int j,int k)
{
hh=i;
mm=j;
ss=k;
}
void display(void)
{
cout<<" \tTime is - "<<hh<<":"<<mm<<":"<<ss<<" hrs\n";
}
Time addtime(Time t1);
};
Time Time::addtime (Time t1)
{
Time t3;
int sq,mq;
sq=(t1.ss+ss)/60;
t3.ss=(t1.ss+ss)%60;
mq=(sq+t1.mm+mm)/60;
t3.mm=(sq+t1.mm+mm)%60;
t3.hh=mq+t1.hh+hh;
return t3;
}

void main ()
{
clrscr();
Time tm1,tm2,tm3;
int a,b,c;
cin>>a>>b>>c;
tm1.gettime(a,b,c);
cout<<"\ntm 1 : ";
tm1.display();
cin>>a>>b>>c;
tm2.gettime(a,b,c);
cout<<"\ntm 2 : ";
tm2.display();
tm3=tm2.addtime(tm1);
cout<<" \n The addition of two times gives tm3 : ";
tm3.display();
getch();
}

13.
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
void main()
{
clrscr();
int i=20;
char *str="SOFTWARE";
for(int l=0;l<8;l++)
{
cout<<setw(i--);
for(int j=0;j<=l;j++)
{
cout<<" ";
cout<<str[j];
}
cout<<"\n";

}
getch();
}

9.
#include <iostream.h>
#include<conio.h>
class Shape
{ public:
void setWidth(int w) { width = w; }
void setHeight(int h) {height = h; }
void setlength(int l) {length = l; }
void perimeter()
{ cout << "perimeter is :"<<2*(width+height+length)<<endl; }
virtual int area() {return (width * height); }
virtual void vol()=0;
protected:
int width; int height; int length;};
class Rectangle: public Shape
{public: Rectangle() { height = 0; }
int area() { return (width * length); }
void vol()
{ cout << "volume is :"<<width*height*length<<endl;}};
class Point: public Shape
{public: Point() { width = 0; height = 0; length = 0;}
virtual int area() {return (width * height);}
virtual void vol()
{cout << "volume is :"<<width*height*length<<endl;}};
class Line: public Point
{public: Line(){ height = 0; length = 0; }
virtual int area(){return (width * height);}
virtual void perimeter()
{cout<<"perimeter is :"<<width<<endl; }
virtual void vol()

{cout << "volume is :"<<width*height*length<<endl;}};


class Square: public Shape
{public: Square() { height=0; }
virtual void setWidth(int w) {width =length = w; }
virtual int area() {return (width * length); }
virtual void vol()
{cout << "volume is :"<<width*height*length<<endl; }};
class Cube: public Square
{public: Cube(int w) {width =w;length=height=w;}
int area() { return (6*width * width);}
void vol()
{cout << "volume is :"<<(width*length*height)<<endl;}
void perimeter(){cout << "perimeter is :"<<12*width<<endl;}};
class Circle: public Shape
{public: Circle() { length=0; }
virtual void setWidth(int w) {width = w; }
virtual int area() {return (width * width * 3.14);}
virtual void vol() {cout << "volume is :"<<length<<endl;}
virtual void perimeter(){cout<<"perimeter is :"<<2*3.14*width<<endl;}};
class Sphere: public Circle
{ public: void setWidth(int w) {width = w;}
int area(){return (width * width * 3.14*4);}
void vol()
{ cout << "volume is :"<<(4/3)*3.14*width*width*width <<endl;}
void perimeter(){ cout<<"perimeter is :"<<2*3.14*width<<endl;}};
int main()
{ clrscr();
Shape *sptr;
Rectangle Rect;
Rect.setWidth(5); Rect.setlength(7);
cout << "area of rectangle :" << Rect.area() << endl;
Rect.vol(); Rect.perimeter();
Square sq;
sq.setWidth(5);
cout<<"\nArea of Square :"<<sq.area()<<endl;
sq.perimeter(); sq.vol();
Point pt;
cout<<"\nArea of point :"<<pt.area()<<endl;
pt.perimeter(); pt.vol();
Line l;
l.setWidth(10);
cout<<"\nArea of line :"<<l.area()<<endl;
l.perimeter(); l.vol();
Circle cir;
cir.setWidth(8);
cout<<"\nArea of Circle :"<<cir.area()<<endl;
cir.perimeter(); cir.vol();
Cube cue(6);
cout<<"\nArea of Cube :"<<cue.area()<<endl;
cue.perimeter(); cue.vol();

Sphere sph;
sph.setWidth(11);
cout<<"\nArea of Sphere :"<<sph.area()<<endl;
sph.perimeter(); sph.vol();
getch();
return 0;}

6.#include<iostream.h>
#include<conio.h>
#include<math.h>
class complex

{float i,r;
public:
complex()
{r=0;i=0;}
complex(int a,int b)
{r=a;i=b;}
complex(complex &c)
{r=c.r;i=c.i;}
friend istream &operator>>(istream &input,complex &c )
{ input>>c.r>>c.i;
return input;
}
friend ostream &operator<<(ostream &output,complex &c)
{output<<c.r<<"+("<<c.i<<")i";
return output;
}
int operator==(complex c)
{
if((i==c.i)&&(r==c.r))
{return 1;}
else
{return 0;}
}
int operator!=(complex c)
{
if((i!=c.i)||(r!=c.r))
{return 1;}
else
{return 0;}
}
complex operator+(complex c)
{
complex temp;
temp.i=i+c.i;
temp.r=r+c.r;

return(temp);
}
complex operator-(complex c)
{
complex temp;
temp.i=i-c.i;
temp.r=r-c.r;
return(temp);
}
complex operator*(complex c)
{
complex temp;
temp.i=i*c.i-r*c.r;
temp.r=r*c.i+i*c.r;
return(temp);
}
complex operator/(complex c)
{
complex temp;
temp.i=(i*c.i+r*c.r)/(c.i*c.i+c.r*c.r);
temp.r=(i*c.r-r*c.i)/(c.i*c.i+c.r*c.r);
return(temp);
}
float operator!()
{float a;
a=sqrt(i*i+r*r);
return(a);}
};
void main()
{complex c1(3,4),c2(12,13);
cout<<"Default values of \n c1:"<<c1<<" c2:"<<c2;
cout<<"\nEnter c1:";
cin>>c1;
cout<<"\nEnter c2:";
cin>>c2;

cout<<"Modified values of \n c1:"<<c1<<" c2:"<<c2;


cout<<"\nSum="<<(c1+c2);
cout<<"\nDifference="<<(c1-c2);
cout<<"\nProduct="<<(c1*c2);
cout<<"\nDivison="<<(c1/c2);
cout<<"\nAbsolute value of |c1| ="<<!c1<<" |c2| ="<<!c2;
getch();
}

17.
#include<iostream.h>
#include<conio.h>
template <class T>
void sort(T a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (a[j]<a[i])
{
temp=a[i];

a[i]=a[j];
a[j]=temp;
}
}
}
}
void main()
{
int a[100],i,n;
clrscr();
cout<<"Enter the number of elements:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,n);
cout<<"After sorting:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
}

Vous aimerez peut-être aussi