Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rajeev oops 2nd march

1,387 views

Published on

Published in: Education, Technology

Rajeev oops 2nd march

  1. 1. OBJECT ORIENTED PROGRAMMINGS 2010Q 1) Define a class to represent a bank account. Include the following members:Data Members:  Name of the Depositor  Account Number  Type of Account  Balance amount in the accountMember Functions:  To assign the initial values.  To deposit an account.  To withdraw an amount after checking the balance.Write a C++ main program to display account number, name and balance. 1|Page
  2. 2. OBJECT ORIENTED PROGRAMMINGS 2010#include<iostream.h>#include<conio.h> //for clrscr()#include<stdio.h> //for gets()#include<iomanip.h>const int LEN=30;const int max=100;class members{ private: char name[LEN]; unsigned long bal_amount; char acctype[LEN]; public: unsigned long accnumb; members() { bal_amount =1000000; } void getdata() { cout<<"Enter Name:"<<endl; gets(name); cout<<"Enter AccountNo:"<<endl; cin>>accnumb; cout<<"Enter the Account-type:"<<endl; gets(acctype); cout<<"Enter the balance amount:"<<endl; cin>>bal_amount; } void putdata() { cout<<"Name: "<<name<<"t"; cout<<"AccountNo: "<<accnumb<<"t"; cout<<"Account Type: "<<acctype<<"t"; cout<<"Balance Amount: "<<bal_amount<<"t"; cout<<endl; } void withdraw() { unsigned long wdraw_amount; cout<<"Enter the amount U want to withdraw: t"; cin>>wdraw_amount; cout<<endl; if(wdraw_amount>bal_amount || wdraw_amount%100!=0) { cout<<"U have entered the wrong amount."<<endl; cout<<"Amount less than balance and multiple of 100 only can bewithdrawn"<<endl; wdraw_amount=0; } else2|Page
  3. 3. OBJECT ORIENTED PROGRAMMINGS 2010 bal_amount= bal_amount-wdraw_amount; cout<<"Amount withdrawn: t"<<wdraw_amount<<"t"; cout<<"Balance: t"<<bal_amount<<"t"<<endl; } void check_balance() { cout<<"Balance: t"<<bal_amount<<endl; } void deposit_amount() { unsigned long d_amount; cout<<"Enter the amount U want to deposit: t"; cin>>d_amount; cout<<endl; bal_amount=bal_amount+d_amount; cout<<"Balance: t"<<bal_amount<<endl; }};void main(){ clrscr(); members m[max]; unsigned long acc_no; int n; cout<<"The no of records U want 2 be entered: t"<<endl; cin>>n; cout<<"Enter the following Records"<<endl; for (int i=0;i<n;++i) { cout<<"Record:t"<<i+1<<endl; m[i].getdata(); } clrscr(); for (i=0; i<n; ++i) {cout<<"Record "<<i+1<<endl; m[i].putdata(); } char ch; cout<<"Do u want 2 continue for any account?"<<endl; cin>>ch; clrscr(); while(ch==y) { cout<<"Enter ur account not"<<endl; cin>>acc_no; for(i=0; i<n; i++) { if(acc_no==m[i].accnumb) { int opt;3|Page
  4. 4. OBJECT ORIENTED PROGRAMMINGS 2010 m[i].putdata(); char ans; cout<<"Do U want 2 continue for any account-process(y/n)?t"; cin>>ans; cout<<endl; while(ans==y) { cout<<"Enter ur option (1-check balance, 2-deposit, 3-withdraw)"<<endl; cin>>opt; cout<<endl; switch(opt) { case 1: m[i].check_balance(); break; case 2: m[i].deposit_amount(); break; case 3: m[i].withdraw(); break; deafault: cout<<"Wrong Option"<<endl; } cout<<"Do U want 2 continue for next withdrawl/ deposit/ balance-inquiry?"<<endl; cin>>ans; cout<<endl; } } } cout<<"Do U want 2 continue for any other account?"<<endl; cin>>ch; } getch(); }OUTPUTThe no of records U want 2 be entered:3Enter the following RecordsRecord 1Name:SarveshEnter Account No:123456Enter the Account-type:CurrentEnter the balance amount:912345674|Page
  5. 5. OBJECT ORIENTED PROGRAMMINGS 2010Record 2Name:PrashantEnter Account No:456789Enter the Account-type:SavingEnter the balance amount:90234567Record 3Name:RajeevEnter Account No:789012Enter the Account-type:CurrentEnter the balance amount:98765432Clear-screen..............Record 1Name: Sarvesh Account No: 123456 Account-type: Current Balance amount: 91234567Record 2Name: Prashant Account No: 456789 Account-type: Saving Balance amount: 90234567Record 3Name: Rajeev Account No: 789012 Account-type: Current Balance amount: 98765432Do u want 2 continue for any account?yClear-screen...Enter ur account no789012Name: Rajeev Account No: 789012 Account-type: Current Balance amount: 98765432Do U want 2 continue for any account-process(y/n)? yEnter ur option (1-check balance, 2-deposit, 3-withdraw)1Balance: 98765432Do U want 2 continue for next withdrawl/ deposit/ balance-inquiry?yEnter ur option (1-check balance, 2-deposit, 3-withdraw)2Enter the amount U want to deposit: 67890543Balance: 166655975Do U want 2 continue for next withdrawl/ deposit/ balance-inquiry?yEnter ur option (1-check balance, 2-deposit, 3-withdraw)3Enter the amount U want to withdraw: 4567890U have entered the wrong amount.Amount less than balance and multiple of 100 only can be withdrawnAmount withdrawn: 0 Balance: 166655975Do U want 2 continue for next withdrawl/ deposit/ balance-inquiry?yEnter ur option (1-check balance, 2-deposit, 3-withdraw)5|Page
  6. 6. OBJECT ORIENTED PROGRAMMINGS 20103Enter the amount U want to withdraw: 4567800Amount withdrawn: 4567800 Balance: 162088175Do U want 2 continue for next withdrawl/ deposit/ balance-inquiry?nDo U want 2 continue for any other account?n Q 2) Write an operator overloading program to write S3+=S2.6|Page
  7. 7. OBJECT ORIENTED PROGRAMMINGS 2010#include<iostream.h>#include<conio.h>class numb{ private: int i; public: numb() { i=10; } void operator++() { i=i+1; cout<<i<<endl; }};void main(){ numb n; clrscr(); int j; cout<<"How many times u want to increment the value?t"; cin>>j; for(int m=0;m<j;++m) {n.operator++(); } getch();}OUTPUT7|Page
  8. 8. OBJECT ORIENTED PROGRAMMINGS 2010How many times u want to increment the value? 81112131415161718// overloading prefix ++ incrementer operator for obtaining Fibonacci series#include<iostream.h>#include<conio.h>class fibonacci{ private: unsigned long int f0, f1, fib; public: fibonacci(); //constructor void operator++(); void display();};fibonacci::fibonacci(){ f0=0; f1=1; fib=f0+f1;}void fibonacci::display(){ cout<<fib<<"t";}void fibonacci::operator++(){f0=f1; f1=fib; fib=f0+f1;}8|Page
  9. 9. OBJECT ORIENTED PROGRAMMINGS 2010void main(){clrscr();fibonacci obj;int n;cout<<"How many fibonacci numbers are to be displayed? n";cin>>n;for(int i=0; i<=n-1;++i){obj.display();++obj;}getch();}OUTPUTHow many fibonacci numbers are to be displayed?101 2 3 5 8 13 21 34 55 89Q 3) Write an operator overloading program to Overload ‘>’ operator so as tofind greater among two instances of the class.9|Page
  10. 10. OBJECT ORIENTED PROGRAMMINGS 2010//overloading of comparison operators#include<iostream.h>#include<conio.h>class sample{ private: int value; public: sample(); sample( int one); void display(); int operator > (sample obj);};sample::sample(){ value = 0; }sample::sample( int one)10 | P a g e
  11. 11. OBJECT ORIENTED PROGRAMMINGS 2010{ value = one;}void sample::display(){ cout<<"value "<<value<<endl;}int sample::operator > (sample obja){ return (value > obja.value);}void main(){ clrscr(); sample obja(20); sample objb(100); cout<<(obja>objb)<<endl; cout<<(objb>obja)<<endl; getch();}OUTPUT0111 | P a g e
  12. 12. OBJECT ORIENTED PROGRAMMINGS 2010 Q 4) WAP using Single and Multiple Inheritance.//single inheritance using array of objects#include<iostream.h>#include<conio.h>#include<stdio.h>#include<string.h>#include<iomanip.h>const int max=100;class basic_info{ private: char name[30]; long int rollno; char sex; public: void getdata(); void display();}; //end of class declarationclass physical_fit:public basic_info{ private: float height,weight; public: void getdata(); void display();};void basic_info::getdata()12 | P a g e
  13. 13. OBJECT ORIENTED PROGRAMMINGS 2010{ cout<<"ENTER A NAME ? n"; cin>>name; cout<<"ROLL No. ? n"; cin>>rollno; cout<<"SEX ? n"; cin>>sex;}void basic_info::display(){ cout<<name<<" "; cout<<rollno<<" "; cout<<sex<<" ";}void physical_fit::getdata(){ basic_info::getdata(); cout<<"HEIGHT ? n"; cin>>height; cout<<"WEIGHT ? n"; cin>>weight;}void physical_fit::display(){ basic_info::display(); cout<<setprecision(2); cout<<height<<" "; cout<<weight<<" ";}void main(){ clrscr(); physical_fit a[max]; int i,n; cout<<"HOW MANY STUDENTS ? n"; cin>>n; cout<<"ENTER THE FOLLOWING INFORMATION n"; for(i=0;i<=n-1;++i) { cout<<"RECORD : "<<i+1<<endl; a[i].getdata(); } cout<<endl; cout<<"_________________________________________________ n"; cout<<"NAME ROLLNO SEX HEIGHT WEIGHT n"; cout<<"_________________________________________________ n"; for(i=0;i<=n-1;++i)13 | P a g e
  14. 14. OBJECT ORIENTED PROGRAMMINGS 2010 { a[i].display(); cout<<endl; } cout<<endl; cout<<"_________________________________________________ n"; getch();}OUTPUTHOW MANY STUDENTS?3ENTER THE FOLLOWING INFORMATIONRECORD: 1ENTER A NAME?SarveshROLL No. ?13 SEX?M HEIGHT?174WEIGHT?59RECORD : 2ENTER A NAME?PrashantROLL No. ?19 SEX ?M HEIGHT?166WEIGHT?63RECORD : 3ENTER A NAME ?RajeevROLL No. ?23 SEX ?M HEIGHT?14 | P a g e
  15. 15. OBJECT ORIENTED PROGRAMMINGS 2010164WEIGHT?53_________________________________________________ NAME ROLLNO SEX HEIGHT WEIGHT _________________________________________________Sarvesh 13 M 174 59Prashant 19 M 166 63Rajeev 23 M 164 53_________________________________________________#include<iostream.h>#include<conio.h>#include<stdio.h>#include<string.h>class baseA{ public: int a;};class baseB{ public: int a;};15 | P a g e
  16. 16. OBJECT ORIENTED PROGRAMMINGS 2010class baseC{ public: int a;};class derivedD: public baseA,public baseB, public baseC{ public: int a;};void main(){ clrscr(); derivedD objd; objd.a=10; //local to the derived class cout<<"n VALUE OF a IN THE DERIVED CLASS = "<<objd.a; cout<<endl; cout<<"n VALUE OF a IN THE baseA = "<<objd.baseA::a; cout<<endl; cout<<"n VALUE OF a IN THE baseB = "<<objd.baseB::a; cout<<endl; cout<<"n VALUE OF a IN THE baseC = "<<objd.baseC::a; cout<<endl; cout<<endl; getch();}#include<iostream.h>#include<conio.h>#include<stdio.h>#include<string.h>class baseA{ public: int a;};class baseB16 | P a g e
  17. 17. OBJECT ORIENTED PROGRAMMINGS 2010{ public: int a;};class baseC{ public: int a;};class derivedD: public baseA,public baseB, public baseC{ public: int a;};void main(){ clrscr(); derivedD objd; objd.a=10; objd.baseA::a=20; objd.baseB::a=30; objd.baseC::a=40; cout<<"n VALUE OF a IN THE DERIVED CLASS = "<<objd.a; cout<<endl; cout<<"n VALUE OF a IN THE baseA = "<<objd.baseA::a; cout<<endl; cout<<"n VALUE OF a IN THE baseB = "<<objd.baseB::a; cout<<endl; cout<<"n VALUE OF a IN THE baseC = "<<objd.baseC::a; cout<<endl; cout<<endl; getch();}OUTPUTVALUE OF a IN THE DERIVED CLASS = 10VALUE OF a IN THE baseA = 20VALUE OF a IN THE baseB = 3017 | P a g e
  18. 18. OBJECT ORIENTED PROGRAMMINGS 2010VALUE OF a IN THE baseC = 4018 | P a g e

×