Sie sind auf Seite 1von 64

SGHCMT

//WRITE A PROGRAM TO CONVERT A LOWERCASE LETTER TO UPPERCASE CHARACTER USING CONDITIONAL OPERATOR.
#include <iostream.h> #include <conio.h> void main() { char c1; clrscr(); cout<<"enter a character="; cin>>c1; char c2=(c1>='a'&&c1<='z')?('A'+c1-'a'):c1; cout<<"Uppercase equivalent="<<c2; getch(); } /*****OUTPUT enter a character=m Uppercase equivalent=M */

Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO CONVERT A BINARY NUMBER TO A DECIMAL NUMBER.


#include <iostream.h> #include <conio.h> #include <math.h> void main() { int i,r,count=0; long int b,n=0; clrscr(); cout<<"enter binary no="; cin>>b; while(b>0) { r=b%10; n=n+r*pow(2,count); count=count+1; b=b/10; } cout<<"\ndecimal no.="<<n; getch(); } /*******OUTPUT enter binary no=10111 decimal no.=23 */

Manjot Kaur (524)

SGHCMT

2\\WRITE A PROGRAM TO SUM OF DIGITS TO A NO. TO REDUCE IT UNTIL IT IS A SINGLE UNIT NO.
#include<iostream.h> #include<conio.h> class digit { private: int x,r,sum,sum1,r1; public: void dig(); }; void digit::dig() { cout<<"Enter a three digit no."<<endl; cin>>x; sum=0; while(x>0) { r=x%10; x=x/10; sum=sum+r; } if(sum>9) { sum1=0; while(sum>0) { r1=sum%10; sum=sum/10; sum1=sum1+r1; } cout<<"sum1:"<<sum1; }else { cout<<"Sum="<<sum; }} void main() { class digit d1; d1.dig(); getch(); }

Manjot Kaur (524)

SGHCMT /*******OUTPUT /* Enter a three digit no.115 Sum=7 */

Manjot Kaur (524)

SGHCMT

3 //WRITE A PROGRAM TO SHOW THE IMPORTANCE OF BREAK & CONTINUE STATEMENT.


#include<iostream.h> #include<conio.h> #include <stdlib.h> void main() { int i,z; clrscr(); cout<<endl<<"\n1.IMPORTANCE OF BREAK"; cout<<endl<<"\n2.IMPORTANCE OF CONTINUE"; do { cout<<endl<<"Which case = "; cin>>z; switch(z) { case 1: { cout<<endl<<"\n1.IMPORTANCE OF BREAK"; for(i=0;i<=10;i++) { if(i==5) { break; } cout<<"\n"<<i; } break; } case 2: { cout<<endl<<"\n1.IMPORTANCE OF CONTINUE"; for(i=0;i<=10;i++) { if(i==5) { continue; } cout<<"\n"<<i; } break; } } } 5 Manjot Kaur (524)

SGHCMT while(z<3); getch(); } /*******OUTPUT 1.IMPORTANCE OF BREAK 2.IMPORTANCE OF CONTINUE Which case = 1 1.IMPORTANCE OF BREAK 0 1 2 3 4 Which case = 2 S 1.IMPORTANCE OF CONTINUE 0 1 2 3 4 6 7 8 9 10 Which case = 3 */

Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO SHOW FACTORIAL OF A NUMBER USING FUNCTIONS.


#include<iostream.h> #include <conio.h> int fact(int x); void main() { clrscr(); int n,k; cout<<"\ninput the number="; cin>>n; k=fact(n); cout<<"\nfact="<<k; getch(); } int fact(int x) { int prod=1; if(x==0||x==1) prod=1; else { for(int i=2;i<=x;i++) prod=prod*i; } return(prod); }

/****** OUTPUT
input the number=6 fact=720 */

Manjot Kaur (524)

SGHCMT

5//WRITE A PROGRAM TO SUM OF SERIES x-x3/3!+x5/5!-x7/7! +..........n TERMS.


#include <iostream.h> #include <conio.h> #include <math.h> int main() { clrscr(); int n,i; float x,sum; float cal_ser(float,int); cout<<"enter x & n="; cin>>x>>n; sum=cal_ser(x,n); cout<<"sum of series="<<sum; getch(); } float cal_ser(float x,int n) { int fact(int); float s=0; for(int i=1;i<=n;i++) s=s+(pow(x,2*i-1)*pow(-1,i+1))/fact(2*i-1); return(s); } int fact(int m) { int f=1; for(int i=2;i<=m;i++) f=f*i; return(f); } /*******OUTPUT enter x & n=2 .03 sum of series=0.933333 */

Manjot Kaur (524)

SGHCMT

6//WRITE A PROGRAM TO SUM OF N TERMS USING DEFAULT ARGUMENTS


#include <iostream.h> #include <conio.h> void sum(int a=4,int b=6) { int c; cout<<a<<endl<<b<<endl; c=a+b; cout<<endl<<"Sum is:"<<c<<endl; return; } void main() { int a,b; clrscr(); sum(); sum(10); sum(5,6); getch(); } /*******OUTPUT 4 4 Sum is:8 12 12 Sum is:24 8 7 Sum is:15 */

Manjot Kaur (524)

SGHCMT

7//WRITE A PROGRAM TO FIND THE FIBONOCCI SERIES USING RECURSION


#include<iostream.h> #include<conio.h> int fib (int); //prototype void main() { int m,n,i; clrscr(); cout<<"enter numbers of terms="; cin>>n; for(i=1;i<=n;i++) cout<<fib(i)<<"\t"; getch(); } int fib(int m) { if(m==1||m==2) return(1); else return(fib(m-1)+fib(m-2)); } /*******OUTPUT enter numbers of terms=5 1 1 2 3 5 */

10

Manjot Kaur (524)

SGHCMT

9//WRITE A PROGRAM TO CALCULATE 2 INT, FLOAT, DOUBLE, CHAR NUMBER WITH FUNCTION OVERLOADING.
#include <iostream.h> #include <conio.h> void sum(int,int); void sum(float,float); void sum(double ,double ); void sum(char,char); void main() { int a,b; float c,d; double x,y; char m,n; clrscr(); sum(a,b); sum(c,d); sum(x,y); sum(m,n); getch(); } void sum(int a, int b) { int s; cout<<"the value of a & b="; cin>>a>>b; s=a+b; cout<<"\nthe value of sum in int is="<<s<<endl; return; } void sum(float c, float d) { float l; cout<<"\nthe value of c & d="; cin>>c>>d; l=c+d; cout<<"\nthe value of sum in float is="<<l<<endl; return; }

11

Manjot Kaur (524)

SGHCMT void sum(double x,double y) { double f; cout<<"\nthe value of x& y"; cin>>x>>y; f=x+y; cout<<"the value of double is="<<f<<endl; return; } Void sum(char m,char n) { int r; cout<<"\nthe value of m& n="; cin>>m>>n; r=m+n; cout<<"the value of char is="<<r; return; } /*******OUTPUT the value of a & b=9 6 the value of sum in int is=15 the value of c & d=2.5 5.7 the value of sum in float is=8.2 the value of x& y 2.00 3.56 the value of double is=5.56 the value of m& n=a b the value of char is=195*/

12

Manjot Kaur (524)

SGHCMT

8// SWAP 2 NO.S USING PASS BY VALUE, PASS BY ADDRESS, PASS BY REFERENCE
1) PASS BY VALUE #include <iostream.h> #include <conio.h> #include <stdlib.h> void swap(int a,int b); void main() { int a,b,z; clrscr(); cout<<"input the values of a & b"; cin>>a>>b; cout<<"\nthe value of a&b before swapping="<<a<<"\t"<<b; swap(a,b); getch(); } void swap(int a,int b) { int t; t=a; a=b; b=t; cout<<"\nthe value of a & b in called fun="<<a<<"\t"<<b<<endl; return; } /*******OUTPUT input the values of a & b 9 4 the value of a&b before swapping=9 4 the value of a & b in called fun=9 4 */

13

Manjot Kaur (524)

SGHCMT

2)PASS BY ADDRESS
#include <iostream.h> #include <conio.h> void swap(int *a,int *b); void main() { int a,b; clrscr(); cout<<"input the values of a & b"; cin>>a>>b; cout<<"\nthe value of a&b before swapping"<<"\t"<<a<<"\t"<<b; swap(&a,&b); cout<<"\nthe value of a&b after swapping"<<"\t"<<a<<"\t"<<b; getch(); } void swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; return; } /*****OUTPUT input the values of a & b 2 8 the value of a&b before swapping 2 the value of a&b after swapping 8 8 2*/

14

Manjot Kaur (524)

SGHCMT

3)PASS BY REFERENCE
#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { int x,y; clrscr(); void swap(int &i,int &j); cout<<"Enter the value of X & Y"<<endl; cin>>x>>y; swap (x,y); cout<<"After swap the variables are"<<endl; cout<<"Value of X is"<<x<<endl<<"Value of Y is"<<y<<endl; getch(); } void swap(int &i,int &j) { int k; k=i; i=j; j=k; } /*cout<<"***output***"; Enter the value of X & Y 4 6 5 After swap the variables are Value of X is6 Value of Y is4*/

15

Manjot Kaur (524)

SGHCMT

11//WRITE A PROGRAM TO INSERT & DELETE AN ELEMENT FROM AN ARRAY.


#include<iostream.h> #include<conio.h> void main() { int a[20],k,n,i,t,z,item,temp; clrscr(); cout<<"how many elements"; cin>>n; for(i=0;i<n;i++) { cout<<"enter the element"; cin>>a[i]; } cout<<endl<<"insertion & deletion from an array"; cout<<endl<<"insert an element from an array"; cout<<endl<<"delete an element from an array"; do { cout<<endl<<"which case do u want to execute="; cin>>z; switch(z) { case 1: { cout<<endl<<"INSERT AN ELEMENT"; cout<<"\nwhich location u want to insert the element"; cin>>k; cout<<"\nenter the element"; cin>>item; for(i=n-1;i>=k-1;i--) a[i+1]=a[i]; a[k-1]=item; n++; for(i=0;i<n;i++) cout<<"\n"<<a[i]; break; } case 2: { cout<<"\n DELETE AN ELEMENT"; cout<<"\nwhich location u want to delete the element"; cin>>k; temp=a[k]; 16

Manjot Kaur (524)

SGHCMT for(i=k;i<=n;i++) a[i]=a[i+1]; n--; for(i=0;i<n;i++) cout<<"\n"<<a[i]; break; } } }while(z<3); getch(); } /*******OUTPUT how many elements 4 enter the element12 enter the element23 enter the element34 enter the element56 insertion & deletion from an array insert an element from an array delete an element from an array which case do u want to execute=1 INSERT AN ELEMENT which location u want to insert the element1 enter the element90 90 12 23 34 56 which case do u want to execute=2 DELETE AN ELEMENT which location u want to delete the element3 90 12 23 56 which case do u want to execute=3

17

Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO FIND THE SUM OF 2 MATRIX BY PASSING A 2-D ARRAY TO FUNCTION.
#include<iostream.h> #include<conio.h> #include <stdlib.h> void input(int a[][10],int m,int n); void read(int b[][10],int p,int q); void add(int a[][10],int b[][10],int c[][10],int m,int n); void output(int c[][10],int m,int n); void main() { int a[10][10],b[10][10],c[10][10],m,n,p,q; clrscr(); cout<<"how many rows and columnns in A matrix"; cin>>m>>n; cout<<"how many rows and columnns in B matrix"; cin>>p>>q; if((m!=p)&&(n!=q)) { cout<<"addition is not possible"; getch(); exit(0); } input(a,m,n); read(b,m,n); add(a,b,c,m,n); output(c,m,n); getch(); } void input(int a[][10],int m,int n) { int i,j; cout<<"input the rows & columns of 1st matrix="; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin>>a[i][j]; } return; } void read(int b[][10],int p,int q) { int i,j; cout<<"input the rows & columns of 2nd matrix="; for(i=0;i<p;i++) 18 Manjot Kaur (524)

SGHCMT { for(j=0;j<q;j++) cin>>b[i][j]; } return; } void add(int a[][10],int b[][10],int c[][10],int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; } return; } void output(int c[][10],int m,int n) { int i,j; cout<<"\naddition is="; for(i=0;i<m;i++) { cout<<endl; for(j=0;j<n;j++) cout<<"\t"<<c[i][j]; } return; } /*cout<<"****output****"; how many rows and columnns in A matrix2 2 how many rows and columnns in B matrix2 2 input the rows & columns of 1st matrix=3 4 5 6 input the rows & columns of 2nd matrix=1 2 3 4 addition is= 4 8 6 10*/ 19 Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO CHECK A STRING IS PALINDROME OR NOT USING STRING FUNTION.


#include <iostream.h> #include <conio.h> #include <string.h> void main() { clrscr(); char a[20],c[20]; cout<<"enter the string:="; cin.get(a,20); strcpy(c,a); strrev(a); strlen(a); if(strcmp(c,a)==0) { cout<<"string="<<c<<"is palindrome"; } else cout<<c<<"is not palindrome"; getch(); } /******OUTPUT enter the string:=madam string= madam is palindrome */

20

Manjot Kaur (524)

SGHCMT

14//WRITE A PROGRAM TO DISPLAY THE EMPLOYEE DETAILS BY PASSING A STRUCTURE VARIABLE BY VALUE & THEN BY REERENCE TO A FUNCTION. 1)PASSED BY VALUE TO FUNCTION.
#include <iostream.h> #include <conio.h> struct employee { int emp_code; char emp_name[10]; int dept_code; double salary; }; void main() { clrscr(); void display(employee); employee emp1={512,"rahul",7,3612.3}; cout<<"employee details are:=\n"; display(emp1); getch(); } void display(employee e) { cout<<"employee code="<<e.emp_code; cout<<"\nname="<<e.emp_name; cout<<"\ndept. code="<<e.dept_code; cout<<"\nsalary="<<e.salary; return; } /******OUTPUT employee details are:= employee code=512 name=rahul dept. code=7 salary=3612.3 */

21

Manjot Kaur (524)

SGHCMT

PASSED BY REFERENCE TO FUNCTION


#include <iostream.h> #include <conio.h> struct employee { int emp_code; char emp_name[10]; int dept_code; double salary; }; void main() { clrscr(); void display(employee); void read(employee &); employee emp1; cout<<"\ninput the employee information=\n"; read(emp1); cout<<"employee details are:=\n"; display(emp1); getch(); } void read(employee &e) { cin>>e.emp_code>>e.emp_name>>e.dept_code>>e.salary; } void display(employee e) { cout<<"employee code="<<e.emp_code; cout<<"\nname="<<e.emp_name; cout<<"\ndept. code="<<e.dept_code; cout<<"\nsalary="<<e.salary; return; } /*******output***** input the employee information= 104 money 5 23.45 employee details are:= employee code=104 name=money dept. code=5 salary=23.45 */ 22 Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO SHOW THE USE OF NESTED STRUTURE.


#include <iostream.h> #include <conio.h> struct date { int date,month,year; }; struct person { int age; float weight; struct date dob; }; void main() { clrscr(); person p1; cout<<"Enter age and weight:"<<endl; cin>>p1.age>>p1.weight; cout<<"Enter date,month,year"<<endl; cin>>p1.dob.date>>p1.dob.month>>p1.dob.year; cout<<"Date:"<<p1.dob.date <<"\t"; cout<<"Month:"<<p1.dob.month<<"\t"; cout<<"Year:"<<p1.dob.year; getch(); } /******OUTPUT****** Enter age and weight: 18 48 Enter date,month,year 28 12 1989 Date:28 Month:12 Year:1989*/

23

Manjot Kaur (524)

SGHCMT

15//WRITE A PROGRAM TO DISPLAY THE SIZE OF UNION & STRUCTURE.


#include <iostream.h> #include <conio.h> struct student1 { int rollno; char name[20]; float marks; }; union student2 { int rollno; char name[20]; float marks; }; void main() { clrscr(); struct student1 s1; union student2 s2; cout<<"memory requirement for structure = "; cout<<sizeof(s1); cout<<"\n memory requirement for union="; cout<<sizeof(s2); getch(); } /******OUTPUT memory requirement for structure=26 memory requirement for union=20 */

24

Manjot Kaur (524)

SGHCMT 16 //WRITE A PROGRAM TO SHOW THE VARIOUS

OPERATIONS THAT CAN BE PERFORMED ON POINTERES.


#include <iostream.h> #include <conio.h> void main() { int a[5],i,n,*p,*j; p=a; j=p+2; clrscr(); cout<<"input the sizeof array="; cin>>n; for(i=0;i<n;i++) { cout<<"input the element="; cin>>a[i]; } cout<<"the value at p="<<*p<<endl; cout<<"the value at p="<<*(p++)<<endl; cout<<"the value at p="<<*(--p)<<endl; cout<<"the value at p="<<*j<<endl; cout<<"the value at p="<<*(--j)<<endl; getch(); } /*cout<<"*****output*****"; input the sizeof array=3 input the element=23 input the element=34 input the element=45 the value at p=23 the value at p=23 the value at p=23 the value at p=45 the value at p=34*/

25

Manjot Kaur (524)

SGHCMT

19//WRITE A PROGRAM TO FIND THE FACTORIAL OF A NO. USING POINTER TO FUNCTION.


#include<iostream.h> #include <conio.h> int fact(int); void main() { int n; int (*ptr)(int); clrscr(); cout<<"enter no.=:"; cin>>n; ptr=fact; (*ptr)(n); cout<<"fatorial of the no.="<<(*ptr)(n); getch(); } int fact(int n) { if(n==1) return 1; return(n*fact(n-1)); } /******OUTPUT enter no.=:5 fatorial of the no.=120 */

26

Manjot Kaur (524)

SGHCMT

22//WRITE A PROGRAM TO CALCULATE BASIC SALARY.


#include<iostream.h> #include<conio.h> class emp { float gross,sal; float da() { float d; d=sal*0.5; return(d); } float hra() { float h; h=sal*(10.0/100); return(h); } public: void get(); void dis(); }; void emp ::get() { float a,b; cout<<"Enter basic salary"; cin>>sal; a=da(); b=hra(); gross=sal+a+b; } void emp ::dis() { cout<<"gross salary is"<<gross; } void main() { clrscr(); emp e; e.get(); e.dis(); getch(); } /*******OUTPUT Enter basic salary 2000 gross salary is3200 */ 27 Manjot Kaur (524)

SGHCMT

23//WRITE A PROGRAM TO ADD 2 TIMES BY PASSING OBJECTS BY VALUE.


#include<iostream.h> #include<conio.h> class time { int hours,min; public: void read(); void add(time tt1,time tt2); void show(); }; void time::read() { cout<<"\nenter hrs and min:"; cin>>hours>>min; } void time::add(time tt1,time tt2) { int minute=tt1.min+tt2.min; int hrs=minute/60; min=minute%60; hours=hrs+tt1.hours+tt2.hours; } void time::show() { cout<<"\n time is:"; cout<<hours<<":"<<min; } void main() { clrscr(); time t1,t2,t3; t1.read(); t2.read(); t3.add(t1,t2); t3.show(); getch(); } /******OUTPUT enter hrs and min:3 15 enter hrs and min:1 12 time is:4:27 */ 28 Manjot Kaur (524)

SGHCMT

24//WRITE A PROGRAM TO INPUT & DISPLAY THE DETAILS OF STUDENTS USING ARRAY OF OBJECTS.
#include<iostream.h> #include <conio.h> class student { private: char name[10]; int rolno; public: void input() { cout<<"enter name and rollno:"<<endl; cin>>name>>rolno; return; } void show() { cout<<"\nstudent record="; cout<<"name:"<<name<<endl<<"rolno"<<rolno<<endl; return; } }; void main() { clrscr(); int i,n; student s[10]; cout<<"\n how many students="; cin>>n; for(i=1;i<=n;i++) s[i].input(); for(i=1;i<=n;i++) { s[i].show(); } getch(); } /*******OUTPUT how many students=1 enter name and rollno: anshu 17 student record= name:anshu rolno 17 */ 29 Manjot Kaur (524)

SGHCMT

25//WRITE A PROGRAM TO SHOW THE USE O STATIC DATA MEMBERS & STATIC MEMBER FUNCTIONS.
#include <iostream.h> #include <conio.h> class account { int acc_no; double balance; static double rate; public: void read() { cout<<"input account number and balance->"; cin>>acc_no>>balance; } void show() { cout<<"\n account no="<<acc_no; cout<<"\ninterest="<<rate; cout<<"\nbalance="<<balance; } void qtr_rate_cal() { double interest=(balance*rate*0.25)/100; balance=balance+interest; } static void modify_rate(double incr) { rate=rate+incr; cout<<"\n modifying rate of interest="<<rate; } }; double account ::rate=0.05; void main() { clrscr(); account a1,a2; a1.read(); a2.read(); account::modify_rate(0.01); a1.qtr_rate_cal(); a2.qtr_rate_cal(); a1.show(); a2.show(); 30 Manjot Kaur (524)

SGHCMT getch(); } /*******OUTPUT input account number and balance->23 1000 input account number and balance->24 1200 modifying rate of interest=0.06 account no=23 interest=0.06 balance=1000.15 account no=24 interest=0.06 balance=1200.18

*/

31

Manjot Kaur (524)

SGHCMT

20//WRITE A PROGRAM TO SHOW THE USE OF VOID POINTER &THIS POINTER. 1)VOID POINTER
#include <iostream.h> #include <conio.h> void main() { clrscr(); void *p; int a=7; float b=12.5; p=&a; cout<<"a="<<*((int *)p)<<endl; p=&b; cout<<"b="<<*((float *)p)<<endl; getch(); } /* cout<<"***output***"; a=7 b=12.5*/

2)THIS POINTER #include <iostream.h> #include <conio.h> class rectangle { int l,b; public: void setdata(int i,int j) { cout<<"\naddress of object that generates setdata()all:"<<this<<"\n"; this->l=i; this->b=j; } void area() { cout<<"\naddress of object that generates setdata()all:"<<this<<"\n"; cout<<"area="<<this->l*this->b; } }; 32 Manjot Kaur (524)

SGHCMT void main() { rectangle r1; r1.setdata(10,15); r1.area(); getch(); } /*******OUTPUT address of object that generates setdata()all:0x8fc5fff2 address of object that generates setdata()all:0x8fc5fff2 area=150 */

33

Manjot Kaur (524)

SGHCMT

18//WRITE A PROGRAM TO CALCULATE THE AVERAGE O N NO.S OF AN ARRAY BY ALLOCATING & DEALLOCATING MEMORY DYNAMICALLY.
#include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int *p; int n,sum=0; cout<<"neter how many elements="; cin>>n; p=new int[n];//allocates memory for n 'int'values dynamically i(p==null) if(p==NULL) { cout<<"memory allocation error"; exit(0); } for(int i=0;i<n;i++) { cout<<"enter value :"<<i+1<<"="; cin>>*(p+i); sum=sum+*(p+i); } float avg=float(sum)/n; cout<<"average o given value = "<<avg; delete[] p; getch(); } /*******OUTPUT neter how many elements=4 enter value :1=25 enter value :2=15 enter value :3=17 enter value :4=28 average of given value = 21.25

*/

34

Manjot Kaur (524)

SGHCMT 27//WRITE A PROGRAM TO SHOW VARIOUS TYPES OF

CONSTRUCTORS. 1)DEAULT CTOR


#include <iostream.h> #include <conio.h> class area { int l,b; public: area() { l=5;b=10; } void display(); }; void area:: display() { cout<<"\narea of rectangle=\n"<<l*b; return; } void main() { clrscr(); area r; r.display(); getch(); } /*cout<<"***output***"; area of rectangle=50*/ 2)PARAMETERIZED CTOR #include <iostream.h> #include <conio.h> class area { int l,b; public: area(int a,int c) { l=a; b=c; } void display(); 35 Manjot Kaur (524)

SGHCMT }; void area:: display() { cout<<"\narea of rectangle=\n"<<l*b; return; } void main() { clrscr(); area r(6,4); r.display(); getch(); } /*******OUTPUT area of rectangle= 24 */ 3)COPY CTOR #include <iostream.h> #include <conio.h> class test { int a; public: test(int x) { a=x; } test(test &ob) { a=ob.a; } void display(); }; void test::display() { cout<<"\nvalue of a"<<a; return; } void main() { clrscr(); test t1(20); test t2(t1); t1.display(); t2.display(); getch(); 36 Manjot Kaur (524)

SGHCMT } /********OUTPUT value of a=20 value of a=20 */ 4)DYNAMIC CTOR #include <iostream.h> #include <conio.h> class height { int feet; double inches; public: height(int f) { feet=f; inches=0.0; } height( double f) { feet=int(f); inches=(f-int(f))*12.0; } height() { feet=inches=0; } height(int f,double in) { feet=f; inches=in; } void show() { cout<<"Feet="<<feet<<"inches="<<inches<<"\n"; } }; void main() { clrscr(); height h1,h2,h3; h1=height(5); h2=height(5.5); h3=height(5.8); h1.show(); h2.show(); 37

Manjot Kaur (524)

SGHCMT h3.show(); getch(); } /******OUTPUT Feet=5inches=0 Feet=5inches=6 Feet=5inches=9.6 */ 5)MULTIPLE CTOR #include <iostream.h> #include <conio.h> class area { int l,b; public: area(int a) { l=4; b=a; } area(int m,int n) { l=m; b=n; } void cal(); }; void area::cal() { cout<<"\narea is="<<l*b; } void main() { clrscr(); area a2(5); area a3(6,9); a1.cal(); a2.cal(); a3.cal(); getch(); } /******OUTPUT area is=20 area is=54 */

38

Manjot Kaur (524)

SGHCMT

28//WRITE A PROGRAM TO SHOW THE USES OF NESTED & CONTAINER CLASSES.


#include <iostream.h> #include <conio.h> class student { int rno; char name[10]; public: class date { int dd,mm,yy; public: void read(); void show(); }dob; void read(); void show(); }; void student::date::read() { cin>>dd>>mm>>yy; } void student::date::show() { cout<<"\ndate="<<dd<<"\nmonth="<<mm<<"\nyear="<<yy; } void student::read() { cout<<"\ninput roll no & name="; cin>>rno>>name; cout<<"\ninput the date of birth ="; dob.read(); } void student::show() { cout<<"roll no="<<rno<<"\nname"<<name; dob.show(); } void main() { clrscr(); student s1; s1.read(); s1.show(); 39 Manjot Kaur (524)

SGHCMT getch(); } /*******OUTPUT input roll no & name=524 money input the date of birth =1-12-1989 roll no=524 name money date=1 month=12 year=1989 */

40

Manjot Kaur (524)

SGHCMT

29//WRITE A PROGRAM TO SHOW THE USE OF FRIEND FUNTION & FRIEND CLASSES.
1)FRIEND FUNCTION /*#include <iostream.h> #include <conio.h> class test { int a; public: test(int x) { a=x; } friend void display(test t1); }; void display(test t1) { cout<<"value of a->"<<t1.a; } void main() { clrscr(); test t(5); display(t); getch(); } /*******OUTPUT value of a->5 */

//FRIEND CLASSES #include<iostream.h> #include <conio.h> class xxx { int x,y; public: xxx(int xx,int yy) { x=xx; 41 Manjot Kaur (524)

SGHCMT y=yy; } friend class yyy; }; class yyy { public: void f1(xxx objx) { cout<<"\nx="<<objx.x; } void f2(xxx objy) { cout<<"\ny="<<objy.y; } }; void main() { xxx ob1(10,15); yyy ob2; ob2.f1(ob1); ob2.f2(ob1); getch(); } /*******OUTPUT x=10 y=15 x=10 y=15

*/

42

Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO ADD 2 COMPLEX NO.S USING CONSTRUCTORS & ALSO SHOW THE WORKING OF DESTRUCTOR.
#include<iostream.h> #include<conio.h> class complex { private: int x,y,a,b,z,w; public: complex() { a=b=x=y=11; } complex(int c) { x=y=c; } complex (int m, int n) { a=m; b=n; } void add_disp(complex c1,complex c2) { z=c1.x+c2.a; w=c1.y+c2.b; cout<<endl<<"sum is "<<z<<" + "<<w<<"i"; } ~complex() { cout<<"\ndistructor invoked"<<z<<"+ "<<w<<"i"; } }; //end of class void main() { complex c1(28); complex c2(31,17); complex c3; c3.add_disp(c1,c2); getch(); } /********OUTPUT sum is 59 + 45i distructor invoked-14+ 10177i distructor invoked0+ 8105i */ 43 Manjot Kaur (524)

SGHCMT

12//WRITE A PROGRAM TO INSERT & DELET A STRING FROM ARRAY OF STRINGS.


#include<iostream.h> #include<conio.h> #include <string.h> #include <stdlib.h>; void main() { int k,n,i,t,z,item,temp,pos,len; char a[20][20],b[20]; clrscr(); cout<<"how many no. of strings="; cin>>n; for(i=0;i<n;i++) { cout<<"enter the strings"; cin>>a[i]; } cout<<endl<<"insertion & deletion from an array of strings"; cout<<endl<<"insert an element from an array of string"; cout<<endl<<"delete an element from an array of string"; do { cout<<endl<<"which case do u want to execute="; cin>>z; switch(z) { case 1: { cout<<"enter the position where u want insert="; cin>>pos; if(pos>n) { cout<<"d POSITION entered is out of range"; getch(); exit(0); } cout<<"enter d string to insert="; cin>>b; for(i=n;i>=pos;i--) strcpy(a[i+1],a[i]); strcpy(a[pos],b); n++; cout<<"\n elements are="; 44 Manjot Kaur (524)

SGHCMT for(i=0;i<n;i++) cout<<a[i]<<"\n"; break; } case 2: { cout<<"enter the position from where the element is to be deleted="; cin>>pos; fsor(i=pos-1;i<=n;i++) strcpy(a[i],a[i+1]); n--; cout<<"\n elements are="; for(i=0;i<n;i++) cout<<a[i]<<"\n"; } } }while(z<3); getch(); } /*******OUTPUT how many no. of strings=2 enter the strings money enter the strings sheenu insertion & deletion from an array of strings insert an element from an array of string delete an element from an array of string which case do u want to execute=1 enter the position where u want insert=1 enter d string to insert=rajni elements are=money rajni sheenu which case do u want to execute=3 */

45

Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO SHOW VARIOUS TYPES OF INHERITANC.


1)SINGLE INHERITANCE #include<iostream.h> #include<conio.h> class base { int roll_no; char name[20]; public: void datain() { cout<<"ENTER ROLL NUMBER OF A STUDENT:- "; cin>>roll_no; cout<<"ENTER NAME OF A STUDENT:"; cin>>name; } void dataout() { cout<<"\n\t\t\t"<<"------INFORMATION OF A STUDENT------"<<endl<<endl; cout<<"ROLL NUMBER OF A STUDENT:- "<<roll_no<<endl; cout<<"NAME OF A STUDENT:"<<name<<endl; } }; class child:public base { int age,mo; public: void childin() { datain(); cout<<"ENTER AGE OF A STUDENT:- "; cin>>age; cout<<"ENTER MARKS OF A STUDENT:- "; cin>>mo; } void childout() { dataout(); cout<<"AGE OF A STUDENT:- "<<age<<endl; cout<<"MARKS OF A STUDENT:- "<<mo<<endl; } }; void main() 46 Manjot Kaur (524)

SGHCMT { child x; clrscr(); x.childin(); x.childout(); getch(); } /*********OUTPUT ENTER ROLL NUMBER OF A STUDENT:- 507 ENTER NAME OF A STUDENT:sudesh ENTER AGE OF A STUDENT:19 ENTER MARKS OF A STUDENT:85 ------INFORMATION OF A STUDENT-----ROLL NUMBER OF A STUDENT:- 507 NAME OF A STUDENT:sudesh AGE OF A STUDENT:19 MARKS OF A STUDENT:85 2)MULTIPLE INHERITANCE #include<iostream.h> #include<conio.h> class student { int roll_no; char name[20]; public: void getdata() { cout<<"ENTER ROLL NUMBER OF A STUDENT:- "; cin>>roll_no; cout<<"ENTER NAME OF A STUDENT:"; cin>>name; } void display() { cout<<"\n\t\t\t"<<"------INFORMATION OF STUDENT-----"<<endl<<endl; cout<<"ROLL NUMBER OF A STUDENT:- "<<roll_no<<endl; cout<<"NAME OF A STUDENT:"<<name<<endl; } }; class sports { char game[20]; public: Manjot Kaur 47 (524)

SGHCMT void getgame() { cout<<"ENTER THE NAME OF GAME:"; cin>>game; } void putgame() { cout<<"THE NAME OF GAME:"<<game<<endl; } }; class test:public student,public sports { int sub1,sub2; public: void readdata(); void putdata(); }; void test::readdata() { getdata(); cout<<"ENTER MARKS OF THE SUBJECT 1:- "; cin>>sub1; cout<<"ENTER MARKS OF THE SUBJECT 2:- "; cin>>sub2; getgame(); } void test::putdata() { display(); cout<<"MARKS OF SUBJECT 1:"<<sub1<<endl; cout<<"MARKS OF SUBJECT 2:"<<sub2<<endl; putgame(); } void main() { test x; clrscr(); x.readdata(); x.putdata(); getch(); } /*********OUTPUT ENTER ROLL NUMBER OF A STUDENT:- 23 ENTER NAME OF A STUDENT:money ENTER MARKS OF THE SUBJECT 1:- 67 ENTER MARKS OF THE SUBJECT 2:- 78 ENTER THE NAME OF GAME:cricket 48

Manjot Kaur (524)

SGHCMT ------INFORMATION OF STUDENT----ROLL NUMBER OF A STUDENT:- 23 NAME OF A STUDENT:money MARKS OF SUBJECT 1:67 MARKS OF SUBJECT 2:78 THE NAME OF GAME:cricket 31

*/

3)MULTILEVEL INHERITANCE

#include<iostream.h> #include<conio.h> class student { int roll_no; char name[25]; public: void getdata() { cout<<"ENTER ROLL NUMBER OF A STUDENT:- "; cin>>roll_no; cout<<"ENTER NAME OF A STUDENT:"; cin>>name; } void dataout() { cout<<"\n\t\t\t"<<"------INFORMATION OF A STUDENT------"<<endl<<endl; cout<<"ROLL NUMBER OF A STUDENT:- "<<roll_no<<endl; cout<<"NAME OF A STUDENT:"<<name<<endl; } }; class test:public student { protected: int sub1,sub2; public: void readdata() { getdata(); cout<<"ENTER MARKS OF THE SUBJECT 1:- "; cin>>sub1; cout<<"ENTER MARKS OF THE SUBJECT 2:- "; cin>>sub2; } Manjot Kaur 49 (524)

SGHCMT void putdata() { dataout(); cout<<"MARKS OF SUBJECT 1:cout<<"MARKS OF SUBJECT 2:} }; class total:public test { int total; public: void compute() { total=sub1+sub2; cout<<"TOTAL MARKS:} }; void main() { total x; clrscr(); x.readdata(); x.putdata(); x.compute(); getch(); } /*********OUTPUT ENTER ROLL NUMBER OF A STUDENT:- 507 ENTER NAME OF A STUDENT:sudesh ENTER MARKS OF THE SUBJECT 1:- 87 ENTER MARKS OF THE SUBJECT 2:- 75 ------INFORMATION OF A STUDENT-----ROLL NUMBER OF A STUDENT:- 507 NAME OF A STUDENT:sudesh MARKS OF SUBJECT 1:87 MARKS OF SUBJECT 2:75 TOTAL MARKS:162

"<<sub1<<endl; "<<sub2<<endl;

"<<total;

50

Manjot Kaur (524)

SGHCMT //HIERARCHIAL INHRITANCE #include<iostream.h> #include<conio.h> class student { int roll_no; char name[25]; public: void getdata() { cout<<"ENTER ROLL NUMBER OF A STUDENT:- "; cin>>roll_no; cout<<"ENTER NAME OF A STUDENT:"; cin>>name; } void display() { cout<<"\n\t\t\t"<<"------INFORMATION OF A STUDENT------"<<endl<<endl; cout<<"ROLL NUMBER OF A STUDENT:- "<<roll_no<<endl; cout<<"NAME OF A STUDENT:"<<name<<endl; } }; class bsc:public student { int sub1,sub2,sub3; public: void readdata() { getdata(); cout<<"ENTER MARKS OF THE SUBJECT 1:- "; cin>>sub1; cout<<"ENTER MARKS OF THE SUBJECT 2:- "; cin>>sub2; cout<<"ENTER MARKS OF THE SUBJECT 3:- "; cin>>sub3; } void putdata() { display() cout<<"MARKS OF SUBJECT 1:- "<<sub1<<endl; cout<<"MARKS OF SUBJECT 2:- "<<sub2<<endl; cout<<"MARKS OF SUBJECT 3:- "<<sub3<<endl; } }; class bcom:public student Manjot Kaur 51 (524)

SGHCMT { int sub1,sub2,sub3,sub4,sub5; public: void readdata() { cout<<"\n\t\t\t"<<"------INFORMATION OF SECOND STUDENT------"<<endl<<endl; getdata(); cout<<"ENTER MARKS OF THE SUBJECT 1:- "; cin>>sub1; cout<<"ENTER MARKS OF THE SUBJECT 2:- "; cin>>sub2; cout<<"ENTER MARKS OF THE SUBJECT 3:- "; cin>>sub3; cout<<"ENTER MARKS OF THE SUBJECT 4:- "; cin>>sub4; cout<<"ENTER MARKS OF THE SUBJECT 5:- "; cin>>sub5; } void putdata() { display(); cout<<"MARKS OF SUBJECT 1:"<<sub1<<endl; cout<<"MARKS OF SUBJECT 2:"<<sub2<<endl; cout<<"MARKS OF SUBJECT 3:"<<sub3<<endl; cout<<"MARKS OF SUBJECT 4:"<<sub4<<endl; cout<<"MARKS OF SUBJECT 5:"<<sub5<<endl; } }; void main() { bsc x; bcom y; clrscr(); x.readdata(); x.putdata(); y.readdata(); y.putdata(); getch(); }

52

Manjot Kaur (524)

SGHCMT

/*********OUTPUT*********** ENTER ROLL NUMBER OF A STUDENT:- 507 ENTER NAME OF A STUDENT:sudesh ENTER MARKS OF THE SUBJECT 1:- 64 ENTER MARKS OF THE SUBJECT 2:- 75 ENTER MARKS OF THE SUBJECT 3:- 54 ------INFORMATION OF A STUDENT-----ROLL NUMBER OF A STUDENT:- 507 NAME OF A STUDENT:sudesh MARKS OF SUBJECT 1:64 MARKS OF SUBJECT 2:75 MARKS OF SUBJECT 3:54 ------INFORMATION OF SECOND STUDENT-----ENTER ROLL NUMBER OF A STUDENT:- 284 ENTER NAME OF A STUDENT:sudesh ENTER MARKS OF THE SUBJECT 1:- 75 ENTER MARKS OF THE SUBJECT 2:- 64 ENTER MARKS OF THE SUBJECT 3:- 85 ENTER MARKS OF THE SUBJECT 4:- 75 ENTER MARKS OF THE SUBJECT 5:- 86 ------INFORMATION OF A STUDENT-----ROLL NUMBER OF A STUDENT:- 284 NAME OF A STUDENT:sudesh MARKS OF SUBJECT 1:75 MARKS OF SUBJECT 2:64 MARKS OF SUBJECT 3:85 MARKS OF SUBJECT 4:75 MARKS OF SUBJECT 5:86 */

53

Manjot Kaur (524)

SGHCMT //HYBRID INHERITANCE #include <iostream.h> #include <conio.h> class student { protected: int rno; public: void get_no(int); void put_no(); }; void student::get_no(int x) { rno=x; } void student::put_no() { cout<<"roll no="<<rno; } class test:public student { protected: int part1,part2; public: void get_marks(int,int); void put_marks(); }; void test ::get_marks(int a,int b) { part1=a; part2=b; } void test::put_marks() { cout<<"\nmarks obtained="<<part1<<"\t"<<part2; } class sport { protected: int score; public: void get_score(int); void put_score(); }; void sport::get_score(int m) 54

Manjot Kaur (524)

SGHCMT { score=m; } void sport::put_score() { cout<<"\nscore is="<<score; } class report:public test,public sport { public: int result1; void result(); }; void report::result() { result1=part1+part2+score; put_no(); put_marks(); put_score(); cout<<"\ntotal"<<result1; } void main() { clrscr(); report r; r.get_no(3); r.get_marks(40,50); r.get_score(45); r.result(); getch(); } /******OUTPUT roll no=3 marks obtained=40 score is=45 total 135 */ 50

55

Manjot Kaur (524)

SGHCMT

32//WRITE A PROGRAM TO SHOW FUNCTION OVERRIDING.


#include <iostream.h> #include <conio.h> class person //base class { private: char name[20]; int phno; public: void read() { cout<<"enter name and phno="<<endl; cin>>name>>phno; } void show() { cout<<"name="<<name<<endl; cout<<"phone no="<<phno<<endl; } }; class student:public person //publicaly derived class { private: int rollno; char course[20]; public: void read() { person::read(); //referers to read() of 'person' class cout<<"enter rollno and course="<<endl; cin>>rollno>>course; } void show() { person::show();//refers to show() of person class cout<<"rollno="<<rollno<<endl; cout<<"course="<<course<<endl; } }; void main() { clrscr(); 56 Manjot Kaur (524)

SGHCMT student s1; s1.read(); cout<<"displaying student information"; s1.show(); getch(); } /*********OUTPUT enter name and phno= bhanu 567 enter rollno and course= 1 bca displaying student information name=bhanu phone no=567 rollno=1 course=bca */

57

Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO SHOW CONSTRUCTOR INHERITANCE IN DERIVED CLASSES.


#include<iostream.h> #include<conio.h> class alpha { int x; public: alpha(int i) { x=i; cout<<endl<<"Alpha initialized"; } void show_x(void) { cout<<"x ="<<x<<"\n"; } }; class beta{ float y; public: beta(float j) {y=j; cout<<endl<<"Beta initialized "; } void show_y() { cout<<"y = "<<y; } }; class gamma:public beta,public alpha { int m,n; public: gamma(int a,float b,int c,int d): alpha(a),beta(b) { m=c;n=d; cout<<endl<<"Gamma initialized "; } void show_mn() { cout<<endl<<"m= "<<m; cout<<endl<<"n= "<<n; } }; 58 Manjot Kaur (524)

SGHCMT void main() { clrscr(); gamma g(5,2.5,2,3); cout<<endl; g.show_x(); g.show_y(); g.show_mn(); getch(); } /********OUTPUT Beta initialized Alpha initialized Gamma initialized x =5 y = 2.5 m= 2 n= 3 */

59

Manjot Kaur (524)

SGHCMT

4//WRITE A PROGRAM TO SHOW THE USE OF MANIPULATER (SETW,SETFILL,SETWIDTH,HEX,DEC,OCT,SET PRECISION)USING INLINE FUCTION.
#include<iostream.h> #include<conio.h> #include<iomanip.h> inline void setfil() { int n=17; float f=90.8345; cout<<"n="<<setw(11)<<setfill('#')<<n<<endl; cout<<"float="<<setprecision(2)<<setfill('~')<<f; } inline void setw() { int age=34; char a='x'; float f=89.1; cout<<"age is"<<setw(5)<<age<<endl; cout<<"character is "<<setw(4)<<a<<endl; cout<<"f is "<<setw(4)<<f<<endl; } inline void setbase() { int n,n1,n2; cout<<"enter n="; cin>>n; cout<<"enter n1="; cin>>hex>>n1; cout<<"enter n2="; cin>>oct>>n2; cout<<"hexadecimal eqivalent="<<hex<<n<<endl; cout<<"decimal equivalent="<<dec<<n<<endl; cout<<"octal equivalent="<<oct<<n<<endl; cout<<"hexadecimal eqivalent="<<hex<<n1<<endl; cout<<"decimal equivalent="<<dec<<n1<<endl; cout<<"octal equivalent="<<oct<<n1<<endl; cout<<"hexadecimal eqivalent="<<hex<<n2<<endl; cout<<"decimal equivalent="<<dec<<n2<<endl; cout<<"octal equivalent="<<oct<<n2<<endl; } 60 Manjot Kaur (524)

SGHCMT void main() { clrscr(); setfil(); setw(); setbase(); getch(); } /*********OUTPUT n=#########17 float=90.83age is~~~34 character is x f is 89.1 enter n=2 enter n1=1 enter n2=3 hexadecimal eqivalent=2 decimal equivalent=2 octal equivalent=2 hexadecimal eqivalent=1 decimal equivalent=1 octal equivalent=1 hexadecimal eqivalent=3 decimal equivalent=3 octal equivalent=3 */

61

Manjot Kaur (524)

SGHCMT

10//WRITE A PROGRAM TO CREATE A FUNCTION pow() TO RAISE A NO. N TO POWER N.USE THE CONCEPT OF FUNCTION OVERLODING TO CALCULATE 5(3),N(3),N(N).
#include<iostream.h> #include<conio.h> void main() { int i,n,p=1; clrscr(); for(i=1;i<=3;i++) p=p*5; cout<<"value of 5 raise to power 3="<<p; cout<<endl<<"enter value of n="; cin>>n; p=1; for(i=1;i<=3;i++) p=p*n; cout<<"value of n raise to power 3="<<p; p=1; for(i=1;i<=n;i++) p=p*n; cout<<endl<<"value of n raise to power n="<<p; getch(); } /******output**** value of 5 raise to power 3=125 enter value of n=3 value of n raise to power 3=27 value of n raise to power n=27*/

62

Manjot Kaur (524)

SGHCMT

//WRITE A PROGRAM TO REVERSE A STRING.


#include <iostream.h> #include <conio.h> #include <string.h> #include <stdio.h> void main() { char a[10]; int i,s; clrscr(); cout<<"enter any string="; gets(a); cout<<"name is="<<a<<endl; s=strlen(a); cout<<"reverse of string is="; for(i=s;i>=0;i--) cout<<a[i]; getch(); } /*******OUTPUT enter any string=aman name is=aman reverse of string is=nama

*/

63

Manjot Kaur (524)

SGHCMT

64

Manjot Kaur (524)

Das könnte Ihnen auch gefallen