Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views37 pages

C++ Assignment All Questions and Answers

The document provides a comprehensive list of practical C++ programming exercises, including creating a simple calculator, converting seconds to hours, and calculating volumes of geometric shapes. It also covers object-oriented programming concepts such as classes, inheritance, operator overloading, and exception handling. Additionally, it includes tasks for file I/O operations and memory management in C++.

Uploaded by

as8487928
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)
23 views37 pages

C++ Assignment All Questions and Answers

The document provides a comprehensive list of practical C++ programming exercises, including creating a simple calculator, converting seconds to hours, and calculating volumes of geometric shapes. It also covers object-oriented programming concepts such as classes, inheritance, operator overloading, and exception handling. Additionally, it includes tasks for file I/O operations and memory management in C++.

Uploaded by

as8487928
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/ 37
List of Practical : Write a C++ program to create a simple calculator. 1. a) b) 2 2. “al, b) Co} 3. a) b) °) d) Write a C++ program to convert seconds into hours, minutes and seconds. Write a C+ + program to find the volume of a square, cone, and rectangle. Write a C++ program to find the greatest of three numbers. Write a C++ program to find the sum of even and odd n natural numbers Write a C-++ program to generate all the prime numbers between 1 and n, where nis a value supplied by the user. Write a C++ program using classes and object Student to print name of the student, roll_no. Display the same. Write a C++ program for Structure bank employee to print name of the employee, account_no: & balance. Display the samé also display the balance after withdraw and deposit Write a C++ Program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class. Write a Program to find Maximum out of Two Numbers using friend function. Note : Heré one number is a member of one class and the other number is member of some other class. Write a C++ Program using copy constructor to copy data of an object to another object. Write a C++ Program to allocate memory dynamically for an object of a given class using class’s constructor. Write a C++ program to design a class representing complex numbers and having the functionality of performing addition & multiplication of two complex numbers using operator overloading. Write a C++ program to overload new/delete operators in a class. a STUDENT class using pointer to Write a C++ program to access members of object members Write 2 C-++ Program to generate Fibonacci Series by using Constructor to initialize the Data Members. Write a C++ Program that illustrate single inheritance. Write a C-+-+ Program that illustrate multipe inheritance. Write a C++ Program that illustrate multi level inheritance Write a C++ Program that illustrate Hierarchical inheritance. .e constructors are implemented and the Jasses are inherited. Use three classes base class and gamma is Write a C++ Program illustrating how thi order in which they are called when the cl named alpha, beta, gamma such that alpha,beta are derived class inheriting alpha & beta jw) 5. \a) Write a C++ Program to design a stuent class representing student roll no. anda f the student in 9. ae b) Write a'C++ Program to get maximum of two number using Class template. 7. a Write a C++ program illustrating the use of virtual functions in class. test class (derived class of student) representing the scores of various subjects and sports class representing the score in sports. The sports and test class should be inherited by a result class having the functionality to add the scores and display the final result for a student. 6. a) Write a C++ program to maintain the records of person with details (Name and Age) and find the eldest among them. The program must use this pointer to return the result. b) Write a C++ program to design a class representing the information regarding digital library (books, tape: book & tape. should be separate classes having the base class as media). The class should have the functionality for adding new item, issuing, deposit etc. the program should use the runtime polymorphism. ‘rite a C+ + program to show conversion from string to int and vice-versa. b) Write a C++ program implementing basic operation of class ios i.e. setf, unset precision etc. . c) Write a C++ program to implement I/O operations on characters. 1/ operations includes inputting a string, Calculating length of the string, Storinj the String in a file, fetching the stored characters from it, etc. d) ‘ite a C++ program to copy the contents of one file to another. e) Write a C++ program to perform read/write binary I/O operation on a fil (i.e. write the object of a structure/class to file). f) Write a C++ program to implement the exception handling with multiple cat statements. a) Write a C++ Program to create Simple calculator using Class template. jde fe # de inl! void mainO {sat numOne, numTwo, res; int choices do { cout<<"1. Addition\n"; cout<<"2. Subtraction\n" cout<<"3. Multiplication\n"; cout<<"4. Division\n"; cout<<"5, Exit\n\n"; cout<<"Enter Your Choice(1-5): "; cin>>choice; if(choice>=1 && choice<=4) ( { cout<<"\nEnter any two Numbers: cin>>numOne>>numTwo; } switch(choice) ‘ case I: res = numOnet+numTwo; cout<<"\nResult = "< #include void main() { int seconds, hours, minutes; clrser (); cout<<"\n Enter time in seconds: cin>>seconds; minutes=seconds/60; hours=minutes/60; cout< #include void main () { const float pi = 3.14159; float r,h,Lb,s,vol; clrser(); cout<<"\n Enter the radius and height of the cone: "; cin>>i>>h; // Cone's volume. vol=(1.0/3.0)*pi*(r*r)*h; cout <<"\n The volume of the Cone is: " << vol ; cout<< "\n\n Enter the length, breadth and height of a Rectangle: "; cin>>l>>b>>h; // Rectangle's volume. | patel | oie(itb* hs pt ae _ teerinThe volume of the Rectangle is:" << vol; cout<< "nin Enter the side of a Square: ino ?33 | jjsquare’s volume, | yole(s*s*8)5 cout<<"n, The volume of the Square is: " << vol ; getohOs : | pinclude finclude | void main 0 qed int nl, n2, n3; clrser(); cout << "Enter three number: cin >> nl >> n2 >> 3; I check if n1 is the largest number if(nl >= n2 && nl >= n3) cout << "Largest number: " <<; I/ check if n2 is the largest number else if(n2 >= nl && n2>=n3) cout << "Largest number: " < #include void main() { int number, maximum, ‘evenSum = 0, oddSum = f clrser(); cout << "\nPlease Enter the Maximum Limit for Even & Odd Numbers = cin >> maximum; for(number = 1; number <= maximum; number++) { if (number % 2 ==0) a evenSum = evenSum + number; } else oddSum = oddSum + number; } 3 "<< evenSum; << oddSum; cout << "\nThe Sum of All Even Numbers upto " << maximum <<" cout << "\nThe Sum of All Odd Numbers upto " << maximum <<" getch(); “© Write ac #include #include void main() { int num, i, upto; // Take input from user cout << "Find prime numbers upto : "; ‘cin >> upto; cout << endl << "Alll prime numbers upto " << upto <<" are : "<< endl; for(num = 2; num <= upto; num++) : for(i = 2; i <= (num / 2); i++) { iffmum % i == 0) { num; break; } } 1/ If the number is prime then print it. cout << num <<"; } Try _] getch(); 267 Write CH pronran » roll_no. Display wn vinclude ginclude class Student 7 private: char name[30}; int rollNo; public: /Imember function to get stude void getDetails(voiay; nt's details /Imember function to print student's details void putDetails(void); he ‘member function definition, outside of the class void Student::getDetails() { clrser(); cout << "Enter Name: "; cin >> name; cout << "Enter Roll Number: "; cin >> rollNo; } MImember function definition, outside of the class void Student::putDetails() ( | cout << "Student details:\n"; “cout <<"HHenttenataneensenenn, i | cout << "\nName:"<< name < #include #include #include class Bank { private: int Accno; string AccName; char AccType; float BalanceAmt; public: Void getAccountDetails() { cout<<"Please enter account details: cout<<"Account Number:"; cin>>Accno; cout<<"Account Holder Name:"; cin>>AccName; Cout<<"AccountType(c-Current,s-Savings):"; ‘>AceType; cout<<"Balance Amount:"; cin>>BalanceAmt; 3 void printAccountDetails() { : cout<<"Account holder details: ‘< 4include class test { int objNo; ro sy Obleet Oriented Programming with C++ (FYBSCA1T, (oy static int objCnt; “ty public: test() t objNo = ++objCnt; } ~test() { ~-objCnt; 3 void printObjNumber(void) t cout << "object number ;" << objNo << "\n"5 p static void printObjCount(void) { cout << "count:" << objCnt<< "\n"; rH int test::0bjCnt; void main) { test t1, 12; test::printObjCount(); test t3; test::printObjCount(); t1.printObjNumber(); 12.printObjNumber(); {3.printObjNumber(); getch; Z (a) Write a Program to find Maximum out of Two Numbers using friend functic Here one number is a member of one class and the other number is membe! _ other class, : ‘ #include #include class a; class b { int number; protic public: wor a bint x) { number=x; s } void friend greatest(a al,b b1); h class { | int number; public: a(int x) { number=x; } void friend greatest(a al,b b1); ‘ void greatest(a al,b bl) { if(a1 number>b1 number) { i cout<<"\n Number in class A is greatest i. } else if(a1 number>num; aal(aum); cout<<"\n Enter number for class B- cin>>num; bbi(num); greatest(al.b1)s 7 rrr Object Oriented Programming with C** (FYBSCAT) Semyy cout<<"\n"; getch(); #include #include class Demo { private: int num1, num2; public: Demo(int n1, int n2) { num1 =nl; num2 = n2; 3 Demo(const Demo &n) { num] = n.num1; num2 = n.num2; J void display) { cout<<"\nnum1 = "<< num! < #include class Memory ( const char* p; public: // default constructor Memory() { P= new char{6];- P="Sweta"; 3 void display() cout << p << endl; ye ‘ void main() { Memory obj; clrscr(); obj.display(); getch(); a “4 allocating memory at run tim time #include #include 1/Creating class Complex class Complex if public: int real,img; Hladd function to add two matrices void add(Complex c1,Complex c2) { int x,y; x=cl.realtc2.real; _-y=cl.img+c2.img; 3 ~ cout<<"in("<>a.real>>a.img; cout<<"\nEnter real and imaginary part of second complex number? cin>>b.real>>b.img; c.add(a,b); d.multiply(a,b); getch(); } jew/delete operators invalclass) nn i) Writeaict #include #include #include class CustomMemory { public: void* operator new(size_t objectSize);//Overloaded new Void operator delete(void* ptr); //Overloaded delete F program to overlo oF void* CustomMemory::operator new(size_t objectSize) { cout<<"Custom memory allocation”< #include class Student { public: int roll_num; void print() { cout << } "\nRoll Number is “<*ptr=20; sp->print0s getchOs ‘include #include class fibonacei { ong int a,b; //data members public: fibonacci() //special member function constructor { } : void fibseries(int n) //member function { int isnext; cout<<"\n Resultant fibonacci series"; cout<<"\n-- ——----\n"; for(i=0;i> mo; cout <<" Enter the marks of five subjects " << endl; J use for loop for (i=0;i<5;i+4) { cin >> marks[ij; for (i=0;1<55 i++) { // store the sum of five subject sum = sum + marks[iJ; 3 h 1/ create base class2 class sports_mark { protected: int s_mark; public: void get_mark() { cout << "\n Enter the sports mark: "; cin >> s_mark; t + create a result as the child class to inherit functions of the parent class: student detail amt sports_mark.*/ class result: public student_detail, public sports_mark { int tot; | float avg; public: // create member function of child class void disp 0) { practicals tot=sum +5 mark; Vg = tot / 6; / total mar cout <<"\n \n\t Roll N cout <<" Ks Of six subject /g lo: "< #include / Base class A ram that illustrate multilevel inheritance, class A { public: AO { inta=5,b=6,¢; c=ath; cout << "Sum is:" < “#inelude class A //single base class { public: int x, ys void getdata() { cout << "\nEnter value of x and y: cin>>x>>y; b class B : public A //B is derived from class base { public: void product() { cout << "\nProduct= "<< x * ys } B class C : public A //C is also derived from class base { public: void sum(, { cout << "\nSum="< #include class alpha { int x; public: alpha(int i) { x=i; cout<<"alpha initialized\n"; : } void show_x(void) { cout<<"x="< + 1 MYA GAT, 282 wes" oyjet Oriented Programmi wn cor ey ds class gamma : public beta, public alpha { int m,n; publi gamma(int a, float b, int c, int d): alpha(a), beta(b) ( m=c;n=d; cout<<"gamma initialized\n"; } void show_mn(void) { #include class student { protected: int roll_number; public: void get number(int a) { roll_number = a; } void put _number(void) { rrr cout<<"Roll No: } Me class test : public student { protected: float partl, part2; < #include #include 3 class person { char name[20]; float age; public: person(char *5, float a) { strepy(name, s); age =a; : person & person :: greater(person & x) { if(x.age >= age) return x; else return *this; } void display(void) { cout<<"Name:"< j #include ‘class Base i public: “void display) i /cout<<"\n Display Base"; 0 | virtual void show() “ cout<<"\n Show Base"; B UB class Derived : public Base cout<<"\n Display Derived"; } void show() { cout<<"\n Show Derived"; } hi void main() sf jalfunctions in class. AT) Semsy ing with Ce EYE Se. 286 ren Object Oriented Program” Base B; Derived D; Base *bptr; cout<<"\n bptr points to Base\n"; bptr= &B; : bptr display (); bpir ->show 0); cout<<"\n\n bptr points to derived\n"; bpir=&D; bptr display 0; bptr ->show (); getch(); } 5 resenting the information regarding 7. (b) Write a CH program to design lass represen ne cs having the bas digital library (books, tape: book & tape should be s E ranean class as media). The class should have the functionality for adding new item, issuing, deposit ete. the program should use the runtimé polymorphism. finelude : #include #include class media { protected: char title[50]; float price; public: media(char *s, float a) { strepy(title, s); price = a; } virtual void display()(} B class book : public media it int pages; public: book(char *s, float a, int p) : media(s,a) { pages = p; i, void display; B : practicals joss tape : public media { froat times lic: Fpe(char * § Hoat a, float t):medin(s,a) { time = Lie oid display; I oid book ::display() { cout<<"\n Title:"<>title; cout<<"\n Price cin>>price; cout<<"\n Pages:"; cin>>pages; book book (title, price; pages); cout<<"\n Enter Tape Details"; cout<<"\n Title:"; cin>>title; cout<<"\n Price:"; cin>>price; ‘cout<<"\n Play Times(mins):"; cin>>time; ‘tape tapel (title, price, time); Lace rr Mee TS media* list(2); list[0] = &book1; list{1] = &tapel; cout<<"\n Media Details"; cout<<"\n... list[0}->display (); cout<<' Tape. list{1]->display getchO; } 100k... #include #include #include #include class string { private: char str[20]; public: string() { str[0] = ‘\o'; } string( char * s ) { strepy( str, s ); } string( int a) fs : itoa( a, str, 10 ); } operator int() { inti=0, 1, ss=0, k=1; 1=strlen( str) - 1; while(1>=0) { ss=ss+(str[1]-48)*k; practicals , rere 289 t= 105 } eturn (85 J; } yoid displaydata( ) { cout << Str; } i ; yoid main( ) ( ( string sl = 123; cout << endl << "s1: sl.displaydata( ); s1= 150; cout << endl <<"; sl.displaydata( ); string s2 ("123"); { int i = int( s2 ); cout << endl << "i=" << i; string s3 ("456"); i=s3; cout << endl << "i=" << i; getch0; } #include #include void main() { int i= 525 float a = 425.0; float b = 123.500328; "Dream. Then make it happen!"; cout.setf( ios::showpos ); “cout << i << endl; with C++ (FYBSe-L.T) (Sem, 290 gee Object Oriented Programming, y cout << i << endl; cout.setf{ ios::oct, ios::basefield ); cout < ginclude erations 9; sett of the FH ginclude ginclude yoid main() { char string[80]; cout<<"Enter a String \n"; cin>>string; int len = strlen(string); fstream file; file.open("TEXT", jos:sin | io for(int i=0;i #include #include void main() { char source[ 67 ], target[ 67 J; char ch; cout << endl << "Enter source filename"; cin >> source; cout << endl << "Enter target filename"; cin >> target: ifstream infile ( source ); ofstream outfile ( target )s while( infile ) { ming with C++ (FYBSEALT) Sem, yy 292 rare Object Oriented Program! infile.get( ch ); outfile.put( ch ); #include #include #include void main() { struct employee { char namef 20 ]; int age; float basic; float gross; employee e; char ch ="Y'; ofstream outfile; outfile.open( "EMPLOYEE.DAT", ios::out | ios::binary ); while(ch == "y") bf : + cout << endl <<"Enter a record"; + cin >> e.name >> e.age >> e.basic >> e.gross; outfile.write(( char *)&e, sizeof(e)); | cout << endl << "Add Another Y/N' cin >> ch; bo | outfile.close(); | ifstream infile; infile.open("EMPLOYEE.txt", ios::in | ios: while(infile.read((char *)&e,sizeof(e))) { cout << endl << e.name << "\t" << e.age << "\t" << e.basic << "t" |e 3 } inary ); << e.gross; jude gincll syoid test(int *) { wy { jee) throw X3 else if) throw 'x'5 else if(—=-1) throw 1.05 cout<<"End of try-black\n"; hb} " catch(char ©) { cout<<"Caught a Character\n" ay -catch(int c) { cout<<"Caught an Integerin"; } catch(double ©) { cout<<"Caught a Double\n"; } out<<"End of try-catch system\n"s } void main() { cou cout<<" test(1); | cout<<"x==0\n"s | test(0); | cout<<" j test(2); getchQ; } 20/F YB Se. (1.T.) - Object Oriented. t< #include void divide(double x, double y) { cout<<"Inside Funetion\n"; wy { ify==0.0) throw ys else cout<<"Division ="< #include template class Calculator { private: | | Practicals ree 205 ‘T num], num2; publics h Caleulator(T m1, n2) { uml = nl; num2 = n2; void displayResult() { cout << "Numbers are: " << num <<" and" << num2 <<"." << endl; cout << "Addition is: " << add() << en cout << "Subtraction i cout << "Product cout << "Di "<< subtract() << endl; << multiply() << endl; "<< divide() << endl; Tadd) { return num + num2; } T subtract() { return num! - num2; } ‘T multiply { return numt * num2; } T divide() { return num] / num2; } void main() { Calculator intCale(2, 1); Calculator floatCale(2.4, 1.2); cout << "Int results:" << endl; intCale.displayResult(); cout << end] << "Float results:” << endl; floatCale.displayResult(); getch(); Ibject Oriented Programming with C** (EY BSL) (Sey, Object Orient #include Hinclude #include 1 Template Declaration template / Template Class class TClassMax { Tx, y; public: TClassMax() { J TClassMax(T first, T second) { X= first; y = second; } T getMaximun() { if(x> y) return x; else retum y; ss h void main() { ‘TClassMax iMax; // (100, 75); inta, b, i; ‘TClassMax fMax; // (90.78, 750.98); float c, d, j; cout << "Class Template Programs : Ger cout << "Enter A,B values(intege: cin >> a>>b; iMax = TClassMax(a, b); i> iMax.getMaximun; neric Programming : Get Maximum Number \n": = cout << Result Max Int :" << i; cout << *\p\nEnter C,D values(float):"; ein 2 774 (Max = TClassMax(c, d); (Max.getMaximun(); ut << "Result Max Float : " <

You might also like