Sie sind auf Seite 1von 42

GURBAKSHI Program 1:

09/cse/3621

raising a no. n to a power p is the same as multiplying n by itself p times.write a funct. Called power() that takes a double value for n and an int value for p, and returns the result as double value. Use a default argument of 2 for pp, so that if this argument is omitted,the no. will be squared. Write a main() funct. That gets values from the user to test this funct.

-1-

GURBAKSHI Program 1: # include<iostream.h> double power (double n,int p=2) { int t=1; int i; for(i=1;i<=p;i++) { t=t*n; } return t; } int main() { double n,result1=0,result2=0; int p; cout<<"enter no."; cin>>n; result1=power(n); cout<<"result1 is"<<result1; cout<<"enter raising power"; cin>>p; result2=power(n,p); cout<<"result2 is"<<result2; return 0; }

09/cse/3621

-2-

GURBAKSHI Output: enter no. 4 result1 is16 enter raising power 2 result2 is16 enter no. 6 result1 is36 enter raising power 4 result2 is 1296

09/cse/3621

-3-

GURBAKSHI program 2:

09/cse/3621

a point on the 2 d plane can be represented by two no.an x coordinate and a y coordinate. For eg., (4,5) represents a point 4 units to the right of the origin along the x axis and 5 units up the y axis. The sum of two points can be defined as a new pt. whose x coordinate is the sum of the x coordinates of the points and whose y coordinte is the sum of their y coordinates. Write a program that uses a structure called pt. to model a pt. define three pts., and have the user input values to two of them. Then set the third pt. equal to the sum of the other two, And display the value of the new pt.

-4-

GURBAKSHI Program 2: #include<iostream.h> struct point { int x,y; }; main() { point 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.y; cout<<"coordinates of p1+p2 are "<<p3.x<<"and"<<p3.y; return 0; }

09/cse/3621

-5-

GURBAKSHI output: enter coordinates of p1 6 7 enter coordinates of p2 6 7 coordinates of p1+p2 are 12and14

09/cse/3621

-6-

GURBAKSHI program 3:

09/cse/3621

create the equivalent of a four funct. Calculator. The program should request the user to enter a no.,an operator,and another no. it should then carry out the specified arithmetical operation: adding,subtracting,multiplying,or dividing the two no. finally it should display the result. When it finishes the calculation,the program should ask if the user want to do another calculation.the response can be yorn.

-7-

GURBAKSHI program 3: #include<iostream.h> #include<math.h> int main() { int m,n,r; char op; char ch; do {cout<<"enter no's"; cin>>m>>n; cout<<"enter operator"; cin>>op; switch(op) { case '+': r=m+n; cout<<"result is"<<r; break; case '-': r=m-n; cout<<"result is"<<r; break; case '/': if(n>0) { r=m/n; cout<<"result is"<<r; } else cout<<"divide by zero error "; break; case '*': r=m*n; cout<<"result is"<<r; break; default : cout<<"wrong operator"; } cout<<"do you want to do next operator 'y/n' "; cin>>ch; }while(ch=='y'|| ch=='Y'); return 0; }

09/cse/3621

-8-

GURBAKSHI

09/cse/3621

Output: enter no's 4 2 enter operator / result is2 do you want to do next operator 'y/n' Y enter no's 5 3 enter operator * result is15 do you want to do next operator 'y/n' n

-9-

GURBAKSHI program 4:

09/cse/3621

imagine a toolbooth with a class called toll booth. The two data items are a type unsigned int into hold the total no. of cars and a type double to hold the total amount of money collected . a constructor initializes both these to 0. a member funct. Called paying car() increments the car total and adds 0.50 to the cash total.another funct. Called nopaycar(),increments the car total but adds nothing to the cash total. Finally , amember funct. Called displays the two toals. Include a program to test this class. This program should allow the user to push one key to count a payingcar(), and another to count a nonpayingcar().pushing another key should cause the program to print out the total cars and total cash and then exit.

- 10 -

GURBAKSHI program 4:

09/cse/3621

#include<iostream.h> #include<string.h> class toolbooth { unsigned int tc; double tm; public: toolbooth() { tc=0; tm=0; } void payingcar() { tc=tc+1; tm=tm+0.50; } void nopayingcar() { tc=tc+1; tm; } void display() { cout<<"total no. of car"<<tc<<"total cost"<<tm; } }; main() { toolbooth p; char ch; while(ch!='c') { cout<<"enter choice"; cin>>ch; if(ch=='a') p.payingcar(); else if(ch=='b') p.nopayingcar(); else cout<<"exit"; }

- 11 -

GURBAKSHI p.display(); return 0; }

09/cse/3621

- 12 -

GURBAKSHI output: enter choice a enter choice a enter choice a enter choice a enter choice b enter choice b enter choice c exit total no. of car 6 total cost 2

09/cse/3621

- 13 -

GURBAKSHI program 5:

09/cse/3621

a phone no., such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767) and the no. (8900). Write a program that uses a structure variables of type phone. Initialize one, and have the user input a no. for the other one. Then display both no.

- 14 -

GURBAKSHI program 5:

09/cse/3621

#include<iostream.h> struct phone { int area; int exchange; int number; }; int main() { phone p1,p2; p1.area=212; p1.exchange=767; p1.number=8900; cout<<"enter area"<<endl; cin>>p2.area; cout<<"enter exchange"<<endl; cin>>p2.exchange; cout<<"enter number"<<endl; cin>>p2.number; cout<<"my no. is("<<p1.area<<")"<<" "<<p1.exchange<<"-"<<p1.number; cout<<"your no. is("<<p2.area<<")"<<" "<<p2.exchange<<"-"<<p2.number; return 0; }

- 15 -

GURBAKSHI

09/cse/3621

Output: enter area 415 enter exchange 555 enter number 1212 my no. is (212) 767-8900 your no. is (415) 555-1212

- 16 -

GURBAKSHI program 6:

09/cse/3621

write a funct. Called reversit() that reverses a string. Use a for loop that swaps the first and last characters,then the second and next to last characters and so on.the string should be passed to reversit() as an argument. Write a program to exercise reversit().the program should get a string from the user,call reversit() , and print out the result.

- 17 -

GURBAKSHI program 6: # include<iostream.h> # include<conio.h> # include<string.h> # include<stdio.h> void main() { clrscr(); char s[50],temp[50]; int i,l; cout<<"\nenter the string="; gets(s); l = strlen(s); for( i=0; i<l/2; ++i) { temp[i] = s[i]; s[i] = s[l-i-1]; s[l-i-1] = temp[i]; } cout<<"\nstring="; puts(s); getch(); }

09/cse/3621

- 18 -

GURBAKSHI

09/cse/3621

Output: enter the string= able was i string= i saw elba enter the string= computer science string=ecneics retupmoc

- 19 -

GURBAKSHI program 7:

09/cse/3621

create two classes DM and DB which store the value of distances. DM stores distances in metres and centimeters and DBin feet and inches. Write a program that can read values fortheclass objects and add one object of DM with another object of DB. Use a friend funct. To carry out the addition operation.the object that stores the results maybe a DM object or DB object,depending on the units in which the results are required. the display should be in the format of feet and inches or metres and cantimeters depending on the object on display.

- 20 -

GURBAKSHI program 7: #include<iostream.h> class db; class dm { public: int cm; int m; void put() { cout<<"enter meter"; cin>>m; cout<<"enter centimeter"; cin>>cm; } friend void sum(dm,db); void get() { cout<<" metre = "<<m<<" "<<" centimeter = "<<cm; } }; class db { public: int f; int in; void give() {cout<<"enter feet"; cin>>f; cout<<"enter inches"; cin>>in; } friend void sum(dm x,db y); void display() { cout<<"feet"<<f<<"inches"<<in; } }; void sum(dm x,db y) { dm h; h.cm=(x.cm+(y.f)*(30))%100; h.m=(x.cm+(y.f)*(30))/100; h.m=h.m+(x.m+(y.in)/40)/1; h.cm=h.cm+((x.m+(y.in)/40)%1)*(100);; h.get();

09/cse/3621

- 21 -

GURBAKSHI } int main() { dm a; db b; a.put(); b.give(); sum(a,b); return 0; }

09/cse/3621

- 22 -

GURBAKSHI output: enter meter 100 enter centimeter 1000 enter feet 1000 enter inches 4000 metre = 510 centimeter = 0

09/cse/3621

- 23 -

GURBAKSHI

09/cse/3621

program 8: class father{ protected:int age; public: father(int x) {age=x;} Virtual void iam() {cout<<I am the father,my age is:<<age<<endl;} }; Derive the two classes son and daughter from the above class and for each ,define iam() to write our similar but appropriate msg. you should also define suitable constructors for these classes. Now,write a main() that creates objects of the three classes and then calls iam() for them. Declare pointer to father.succesively,assign add. Of objects of the two derived classes to this pointer and in each case,call iam() through the pointer to demonstrate polymorphism in action.

# include<iostream.h> # include<conio.h> class father { protected : int age; public : father(int x) { age = x; } virtual void iam() { cout<<"I AM FATHER AND MY AGE IS"<<age<<endl; } }; class son:public father { public : son(int x):father(x) {} void iam() { cout<<"I AM SON AND MY AGE IS "<<age<<endl; }

- 24 -

GURBAKSHI }; class daughter:public father { public : daughter(int x):father(x) {} void iam() { cout<<"I AM DAUGHTER AND MY AGE IS"<<age<<endl; } }; void main() { clrscr(); father f(50); son s(20); daughter d(22); f.iam(); s.iam(); d.iam(); father*p; p = &f; p->iam(); p = &s; p->iam(); p = &d; p->iam(); getch(); } Output: I AM FATHER AND MY AGE IS50 I AM SON AND MY AGE IS 20 I AM DAUGHTER AND MY AGE IS22 I AM FATHER AND MY AGE IS50 I AM SON AND MY AGE IS 20 I AM DAUGHTER AND MY AGE IS22

09/cse/3621

- 25 -

GURBAKSHI

09/cse/3621

program 9: Make aclass employee with a name and salary.make a class manager inherit from employee. Add an instance variable,named department of type string.supply a method to tostring that prints the managers name,department,salary.make class executive inherit from manager.supply a method tostring that prints the string executive followed by the info. Stored in the manager superclass object.supply a test program that tests these classes And methods. #include<iostream.h> #include<string.h> #include<stdio.h> #include<conio.h> class employee {protected: char name[20]; float salary; public: void put() { cout<<"enter name"<<endl; cin>>name; cout<<"enter salary"<<endl; cin>>salary; } }; class manager:public employee { char department[20]; public: void enter() { put(); cout<<"enter department"<<endl; cin>>department; } virtual void tostring() { cout<<"name="<<name<<endl; cout<<"salary="<<salary<<endl; cout<<"department="<<department<<endl; } }; class executive:public manager { public: void insert()

- 26 -

GURBAKSHI { enter(); } void tostring() { manager::tostring(); } }; void main() {clrscr(); executive n; n.insert(); n.tostring(); getch(); } Output: enter name james enter salary 100000 enter department manager name=james salary=100000 department=manager

09/cse/3621

- 27 -

GURBAKSHI

09/cse/3621

program 10: Create a class rational which represents a numerical value by two double values numerator and denominator .include the following public member funct.: 1. constructor with no arguments(default). 2. constructor with two arguments. 3. void reduce() that reduces the rational no. by eliminating the hcf bet. The numerator and denominator. 4. overload + operator to add two rational no. 5. overload >> operator to enable input through cin. 6. overload << operator to enable output through cout. 7. write a main() to test all the funct. In the class. #include<iostream.h> #include<conio.h> class Rational { int num,den; public: Rational() { num =den==0; } Rational(int n,int d):num(n),den(d) {} void reduce(Rational& r1) { int p,h; if(r1.num>r1.den) p=r1.den; else p=r1.num; for(int i=1;i<=p;i++) { if(r1.num%i==0 & r1.den%i==0) { h=i; } } r1.num /= h; r1.den /= h; } void display(Rational& r)

- 28 -

GURBAKSHI { cout<<"rational number ="<<r.num<<"/"<<r.den<<endl; } friend Rational operator+(Rational,Rational); friend istream& operator>>(istream& ,Rational& ); friend ostream& operator<<(ostream& ,Rational& ); }; Rational operator+(Rational r1,Rational r2) { r1.reduce(r1); r2.reduce(r2); Rational c; int p,h; if(r1.den>r2.den) p=r2.den; else p=r1.den; for(int i=1;i<=p;i++) { if(r1.den%i==0 & r2.den%i==0) { h=i; } } int lcm = (r1.den*r2.den)/h; int n1=lcm/r1.den; int n2=lcm/r2.den; int m=(n1*r1.num)+(n2*r2.num); c.num = m; c.den = lcm; c.reduce(c); c.display(c); return c; } istream& operator>>(istream& ain,Rational& p) { ain>>p.num; ain>>p.den; return ain; } ostream& operator<<(ostream& gout,Rational& s) { gout<<s.num; gout<<s.den;

09/cse/3621

- 29 -

GURBAKSHI return gout; } void main() {clrscr(); Rational r(10,30); Rational r1(5,10); Rational r2; Rational p1,p2; cout<<"enter value of p1"<<endl; cin>>p1; cout<<"enter value of p2"<<endl; cin>>p2; cout<<"value of p1"<<" "<<p1<<endl; cout<<"value of p2"<<" "<<p2<<endl; r1.reduce(r1); r.reduce(r); r.display(r); r1.display(r1); r2 = r + r1; getch(); } Output: enter value of p1 20 25 enter value of p2 40 50 value of p1 20 25 value of p2 40 50 rational number =1/3 rational number =1/2 rational number =5/6

09/cse/3621

- 30 -

GURBAKSHI Program 11:

09/cse/3621

Create a base class called shape .use this class to store two double type value that could be used to compute the area of figures.derive two specific classes called triangle and rectangle from the base shape.add to the base class,a member function get data() to initialize base class data members and another member function display area() to compute and display the area of figures.make display area() as a virtual function and redefine this fuction in the derived classes to suit their requirements. Using these three classes,design a program that will accept dimensions of a triangle or a rectangle interactively and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles and as base and height in the case of triangles and used as follows: Area of rectangle =x*y Area of triangle =1/2*x*y

#include<iostream> using namespace std; class shape { protected: double a,b; public: void get() { cout<<"enter value of a and b"; cin>>a>>b; } virtual void displayarea() {} };

- 31 -

GURBAKSHI

09/cse/3621

class triangle:public shape { public: void displayarea() { cout<<"area of triangle is"<<" "<<(a*b)/2; } }; class rectangle:public shape { public: void displayarea() { cout<<"area of rectangle is "<<" "<<(a*b); } }; int main() { int choice; triangle t; rectangle r; shape*s; cout<<"enter choice"; cin>>choice;

- 32 -

GURBAKSHI if(choice==1) s=&t; else s=&r; s->get(); s->displayarea return 0; } Output: enter choice 1 enter value of a and b 56 area of triangle is 15 enter choice 2 enter value of a and b 56 area of rectangle is 30

09/cse/3621

- 33 -

GURBAKSHI

09/cse/3621

Program 12: EXCEPTION AND INHERITANCE

#include<iostream> #include<exception> using namespace std; class A { int a; }; class B:public A { }; class C:public A { }; int main() { A a1; B b1; C c1; int a; cout<<"enter the object no"<<endl; cin>>a; try { if(a==1) throw b1; else if(a==2) throw c1; else throw a1; }

- 34 -

GURBAKSHI catch(C) { cout<<" C type exception"<<endl; } catch(A) { cout<<" A type exception"<<endl; } catch(B) { cout<<" B type exception"<<endl; } return 0; } Ouput: enter the object no 1 A type exception enter the object no 2 C type exception enter the object no 3 A type exception

09/cse/3621

- 35 -

GURBAKSHI Program 13:

09/cse/3621

#include<iostream.h> struct date { int y; int m; int d; }; class b { char n[50]; char d[50]; date doa; date dod; public: void put() { cout<<"enter name"<<endl; cin>>n; cout<<"enter disease"<<endl; cin>>d; cout<<"enter admission date"<<endl; cin>>doa.y>>doa.m>>doa.d; cout<<"enter discharge date"<<endl; cin>>dod.y>>dod.m>>dod.d; } void display() { cout<<"patient name is"<<" "<<n<<endl; cout<<"disease of patient is"<<" "<<d<<endl; cout<<"date of admisson is"<<" "<<doa.y<<"/"<<doa.m<<"/"<<doa.d<<endl; cout<<"date of discharge is"<<" "<<dod.y<<"/"<<dod.m<<"/"<<dod.d<<endl; } }; class d:public b { int a; public: void enter() { put(); cout<<"enter age"<<endl; cin>>a;

- 36 -

GURBAKSHI

09/cse/3621

} int age() { return a; } void show() { cout<<"this is not a pedratic patient"<<" "<<"and age of patient is"<<" "<<a<<endl; display(); } void showage() { cout<<"this is a pedratic patient"<<" "<<"and age of patient is"<<" "<<a<<endl; display(); } }; int main() { d x; for(int i=0;i<3;i++) { x.enter(); if(x.age()<12) { x.showage(); } else { x.show(); } } return 0; } ouput: enter name ricky enter disease malaria enter admission date 2003 4 3 enter discharge date 2003 4 9 enter age 9

- 37 -

GURBAKSHI this is a pedratic patient and age of patient is 9 patient name is ricky disease of patient is malaria date of admisson is 2003/4/3 date of discharge is 2003/4/9 enter name micky enter disease fever enter admission date 2009 7 22 enter discharge date 2009 7 24 enter age 27 this is not a pedratic patient and age of patient is 27 patient name is micky disease of patient is fever date of admisson is 2009/7/22 date of discharge is 2009/7/24 enter name vicky enter disease jaundice enter admission date 2005 6 2 enter discharge date 2005 6 7 enter age 6 this is a pedratic patient and age of patient is 6 patient name is vicky disease of patient is jaundice date of admisson is 2005/6/2 date of discharge is 2005/6/7

09/cse/3621

- 38 -

GURBAKSHI

09/cse/3621

Program 13: A hospital wants to create a database regarding its indoor patients. The informations to store include 1. name of patient 2. date of admission 3. disease 4. date of discharge create the strcture to store the date (year ,month and date as its mamber). Create a base class to store the above information. The member fanctions should include functions to enter informations and display a list of all patients in the database. Create a drived classto store the age of the patients. List the information above all the pediatric patients(less then tweieve years in age) #include<iostream.h> struct date { int y; int m; int d; }; class b { char n[50]; char d[50]; date doa; date dod; public: void put() { cout<<"enter name"<<endl; cin>>n; cout<<"enter disease"<<endl; cin>>d; cout<<"enter admission date"<<endl; cin>>doa.y>>doa.m>>doa.d; cout<<"enter discharge date"<<endl; cin>>dod.y>>dod.m>>dod.d; } void display() { cout<<"patient name is"<<" "<<n<<endl; cout<<"disease of patient is"<<" "<<d<<endl; cout<<"date of admisson is"<<" "<<doa.y<<"/"<<doa.m<<"/"<<doa.d<<endl; cout<<"date of discharge is"<<" "<<dod.y<<"/"<<dod.m<<"/"<<dod.d<<endl; }

- 39 -

GURBAKSHI

09/cse/3621

}; class d:public b { int a; public: void enter() { put(); cout<<"enter age"<<endl; cin>>a; } int age() { return a; } void show() { cout<<"this is not a pedratic patient"<<" "<<"and age of patient is"<<" "<<a<<endl; display(); } void showage() { cout<<"this is a pedratic patient"<<" "<<"and age of patient is"<<" "<<a<<endl; display(); } }; int main() { d x; for(int i=0;i<3;i++) { x.enter(); if(x.age()<12) { x.showage(); } else { x.show(); } } return 0; } Output: enter name ricky enter disease malaria

- 40 -

GURBAKSHI enter admission date 2003 4 3 enter discharge date 2003 4 9 enter age 9 this is a pedratic patient and age of patient is 9 patient name is ricky disease of patient is malaria date of admisson is 2003/4/3 date of discharge is 2003/4/9 enter name micky enter disease fever enter admission date 2009 7 22 enter discharge date 2009 7 24 enter age 27 this is not a pedratic patient and age of patient is 27 patient name is micky disease of patient is fever date of admisson is 2009/7/22 date of discharge is 2009/7/24 enter name vicky enter disease jaundice enter admission date 2005 6 2 enter discharge date 2005 6 7 enter age 6 this is a pedratic patient and age of patient is 6 patient name is vicky disease of patient is jaundice date of admission is 2005/6/2 date of discharge is 2005/6/7

09/cse/3621

- 41 -

GURBAKSHI

09/cse/3621

- 42 -

Das könnte Ihnen auch gefallen