C++ Program

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 100

Control Statements

Samrity

90420305659

1.

WAP to find that the input value is even or odd :

#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; cout<<"enter the value of n"; cin>>n; if(n%2==0) { cout<<"no. is even"; } else { cout<<"no. is odd"; } getch(); }

Output :

Samrity

90420305659

2. WAP to find that input character is vowel or consonant :


#include<iostream.h> #include<conio.h> void main() { clrscr(); char ch; cout<<"enter any character"; cin>>ch; switch(ch) { case'a': case'e': case'i': case'o': case'u': case'A': case'E': case'I': case'O': case'U': { cout<<"entered character is a vowel"; break; } default: { cout<<"entered character is a consonant"; } } getch();

Samrity

90420305659

Output :

Samrity

90420305659

3.

WAP to calculate the gross salary of an employee :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int bs; float t; cout<<"Enter the basic salary of an employee"; cin>>bs; if(bs<5000) { t=0.5+bs; cout<<"Total salary is"<<t; } else if(bs>5000) { t=0.9+bs; cout<<"Total salary is"<<t; } getch(); }

Output :

Samrity

90420305659

4. WAP to print the table of a no. upto a specific range :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int r,s,n,i; cout<<"Enter the number:"; cin>>n; cout<<"Enter the range:"; cin>>r; for(i=1;i<=r;i++) { s=n*i; cout<<n<<"*"<<i<<"="<<s<<endl; } getch(); }

Output :

Samrity

90420305659

5. WAP to reverse an integer value :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,temp,rev=0; cout<<"Enter any no."; cin>>n; while(n>0) { temp=n%10; rev=(rev*10)+temp; n=n/10; } cout<<"Reverse of the no.is"<<rev; getch(); }

Output :

Samrity

90420305659

6. WAP to print Fibonacci series :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,a=0,b=1,i,c; cout<<"Enter any no."; cin>>n; cout<<a<< <<b; for(i=0;i<=n-2;i++) { c=a+b; cout<< <<c<< ; a=b; b=c; } getch(); }

Output :

Samrity

90420305659

7. WAP to find that a no. is Armstrong or not :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,temp=0,temp1,temp2=0,num; cout<<"enter any no."; cin>>n; num=n; while(n>0) { temp=n%10; temp1=temp*temp*temp; temp2=temp2+temp1; n=n/10; } if(num==temp2) { cout<<"no. is Armstrong"; } else { cout<<"no. is not Armstrong"; } getch(); }

Samrity

90420305659

Output :

Samrity

10

90420305659

8. WAP to find the sum of digits of a no. :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,temp,sum=0; cout<<"enter any no."; cin>>n; while(n>0) { temp=n%10; n=n/10; sum=sum+temp; } cout<<"Sum of digits is"<<sum; getch(); }

Output :

Samrity

11

90420305659

9. WAP to check that a no. is palindrome or not :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,temp,rev=0,num; cout<<"enter any no."; cin>>n; num=n; while(n>0) { temp=n%10; rev=(rev*10)+temp; n=n/10; } if(rev==num) { cout<<"The no. is palindrome"; } else { cout<<"The no. is not palindrome"; } getch(); }

Samrity

12

90420305659

Output :

Samrity

13

90420305659

10.

WAP to check the greatest of 3 input values :

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<"Enter 3 No.one by one : "; cin>>a>>b>>c; if ((a>b) && (a>c)) { cout<<"The greatest of the given 3 no. is : "<<a<<endl; } else if ((b>c) && (b>a)) { cout<<"The greatest of the given 3 no. is : "<<b<<endl; } else { cout<<"The greatest of the given 3 no. is : "<<c; } getch(); }

Output :

Samrity

14

90420305659

11.

WAP to find the no. of days in a month :

#include<iostream.h> #include<conio.h> void main() { int m; clrscr(); cout<<"Enter the month no. "; cin>>m; if(m>12) { cout<<"Invaild no."; } else if(m==4||m==6||m==9||m==11) { cout<<"Month has 30 days"; } else if (m==2) { cout<<"Month has 28 or 29 days"; } else { cout<<" month has 31 days"; } getch(); }

Samrity

15

90420305659

Output :

Samrity

16

90420305659

12.

WAP to print the following pattern :

#include <iostream.h> #include<conio.h> void main() { clrscr(); int space,star,i,j,m,n,k; cout<<"Enter no of rows:"; cin>>n; m=(2*n)-1; for(i=1;i<=n;i++) { star=(2*i)-1; space=(m-star)/2; for(j=1;j<=space;j++) { cout<<" "; } for(k=1;k<=star;k++) { cout<<"*"; } cout<<endl; } getch(); }

Samrity

17

90420305659

Output :

Samrity

18

90420305659

13.

WAP to print the following pattern :

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i=1,j=1; cout<<Enter any no.:"; cin>>n; while(i<=n) { while(j<=i) { cout<<j; j++; } cout<<endl; i++; } getch(); }

Output :

Samrity

19

90420305659

14. WAP to print the following pattern :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,i,j; cout<<"Enter any number:"; cin>>n; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { cout<<i<<" "; } cout<<endl; } getch(); }

Output :

Samrity

20

90420305659

15.

WAP to print the following pattern :

#include<iostream.h> #include<conio.h> void main() { clrscr(); for(int i=1;i<=5;i++) { cout<<endl; for(int j=1;j<=5;j++) { cout<<i*j<<"\t"; } } getch(); }

OUTPUT :

Samrity

21

90420305659

Arrays

Samrity

22

90420305659

1. WAP to enter n elements in an array and display the sum of those elements :
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[5],n,i,sum=0; cout<<"Enter the size of array"; cin>>n; cout<<"Enter the elements of array:"; for(i=0;i<n;i++) { cin>>a[i]; sum=sum+a[i]; } cout<<"The sum is"<<sum; getch(); }

Output :

Samrity

23

90420305659

2. WAP to perform Bubble Sort :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,i,k,m,j,a[50],temp; cout<<"Enter the size of array:"; cin>>n; cout<<"Enter the elements of array:"; for(i=0;i<n;i++) { cin>>a[i]; } for(i=n-1;i>0;i--) { for(j=0;j<i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } cout<<"The sorted list is:"; for(i=0;i<n;i++) { cout<<a[i]<<endl; } getch(); }

Samrity

24

90420305659

Output :

Samrity

25

90420305659

3. WAP to perform Linear Search :


#include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int i,a[5],n; for(i=0;i<5;i++) { cout<<"Enter the value of a["<<i<<"]="; cin>>a[i]; } cout<<"Enter any number: "; cin>>n; for(i=0;i<5;i++) { if(a[i]==n) { cout<<"Position is "<<i+1; getch(); exit(0); } } cout<<"Element is not present in the list"; getch(); }

Samrity

26

90420305659

Output :

Samrity

27

90420305659

4. WAP to perform Binary Search :


#include<iostream.h> #include<conio.h> void main() { int n,i,item,beg,end,mid,la[30]; clrscr(); cout<<"Enter n:"; cin>>n; for(i=1;i<=n;i++) { cout<<"la["<<i<<"]="; cin>>la[i]; } cout<<"Enter item:"; cin>>item; beg=1; end=n; mid=(beg+end)/2; while( (beg<=end) && (la[mid]!=item) ) { if(item<la[mid]) { end=mid-1; } else { beg=mid+1; } mid=(beg+end)/2; } if(la[mid]==item) { cout<<"la["<<mid<<"]="<<item; cout<<"\n item found"; } else { cout<<"item not found"; } Samrity
28 90420305659

getch(); }

Output:

Samrity

29

90420305659

5. WAP to perform Selection Search :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n, a[50],i,j,temp; cout<<"Enter the size of array:"; cin>>n; cout<<"Enter the elements of array: "<<endl; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<(n-1);i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<endl<<"Sorted list is : "<<endl; for(i=0;i<n;i++) { cout<<a[i]<<endl; } getch(); }

Samrity

30

90420305659

Output :

Samrity

31

90420305659

5. WAP to show addition of two matrices :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[2][2],b[2][2],c[2][2],i,j; cout<<"Enter the elements of 1st matrix:"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cin>>a[i][j]; } } cout<<"Enter the elements of 2nd matrix:"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cin>>b[i][j]; } } cout<<"\nAddition of 2 matrices is:\n "; 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"; } getch(); }

Samrity

32

90420305659

Output :

Samrity

33

90420305659

6.

WAP to show multiplication of 2 matrices :

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[2][2],b[2][2],s[2][2],r,c,k; cout<<"Enter the elements of 1st matrix:"; for(r=0;r<2;r++) { for(c=0;c<2;c++) { cin>>a[r][c]; } } cout<<"Enter the elements of 2nd matrix:"; for(r=0;r<2;r++) { for(c=0;c<2;c++) { cin>>b[r][c]; } } for(r=0;r<2;r++) { for(c=0;c<2;c++) { s[r][c]=0; for(k=0;k<2;k++) { s[r][c]=s[r][c]+a[r][k]*b[k][c]; } } } cout<<"\nMultiplication of 2 matrices is \n"; for(r=0;r<2;r++) { for(c=0;c<2;c++) { cout<<"\t"<<s[r][c]; } Samrity
34 90420305659

cout<<"\n"; } getch(); }

Output :

Samrity

35

90420305659

7.

WAP to insert, update, delete, find & display an array :

#include<conio.h> #include<process.h> #include<iomanip.h> #include<string.h> #include<iostream.h> #include<stdio.h> void main() { clrscr(); int a[15],i,j,n; cout<<"enter an array of 10 no"; for(i=0;i<10;i++) { cin>>a[i]; } cout<<"\nur array:"; for(i=0;i<10;i++) { cout<<" "<<a[i]<<" "; } cout<<"\nenter ur choice:"; cout<<"\n1 insertion"; Samrity
36 90420305659

cout<<"\n2 deletion"; cout<<"\n3 searching"; cout<<"\n4 quit"; cin>>n; char p; switch(n) { case 1: cout<<"\nselect"; cout<<"\na at begining"; cout<<"\nb at any position"; cout<<"\nc at end"; cin>>p; int q; cout<<"enter no. to b inserted:"; cin>>q; if(p=='a') { for(i=9;i>=0;i--) { a[i+1]=a[i]; } a[0]=q; } Samrity
37 90420305659

if(p=='b') { int l; cout<<"enter position at which no. to be inserted"; cin>>l; for(i=9;i>=l;i--) { a[i+1]=a[i]; } a[i]=q; } if(p=='c') a[10]=q; else {

cout<<"invalid"; } break; case 2: int m; cout<<"\nenter positon at which no. to b deleted:"; cin>>m; for(i=m;i<10;i++) Samrity
38 90420305659

{ a[i-1]=a[i]; } for(i=0;i<9;i++) { cout<<" "<<a[i]<<" "; } break; case 3: int k; cout<<"\nenter no. to b search"; cin>>k; int ctr=0; for(i=0;i<10;i++) { if(a[i]==k) { cout<<"\nno found at"<<i+1; ctr++; } } if(ctr==0) cout<<"no. not present"; break; Samrity
39 90420305659

case 4: exit(0); break; default: cout<<"invalid"; } getch(); }

OUTPUT:

Samrity

40

90420305659

Samrity

41

90420305659

Strings

Samrity

42

90420305659

1. Wap to perform the following inbuilt functions on Strings : a. Strlen b. strrev c. strcpy d. strcmp e. strcat
#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char a[10],b[10];int l,l1; cout<<"Enter first string: "; gets(a); cout<<"Enter second string: "; gets(b); l=strlen(a); l1=strlen(b); cout<<"Length of string 1 is "<<l<<endl; cout<<"Length of string 2 is "<<l1<<endl; if(strcmp(a,b)==0) { cout<<"Strings are equal"; } else { cout<<"Strings are not equal"<<endl; } strcat(a,b); cout<<"Concatenated string is: "<<a<<endl; strcpy(a,b); cout<<"Copied string is: "<<a<<endl; strrev(a); cout<<"Reversed string is: "<<a<<" "<<b; getch(); }

Samrity

43

90420305659

Output :

Samrity

44

90420305659

2. WAP to check whether a string is palindrome or not :


#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr(); char a[20]; int l1,i,j,temp; cout<<"Enter the string: "; gets(a); l1=strlen(a); for(i=0;i<l1;i++) { for(j=i;j<11;j++) { if(a[i]==a[j]) { temp=1; break; } else { temp=0; } break; } } if(temp==1) { cout<<"String is a palindrome"; } else { cout<<"String is not a palindrome"; } getch(); }

Samrity

45

90420305659

Output :

Samrity

46

90420305659

3. WAP to display how many times a given character comes in a string :


#include<iostream.h> #include<conio.h> void main() { clrscr(); int i=0,c=0; char ch; char s[50]; cout<<"Enter the string "; cin>>s; cout<<"Enter the character 2 search:"; cin>>ch; for(i=0;s[i]!='\0';i++) { if(s[i]==ch) { c+=1;} else { continue; } } cout<<"Number of characters in string--> "<<c<<endl; getch(); }

Samrity

47

90420305659

Output :

Samrity

48

90420305659

Structures

Samrity

49

90420305659

1. WAP using structures to enter and display the information of an employee (name, employee no., salary) :
#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> struct emp { int n,sal; }; struct emp1 { char name[10]; emp s1,s2; }s3; void main() { clrscr(); cout<<"Enter the name of employee: "; gets(s3.name); cout<<"Enter the no. of employee; "; cin>>s3.s1.n; cout<<"Enter the salary of employee; "; cin>>s3.s1.sal; getch(); }

Samrity

50

90420305659

Output :

Samrity

51

90420305659

2. WAP to show the concept of arrays of structures :


#include<iostream.h> #include<conio.h> struct student { int marks[3]; char name[20]; }; void main() { student s[3]; clrscr(); int i,j; for(i=0;i<3;i++) { cout<<"Enter the name of student "<<i+1<<": "; cin>>s[i].name; for(j=0;j<3;j++) { cout<<"Enter the marks of subject "<<j+1<<": "; cin>>s[i].marks[j]; } } getch(); }

Samrity

52

90420305659

Output :

Samrity

53

90420305659

3. WAP to show the concept of arrays within structure :


#include<iostream.h> #include<conio.h> struct student { int marks[3]; char name[20]; }; void main() { student s[3]; clrscr(); int i,j; for(i=0;i<3;i++) { cout<<"Enter the name:"; cin>>s[i].name; for(j=0;j<3;j++) { cout<<"Enter the marks:"; cin>>s[i].marks[j]; } } getch(); }

Samrity

54

90420305659

Output :

Samrity

55

90420305659

4. WAP to show the concept of structure within structure :


#include<iostream.h> #include<conio.h> struct student { int m; float p; }; struct student1 { char name[10]; student s[3]; }s1; void main() { clrscr(); int i; for(i=0;i<3,i++) { cout<<"Enter the name: "; cin>>s1.name; cout<<"Enter marks: "; cin>>s1.s[i].m; cout<<"Enter percentage: "; cin>>s1.s[i].p; } getch(); }

Samrity

56

90420305659

Output :

Samrity

57

90420305659

Function s

Samrity

58

90420305659

1.

WAP to find factorial of a no. using recursion :

#include<iostream.h> #include<conio.h> int f,n; int fact(int n) { int val=1; if(n==1) { return val; } else { val=n*fact(n-1); return val; } } void main() { clrscr(); int fact(int); cout<<"Enter any no.: "; cin>>n; f=fact(n); cout<<"Factorial is "<<f<<endl; getch(); }

Samrity

59

90420305659

Output :

Samrity

60

90420305659

2.

(a) WAP to swap 2 nos using Pass by Value :

#include<iostream.h> #include<conio.h> void swap(int a,int b) { int temp; temp=a; a=b; b=temp; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; } void main() { clrscr(); int x,y; void swap(int,int); x=10; y=20; swap(x,y); cout<<"Values after swapping are:"<<endl; cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; getch(); }

Samrity

61

90420305659

Output :

Samrity

62

90420305659

b.

WAP to swap 2 nos using Call by Reference :

#include<iostream.h> #include<conio.h> void swap(int &a,int &b) { int temp; temp=a; a=b; b=temp; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; } void main() { clrscr(); int x,y; void swap(int &,int &); x=10; y=20; swap(x,y); cout<<"Values after swapping are:"<<endl; cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; getch(); }

Samrity

63

90420305659

Output :

Samrity

64

90420305659

Pointers

Samrity

65

90420305659

1.

WAP to increment values of variables using pointers :

#include<iostream.h> #include<conio.h> void increment(int *x,int *y) { *x=*x+5; *y=*y+5; cout<<"Value of a during function call is "<<*x<<endl; cout<<"Value of b during function call is "<<*y<<endl; } void main() { clrscr(); int a,b; a=5,b=10; cout<<"Value of a before increment is "<<a<<endl; cout<<"Value of b before increment is "<<b<<endl; increment(&a,&b); cout<<"Value of a after increment is "<<a<<endl; cout<<"Value of b after increment is "<<b<<endl; getch(); }

Output :

Samrity

66

90420305659

2.

WAP to access Arrays through Poi nters :

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[5],i,*p; cout<<"Enter the elements of array:"<<endl; for(i=0;i<5;i++) { cin>>a[i]; } p=&a[0]; cout<<"Acessed array is:"<<endl; for(i=0;i<5;i++) { cout<<*(p+i)<<endl; getch(); } }

Output :

Samrity

67

90420305659

Classes & Objec ts

Samrity

68

90420305659

1.

WAP to enter & display values of variables :

#include<iostream.h> #include<conio.h> class A { int a; public: int b,c; void enter() { cout<<"Enter the values of a,b & c:"<<endl; cin>>a>>b>>c; } void show() { cout<<a<<" "<<b<<" "<<c; } }; void main() { clrscr(); A A1; A1.enter(); A1.show(); getch(); }

Samrity

69

90420305659

Output :

Samrity

70

90420305659

Constructor & Destructors

Samrity

71

90420305659

1.

WAP to show the use of simple, parameterized & copy constructor :

#include<iostream.h> #include<conio.h> class abc { private: int a; float b; public: abc() { a=0; b=0.0; } abc(int x,float y) { a=x; b=y; } abc (abc &m) { a=m.a; b=m.b; } void show() { cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; }}; void main() { clrscr(); int a1; float a2; Samrity
72 90420305659

cout<<"Enter value of a1 "; cin>>a1; cout<<"Enter value of a2 "; cin>>a2; abc m1,m2(20,42.2); abc m3(m2); abc m4(a1,a2); m1.show(); //default constructor m2.show(); //parameterized constructor m3.show(); //copy constructor m4.show(); //dynamic constructor getch(); }

Output :

Samrity

73

90420305659

Inheritance

1.

WAP to show single inheritance :


74 90420305659

Samrity

#include<iostream.h> #include<conio.h> class B { private: int x; protected: int y; public: int z; void getdata(); void showdata(); }; class D : public B { private:int k; public: void getk(); void output(); }; void B :: getdata() { cout<<"enter the values of x,y and z:-"; cin>>x>>y>>z; } void B:: showdata() { cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; cout<<"z="<<z<<endl; } void D::getk() {cout<<"enter k:"; cin>> k; } void D :: output() { int s= y+z+k; cout<<"y+z+k="<<s<<endl;} void main() { clrscr(); D d1; d1.getdata(); d1.getk(); Samrity
75 90420305659

d1.showdata(); d1.output(); getch(); }

Output :

Samrity

76

90420305659

2. WAP to show multiple inheritance :


#include<iostream.h> #include<conio.h> class biodata { char n[25]; int sem; char branch[10]; public: void getb() { cout<<"Enter the name - ";cin>>n; cout<<"Enter the semester - ";cin>>sem; cout<<"Enter the branch - ";cin>>branch; } void showb() { cout<<"The name - "<<n; cout<<endl<<"The semester - "<<sem; cout<<endl<<"The branch - "<<branch; } }; class result { float r; public: void getr() { cout<<"Enter the total marks - ";cin>>r; } void showr() {cout<<endl<<"The total marks - "<<r; } }; class final:public biodata,public result { char grade; public:void getg() { cout<<"Enter the grade - ";cin>>grade; } void showg() Samrity
77 90420305659

{ cout<<endl<<"The grade - "<<grade; } }; void main() { clrscr(); final f; f.getb(); f.getr(); f.getg(); f.showb(); f.showr(); f.showg(); getch(); }

Output :

Samrity

78

90420305659

3. WAP to show multilevel inheritance :


#include<iostream.h> #include<conio.h> class a { char n[20]; public: void input() { cout<<"Enter name--> "; cin>>n; } void output() { cout<<"Name is "<<n<<endl; } }; class b : public a { int r; public : void getdata() { a :: input(); cout<<"Enter roll number--> "; cin>>r; } void putdata() { a :: output(); cout<<"Roll No. is "<<r<<endl; } }; class c : public b { int m; public: void get() { getdata(); cout<<"Enter marks--> "; Samrity
79 90420305659

cin>>m; } void put() { putdata(); cout<<"Marks obtained are "<<m<<endl; } }; void main() { clrscr(); c c1; c1.get(); c1.put(); getch(); }

Out[ut :

Samrity

80

90420305659

4. WAP to show hierarchical inheritance :


#include<iostream.h> #include<conio.h> class a { char n[20]; int r; public: void input() { cout<<"Enter name & rollno.--> "; cin>>n>>r;} void output() { cout<<"Name & roll nnumber is "<<n<<" "<<r<<endl;}}; class b: public a { int b,c,p; public: int t; public: void input() { a :: input(); cout<<"Enter marks in biology--> "; cin>>b; cout<<"Enter marks in chemistry--> "; cin>>c; cout<<"Enter marks in physics--> "; cin>>p; t=b+c+p; } void output() { a :: output(); cout<<"Biology: "<<b<<"\t"<<"Chemistry: "<<c<<"\t"<<"Physics: "<<p<<endl; cout<<"Total "<<t; } }; class c: public a { Samrity
81 90420305659

int c1,j1,s1; public: int t; public: void input() { a :: input(); cout<<"Enter marks in c++ "; cin>>c1; cout<<"Enter marks in java "; cin>>j1; cout<<"Enter marks in sql "; cin>>s1; t=c1+j1+s1; } void output() { a:: output(); cout<<"C++ "<<c1<<"\t"<<"Java "<<j1<<"\t"<<"Sql "<<s1<<endl; cout<<"Total "<<t; } }; void main() { clrscr(); b bb; c cc; cout<<"Enter information of medical student "<<endl; bb.input(); cout<<"Enter information of computer student "<<endl; cc.input(); cout<<"\t Computer student "<<endl; cc.output(); cout<<endl<<"\t Medical student "<<endl; bb.output(); getch(); }

Samrity

82

90420305659

Output :

Samrity

83

90420305659

5. WAP to show hybrid inheritance :


#include<iostream.h> #include<conio.h> class a { char n[20]; int r; public: void input() { cout<<"Enter name & rollno.--> "; cin>>n>>r; } void output() { cout<<"\tName & roll nnumber is "<<n<<" "<<r<<endl; } }; class b: public a { public:int b,c,p; public: void input() { a :: input(); cout<<"Enter marks in biology--> "; cin>>b; cout<<"Enter marks in chemistry--> "; cin>>c; cout<<"Enter marks in physics--> "; cin>>p; } void output() { a :: output(); cout<<"Biology: "<<b<<"\t"<<"Chemistry: "<<c<<"\t"<<"Physics: "<<p<<endl; } }; class c: public a { public: Samrity
84 90420305659

int c1,j1,s1; public: void input() { a :: input(); cout<<"Enter marks in c++ "; cin>>c1; cout<<"Enter marks in java "; cin>>j1; cout<<"Enter marks in sql "; cin>>s1; } void output() { a:: output(); cout<<"C++ "<<c1<<"\t"<<"Java "<<j1<<"\t"<<"Sql "<<s1<<endl; } }; class d:public b, public c { int t1,t2; public: void input() { b:: input(); c:: input(); t1=b+c+p; t2=c1+j1+s1; } void output() { b:: output(); cout<<"\t\tTotal is "<<t1<<endl; c:: output(); cout<<"\t\tTotal is "<<t2<<endl; } }; void main() { clrscr(); d dd; dd.input(); dd.output(); Samrity
85 90420305659

getch(); }

Output :

Samrity

86

90420305659

Polymorphism

Samrity

87

90420305659

1.

WAP to perform Function Overloading (type of arguements) :

#include<iostream.h> #include<conio.h> class A { int a,b; public: void sum(float x, int y) { cout<<"Sum is "<<x+y<<endl; } void sum(int x, int y) { a=x; b=y; cout<<"Sum is "<<a+b<<endl; } void sum(int x, int y, int k,int l) { a=x+y; b=k+l; cout<<"Sum is "<<a+b; } }; void main() { clrscr(); float f; cout<<"Enter the value of F:"; cin>>f; A a1; a1.sum(20,40); a1.sum(f,40); a1.sum(20,40,60,80); getch(); }

Samrity

88

90420305659

Output :

Samrity

89

90420305659

2.

WAP to perform function overloading ( No. of arguments ) :

#include<iostream.h> #include<conio.h> class A { int a,b; public: void sum(int x,int y) { a=x; b=y; cout<<"Sum is "<<a+b<<endl; } void sum(int x,int y,int z,int l) { a=x+y; b=z+l; cout<<"Sum is "<<a+b<<endl; } }; void main() { clrscr(); A A1; A1.sum(20,40); A1.sum(20,40,60,80); getch(); }

Samrity

90

90420305659

Output :

Samrity

91

90420305659

3.

WAP to overload unary operator ++ :

#include<iostream.h> #include<conio.h> class A { int a,b; public: void enter() { cin>>a>>b; } void show() { cout<<a<<b; } void operator ++() { a=a+1; b=b+1; } }; void main() { clrscr(); A A1; A1.enter(); A1++; A1.show(); getch(); }

Samrity

92

90420305659

Output :

Samrity

93

90420305659

4. WAP to overload binary operator * :


#include<iostream.h> #include<conio.h> class A { int a,b; public: void enter () { cout<<"Enter the value of A: "; cin>>a; cout<<"Enter the value of B: "; cin>>b; } void show() { cout<<"The value of a is :"<<a<<endl; cout<<"The value of b is : "<<b<<endl; } A operator *(A B) { A temp; temp.a=a*B.a; temp.b=b*B.b; return(temp); }}; void main() { clrscr(); A a1,a2,a3; a1.enter(); a2.enter(); a3=a1*a2; a3.show(); getch(); }

Samrity

94

90420305659

Output :

Samrity

95

90420305659

5.

WAP to show the concept of friend function :

#include<iostream.h> #include<conio.h> class A { int a,b; public: void enter() { cin>>a>>b; } void show() { cout<<a<<b; } friend void fun(A); }; void fun(A B) { cout<<"Enter the values of a and b:"<<endl; cin>>B.a; cin>>B.b; B.show(); } void main() { clrscr(); A A1; fun(A1); getch(); }

Samrity

96

90420305659

Output :

Samrity

97

90420305659

6.

WAP to show the concept of virtual function :

#include<iostream.h> #include<conio.h> class A { public: virtual void show() { cout<<"Show of A"<<endl; } }; class B:public A { public: void show() { cout<<"Show of B"; } }; void main() { clrscr(); A*P; A A1; B B1; P=&A1; P->show(); P=&B1; P->show(); getch(); }

Samrity

98

90420305659

Output :

Samrity

99

90420305659

Samrity

100

90420305659

You might also like