22

Click here to load reader

OBJECT ORIENTED PROGRAMMIG (OOP's)

Embed Size (px)

Citation preview

Page 1: OBJECT ORIENTED PROGRAMMIG (OOP's)

OBJECT ORIENTED PROGRAMMIG

LAB FILE

Submitted To: Submitted By: MRS. ARCHANA SANDHU SHAHRUKHANE ALAM Roll no: 13017001009

Branch: C.S.E 5th Sem

PURAN MURTI COLLEGE OF ENGINEERING, KAMI, SONEPAT DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Page 2: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 1

//write program for a function called power() that takes a double value for n and an int value for p and return result as double value.

#include <iostream>#include <conio.h>

using namespace std;

double power(double ,int); //Power variable of type double which takes arguments (double, int)

int main(){ double x; //initializing the variable x and i int i; cout<<"please enter the number"<<endl; cin>>x; cout<<"please enter the integer power that you want this number raised to"<<endl; cin>>i; cout<<x<<" raise to power "<<i<<" is equal to ="<<power(x,i)<<endl; return 0;

}

//definition of the function Power

double power(double x, int power){ double result; int i; result =1.0; for (i=1; i<=power;i++) { result = result*x; } return(result);}

Output:-

Page 3: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 2

//PROGRAM TO ADD COORDINATES OF TWO POINTS USING STRUCTURE.

#include<conio.h>

#include<iostream>

using namespace std;

int main()

{

struct point

{

int x,y;

}p1,p2,p3;

cout<<"Enter coordinates of P1: ";

cin>>p1.x>>p1.y;

cout<<"Enter coordinates of P2: ";

cin>>p2.x>>p2.y;

p3.x=p1.x+p2.x;

p3.y=p1.y+p2.

cout<<" coordinates of P1+P2 are: "<<p3.x<<" & " << p3.y;

getch();

return 0;

}

Page 4: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 2

output:-

Page 5: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 3

//write a program for the equivalent of a four function calculator.

#include<iostream>#include<conio.h>#include<stdio.h>using namespace std;void output(float l){cout<<"Answer := ";if(l!=NULL)cout<<l<<endl;elsecout<<"ERROR"<<endl;}void cal(float x,char c,float y){float l;switch(c){case '+' : l=x+y;break;case '-' : l=x-y;break;case '/' : if(y!=0)l=x/y;elsel=NULL;break;case '*' : l=x*y;break;default: l=NULL;break;}output(l);}void read(){float x,y;

Page 6: OBJECT ORIENTED PROGRAMMIG (OOP's)

char c;cout<<"Enter first number,operator,second number : "<<endl;cin>>x;cin>>c;cin>>y;cal(x,c,y);}int main(){char c='y';while(c=='y'||c=='Y'){read();cout<<"DO another (y|n) :";fflush(stdin);cin>>c;}return 0;}

Output:-

Page 7: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 4

//write a program for create two class DM and DB which store the value of distance and use a friend function to carry out the addition operation.

#include<iostream>#include<conio.h>using namespace std;class DM;class DB{

float f,inch;public:

DB(){}DB(float a,float b){

f=a;inch=b;

}void putdata(){

cout<<"\nFEET:"<<f<<", INCH:"<<inch;}float getf(){

return f;}

float getInch(){

return inch;}

};

class DM{

float m,cm;public:

DM(){}DM(float a,float b)

Page 8: OBJECT ORIENTED PROGRAMMIG (OOP's)

{m=a;cm=b; }

void putdata(){

cout<<"Meter="<<m<<", Centimeter="<<cm<<endl;}friend DM operator+(DM &t1,DB &t2){

DM t3;DM temp = t2;

t3.m = t1.m + temp.m;t3.cm = t1.cm + temp.cm;

return t3;}

DM(DB & t1){

float t=t1.getf()*30;t+=t1.getInch()*30/12;m=t/100;cm=(int)t%100;

}};

int main(){

DM dm1(6,6);DB db(3,4);

DM dm2 = dm1 + db;

dm2.putdata();

getch();return 0;

}

Page 9: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 4Output:-

Page 10: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 5

//write a program to create a class rational which represent a numerical value by two double values- NUMERATOR & DENOMINATOR.

#include<iostream>#include<conio.h>

class rational{private: int hcf(int,int); double num,den;public: rational() { num=1; den=1; } rational(int x,int y) { num=x; den=y; } rational operator+ (rational &r) { rational temp; temp.num=num*(r.den)+den*(r.num); temp.den=den*(r.den); return temp; } void reduce() { rational temp1; int h=hcf(num,den); temp1.num=num/h; temp1.den=den/h; cout<<endl<<"After reduction: "<<temp1.num<<"/"<<temp1.den; } friend ostream & operator<<(ostream & x1,rational &r4);

Page 11: OBJECT ORIENTED PROGRAMMIG (OOP's)

friend istream & operator>>(istream & x2,rational &r5);};int rational::hcf(int a,int b) { int r=a%b; while(r) { a=b; b=r; r=a%b; } return b; }

ostream & operator<<(ostream & x1,rational &r4){x1<<"Answer is: "<<r4.num<<"/"<<r4.den;return x1;}istream & operator>>(istream & x2,rational &r5){cout<<"Enter values numerator & denominator: ";x2>>r5.num;x2>>r5.den;return x2;}int main(){

rational r1(5,6),r2(0,0),r3(0,1);cin>>r2;r3=r1+r2;cout<<r3;r3.reduce();getch();return 0;}

Page 12: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 5

Output:-

Page 13: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 6

//write a program for a database regarding to indoor patients with a structure to store the data and base class to store the information of patients.

#include<iostream>#include<conio.h>using namespace std;struct date{int dd;int mm;int yy;};struct patient{char name[20];struct date doa;char diseases[20];struct date dod;};class hospital{protected:struct patient p[50];int i,n;public:void get_data(){cout<<"Enter no of patients=";cin>>n;for(i=0;i<n;i++){cout<<endl<<"Enter name of the patient number"<<i+1<<":";cin>>p[i].name;cout<<endl<<"Enter day of admission=";cin>>p[i].doa.dd;cout<<"Enter month of admission=";cin>>p[i].doa.mm;cout<<"Enter year of admission=";

Page 14: OBJECT ORIENTED PROGRAMMIG (OOP's)

cin>>p[i].doa.yy;cout<<"Enter diseases of the patient=";cin>>p[i].diseases;//cout<<"Enter the date of discharge of the patient";cout<<endl<<"Enter day of discharge=";cin>>p[i].dod.dd;cout<<"Enter month of discharge=";cin>>p[i].dod.mm;cout<<"Enter year of discharge=";cin>>p[i].dod.yy;}}void display(){for(i=0;i<n;i++){cout<<endl<<"Name of the patient is="<<p[i].name;cout<<endl<<"Date of admission of the patient is="<<p[i].doa.dd<<"/"<<p[i].doa.mm<<"/"<<p[i].doa.yy; cout<<endl<<"Diseases: "<<p[i].diseases; cout<<endl<<"Date of discharge: "<<p[i].dod.dd<<"/"<<p[i].dod.mm<<"/"<<p[i].dod.yy; }}};class dage:public hospital{protected: int age[50];public:void getage(){cout<<endl<<"Enter no. of patients again same as value entered before";cin>>n;for(i=0;i<n;i++){cout<<"Enter age of patient no"<<i+1<<":";cin>>age[i];}}void displayd(){int j=0;for(i=0;i<n;i++)

Page 15: OBJECT ORIENTED PROGRAMMIG (OOP's)

{if(age[i]<=12){j=j+1;cout<<"Patient(s) whose age is less than 12 : ";cout<<endl<<"Name: "<<p[i].name;cout<<endl<<"Date of admissiom:"<<p[i].doa.dd<<"/"<<p[i].doa.mm<<"/"<<p[i].doa.yy; cout<<endl<<"Diseases of the patient:"<<p[i].diseases; cout<<endl<<"Date of discharge :"<<p[i].dod.dd<<"/"<<p[i].dod.mm<<"/"<<p[i].dod.yy; cout<<endl<<"Age of the patient is : "<<age[i];}} }};int main(){

dage a1;a1.get_data();a1.display();a1.getage();a1.displayd();getch();return 0;}

Page 16: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 6

Output:-

Page 17: OBJECT ORIENTED PROGRAMMIG (OOP's)

Program No – 7

//write a program to imagine a tollbooth with a class tollbooth with constructor initializes both to 0.

#include<iostream>#include<conio.h>using namespace std;

const char ESC=27; /*27 is the ASCII code generated, when we press 'ESCAPE' button.*/const double toll=.5;class tollbooth{ private: unsigned int total_cars; double total_cash; public: tollbooth() { total_cars=0; total_cash=0; } void paying_car() { total_cars+=1; total_cash+=toll; } void nopay_car() { total_cars+=1; } void display() { cout<<endl<<"Total cars: "<<total_cars<<endl<<"Total toll collected: "<<total_cash; }};

int main() {

Page 18: OBJECT ORIENTED PROGRAMMIG (OOP's)

tollbooth booth; char ch; cout<<endl<<"Press 0 for NON PAYING CAR."; cout<<endl<<"Press 1 for TOLL PAYING CAR."; cout<<endl<<"Press ESC to EXIT the PROGRAM:"<<endl;

do { ch=getche(); /*getche() displays the character that we just typed.*/ if(ch=='0') booth.nopay_car(); if(ch=='1') booth.paying_car(); }while(ch!=ESC); booth.display(); getch(); }

Output:-

Page 19: OBJECT ORIENTED PROGRAMMIG (OOP's)