Vous êtes sur la page 1sur 119

Page |1

PROGRAM-1

//wap to accept item code,quantity and unit price of item //compute the total amount and display the details of item . #include<iostream.h> #include<conio.h> void main() { clrscr(); int code,qua,price,unit; cout<<"enter the item_code "; cin>>code; cout<<"enter the quatity of item you want to purchase"; cin>>qua; cout<<"price of one item"; cin>>unit; price=unit*qua; cout<<"\n\n\nThe details for the item are : "; cout<<"\nItem code : "<<code; cout<<"\nquantity : "<<qua; cout<<"\nprice of "<<qua<<" item of code "<<code<<" is "<<price; getch(); }

Page |2

Page |3

PROGRAM-2

//wap to accept employee code and basic salry. compute the total salaary //after adding DA and HRA and display the details for the employee . #include<iostream.h> #include<conio.h> void main() { clrscr(); char name[10]; int sal,code; float da,hra,tsal; cout<<"enter the name of employee : "; cin>>name; cout<<"enter the employee code : "; cin>>code; cout<<"enter the basic salary of employee : "; cin>>sal; if(sal>=7000) { da=sal*0.3; hra=sal*0.2; } else {

Page |4 da=sal*0.2; hra=sal*0.1; } tsal=sal+da+hra; cout<<"\nThe detail of the employee is "; cout<<"\nName :"<<name; cout<<"\nCode :"<<code; cout<<"\n Basic salary :"<<sal; cout<<"\nNet salary :"<<tsal; getch(); }

Page |5

PROGRAM-3

//wap that accept info abt student like name,rollno,marks in 5 subject //and after assigning the grade displaying the details of the student #include<iostream.h> #include<conio.h> void main() { clrscr(); int roll,arr[5],sum=0,per,i; char name[10],grade; cout<<"enter the rollno. of the student "; cin>>roll; cout<<"enter the name of student "; cin>>name; cout<<"enter the marks in 5 subjects"; for(i=0;i<5;i++) cin>>arr[i]; for(i=0;i<5;i++) sum+=arr[i]; per=(sum/5); if(per>90) grade='A'; if(per>60 && per<80) grade='B';

Page |6 if(per>40 && per<60) grade='C'; if(per<40) grade='D'; cout<<"\nTHe details of the student is : "; cout<<"\nNama : "<<name; cout<<"\nRollno :"<<roll; for(i=0;i<5;i++) cout<<"\nmarks in "<<i+1<<" subject is " <<arr[i]; cout<<"\ntotal marks are "<<sum; cout<<"\naverage marks :"<<per; cout<<"\ngrade of the student is : " <<grade; getch(); }

Page |7

PROGRAM-4

//wap to create a calculator for performing arithmetic code: +,-,*,/,% //4.WAP to create a calculator for performing arithmetic //calculations(+,-,*,/) depending upon user choice.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,temp; char ch; cout<<"enter two numbers"; cin>>a>>b; cout<<"enter your choice(+,-,/,*) "; cin>>ch; switch(ch) { case '+': temp = a+b; cout<<"result is "<<temp; break; case '-': temp = a-b; cout<<"result is "<<temp; break;

Page |8 case '*': temp = a*b; cout<<"result is "<<temp; break;

case '/': if(b==0) { cout<<"divide by zero error"; break;} temp = a/b; cout<<"result is "<<temp; break; default: cout<<"wrong choice"; break; } getch(); }

Page |9

PROGRAM-5

//wap to print the product of 5*6*7*8*9*10 #include<iostream.h> #include<conio.h> void main() { int i,res=1 ; clrscr(); cout<<"5*6*7*8*9*10 = "; for(i=5;i<=10;i++) res*=i; cout<<res; getch(); }

P a g e | 10

PROGRAM-6

//wap to check whether the number is palindrome or not // #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,num,dig,rev=0; cout<<"input the number= "; cin>>num; do { dig=num%10; rev=rev*10+dig; num=num/10; } while(num!=0); cout<<"\nthe reverse of the number is : "<<rev; if(n==rev) cout<<"\nthe number is palindrome"; else cout<<"\nthe number is not palindrome"; getch();

P a g e | 11 }

P a g e | 12

PROGRAM-7

//wap to check whether the number is prime or not // #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,flag ; cout<<"\n enter the number "; cin>>n; if(n==2 || n==3) cout<<"number is prime "; for(int i=2 ;i<n/2;i++) { if(n%i==0) { flag=1; break; } else flag=0; } if(flag) cout<<"\n number is prime"; else cout<<"\n number is not prime "; getch(); }

P a g e | 13

P a g e | 14

PROGRAM-8

//wap to print name, address, salary and age of a person #include<iostream.h> #include<conio.h> void main() { clrscr(); char name[10],add[20]; int age,salary ; cout<<"enter the information"; cout<<"enter name : "; cin>>name; cout<<"enter address :"; cin>>add; cout<<"enter age :"; cin>>age; cout<<"enter salary :"; cin>>salary; cout<<"The information is given below "<<endl; cout<<"name "<<name<<endl; cout<<"address "<<add<<endl; cout<<"age "<<age<<endl; cout<<"salary "<<salary<<endl; }

P a g e | 15

P a g e | 16

PROGRAM-9

//wap to perform matrix multiplication #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr1[3][3],arr2[3][3],mat,i,j,k; cout<<"ENTER THE FIRST MATRIX"; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>arr1[i][j];

cout<<"ENTER THE SECOND MATRIX"; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>arr2[i][j];

cout<<"THE RESULTANT MATRIX \n"; for(i=0;i<3;i++) { mat=0; for(j=0;j<3;j++) { //matrix multiplication

P a g e | 17 for(k=0;k<3;k++) { mat+=arr1[i][k]*arr2[k][j]; } cout<<mat<<" "; mat=0; } cout<<endl; } getch(); }

P a g e | 18

P a g e | 19

PROGRAM-10
//wap to find the area of traingle,cicle and rectangle upon user choice //illustrating the function overloading #include<iostream.h> #include<conio.h> int area(int); int area(float,float); int area(int,int); int area(int rad) return(3.14*rad*rad); int area(float l,float b) return(l*b); int area(int base,int height) return(0.5*base*height);

void main() { clrscr(); int base,height,rad,ch; float l,b,ar; cout<<"*****MAIN MENU*****"; cout<<"\n1.traingle"; cout<<"\n2.circle"; cout<<"\n3.rectangle";

P a g e | 20 cout<<"\nenter your choice"; cin>>ch; switch(ch) { case 1 : cout<<"enter the length of base and height of traingle "; cin>>base>>height; ar=area(base,height); cout<<"area of traingle is "<<ar; break; case 2 : cout<<"enter the radius of the circle"; cin>>rad; ar=area(rad); cout<<"area of circle = "<<ar; break; case 3 : cout<<"enter the length and breadth of the rectangle "; cin>>l>>b; ar=area(l,b); cout<<"area of rectangle = "<<ar; break; default : cout<<wrong choice; break;

} getch(); }

P a g e | 21

P a g e | 22

PROGRAM-11
//wap to illustrute the enum in oops #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { enum day {Monday,Tuesday,Wednesday,Thrusday,Friday,Saturday}; int d; cout<<"\n0)Monday"; cout<<"\n1)Tuesday"; cout<<"\n2)Wednesday"; cout<<"\n3)Thrusday"; cout<<"\n4)Friday"; cout<<"\n5)Saturday"; cout<<"\n6)Sunday\n"; cin>>d; if(d==5 || d==6) cout<<"\n\nWEEKENDS !!!!"; else cout<<"\n\nWEEKDAYS !!!!!"; getch(); }

P a g e | 23

P a g e | 24

PROGRAM-12

//wap to find the factorial of the number entered by user in c++ // #include<iostream.h> #include<conio.h> class fact { int num; public: int facto(int i) { int ans=1; for(int j=i;j>=1;j--) ans=ans*j; return(ans); } } f; void main() { clrscr(); int item,fac; cout<<"enter the number whose fact u want" ; cin>>item; fac=f.facto(item); cout<<"factorial of "<<item<<" is "<<fac; getch(); }

P a g e | 25

P a g e | 26

PROGRAM-13
//wap to print the fabonacci series // #include<iostream.h> #include<conio.h> #include<math.h> void fib(int n); void main() { clrscr(); int n; cout<<"enter the value of n"; cin>>n; fib(n); }void fib(int n) { int a[20]; a[0]=0; a[1]=1; cout<<a[0]<<"\t"<<a[1]; cout<<"\t"; for(int i=2;i<n;i++) { a[i]=a[i-2]+a[i-1]; cout<<a[i]; } getch(); }

P a g e | 27

P a g e | 28

PROGRAM-14

//wap to input name,address,age and salary and print them using classes // #include<iostream.h> #include<conio.h> class employee { char name[20]; char add[20]; int age; int salary; public: void getdata() { cout<<"enter the name"; cin>>name; cout<<"enter the address"; cin>>add; cout<<"enter the salary"; cin>>salary; cout<<"enter the age"; cin>>age; } void putdata() { cout<<"\nthe info of the employee is\n";

P a g e | 29 cout<<"\nName :" <<name; cout<<"\nAddress :"<<add; cout<<"\nAge :"<<age; cout<<"\nSalary :"<<salary; } }; employee e; void main() { clrscr(); e.getdata(); e.putdata(); getch(); }

P a g e | 30

PROGRAM-15
//wap to find the power of number entered by the user using the concept of class //
#include<iostream.h> #include<conio.h> #include<math.h> class power { int num; int powe; float item; public: void getdata() { cout<<"enter the number = "; cin>>num; cout<<"enter the power of " <<num<< " u want to find = "; cin>>powe; } void temp() item=pow(num,powe); void putdata() cout<<\n\nanswer\n\n<<num<<" to the power " << powe <<" = "<<item; }p;

void main()

P a g e | 31 { clrscr(); p.getdata(); p.temp(); p.putdata(); getch(); }

P a g e | 32

PROGRAM-16
//wap to swap two values using call by reference and class concept // #include<iostream.h> #include<conio.h> class swapping {public: int a,b;

void get() { cout<<"Enter two numbers : "; cin>>a>>b; } void swap(int &p,int &q) { int t; t=p; p=q; q=t; } void show() { cout<<"\nValue of a = "<<a; cout<<"\nValue of b = "<<b; }

P a g e | 33 };

void main() {clrscr(); swapping s; s.get(); cout<<"\nValues before swapping "; s.show(); s.swap(s.a,s.b); cout<<"\n\nValues after swapping "; s.show(); getch(); }

P a g e | 34

P a g e | 35

PROGRAM-17 // WAP to add two vectors using the new operator

#include<iostream.h> #include<conio.h> #include<process.h> void get(int *,int); void add(int *,int *,int *,int,int); void display(int *,int);

void main() { clrscr(); int value1,value2,value3; cout<<"enter the size of vector 1 and 2 max 10"; cin>>value1>>value2; if(value1>value2) value3=value1; else value3=value2; int *a=new int[value1]; int *b=new int[value2]; int *c=new int[value3]; if(a==0|| b==0|| c==0) exit(0);

P a g e | 36 else { get(a,value1); get(b,value2); add(a,b,c,value1,value2); display(a,value1); display(b,value2); display(c,value3); delete[]a; delete[]b; delete[]c; getch(); } } void get(int *a,int value1) { cout<<"enter the values"; for(int i=0;i<value1;i++) cin>>a[i]; } void display(int *b,int value2) { cout<<" array elements are\n"; for(int i=0;i<value2;i++) cout<<b[i]<<"\n"; } void add(int *a,int *b,int *c,int value1,int value2)

P a g e | 37 { int i; for(i=0;i<value1 && i<value2;i++) { c[i]=a[i]+b[i];

if(i==value1) { for(;i<value2;i++) c[i]=b[i];

} else { for(;i<value1;i++) c[i]=a[i]; } }

P a g e | 38

P a g e | 39

PROGRAM-18
//WAP to create a class and input hours,min,secs twice. Then add them and display their result

#include<iostream.h> #include<conio.h> class time { int hrs,min,sec; public: void getdata() { cout<<"enter hrs min and sec"; cin>>hrs>>min>>sec; } void showdata() { cout<<hrs<<":"<<min<<":"<<sec; } friend time add(time,time); }; time add(time,time); void main() { clrscr(); time a,b,c; a.getdata(); b.getdata();

P a g e | 40 c=add(a,b); c.showdata(); getch(); } time add(time a,time b) { time c; c.hrs=a.hrs+b.hrs+((a.min+b.min)/60); c.min=(a.min+b.min)%60+(a.sec+b.sec)/60; c.sec=(a.sec+b.sec)%60; return c; }

P a g e | 41

P a g e | 42

PROGRAM-19
//WAP to create a class student name,gender,roll no.,marks and age and show details of //students having marks greater than 70

#include<iostream.h> #include<conio.h> #include<stdio.h> class student { char name[10],gender; int roll,marks,age; public: void getdata() { cout<<"enter your name"; gets(name); cout<<"enter gender"; cin>>gender; cout<<"enter roll no marks and age"; cin>>roll>>marks>>age; } void putdata() { cout<<"name="<<name; cout<<"\ngender="<<gender; cout<<"\nroll no="<<roll; cout<<"\nmarks="<<marks; cout<<"\nage="<<age; }

P a g e | 43 int retmarks() { return marks; } }; void main() { clrscr(); int flag=0; student a[3]; for(int i=0;i<3;i++) { cout<<"please enter the details of student no:"<<i+1; a[i].getdata(); } for(int j=0;j<3;j++) { if(a[j].retmarks()>70) { cout<<"the details of student having marks greater than 70 is"; flag=1; a[j].putdata();

} } if(flag==1) cout<<"\nstudent found"; else cout<<"\nno such student";

P a g e | 44 getch(); }

P a g e | 45 PROGRAM-20

//WAP to find largest of three numbers using the friend function #include<iostream.h> #include<conio.h> class large { int i; public: void getdata() { cout<<"enter no."; cin>>i; } void showdata() { cout<<i; } friend large big(large,large,large); }; large big(large,large,large); void main() { clrscr(); large a,b,c; a.getdata(); b.getdata(); c.getdata();

P a g e | 46 large d=big(a,b,c); cout<<"biggest of three is"; d.showdata(); getch(); } large big(large a,large b,large c) { if(a.i>b.i && a.i>c.i) return a; else if(b.i>a.i && b.i>c.i) return b; else return c; }

P a g e | 47

P a g e | 48 PROGRAM-21

//WAP to find greatest of two numbers given in two different classes using a friend function.

#include<iostream.h> #include<conio.h> class b; class a { int i; public: void getdata() { cout<<"enter the no"; cin>>i; } void showdata() { cout<<i; } friend void large(a,b); }; class b { int j; public: void getdata() { cout<<"enter the no"; cin>>j;

P a g e | 49 } void showdata() { cout<<j; } friend void large(a,b); }; void main() { clrscr(); a x; b y; x.getdata(); y.getdata(); large(x,y); getch(); } void large(a x,b y) { if( x.i> y.j) cout<<"larger of two is"<<x.i; else cout<<"larger of two is"<<y.j; }

P a g e | 50

P a g e | 51 PROGRAM-22

//WAP to find sum of two numbers declared in different classes using a friend function.

#include<iostream.h> #include<conio.h> class b; class a { int i; public: void getdata() { cout<<"enter the no"; cin>>i; } void showdata() { cout<<i; } friend void add(a,b); }; class b { int j; public: void getdata() { cout<<"enter the no"; cin>>j;

P a g e | 52 } void showdata() { cout<<j; } friend void add(a,b); }; void add(a x,b y); void main() { clrscr(); a x; b y; x.getdata(); y.getdata(); add(x,y); getch(); } void add(a x,b y) { int c= x.i+y.j; cout<<"sum of two no.s is"<<c; }

P a g e | 53

P a g e | 54

PROGRAM-23
//WAP to find factorial of a number using the constructor. #include <iostream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> class fact { int fct; public: fact(int num) { fct=1;

for(int i=num;i>=1;i--) { fct=fct*i; }

} void show() {cout<<"factorial is : "<<fct; } };

P a g e | 55

void main() { clrscr(); int num; cout<<"\n Enter the number to find its Factorial: "; cin>>num; fact obj(num); obj.show(); getch(); }

P a g e | 56

PROGRAM-24
//WAP to find Fibonacci series using the copy constructor. #include <iostream.h> #include <conio.h> class Fibonacci { long int f,f1,f2,m; public: Fibonacci(int n) { m=n;

} Fibonacci(Fibonacci &x) { x.f1=0; x.f2=1; cout<<"\n The required Fibonacci series is as follows: \n\n "; cout<<" "<<x.f1<<" "<<x.f2; for(int i=2;i<x.m;i++) { x.f=x.f1+x.f2; cout<<" "<<x.f; x.f1=x.f2;

P a g e | 57

x.f2=x.f; } }

}; void main() { int n; clrscr(); cout<<"\n Enter the length of series:"; cin>>n; Fibonacci A(n); Fibonacci B(A); getch();

P a g e | 58

PROGRAM-25
//WAP to add two complex numbers using the constructor overloading. #include<iostream.h> #include<conio.h>

class complex { float x,y; public: complex(){} complex(float a){x=y=a;} complex(float real,float imag) { x=real; y=imag; } friend complex sum(complex,complex); friend void show(complex); }; complex sum(complex c1,complex c2) { complex c3; c3.x=c1.x+c2.y; c3.y=c1.y+c2.y;

P a g e | 59

return(c3); } void show(complex c) { cout<<c.x<<"+i"<<c.y<<"\n"; } void main() { clrscr(); complex A(2.7,3.5); complex B(1.6); complex C;

C=sum(A,B); cout<<"\n A= ";show(A); cout<<"\n B= ";show(B); cout<<"\n -----------"; cout<<"\n C= ";show(C);

//Another way for initialization.

cout<<"\n ~~~~~~~~~~~~~~~|";

complex p,q,r;

P a g e | 60

p=complex(2.5,3.9); q=complex(1.6,2.5); r=sum(p,q);

cout<<"\n"; cout<<"\n P= ";show(p); cout<<"\n Q= ";show(q); cout<<"\n -----------"; cout<<"\n R= ";show(r);

getch(); }

P a g e | 61

PROGRAM-26
//WAP to keep track of number of instances using static data member,constructor and destructor. #include<iostream.h> #include<conio.h>

class test {

static int count; public: test() { count++; cout<<"\n Number of object created: "<<count; }

~test() { cout<<"\n Number of object destroyed: "<<count; count--; }

P a g e | 62

static void showcount(void) { cout<<"\n count: "<<count<<"\n"; } };

int test::count=0;

void main() { clrscr(); cout<<"\n\nMAIN \n"; test::showcount(); test t1,t2,t3,t4; test::showcount(); { cout<<"\n\nBLOCK 1:\n"; test t5; test::showcount(); } test::showcount();

{ cout<<"\n\nBLOCK 2:\n";

P a g e | 63

test t6; test::showcount(); } cout<<"\n\nMAIN \n"; test::showcount(); getch(); }

P a g e | 64

PROGRAM-27 W.A.P TO OVERLOAD UNARY INCREMENT(++) OPERATOR.


#include<iostream.h> #include<conio.h> class counter { int count; public: counter():count(0) {} int get_count() { return count; } void operator ++ () { count++; } }; void main() { clrscr(); counter c1,c2; cout<<"\n c1= "<<c1.get_count(); cout<<"\n c2= "<<c2.get_count(); ++c1; ++c2; ++c2; ++c1; ++c2; ++c2; ++c1; cout<<"\n c1= "<<c1.get_count(); cout<<"\n c2= "<<c2.get_count(); getch(); }

P a g e | 65

PROGRAM-28
W.A.P TO OVERLOAD BINARY + OPERATOR.
#include<iostream.h> #include<conio.h> class complex { float x,y; public: complex() {x= 0.0;y=0.0; } void get() { cout<<"\nEnter real part "; cin>>x; cout<<"Enter imaginary part "; cin>>y; } void show() { cout<<x<<" + i"<<y; } complex operator+(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return temp; }}; void main() { clrscr(); complex c1,c2,c3; cout<<"Enter a complex no. "; c1.get(); cout<<"\nEnter another complex no. "; c2.get(); c3=c1+c2; cout<<"\nSum of both is: "; c3.show(); getch(); }

P a g e | 66

P a g e | 67

PROGRAM-29
W.A.P TO OVERLOAD BINARY < OPERATOR.
#include<iostream.h> #include<conio.h> class time { int hrs,min,sec; public: void input(); void show(); int operator<(time); }; void time::input() {cout<<"\nEnter Hours "; cin>>hrs; cout<<"Enter Minutes "; cin>>min; cout<<"Enter Seconds "; cin>>sec; do{ if(sec>=60) {min=min+1; sec=sec-60; } if(min>=60) { hrs=hrs+1; min=min-60; }}while((min>60)||(sec>60)); } void time::show() { cout<<"\nhrs:min:sec "<<hrs<<":"<<min<<":"<<sec; } int time::operator<(time t) { long int tsec,tsec1; tsec=sec+(min*60)+(hrs*60*60); tsec1=t.sec+(t.min*60)+(t.hrs*60*60); if(tsec<tsec1) return 1; else return 0; } void main() { clrscr();

P a g e | 68 time t1,t2; cout<<"Enter Time 1 : "; t1.input(); cout<<"\nEnter Time 2 : "; t2.input(); cout<<"\nTime 1 : "; t1.show(); cout<<"\n\nTime 2 : "; t2.show(); if(t1<t2) cout<<"\n\nTime 1 is smaller than Time 2 "; else cout<<"\n\nTime 1 is not smaller than Time 2 "; getch(); }

P a g e | 69

PROGRAM-30 W.A.P TO OVERLOAD ASSIGNMENT (=) OPERATOR.


#include<iostream.h> #include<conio.h> class complex { float x,y; public: complex() {x= 0.0;y=0.0; } void get() { cout<<"\nEnter real part "; cin>>x; cout<<"Enter imaginary part "; cin>>y; } void show() { cout<<x<<" + i"<<y; } complex operator=(complex c) { x=c.x; y=c.y; return c; }}; void main() { clrscr(); complex c1,c2; cout<<"Enter a complex no. "; c1.get(); cout<<"\nEnter another complex no. "; c2.get(); cout<<"\nc1= "; c1.show(); cout<<"\nc2= "; c2.show(); c1=c2; cout<<"\n\nAfter appling assignment operator "; cout<<"\n\nc1= "; c1.show(); cout<<"\nc2= "; c2.show(); getch(); }

P a g e | 70

P a g e | 71

PROGRAM-31 WAP to overload new and delete operator.


#include <iostream.h> #include<conio.h> #include<stdlib.h> class X {int a, b, c; public: ~X() { cout << "Inside destructor of class X\n" ; } void operator delete( void* ptr); void* operator new(size_t size); void* operator new(size_t size, void* ptr); void* operator new(size_t size, int flag); }; void* X::operator new(size_t size) { void* ptr; cout << "Standard class new called.\r\n"; ptr = (void*) ::new unsigned char[size]; return ptr; } void* X::operator new(size_t size, void* ptr) { size = size; cout << "Placement class new called.\r\n"; return ptr; } void* X::operator new(size_t size, int flag) { void* ptr; cout << "Overloaded class new called. "; cout << "Flag was " << flag << ".\r\n"; ptr = (void*) ::new unsigned char[size]; return ptr; } void X::operator delete( void* ptr) { cout << "Class delete called.\r\n"; free(ptr); } void main() { clrscr(); X* objl = new X; delete objl; X* obj2; void* buf = (void*) ::new unsigned char[sizeof(X)]; obj2 = new(buf) X; free(buf); X* obj3 = new(64) X; delete obj3;}

P a g e | 72

P a g e | 73

PROGRAM-32 W.A.P TO OVERLOAD UNARY MINUS(-)OPERATOR USING FRIEND FUNCTION.


#include<iostream.h> #include<conio.h> class space { int x,y,z; public: void getdata(); void display(); friend void operator-(space &s); }; void space::getdata() { cout<<"Enter three numbers "; cin>>x>>y>>z; } void space::display() { cout<<"\n\n Three Numbers are:"; cout<<x<<" "; cout<<y<<" "; cout<<z<<" "; } void operator-(space &s) { s.x= -s.x; s.y= -s.y; s.z= -s.z; } void main() { clrscr(); space s1; s1.getdata(); s1.display(); -s1; cout<<"\n\n After Negation we have: "; s1.display(); getch(); }

P a g e | 74

P a g e | 75

PROGRAM-33 Create an abstract base class called figure and derive two classes close and open from that. Declare 2 more classes called polygon and ellipse using the close class. Create derived classes line and polyline from the open class. All classes must have appropriate member function including constructor and destructor.
#include<iostream.h> #include<conio.h> class figure { public: figure(){cout<<"\nConstructor of figure called ";} ~figure(){cout<<"\nDestructor of figure called ";} }; class open:public figure { public: open(){cout<<"\nConstructor of open called ";} ~open(){cout<<"\nDestructor of open called ";} }; class close:public figure { public: close(){cout<<"\nConstructor of close called ";} ~close(){cout<<"\nDestructor of close called ";} }; class polygon:public close { public: polygon(){cout<<"\nConstructor of polygon called ";} ~polygon(){cout<<"\nDestructor of polygon called ";}}; class ellipse:public close { public: ellipse(){cout<<"\nConstructor of ellipse called ";} ~ellipse(){cout<<"\nDestructor of ellipse called ";}}; class line:public open { public: line(){cout<<"\nConstructor of line called ";} ~line(){cout<<"\nDestructor of line called ";}}; class polyline:public open { public: polyline(){cout<<"\nConstructor of polyline called ";} ~polyline(){cout<<"\nDestructor of polyline called ";}

P a g e | 76 }; void main() { clrscr(); cout<<"\n\nAn line l; cout<<"\n\nAn polygon p; cout<<"\n\nAn open o; cout<<"\n\nAn close c; getch(); }

object of class line created\n "; object of class polygon created\n "; object of class open created\n "; object of class close created\n ";

P a g e | 77

PROGRAM-34 Create a base class basic_info with data members name,roll no,sex and two member fuctions getdata and display.derive a class physical fit from basic_info which has the data members height and weight and member functions getdata and display. Display all the information using object of derived class.
#include<iostream.h> #include<conio.h> #include<stdio.h> class basic_info { char name[20],sex; long roll; public: void getdata(); void display(); }; void basic_info::getdata() { cout<<"\n Enter your name:"; gets(name); cout<<"\n Enter your Sex:"; cin>>sex; cout<<"\n Enter your Roll number: "; cin>>roll; } void basic_info :: display() { cout<<"\n Name:"; puts(name); cout<<"\n Sex:";cout<<sex; cout<<"\n\n Roll Number:";cout<<roll; } class physical_fit:public basic_info { float height,weight; public: void getdata(); void display(); }; void physical_fit::getdata() { basic_info::getdata(); cout<<"\n Enter your height(cms): "; cin>>height; cout<<"\n Enter your weight(kgs): "; cin>>weight; } void physical_fit::display()

P a g e | 78 { basic_info::display(); cout<<"\n\n Height(cms):"<<height; cout<<"\n\n Weight(kgs):"<<weight; } void main() { basic_info b; physical_fit p; clrscr(); p.getdata(); clrscr(); p.display(); getch(); }

P a g e | 79

P a g e | 80

PROGRAM-35 Create class first with data members book no,book name and member function getdata and putdata.create a class second with data members author name, publisher and member function getdata and showdata .Derive a class third from first and second with data members no of pages and year of publication. Display all these information using array of objects of third class.
#include<iostream.h> #include<conio.h> #include<stdio.h> class first { char bk_no[20]; char bk_name[50]; public: void getdata1() { cout<<"\n Enter Book Name: "; gets(bk_name); cout<<"\n Enter Book Number: "; gets(bk_no); } void putdata1() { cout<<"\n Book Number:"; puts(bk_no); cout<<"\n Book Name:"; puts(bk_name); } }; class second { char athr_name[20],publisher[80]; public: void getdata2() { cout<<"\n Enter Author Name: "; gets(athr_name); cout<<"\n Enter Publisher: "; gets(publisher); } void putdata2() { cout<<"\n Author Number:";

P a g e | 81 puts(athr_name); cout<<"\n Publisher:"; puts(publisher); } }; class third:public first,public second { long pages,year; public: void getdata3() { getdata1(); getdata2(); cout<<"\n Enter Number of Pages: "; cin>>pages; cout<<"\n Enter year of Publication: "; cin>>year; } void putdata3() { putdata1(); putdata2(); cout<<"\n Number of Pages are: "<<pages; cout<<"\n Year of Publication is: "<<year; } }; void main() { third t[50]; int n; clrscr(); cout<<"\n How many book's information you want to enter:"; cin>>n; for(int i=1;i<=n;i++) { t[i].getdata3(); clrscr(); } for(i=1;i<=n;i++) { clrscr(); t[i].putdata3(); cout<<"\n\n\n Press Enter to continue..........\n"; getch(); } }

P a g e | 82

P a g e | 83

PROGRAM-36 Create a base class called shape use this class to store two double type values that could be used to compute the area of fig. Derive the specific class called TRIANGLE and RECTANGLE from the data shape. Add to base class, a member function get - data ( ) to initialize base class data members and another member and another member function display_area( ) to compute and display the area of the figure. Make display_area() as a virtual function and redefine function in the derived classes to suit their requirements.Using these 3 classes design a program that will accept dimension of RECTANGLE or TRIANGLE interactivity and display the area.
#include<iostream.h> #include<conio.h> class shape { public: double a,b; void getdata() { cout<<"Enter two values : "; cin>>a>>b; } virtual void display_area()=0; }; class triangle:public shape { public: void display_area() { cout<<"\nArea of triangle is : "<<(0.5*a*b)<<" square units"; } }; class rectangle:public shape { public: void display_area() { cout<<"\nArea of rectangle is : "<<(a*b)<<" square units"; }

P a g e | 84 }; void main() { clrscr(); shape *b; char c; triangle t; rectangle r; cout<<"\t\tCHOICE MENU"; cout<<"\n1.Area of triangle "; cout<<"\n2.Area of rectangle "; cout<<"\nEnter choice "; cin>>c; if(c=='1') { b=&t; b->getdata(); b->display_area(); } else if(c=='2') { b=&r; b->getdata(); b->display_area(); } else cout<<"\n\nWrong choice terminating.... "; getch(); }

P a g e | 85

P a g e | 86

PROGRAM-37 Design three classes student,exam,result.the student class has datamembers such as roll no,name.Create a class exam by inheriting the student class.The exam class adds data members representing the marks scored in six subjects.Derive the result from exam class and has it own data members such as totalmarks.Write a program to model this relationship.
#include<iostream.h> #include<conio.h> #include<stdio.h> class student { public: long int roll,cls; char name[40]; void getdata() { cout<<"\n Enter your name:"; gets(name); cout<<"\n Enter your class:";cin>>cls; cout<<"\n Enter your roll number:";cin>>roll; } void display() { cout<<"\n Name: "; puts(name); cout<<"\n Class: "<<cls; cout<<"\n Roll number: "<<roll; } }; class exam:public student { public: int marks[6]; void getdata1() { getdata(); cout<<"\n Enter your marks in six subjects:"; for(int i=1;i<=6;i++) { cout<<"\n Subject"<<i<<": "; cin>>marks[i]; } } void display1()

P a g e | 87 { display(); cout<<"\n Marks in Six subjects:"; for(int i=1;i<=6;i++) { cout<<"\n Subject"<<i<<": "; cout<<marks[i]; } } }; class result:public exam { int totalmarks,avg; public: void getdata() { getdata1(); } void final_result() { display1(); totalmarks=0; for(int i=1;i<=6;i++) { totalmarks=totalmarks+marks[i]; } cout<<"\n Total Marks are: "<<totalmarks; avg=totalmarks/6; cout<<"\n Percentage is: "<<avg<<"%"; } }; void main() { result r; clrscr(); r.getdata(); r.final_result(); getch(); }

P a g e | 88

P a g e | 89

PROGRAM-38
WAP to access the members of student class using the pointer to object members. #include <iostream.h> #include <conio.h>

class M { int x,y; public: void set_xy(int a,int b) { x=a; y=b; } friend int sum(M); }; int sum(M m) { int M::* px= &M :: x; int M::* py= &M :: y; M *pm=&m; int S; S=m.*px+pm->*py; return S;

P a g e | 90

} void main() { M n; clrscr(); cout<<" Friend function sum is accessed\n "; void (M::*pf)(int,int)=&M::set_xy; (n.*pf)(10,20); cout<<"\n SUM= "<<sum(n)<<"\n"; M *op=&n; (op->*pf)(30,40); cout<<"\n SUM= "<<sum(n)<<"\n"; getch(); }

P a g e | 91

PROGRAM-39
WAP to demonstrate the use of this pointer. #include<iostream.h> #include<conio.h> #include<string.h>

class person { char name[20]; float age; public: person(char *s,float a) { strcpy(name,s); age=a; } person & person ::greater(person &x) { if(x.age>=age) return x; else return *this; } void display()

P a g e | 92

{ cout<<"\nName- "<<name<<"\n"<<"Age- "<<age<<"\n"; } }; void main() { clrscr(); person p1("ABC",37),p2("DEF",38),p3("GHI",31); person p=p1.greater(p3); cout<<"Elder person between ABC and GHI: "; p.display(); p=p1.greater(p2); cout<<"Elder person between ABC and DEF : "; p.display(); getch(); }

P a g e | 93

PROGRAM-40
Create a class called LIST with two pure virtual functions STORE{} and RETREIVE{} to store a value called STORE and to retrieve called retrieve function .Derive the two classes stack nd queue from it and overwrite store and retrieve. #include<iostream.h> #include<conio.h>

class list { public: int a,b; virtual void store()=0; virtual void retrieve()=0; };

class stack : public list { char name[10]; float qty; public: void store() { cout<<"\n Enter the Name of Product: "; cin>>name; cout<<"\n Enter the Quantity: ";

P a g e | 94

cin>>qty; } void retrieve() { cout<<"\n Name of Product: "<<name; cout<<"\n Quantity: "<<qty; } }; class queue : public list { int n,hh,mm; public: void store() { cout<<"\n Enter how many products are waiting in a queue: "; cin>>n; cout<<"\n Enter how much time is left approximately : "; cout<<"\n hh: ";cin>>hh; cout<<"\n mm: ";cin>>mm; } void retrieve() { cout<<"\n Products Queued for Waiting: "<<n; cout<<"\n Time left= "<<hh<<" : "<<mm;

P a g e | 95

} }; void main() { clrscr(); list* arr[2]; stack st; queue qu; arr[0]=&st; arr[1]=&qu; arr[0]->store(); arr[1]->store(); arr[0]->retrieve(); arr[1]->retrieve(); getch(); }

P a g e | 96

P a g e | 97

PROGRAM-41
WAP where a multiple catch statements are used to handle the various type of exceptions. #include<iostream.h> #include<conio.h>

void test(int x) { try { if(x==1) throw x;

else if(x==0) throw 'x';

else if(x==-1) throw 1.0;

cout<<"\nEnd of try block "; } catch(char c) {cout<<"\nCaught a character "; }

P a g e | 98

catch(int x) {cout<<"\nCaught a integer "; } catch(double d) {cout<<"\nCaught a double "; } cout<<"\nEnd of try-catch block"; } void main() { clrscr(); cout<<"Testing Multiple Catches"; cout<<"\nx==1"; test(1); cout<<"\nx==0"; test(0); cout<<"\nx==-1"; test(-1); cout<<"\nx==2"; test(2); getch(); }

P a g e | 99

P a g e | 100

PROGRAM-42
WAP to define function template for swapping two items of various data type such as integer,float,caharacte. #include<iostream.h> #include<conio.h>

template <class T> void swap(T &x,T &y) { T temp = x; x = y; y = temp; } void computation(int m,int n,char c,char d, float a, float b) { cout<<"\n\n INT variables before swapping : "<<m<<" & "<<n<<"\n "; swap(m,n); cout<<"\n\n INT variables after swap: "<<m<<" & "<<n<<"\n "; cout<<"\n\n CHAR variables before swapping : "<<c<<" & "<<d<<"\n "; swap(c,d); cout<<"\n\n CHAR variables after swap: "<<c<<" & "<<d<<"\n "; cout<<"\n\n FLOAT variables before swapping : "<<a<<" & "<<b<<"\n "; swap(a,b); cout<<"\n\n FLOAT variables after swap: "<<a<<" & "<<b<<"\n ";

P a g e | 101

} void main() { int q,w; float e,r; char c,d; clrscr(); cout<<"\n\n Enter two Integers to be swapped: "; cin>>q>>w; cout<<"\n\n Enter two Floating point numbers to be swapped: "; cin>>e>>r; cout<<"\n\n Enter two Characters to be swapped: "; cin>>c>>d; clrscr(); computation(q,w,c,d,e,r); getch(); }

P a g e | 102

P a g e | 103

PROGRAM-43
WAP to define function template for calculating the square of a given number with different data types. #include<iostream.h> #include<conio.h>

template <class T> void square(T &x) { x = x*x; } void computation(int m, float a) { cout<<"\n\n INT variable before squaring : "<<m<<"\n "; square(m); cout<<"\n\n INT variablem after squaring: "<<m<<"\n "; cout<<"\n\n FLOAT variable before squaring : "<<a<<"\n "; square(a); cout<<"\n\n FLOAT variable after squaring: "<<a<<"\n "; } void main() { int q; float e;

P a g e | 104

clrscr(); cout<<"\n\n Enter Integer to be squared: "; cin>>q; cout<<"\n\n Enter Floating point number to be squared: "; cin>>e; clrscr(); computation(q,e); getch(); }

P a g e | 105

P a g e | 106

PROGRAM-44
WAP to illustrate how template functions can be overloaded. #include<iostream.h> #include<conio.h>

template <class T> void display(T x) { cout<<"\n Template display: "<<x<<"\n"; } void display(int x) { cout<<"\n Explicit display: "<<x<<"\n"; } void main() { int a; float b; char c;

clrscr(); cout<<"\n Enter an Integer: "; cin>>a; cout<<"\n Enter a Floating point number: ";

P a g e | 107

cin>>b; cout<<"\n Enter a Character: "; cin>>c; display(a); display(b); display(c); getch(); }

P a g e | 108

PROGRAM-45
WAP to illustrate how to define and declare a class template for reading two data items from the keyboard and find their sum.

#include<iostream.h> #include<conio.h>

template <class T1, class T2> class Test { T1 a; T2 b; public: Test(T1 x, T2 y) { a=x; b=y; } void show() { cout<<"\n Two numbers whose sum is to be calculated are: "; cout<<"\n a: "<<a<<"\n b: "<<b; cout<<"\n The Sum is: "<<a+b; }

P a g e | 109

}; void main() { int q,r; float w; char e; clrscr(); cout<<"\n Enter an integer and Real number to find the sum of them:"; cin>>q>>w; cout<<"\n Enter an integer and Character to find the sum of them:"; cin>>r>>e; Test <int,float> test1(q,w); Test <int,char> test2(r,e); test1.show(); test2.show(); getch(); }

P a g e | 110

P a g e | 111

PROGRAM-46
WAP to illustrate the use of vector class template for performing the sacalar product of int type vectors. #include<iostream.h> #include<conio.h> const size =3; template <class T> class vector { T *v; public: vector() { v=new T[size]; for(int i=0;i<size;i++) v[i]=0; } vector(T* a) { for(int i=0;i<size;i++) v[i] = a[i]; } T operator*(vector &y) {

P a g e | 112

T sum = 0; for(int i=0;i<size;i++) sum += this -> v[i] * y.v[i] ; return sum; } }; void main() { clrscr(); int x[3]={1,2,3}; int y[3]={4,5,6}; vector <int> v1; vector <int> v2; v1 = x; v2 = y; int R = v1*v2; cout<<"\n We have Vectors (x+2y+3z) and (4x+5y+6z)"; cout<<"\n Integer Scalar product is = "<<R<<"\n"; float x1[3]={1.1,2.2,3.3}; float y1[3]={4.4,5.5,6.6}; vector <float> u1; vector <float> u2; u1 = x1; u2 = y1;

P a g e | 113

float R1 = u1*u2; cout<<"\n We have Vectors (1.1x+2.2y+3.3z) and (4.4x+5.5y+6.6z)"; cout<<"\n Floating point Scalar product is = "<<R1<<"\n"; getch(); }

P a g e | 114

PROGRAM-47
WAP to demonstrate the use of special functions constructor and destructor in class template . The program is used to find the biggest of two entered numbers .

#include<iostream.h> #include<conio.h> template <class T1, class T2> class test { T1 a; T2 b; public: test(T1 x,T2 y) { a=x; b=y; } void show() { if(a>b) cout<<a<<" is greater than "<<b; else cout<<b<<" is greater than "<<a; }

P a g e | 115

~test(){} }; void main() { clrscr(); int q,w; cout<<"\n Enter first two numbers to find the biggest: " ; cin>>q>>w; test <int,int> t1(q,w); cout<<"\n\n"; t1.show(); cout<<"\n\n"; getch(); }

P a g e | 116

PROGRAM-48
WAP to implement string concadenate by use of operator overloading concept.

#include<iostream.h> #include<conio.h> #include<string.h> class string { char *p; int len; public: string() {len = 0; p=0;} string(char *s); string(string &s); ~string() {delete p;} friend string operator +( string &s, string &t); friend void show( string s); friend void length(string s); }; string::string( char *s) { len=strlen(s); p=new char[len+1]; strcpy(p,s);

P a g e | 117

} string::string( string &s) { len=s.len; p=new char[len+1]; strcpy(p,s.p); } string operator +(string &s,string &t) { string temp; temp.len=s.len+t.len; temp.p=new char[temp.len+1]; strcpy(temp.p,s.p); strcat(temp.p,t.p); return(temp); } void show(string s) { cout<<s.p; } void length(string s) { int len; len=strlen(s.p);

P a g e | 118

cout<<len; } void main() { clrscr(); string s1="Mr."; string s2="ABC"; string s3="JKL"; string t3;

t3=s1+s3; cout<<"\n String1 = ";show(s1); cout<<"\n String2 = ";show(s2); cout<<"\n String3 = ";show(s3); cout<<"\n"; cout<<"\n Lengths of given above strings are: \n"; cout<<"\n String1: ";length(s1); cout<<"\n String2: ";length(s2); cout<<"\n String3: ";length(s3); cout<<"\n\n"; cout<<"\n String after concatination of Strings 1&3 = ";show(t3); cout<<"\n Length of new String is: ";length(t3); cout<<"\n\n"; getch();

P a g e | 119

Vous aimerez peut-être aussi