You are on page 1of 21

C++ Practical File

2007

Program to demonstrate Passing array to function (Taking example of Bank)


#include<iostream.h> #include<conio.h> #include<process.h> class bank { private: int accno; char name[20]; float amount; static int acc; public: void getdata(); void deposit(bank *,int,int); void withdraw(bank *,int,int); int search(bank *,int,int); void putdata(bank *,int,int); }; int bank::acc=101;

void bank::getdata() { accno=acc++; cout<<"Your Account no is "<<accno<<endl; cout<<"Enter the Name ";cin>>name; cout<<"Enter the Amount ";cin>>amount; }

void bank::deposit(bank *b,int n,int acc) { float damt; LOVELY INSTITUTE OF MANAGEMENT PAGE 19

C++ Practical File

2007

int chk=0; cout<<"Enter the amount to deposit "; cin>>damt; for(int i=0;i<n;i++) { if(b[i].accno==acc) { b[i].amount=b[i].amount+damt; cout<<"ACCOUNT UPDATED\n "; chk=1; } } if(chk==0) { cout<<"\nTRANSACTION ABORTED"; } }

void bank::putdata(bank *p,int n,int acc) { for(int i=0;i<n;i++) { if(p[i].accno==acc) { cout<<"Name is "<<p[i].name<<"\n"; cout<<"account No is "<<p[i].accno<<"\n"; cout<<"Amount is "<<p[i].amount<<endl; } } }

void bank::withdraw(bank *b,int n,int acc) { float wamt; int chk=0; cout<<"Enter the Amount To WITHDRAW "; LOVELY INSTITUTE OF MANAGEMENT PAGE 20

C++ Practical File

2007

cin>>wamt; for(int i=0;i<n;i++) { if(b[i].accno==acc&&b[i].amount>wamt) { b[i].amount=b[i].amount-wamt; cout<<"ACCOUNT UPDATED"; chk=1; } } if(chk==0) { cout<<"\nTRANSACTION ABORTED\n "; } }

int bank::search(bank *b,int n,int acc) { int a=0; for(int i=0;i<n;i++) { if(b[i].accno==acc) { a=1; } } return a; }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 21

C++ Practical File

2007

void main() { bank *a;int n,opt,ano,rtn; clrscr(); cout<<"\nEnter the no of customer "; cin>>n; a=new bank[n]; if(!a) cout<<"\nAlloaction faioled"; clrscr(); for(int i=0;i<n;i++) { a[i].getdata(); } start: clrscr(); cout<<"\nTo display an information (Press 1 ) "; cout<<"\nTo deposit An Amount (Press 2 ) " ; cout<<"\nTo Withdraw An Amount (Press 3 ) " ; cout<<"\nTo EXIT (Press 4) "; cin>>opt; if(opt<=3) { cout<<"Enter your account No "; cin>>ano; } switch(opt) { case 1: rtn=a[0].search(a,n,ano); if(rtn==1) a[0].putdata(a,n,ano); else cout<<"Account Does't Exist "; getch(); goto start; case 2: rtn=a[0].search(a,n,ano); LOVELY INSTITUTE OF MANAGEMENT PAGE 22

C++ Practical File

2007

if(rtn==1) { a[0].deposit(a,n,ano); a[0].putdata(a,n,ano); } else { cout<<"Account Does't Exist "; } getch(); goto start; case 3: rtn=a[0].search(a,n,ano); if(rtn==1) { a[0].withdraw(a,n,ano); a[0].putdata(a,n,ano); } else { cout<<"Account Does't Exist "; } getch(); goto start; case 4: exit(0); default: cout<<"\nYOU ENTER WRONG CHOICE \tRETRY AGAIN"; getch(); goto start; }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 23

C++ Practical File

2007

LOVELY INSTITUTE OF MANAGEMENT

PAGE 24

C++ Practical File

2007

Program to demonstrate the concept of the Constructor Overloading & Destructor


#include<iostream.h> #include<conio.h> class cplx { int r; int i; static int c; public: void show() { cout<<"r= "<<r<<" i= "<<i<<"\n"; } cplx() { c++; r=0;i=0; } cplx(int a) { c++; r=a;i=0; } cplx(int a,int b) { c++; r=a;i=b; } cplx(cplx &a) { LOVELY INSTITUTE OF MANAGEMENT PAGE 25

C++ Practical File

2007

c++; r=a.r; i=a.i; } ~cplx() { cout<<"\nHello destructor "<<c; c--; } }; int cplx::c=0;

void main() { clrscr(); cplx c1=5; c1.show(); cplx c2(6,7); c2.show(); cplx c4; c4.show(); cplx c3(c1); c3.show(); getch(); }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 26

C++ Practical File

2007

LOVELY INSTITUTE OF MANAGEMENT

PAGE 27

C++ Practical File

2007

Program to demonstrate the concept of Dynamic Allocation of Memory through Constructor


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

class str { char *name; int len; public: str() { char *b; gets(b); len=strlen(b); name=new char[len+1]; strcpy(name,b); } str(char *a) { len=strlen(a); name=new char[len+1]; strcpy(name,a); } void display() { cout<<name; } }; LOVELY INSTITUTE OF MANAGEMENT PAGE 28

C++ Practical File

2007

void main() { clrscr(); str a; char *Name="gurpreet"; str name1(Name); str na("singh"); name1.display(); na.display(); getch(); }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 29

C++ Practical File

2007

Program to demonstrate the concept of ARRAY OF OBJECT


#include<iostream.h> #include<conio.h> class emp { char name[20]; float sal; int age; int dept; public: void getdata() { cin>>name>>sal>>age>>dept; } void putdata() { cout<<name<<"\n"<<sal<<"\n"<<age<<"\n"<<dept; } void sumsalary(emp*,int); void hsal(emp*,int); void avg(emp*,int); }; void emp::sumsalary(emp *a,int t) { float salary=0; for(int i=0;i<t;i++) { salary+=a[i].sal; } cout<<"\nThe total salary is"<<salary; } LOVELY INSTITUTE OF MANAGEMENT PAGE 30

C++ Practical File

2007

void emp::hsal(emp *a,int t) { int max=a[0].sal,c=0; for(int i=0;i<t-1;i++) { if(max<a[i+1].sal) { max=a[i+1].sal; c=i+1; } } cout<<"\nThe information Whose higher salary\n"; a[c].putdata(); }

void emp::avg(emp *a,int t) { int average=0; for(int i=0;i<t;i++) { average+=a[i].age; } cout<<"\nThe average age is: "<<average/t; } void main() { emp a[50]; int b,i; clrscr(); cout<<"Enter no of employee";cin>>b; cout<<"Enter the information of employee (Name,salary,age,department id) "; for(i=0;i<b;i++) { a[i].getdata(); LOVELY INSTITUTE OF MANAGEMENT PAGE 31

C++ Practical File

2007

} a[0].sumsalary(a,b); a[0].hsal(a,b); a[0].avg(a,b); /*for(i=0;i<b;i++) { a[i].putdata(); } */ getch(); }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 32

C++ Practical File

2007

Program to demonstrate the concept of FRIEND FUNCTION


#include<iostream.h> #include<conio.h> class DI; class DM { int meter; int cmeter; public: void getdata(); void putdata(); friend DM add(DM,DI); }; void DM::getdata() { cout<<"Enter the height in METER & CENTIMETER \n Enter Meter "; cin>>meter; cm:cout<<"Enter Centimeter "; cin>>cmeter; if(cmeter>99) { cout<<"wrong Input \n\n Centimeter can Never > 99\n\n ENTER CENTIMETER AGAIN \n"; goto cm; } } void DM::putdata() { cout<<"This is TOTAL heigt: \n"<<meter <<" meter "<<cmeter <<" Centimeter"; }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 33

C++ Practical File

2007

class DI { int feet; int inche; public: void getdata(); friend DM add(DM,DI); };

void DI::getdata() { cout<<"Enter the height in FEET & INCHES \n Enter Feet "; cin>>feet; cout<<"Enter Inches "; ci:cin>>inche; if(inche>11) { cout<<"wrong Input Inches can Never > 11\n\n ENTER INCHES AGAIN \n"; goto ci; } }

DM add(DM dm,DI di) {DM temp;int i,k; i=(dm.meter*100)+dm.cmeter; k=(((di.feet*12)+di.inche)*2.54); i=i+k; temp.meter=i/100; temp.cmeter=i%100; return temp; }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 34

C++ Practical File

2007

void main() { DM dm,final; DI di; clrscr(); dm.getdata(); di.getdata(); final=add(dm,di); final.putdata(); getch(); }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 35

C++ Practical File

2007

Program to demonstrate the Working of VECTOR and perform following task


Enter the Vector elements After ensure from user modify its element Multiply vector with scalar

#include<iostream.h> #include<conio.h> class vector { float vec[10]; public: void getdata(int); void modify(); void multiply(int,int); void putdata(int); }; void vector::putdata(int n) { cout<<"{"; for(int i=0;i<n;i++) { cout<<vec[i]<<","; } cout<<"}"; } void vector::getdata(int a) { for(int i=0;i<a;i++) { cout<<"Enter the Element Of Vector"; cin>>vec[i]; } } LOVELY INSTITUTE OF MANAGEMENT PAGE 36

C++ Practical File

2007

void vector::modify() { int a; cout<<"Enter the position of the element(1,2,3...."; cin>>a; cout<<"Entre the New Value"; cin>>vec[a-1]; } void vector::multiply(int n,int b) { for(int i=0;i<n;i++) { vec[i]=vec[i]*b; } }

void main() { vector v1; int n,val; char opt,opt1; clrscr(); cout<<"Enter the no of vector element"; cin>>n; v1.getdata(n); cout<<"Are u want to change any element(y/n)"; cin>>opt; if(opt=='y') { v1.modify(); } cout<<"Are u want to multiply by scaler(y/n)"; cin>>opt1; if(opt1=='y') { cout<<"Enter the value for scaler multiplication"; cin>>val; v1.multiply(n,val); LOVELY INSTITUTE OF MANAGEMENT PAGE 37

C++ Practical File

2007

} v1.putdata(n); getch(); }

LOVELY INSTITUTE OF MANAGEMENT

PAGE 38

C++ Practical File

2007

TABLE OF CONTENT

Program to demonstrate Passing array to function (Taking example of Bank) ................ 19 Program to demonstrate the concept of the Constructor Overloading & Destructor ...... 25 Program to demonstrate the concept of Dynamic Allocation of Memory through Constructor ................................................................................................................ 28 Program to demonstrate the concept of ARRAY OF OBJECT ............................................. 30 Program to demonstrate the concept of FRIEND FUNCTION ............................................ 33 Program to demonstrate the Working of VECTOR and perform following task ............... 36 Enter the Vector elements ........................................................................................ 36 After ensure from user modify its element ............................................................. 36 Multiply vector with scalar ........................................................................................ 36

LOVELY INSTITUTE OF MANAGEMENT

PAGE 39

You might also like