You are on page 1of 23

Object Oriented Programming in C++ Q1.

Write a program to allow a student to input their personal and academic data, such as roll number, name, parents name, age, address, subjects marks, total and average. Program should display this information in appropriate format.
#include<iostream> using namespace std; void main() { int i,rollno,age,marks=0,total=0; char name[12],parentname[15],address[12]; float avg=0; cout<<"enter enrollno.: "; cin>>rollno; cout<<"\nenter name: "; cin>>name; cout<<"\nenter father name: "; cin>>parentname; cout<<"\nenter ur age: "; cin>>age; cout<<"\nenter home place: "; cin>>address; cout<<"\nenter 5 subject marks"; for( i=0;i<5;i++) { cout<<"\nenter marks: "; cin>>marks; total=total+marks; } avg=total/5; cout<<"\nname: "<<name; cout<<"\nenroll: "<<rollno; cout<<"\nfather name: "<<parentname; cout<<"\nage: "<<age; cout<<"\nplace: "<<address; cout<<"\ntotal marks: "<<total; cout<<"\nprecentage: "<<avg<<endl; }

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

Q2. Write a program to create a nm matrix dynamically. Program should perform addition, subtraction, and multiplication of two matrices, and transpose of a matrix.
#include<iostream> using namespace std; void main() { int i,j,**a,**b,c[2][2],k,n; a=new int*[2]; b=new int *[2]; for(i=0;i<2;i++) { a[i]=new int[2]; b[i]=new int[2]; } cout<<"enter values for 1st matrix:\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cin>>a[i][j]; } } cout<<"\nA="; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<"\t"<<a[i][j]; } cout<<endl; } cout<<"enter values for 2nd matrix:\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cin>>b[i][j]; } } cout<<"\nB="; for(i=0;i<2;i++) { for(j=0;j<2;j++) {
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++


cout<<"\t"<<b[i][j]; } cout<<endl; } do { cout<<"enter choice\n1.addition\n2.substraction\n3.multiplication\n4.tranpose\n"; cin>>n; switch(n) { case 1: cout<<"\naddition of A & B="; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=a[i][j]+b[i][j]; cout<<"\t"<<c[i][j]; } cout<<"\n\t\t"; } break; case 2: cout<<"\nsubstration of A & B="; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=a[i][j]-b[i][j]; cout<<"\t"<<c[i][j]; } cout<<"\n\t\t"; } break; case 3: cout<<"\nmultiplication of A & B="; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=0; for(k=0;k<2;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } cout<<"\t"<<c[i][j]; }
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++


cout<<"\n\t\t\t"; } break; case 4: cout<<"\ntranspose of B="; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=0; c[i][j]=a[i][j]; a[i][j]=b[j][i]; cout<<"\t"<<a[i][j]; } cout<<"\n\t\t"; } cout<<"\ntranspose of A="; for(i=0;i<2;i++) { for(j=0;j<2;j++) { b[i][j]=c[j][i]; cout<<"\t"<<b[i][j]; } cout<<"\n\t\t"; } break; default : cout<<"\nWrong input"; }

}while(n==1||n==2||n==3||n==4);

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

Q3. Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay Rs. 50 toll, but some vip car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount collected. Model this tollbooth with a class called tollbooth. Use appropriate data members and functions. Make appropriate member functions constant. Print out total cars and total cash and then exit.
#include<iostream> using namespace std; class tol { public: char cartype; int sum; int vcar,gcar; tol() { sum=0; vcar=0; gcar=0; } int vipcar() { vcar=vcar+1; return vcar; } void glcar() { sum=sum+50; gcar=gcar+1; } void show() { cout<<"vip car= "<<vcar<<endl<<"generalcar= "<<sum; } }; void main() { tol s; char ch; do { cout<<"\nenter cartype: "; cin>>s.cartype; if(s.cartype=='v')
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

"<<gcar<<endl<<"total

tax=

Object Oriented Programming in C++


s.vipcar(); else s.glcar(); cout<<"want more"; cin>>ch; }while(ch=='Y'||ch=='y'); s.show(); }

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

Q4. Create a class time that has separate int member data for hours, minutes and seconds. One constructor should initialize this data to 0 and others to fixed values. A display member function 11:59:59 format. Function to add two objects of type time passed as arguments.
#include<iostream> using namespace std; class time { public: int hr,min,sec; time() { hr=0; min=0; sec=0; } time gettime(); void add(time,time); }; void time::add(time t1, time t2) { time t; t.sec =t1.sec +t2.sec; t.min=t1.min+t2.min; t.hr=t.hr+t1.hr+t2.hr; if(t.sec>60) { t.sec=t.sec-60; t.min=t.min +1; } if(t.min>60) { t.min=t.min-60; t.hr=t.hr+1; } cout<<"sum of time is: "<<t.hr<<":"<<t.min<<":"<<t.sec; } time time::gettime () { time tt; cout<<"\nenter the hr: "; cin>>tt.hr; cout<<"\nenter the min: "; cin>>tt.min;
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++


cout<<"\nenter the sec: "; cin>>tt.sec ; return tt; } void main() { time t3,t1,t2; t1=t3.gettime(); t2=t3.gettime(); t3.add(t1,t2); }

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

Q5. WAP to Implement OSI model & attach Header Information as it flows from top layers to bottom layers and remove the header information when vice versa occurs.
#include<iostream> using namespace std; class osi { public: char str[50]; int flag; void app(); void pre(); void ses(); void trn(); void net(); void data(); void phy(); osi() { cout<<"enter the string: "; cin>>str; flag=0; } }; void osi::app() { if(flag==1) { cout<<str<<"\tapplication layer at recever end\n"; flag--; } else { cout<<str<<"\tapplication layer at sender end\n"; flag++; } } void osi::pre() { if(flag==2) { cout<<str<<"\n\tpresentation layer at recever end\n"; flag--; } else {
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++


cout<<str<<"\n\tpresentation layer at sender end\n"; ++flag; } } void osi::ses() { if(flag==3) { cout<<str<<"\n\tsession layer at recever end\n"; flag--; } else { cout<<str<<"\n\tsession layer at sender end\n"; flag++; } } void osi::trn() { int i; char str1[40]="tdpu"; cout<<str<<" \ntransport layer at recever end\n"; if(flag==4) { for(i=0;str[i]!='\0';i++) str[i]=str[i+4]; flag--; } else { strcat(str1,str); strcpy(str,str1); flag++; cout<<str<<"\n\ttransport layer at sender end\n"; } } void osi::net() { int i; char str1[40]="ndpu"; cout<<str<<" \nnetwork layer at recever end\n"; if(flag==5) { for(i=0;str[i]!='\0';i++) str[i]=str[i+4]; flag--; } else
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++


{ strcat(str1,str); strcpy(str,str1); flag++; cout<<str<<"\n\tnetwork layer at sender end\n"; } } void osi::data() { int i; char str1[40]="ddpu"; cout<<str<<" \ndata link layer recever end\n "; if(flag==6) { for(i=0;str[i]!='\0';i++) str[i]=str[i+4]; flag--; } else { strcat(str1,str); strcpy(str,str1); ++flag; cout<<str<<"\tdata link layer at sender end\n"; } } void osi::phy() { if(flag==7) { cout<<str<<"\n\tphysical layer at recever end\n"; flag--; } else { cout<<str<<"\n\tphysical layer at sender end\n"; flag++; } } void main() { osi obj; obj.app(); obj.pre(); obj.ses(); obj.trn(); obj.net(); obj.data(); obj.phy();
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++


cout<<"\n at recever\n"; obj.phy(); obj.data(); obj.net(); obj.trn(); obj.ses(); obj.pre(); obj.app(); }

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++ Q7. Define a class for bank application with appropriate members. Define a method to withdraw transfer money from one account to another, using both ways member function as well as friend function.
#include<iostream> using namespace std; class acc { float bal; public: acc() { bal=0; } friend void tran(acc &x,acc &y,float amnt); void show(acc x,acc y); void init(acc &x,acc &y); }; void tran(acc &x,acc &y,float amnt) { x.bal=x.bal+amnt; y.bal=y.bal-amnt; } void acc::show(acc x,acc y) { cout<<"\nfirst account balance: "<<x.bal; cout<<"\nsecond amount bal: "<<y.bal; } void acc::init(acc &x,acc &y) { cout<<"\nenter first account amount"; cin>>x.bal; cout<<"\nenter second account balence"; cin>>y.bal; } void main() { float amnt; acc x; acc y; acc z; cout<<"enter amount to transfer"; cin>>amnt; z.init(x,y); z.show(x,y); tran(x,y,amnt); z.show(x,y); }
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++ OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

Q8. Define a class Complex to represent complex numbers. Write a program to show the sum and product of two complex numbers. Class should contain default constructor, parameterised constructor, copy constructor and destructor.
#include<iostream> using namespace std; class complex { public: int real,img; complex() { real=0; img=0; } complex(int r,int i) { real=r; img=i; } complex(complex &c1) { real=c1.real; img=c1.img; } ~complex() { } void getdata(); void showmult(); void showadd(); friend complex operator+(complex c1,complex c2); friend complex operator*(complex c1,complex c2); }; void complex:: getdata() { cout<<"\nreal="<<real; cout<<"\nimg="<<img<<"i"; } complex operator+(complex c1,complex c2) { complex p; p.real=c1.real+c2.real; p.img=c1.img+c2.img;
PARVEEN KUMAR BREJWAL 00611104413 BCIIT

Object Oriented Programming in C++


return p; } complex operator*(complex c1,complex c2) { complex p2; p2.real=(c1.real*c2.real)-(c1.img*c2.img); p2.img=(c1.real*c2.img)+(c1.img*c2.real); return p2; } void complex::showadd() { cout<<"\naddition= "<<real<<"+"<<img<<"i"; } void complex::showmult() { cout<<"\nmult= "<<real<<"+"<<img<<"i"; } void main() { complex obj1(12,10),obj2(obj1),obj3; obj1.getdata(); cout<<"\n\nsecound value\n"; obj2.getdata(); obj3=obj1+obj2; obj3.showadd(); obj3=obj1*obj2; obj3.showmult(); }

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

Q9. Define a class Item to represent the items in a general store. Define methods to allow customers to purchase items. Write a program to find out the total number of items sold.
#include<iostream> using namespace std; class item { public: char itemname[10]; static int count; void show() { cout<<"total no. of product sold: "; cout<<item::count<<endl<<endl;

} }; int item::count; void main() { item c; char ch; do { cout<<"\nenter item name: "; cin>>c.itemname; item::count=item::count+1; cout<<"\ndo u want more purchase more:Y/N "; cin>>ch; }while(ch=='y'||ch=='Y'); c.show(); }

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

Q10. Write overloaded functions to calculate the cube root of a number. Number may be an integer, float, long or double.
#include<iostream> #include<cmath> using namespace std; class cube { int a; float b; double z; public : void getdata(int x) { cout<<"\ncube of integer number "<<x<<"=" ; a=pow(x,1./3.); cout<<a<<endl; } void getdata(float y) { cout<<"\n cube root of float number "<<y<<"=" ; b=powf(y,1./3.); cout<<b<<endl; } void getdata() { double g; cout<<"\nenter double value for cube root"<<endl ; cin>>g; z=pow(g,1./3.); cout<<z; } }; void main() { float v=8.9; int e=27; cube obj; obj.getdata(e); obj.getdata(v); obj.getdata(); }

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

OUTPUT:

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

Object Oriented Programming in C++

PARVEEN KUMAR BREJWAL 00611104413

BCIIT

You might also like