0% found this document useful (0 votes)
32 views34 pages

OOPS Practical

Practical pdf Xhfhfughkbhtdkgkxkgckchctocbkhonkbuc Tedgfhgihicthg

Uploaded by

Prabin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
32 views34 pages

OOPS Practical

Practical pdf Xhfhfughkbhtdkgkxkgckchctocbkhonkbuc Tedgfhgihicthg

Uploaded by

Prabin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 34
do { float numOne, nu, int choice; COUTK<"1. Addition\n' COU<<"2. Subtraction\n"; COUL<<"3, Myttiplication\n"; cout<<"4. Division\n" cout<<"5. Exit\n\n"; cout<<"Enter Your Choice(1-5) cin>>choice; if{choice>=1 && choice { ) cout<<"\nEnter any two Numbers: "; cin>>numOne>>numTwo; Hi switch(choice) { ‘ case 1: res = numOne+numTwo; res = numOne-numTwo; cout<<"\nResult = "< #include void main() { int seconds, hours, minutes; elrser (Q: . cout<<"\n, jer time in seconds: "; cin>>s minutes=seconds/60; hours=minutes/60; cout= Hinclude void main () { const float pi = 3.14159; float r,h,,b,s,vol; clrserQ; rogram to find the volume of a square, cone, and rectangle. a] cout<<"\n Enter the radius and height of the con cin>>r>>h; 4 Cone's volume. vol=(1.0/3.0)*pi*(r*n)*h; cout <<"\n The volume of the Cone i << vol ; ‘sout<< "\n\n Enter the length, breadth and hei cin>>1>>b>>h; 1/ Rectangle's volume. ight of a Rectangle: ": yy “a rer = yole(I"b*h); t<<"nThe volume of the Rectangle is: <Si square’s volume. yol=(s*s*s); cout<<"\n The volume of the §; quare is: "<< vol; getchO; 7 ) [E_@) Writes > Program to find the great bers, clude — : #include void main () t int nl, n2, n3; clrser(); cout << "Enter three numbers: "; cin >> nl >>n2>>n3; M1 check if n1 is the largest number if{nl >= n2 && nl >=n3) cout << "Largest number: "<< nl; // check if n2 is the largest number else iff{n2 >= nl && n2 >= n3) cout << "Largest number: " << n2; // if neither ni nor n2 are the largest, n3 is the largest else cout << "Largest number: " << n3; getch(); Rib) Write «C+ program to find the sum of even and odd m natural numbers #include #include void main() ( int number, maximum, evenSum = 0, oddSum = elrser(); 266 << evenSum; Cout << "\nThe Sum of All Odd Numbers upto" << maximum <<" =" << oddSum; getch(); } 2. (© Write a’ Co programe se ate all the prime numbers between land ny where wi 2 value supplied by the user. #inelude #include void main() int num, i, upto; 1 Take input from user cout << "Find prime numbers upt cin >> upto; fut endl << "All prime numbers upto " << upto <<" ares" << endl; for(num = 2; num <= upto; num++) { fori = 2; 1 <= (num /2); i++) { if(rum % i == 0) { i=num; break; } } ‘"lfthe number is prime then print it, iff t= num) ++ (EN BSCAT) ( Object Oriented Programming with C 1 emmy cout << "\nPlease En Yer the Maximum Limit for Even & Odd Numbers = "; cin >> maximum: for(number = 1; number <= maximum; number++) oddSum = oddSum + number; ‘en Numbers upto " << maximum << Sout << num <<" "; 4 | 1 | i 1 } rr getch(); @) Write a Gr pre © a roll_no, Display ennaes Glasses and object Student fo print name of the students jostream.h> nio.h> class Student private char name[30]; int roltN public: ‘member function to get student's details void getDetails(void); member function to void putDetails(void); int student's details member function definition, outside of the class void Student::getDetails() clrser(); cout << "Enter Name: " ; cin >> name; cout << "Enter Roll Number: "; cin >> rollNo; member function definition, void Student utDetails() outside of the class cout << "Student details:\n"; cout #include #include #include class Bank { private: int Aceno; string AccName; char AceType; float BalanceAmt; public: void getAccountDetails() { cout<<"Please enter account details:"<>Accno; cout<<"Account Holder Name:"; cin>>AccName; cout<<"AccountType(c-Current,s-S: cin>>AccType; cout<<"Balance Amount:"; cin>>BalanceAmt; } void printAccountDetails() { cout<<"Account holder deta 7 cout<<", ."<tamts cout0) BalanceAmt = BalanceAmt-tamt cout<<"Transation completed"< #include class test { int objNo; [i @) Writea Programa fo find Maximum out of Two Numbers using friend function. Note: Here one number is a member of one class and the other number is member of some ‘nlude=iosteam: finelude int number, if{2 number>bl number) cout<<"\n Number in class A is greatest ie. “<>num; aal(num); t<<"\n Enter number for class B cout cin>>num; b bi(num); greatest(al,b1); ses using fi 272) ee cout<<"\n"; getch(); #include class Demo { private: int num1, num3; public: Demo(int n1, int n2) { num! =n; num2 = n2; } Demo(const Demo &n) { num! = n.num1; num2 = n.num2; } void display() { cout<<"\nnum1 = "<< num! <>a.real>>a.img; ny ee a and imaginary part of second complex number: cin>>b.real>>b.imgs c.add(a,b); damultiply(a.b); getch(); ) Sierload new/delete operators ina class. Giatb) Write a Crh program to, #include #include #include class CustomMemory { public: void* operator new(size_t objectSize);//Overloaded new void operator delete(void* ptr); //Overloaded delete hb void* CustomMemory::operator new(size_t objectSize) { cout<<"Custom memory allocation"< e public: int roll_num; void print) { cout << "\nRoll Number is "<*! ptr=205 sp->print()s getch(); (@ Write a CH Progra the Data Members. ‘finclude include class fibonacci { ong int a,b; //data members “ public: fibonacci() //spe { a bl; } void fibseries(int n) //member function { i tor cial member function construc Resultant fibonacci series"; An"; next=a+b; //Expression cout<>n; Efibseries(n); getch(); kOe Pro} include ‘ #include class base isin, gram that illustrate single inheritanc Ble base class . ren atic int a7 ter the value of x & _gerive : public base ys Usingle derived class private: inty publie: void readdata() cout << "Enter the value of y = cin >> y; } void product() Ih { cout << "Product =" #include // create base class1 class student_detail { M access specifier protected: : int mo, sum, i, marks[5]; access specifier 19IE.Y.B Se. (L77.) ~ Object Oriented Programming with CH (Semtl) sri pracnnent s eee pois wont etait) © Sor se Dinter the Roll Noe t= eM eee aye oe enn seam ® Bnter the marks of ive sulest use for loop for (Oi) cin >> matkstils sumeO; for(i F< Si) store the sum of five subject sum © sum + markst create base class? class sports_mark public: void get_mark() t cout << "Wn Enter the sports marl mark; /* create a result as the child class to inherit functions of the parent class: student_detail and sports_mark.*/ class result: public student_detail, public sports_mark ‘ int tot; float avg; public: !/ create member function of child class Void disp () { ere gum +s marks tot /6;// total marks of six subject /6 1s At Roll Not " << mmo <<" \n Total" << tot << endl wv \p\t Average Marks: " << avg: an object of the derived class 1e base class ' rr obj; ereate imber function of th rest jy call the Mme obj details obj get_markOs obj.dispO+ getoh(s a C+t Program that illustrate jnulti-level inheritance. writ a .cjostream.h> include ginclude #include class A //single base class { public: int x, ¥3 void getdata() { cout << "\nEnter value of x and y:\n"; cin >> x >> Y3 } h class B : public A //B is derived from class base { public: void product() { cout << "\nProduct="<< x * y: } h class C : public A /IC js - { C is also derived from class base Public; Void sum() { CUS MSU ce x i Coa 1 govt co Hlobject of derived class B \ it Hobject of derived class C jl ye etdata( onl oducts pjz.cetdatas —ydeiotream! sh meecconiod he cass alpha { int X3 public: alpha(int i) { xi cout<<"alpha initialized\n"s void show_x(void) { cout<<" b class beta betafloat i) { cout<<"beta initialized\n’ } void show_y(void) { cout<<"y= "< #include class student { protected: int roll_number; public: Void get_number(int a) { roll_number = } im PULnumber(void) oi oo oll No:*< cla: { char name[20}; float age; public: Person(char *s, float a) ( strepy(name, s); age = } Person & person { if(X.age >= return x; else Tetum *this; } void display(voidy { S person, + Breater(person & x) ge) coutser "Name:"<< #include class Base { public: void display() c cout<<"\n Display Base"; y virtual void show() t cout<<"\n Show Base"; 3 ‘8 class Derived : public Base { public: void display() { cout<<"\n Display Derived"; 4 void show) { cout<<"\n Show Derived"; h void main() t ms WOO ENLIST, mare wit om 4 aso ree Whew Rase Ny Derived Dy Nase *hptrs cout <"\n pte points to Daseln"s pyr 8H byte dlispla 08 byte show Qs cout="\nln bpte points to derived\n"; bpir= &D; bptr “display 0); bptr show 0; getehs information regargiyy 7 (b) Write ‘a C++ program to design a class representing cine: classes having here’) digital library (books, tape: book & tape should be sepa adding new Kem, lsat] class as media), The class should have the functionality for fy - ism. deposit ete, the program should use the runtime polymorphis! Hinclude: lostream.h> #include jude jass media protected: char title[S0}; float pric public: media(char *s, float a) strepy(title, s); price =a; virtual void display(){} h class book : public media t int pages; public: book(char *s, float a, int p) : media(s.a) t Pages = p; ) : void display(); e post ties ible? MT etc * 5 Hoat a, float t-mediats.a) ime 4 ya displays couts<"\n_ cout<<"\n Pages:"<>title; cout<<"\n Price:"; cin>>price; cout<<"\n Pages:"; cin>>pages; :>title; cout<<"\n Price:"; cin>>price; cout<<"\n Play Times(mins): cin>>time; tape tape (title, price, time); rrr 287 prcticls te 105 } yeturn (8895 ) void displaydata( ) cout << 8th; } ye ‘void main( ) { string st = 123; cout << endl << s1.displaydata( ); si = 150; cout << endl << "sI="; sl.displaydata( )s string 82 ("123"); jnt i= int( s2 Ji cout << endl <<" i=s3 cout << endl << "is" << i geteh()s 289 Hinclude #include void main() { int i= 525 float a = 425.0; float b = 123.500328; char str[ ] = "Dream, Then make it happen!"; clrscr(); cout.setf{ ios::unitbuf ); cout.setf{ ios::stdio ); cout setf{ ios::showpos }; cout << i << endl; cout.setf{ io: cout.setf{ ios: cout.setf{ ios::hex, ios::basefield ); < = ait Sain tw Implement Mtn, Calculating length of the string, 8 Lerten cena ate eae Stein ae wat ar tring SOF rmter @ SUPE {trea file.opent"t for(int file.pur(strinel EX’ [ios::0ut): file.seek (0): chy fite.get(oh couts yinclude void main() char source[ 67 ], target{ 67 ]: char ch; cout << endl << "Enter source filename"; cin >> source, cout << endl << "Enter target filename"; cin >> target; ifstream infile (source ); ofstream outfile (target ); while( infile ) * We 202 vas infile.get( ch ); outfile.put( ch ); } getchQ; 4 (© Writes GH program (eer file): the object of a structure! #include #include iinclude void main() t struct employee t char name{ 20 J; int age; float basic; float gro B employee e; char ch = ofstream outfile; outfile.open( "EMPLOYEE.DAT", iost:out | while(ch = "'y") { cout << endl <<"Enter a record"; 8 cin>> ename >> e.age >> e.basic >> e.gross; outfile.write(( char *)&e, sizeof{e)); ‘cout << endl << "Add Another YIN"; cin >> ch; ) outfile.close(); ifstream infile; infile.open("EMPLOYEE txt", while(infile, { Cout << endl << e.name << "ys ) } jos::in | ios::binary ); read((char *)&e,sizeof{e))) << ne SABE << Nt" << e basic void test(int x) throw 1.0; cout<<"End of try-black\n"; } catch(char ¢) { cout<<"Caught a Character\n"; } catch(int c) { cout<<"Caught an Integer\n"; } catch(double c) { cout<<"Caught a Double\n"; } cout<<"End of try-catch system\n"; } void main() { cout<<"Testing Multiple Catches\n"; cout<<"x: test(1); cout<<"x° test(0); cout<<" test(2); getch();, } 20/F.Y B.Sc. (I.T.) - Object Oriented Programming with C++ (Sem.-Il) 1\n"; geteh(s } ‘o create Simple calculator using Class template; ite a CH Program t (a) Wri #include #include template class Calculator { private: , als ee 295 num, num2; Pre iblic! we Ncalculator(T nl, T m2) num! = nl; num2 = n2; } yoid displayResult() { cout << "Numbers are: " << numl <<" and " << num2 <<"" << endl cout << "Addition is: " << add() << endl; ° cout << "Subtraction is: " << subtract() << endl; cout << "Product is: " << multiply() << endl; << divide() << endl; cout << "Division i } Tadd( { return num1 + num2; }+ T subtract() { return num! - num2; } T multiply( { return num1 * num2; } T divideQ) { return num1 / num2; } B void main() { Calculator intCale(2, 1); Calculator floatCale(2.4, 1.2); cout << "Int results:" << endl; intCale.displayResult(); cout << endl << "Float results:" << endl; floatCalc.displayResult(); getch(); includ ey) return x3 else return ys, void main() { ‘TClassMax iMax; // (100, 75); int a, b, is TClassMax fMax; // (90.78, 750.98); float ¢, d, j; cout << "Class Template Programs : Generic Programming : Get Maxii ‘Number \n! : imum Number \n"s cout << "Enter A,B values(integer):"; cin >> a>>b; / iMax = TClassMax(a, b); i= iMax.getMaximun(; — SP°St°s2° practicals cout << "Result Max Int : "<< i; cout << "\n\nEnter C,D values(float):"; cin >> e>>d; {Max = TClassMax(c, d); it fMax.getMaximun(); cout << "Result Max Float : '" <

You might also like