Vous êtes sur la page 1sur 37

Q1) Write a C++ program using class to calculate square and cube of given number using inline function.

sol) #include<iostream.h> #include<conio.h> class number { int num; public: int square() { int ans; ans=num*num; return ans; } int cube() { int ans; ans=num*num*num; return ans; } void getdata(); void display(); }; void number::getdata()

{ cout<<"enter number:"; cin>>num; } void number::display() { cout<<"\nNumber="<<num; } void main() { number n1; int x,y; clrscr(); n1.getdata(); n1.display(); x=n1.square(); y=n1.cube(); cout<<"\nsquare is:"<<x; cout<<"\ncube is:"<<y; getch(); } Output: enter number:6 Number=6

Q2) Write a C++ program to find area of triangle, circle, and rectangle using function overloading. sol) #include<iostream.h> #include<conio.h> class shape { int l,b; float radius,h; public: float area(float radius); int area(int l,int b); float area(int b,float h); }; float shape::area(float radius) { float ans; ans=3.14*radius*radius; return ans; } int shape::area(int l,int b) { int ans; ans=l*b; return ans;

} float shape::area(int b,float h) { int ans; ans=(b*h)/2; return ans; } void main() { shape s; float x,r,h1; int z,y,l1,b1; clrscr(); cout<<"enter radius:"; cin>>r; cout<<"Enter length:"; cin>>l1; cout<<"Enter height:"; cin>>h1; cout<<"Enter breadth:"; cin>>b1; x=s.area(r); z=s.area(l1,b1); y=s.area(b1,h1); cout<<"\narea of circle:"<<x;

cout<<"\narea of rectangle:"<<z; cout<<"\narea of triangle:"<<y; getch(); } Output: enter radius:5 Enter length:10 Enter height:12 Enter breadth:15 area of circle:78.5 area of rectangle:150 area of triangle:90

Q) Create a class student containing data members: - Rollno - name -marks1, marks2, marks3 Write necessary member functions: 1. to accept details of all students 2. to display details of one student 3. to display details of all students (Use Function overloading).

sol) #include<iostream.h> #include<conio.h> #include<process.h> class stud { int rno; char name[40]; float marks1,marks2,marks3; public: void getdata(); void display(int rollno); void display(); }; void stud::getdata()

{ cout<<"\nENTER STUDENT DETAILS"; cout<<"\nEnter rollno:"; cin>>rno; cout<<"Enter name:"; cin>>name; cout<<"Enter marks 1:"; cin>>marks1; cout<<"Enter marks2:"; cin>>marks2; cout<<"Enter marks 3:"; cin>>marks3; } void stud::display() { float avg; cout<<"\n"; cout<<"\nSTUDENT DETAILS"; cout<<"\n"; cout<<"\nRoll no:"<<rno; cout<<"\nName:"<<name; cout<<"\nmarks1:"<<marks1; cout<<"\nmarks2:"<<marks2; cout<<"\nmarks3:"<<marks3; avg=(marks1+marks2+marks3)/3;

cout<<"\naverage is:"<<avg; } void stud::display(int rollno) { float avg; if(rno==rollno) { cout<<"Name:"<<name; cout<<"\nRoll no:"<<rollno; cout<<"\nmarks1:"<<marks1; cout<<"\nmarks2:"<<marks2; cout<<"\nmarks3:"<<marks3; avg=(marks1+marks2+marks3)/3; cout<<"\naverage is:"<<avg; }

void main() { stud s[5]; int cnt=0,i=0,choice,rollno; clrscr(); while(1) {

cout<<"\n***MENU****"; cout<<"\n1.Enter details"; cout<<"\n2.Display details of all details."; cout<<"\n3.Display details of a specific student."; cout<<"\n4.Exit"; cout<<"\nplz enter your choice:"; cin>>choice; switch(choice) { case 1: s[i].getdata(); cnt++; i++; break; case 2: for(i=0;i<cnt;i++) { s[i].display(); } break; case 3: cout<<"Enter roll no:"; cin>>rollno; for(i=0;i<cnt;i++) {

s[i].display(rollno); } break; case 4: exit(1); break; default: cout<<"wrong choice"; } } } Output: ENTER STUDENT DETAILS Enter rollno:262 Enter name:siddharth Enter marks 1:78 Enter marks2:87 Enter marks 3:99 ***MENU**** 1.Enter details 2.Display details of all details. 3.Display details of a specific student. 4.Exit plz enter your choice:1

ENTER STUDENT DETAILS Enter rollno:254 Enter name:tanmay Enter marks 1:54 Enter marks2:65 Enter marks 3:66 ***MENU**** 1.Enter details 2.Display details of all details. 3.Display details of a specific student. 4.Exit plz enter your choice:2

STUDENT DETAILS

Roll no:262 Name:siddharth marks1:78 marks2:87 marks3:99 average is:88

STUDENT DETAILS

Roll no:254

Name:tanmay marks1:54 marks2:65 marks3:66 average is:61.666668***MENU**** 1.Enter details 2.Display details of all details. 3.Display details of a specific student. 4.Exit plz enter your choice:

Q4) Write a menu driven C++ program using class to calculate Area and perimeter of rectangle using inline function.

sol) #include<iostream.h> #include<conio.h> class shape { int l,b; public: int area(int l,int b) { int ans; ans=l*b; return ans; } int perimeter(int l,int b) { int ans; ans=2*(l+b); return ans; } }; void main() {

shape s; int len,bre,x,y; clrscr(); cout<<"Enter length:"; cin>>len; cout<<"Enter breadth:"; cin>>bre; x=s.area(len,bre); y=s.perimeter(len,bre); cout<<"\nArea of rectangle:"<<x; cout<<"\nPerimeter of rectangle:"<<y; getch(); }

Output: Enter length:10 Enter breadth:12 Area of rectangle:120 Perimeter of rectangle:44

Q5) Write a C++ program using class to calculate simple interest amount use default value for rate. sol) #include<iostream.h> #include<conio.h> class principal { float amt,si; int yrs,rate; public: principal(float a,int y,int r=5) { amt=a; yrs=y; rate=r; si=(amt*yrs*rate)/100; } void display(); }; void principal::display() { cout<<"\n"; cout<<"\nPrincipal amt:"<<amt; cout<<"\nno: of years:"<<yrs; cout<<"\nrate:"<<rate; cout<<"\nSimple Interest:"<<si;

} void main() { float amt,rate; int yrs; clrscr(); cout<<"Enter Principal amount:"; cin>>amt; cout<<"Enter no: of yrs:"; cin>>yrs; cout<<"Enter rate:"; cin>>rate; principal p1(amt,yrs); principal p2(amt,yrs,rate); p1.display(); p2.display(); getch(); }

Output: Enter Principal amount:10000 Enter no: of yrs:5 Enter rate:4 Principal amt:10000 no: of years:5

rate:5 Simple Interest:2500 Principal amt:10000 no: of years:5 rate:4 Simple Interest:2000

Q6) Create a class FDAccount containing members as: - Fdno - Name - Amt - Interest rate - Maturity amt - Number of months Use parameterized constructor to set appropriate details, where interest rate should be default argument. Calculate maturity amt using interest rate and display all the details. Soln:#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> #include<math.h>

class fda { long int fdno; char name[30]; float amt; float rate; float matamt; float intr; int yrs; public: fda(long int fdn,char n[],float amt1,int yrs1,float r=9.00) { fdno=fdn; strcpy(name,n); amt=amt1; yrs=yrs1; rate=r; } void display(); void calc(); }; void fda::display() { cout<<"\n"; cout<<"\nACCOUNT HOLDERS DETAILS:";

cout<<"\nFD no:"<<fdno; cout<<"\nAccount holders name:"<<name; cout<<"\namount:"<<amt; cout<<"\nNo: of years:"<<yrs; cout<<"\nAMount after maturity:"<<matamt; } void fda::calc() { intr=(amt*(pow(1+rate/100,yrs)))-amt; matamt=amt+intr; } void main() { fda f1(100122,"Siddharth",10000,2); fda f2(100123,"Rahul",5000,2,10); clrscr(); f1.calc(); f2.calc(); f1.display(); f2.display(); getch(); }

Output: ACCOUNT HOLDERS DETAILS: FD no:100122 Account holders name:Siddharth amount:10000 No: of years:2 AMount after maturity:11881 ACCOUNT HOLDERS DETAILS: FD no:100123 Account holders name:Rahul amount:5000 No: of years:2 AMount after maturity:6050

Q8) Write a necessary class to calculate variance & standard deviation of n numbers. Variance = 1/N ? (Xi-A.M.)2 S.D. = vvariance Where A.M. = 1/N ? Xi for all i = 1 to N for all i = 1 to N

Sol) #include<iostream.h> #include<conio.h> #include<math.h>

class variance { int num[30],n; float avg,var,sd; public: void getdata(); void putdata(); void cal(); }; void variance::getdata() { int i; cout<<"enter the number of data:"; cin>>n; for(i=0;i<n;i++)

{ cout<<"enter "<<i+1<<" number:"; cin>>num[i]; } } void variance::cal() { int i,sum=0,sum1=0,j=0; float d[30]; for(i=0;i<n;i++) { sum=sum+num[i]; } avg=sum/n; for(i=0;i<n;i++) { d[j]=(num[i]-avg)*(num[i]-avg); j++; } for(j=0;j<n;j++) { sum1=sum1+d[j]; } var=sum1/n; sd=sqrt(var);

} void variance::putdata() { cout<<"The average is:"<<avg; cout<<"\nthe variance is:"<<var; cout<<"\nthe standard deviation is:"<<sd; } void main() { variance v; clrscr(); v.getdata(); v.cal(); v.putdata(); getch(); }

Output: enter the number of data:6 enter 1 number:2 enter 2 number:4 enter 3 number:6 enter 4 number:8 enter 5 number:10 enter 6 number:12

The average is:7 the variance is:11 the standard deviation is:3.316625

Q9) Imagine a tollbooth at a bridge. A Car passing by the booth is expected to pay a toll. The tollbooth keeps the track of the number of cars that gone by and the total amount of cash collected. Create a class tollbooth with the data members as:-total number of cars passed. -total toll collected. Write necessary member functions: 1. a constructor that initializes both data members to zero. 2. paying car(): when any car passes through the tollbooth, that much toll gets added into total toll collected and total number of cars passed is incremented by one. 3. nonpaying car(): increments the car total but adds nothing to cash total. 4. display (): displays total no. of cars passed and the total amount collected

Sol) #include<iostream.h> #include<conio.h> #include<process.h> class toll { int tcars,ttoll;

public: toll() { tcars=0; ttoll=0; } void paycars(); void npaycars(); void display(); }; void toll::paycars() { tcars=tcars+1; ttoll=ttoll+25; } void toll::npaycars() { tcars=tcars+1; } void toll::display() { cout<<"Total no of cars passed:"<<tcars; cout<<"\ntotal toll collected so far:"<<ttoll; } void main()

{ toll t; int choice; clrscr(); while(1) { cout<<"\n*****MENU****"; cout<<"\n1.Car paying toll."; cout<<"\n2.Car not paying toll."; cout<<"\n3.Display."; cout<<"\n4.Exit"; cout<<"\nenter valid choices:"; cin>>choice; switch(choice) { case 1: t.paycars(); break; case 2: t.npaycars(); break; case 3: t.display(); break; case 4:

exit(1); break; default: cout<<"Wrong choice:"; } } } Output: *****MENU**** 1.Car paying toll. 2.Car not paying toll. 3.Display. 4.Exit enter valid choices:1 *****MENU**** 1.Car paying toll. 2.Car not paying toll. 3.Display. 4.Exit enter valid choices:1 *****MENU**** 1.Car paying toll. 2.Car not paying toll. 3.Display. 4.Exit

enter valid choices:2 *****MENU**** 1.Car paying toll. 2.Car not paying toll. 3.Display. 4.Exit enter valid choices:2 *****MENU**** 1.Car paying toll. 2.Car not paying toll. 3.Display. 4.Exit enter valid choices:3 Total no of cars passed:4 total toll collected so far:50

Q10) 10. Design a class which contain static data member and member function show() which displays number of times display operation is performed irrespective of the object responsible for display using static data member Sol) #include<iostream.h> #include<conio.h>

class number { static int cnt;

public:

void display(); }; int number::cnt=0; void number::display() { cnt++; cout<<"\ndisplay function is called times."<<cnt;

} void main() { number n1,n2,n3,n4,n5; clrscr();

n1.display(); n2.display(); n3.display(); n4.display(); n5.display(); getch(); } Output: display function is called times.1 display function is called times.2 display function is called times.3 display function is called times.4 display function is called times.5

Q11.) Consider the following C++ class Class Person { char Name [20]; char Addr [30]; float Salary; int Property; //in sq. foot area

float tax_amount; Public:

// member functions }; Calculate tax amount by checking salary and the property of the person For salary <5000 For salary >=5000||<=10000 For salary >=10000 tax rate=0 tax rate= 14% of salary. tax rate =16% of salary.

In this tax amount add following amt depending on the size of area in sq.foot For 1000 sq. foot area For >1000|| < 5000 sq. foot area amt=0. amt= 1000

For >5000||<= 10000 sq. foot area amt=3000. sol) #include<iostream.h> #include<conio.h> #include<stdio.h> class person { char name[30]; char addr[30]; float salary; int property; float taxamt; public: void getdata(); void caltax(); void putdata(); person()

{ taxamt=0; } }; void person::getdata() { cout<<"ENTER DETAILS:"; puts("\nenter name:"); gets(name); puts("\nenter address:"); gets(addr); cout<<"\nenter salary:"; cin>>salary; cout<<"\nenter property value in sq feet:"; cin>>property; } void person::caltax() { if(salary<5000) taxamt=0; else if(salary>=5000 || salary<=10000) taxamt=taxamt+0.14*salary; else taxamt=taxamt+0.16*salary; if(property<=1000)

taxamt=0; else if(property>1000 || property<5000) taxamt=taxamt+1000; else if(property>=5000 || property<=10000) taxamt=taxamt+3000; } void person::putdata() { puts(name); cout<<"\n"; puts(addr); cout<<"\n"; cout<<"salary in(Rs):"<<salary; cout<<"\nProperty value in sq feet:"<<property; cout<<"\nTotal tax deducted is:"<<taxamt; } void main() { person p1; clrscr(); p1.getdata(); p1.caltax(); p1.putdata(); getch(); }

Output: ENTER DETAILS: enter name: Siddharth enter address: aundh annexe enter salary: 15000 enter property value in sq feet: 1000 SIddharth Aundh annexe Salary in(Rs):15000 Property Value in sq feet:1000 Total tax deducted is:2400.00

Q12) Write a C++ program which will find the maximum of 3 integer numbers and maximum of 3 float numbers using function overloading. Sol) #include<iostream.h> #include<conio.h> class numbers { int a,b,c; float x,y,z; public: int max(int a,int b,int c); float max(float x,float y,float z); };

int numbers::max(int a,int b,int c) { if(a>b) { if(a>c) return a; } else { if(b>c) return b; else if(c>a) return c; } return 0; } float numbers::max(float x,float y,float z) { if(x>y) { if(x>z) return x; } else {

if(y>z) return y; else if(z>x) return z; } return 0.0; } void main() { numbers n; int p,q,r,s; float a,b,c,d; clrscr(); cout<<"Enter p(integers):"; cin>>p; cout<<"Enter q:"; cin>>q; cout<<"Enter r:"; cin>>r; cout<<"\nenter a(float:"; cin>>a; cout<<"enter b:"; cin>>b; cout<<"enter c:"; cin>>c;

s=n.max(p,q,r); d=n.max(a,b,c); cout<<"\nMax of three int:"<<s; cout<<"\nMax of three float:"<<d; getch(); } Output: Enter p(integers):7 Enter q:11 Enter r:6 enter a(float:5.2 enter b:5.5 enter c:5.3 Max of three int:11 Max of three float:5.5

Vous aimerez peut-être aussi