Archive
C++ Program to implement Generic classes for maintaining Stack – Q26
Q26. Program to implement Generic classes for maintaining Stack:
Implement Stacks using generic classes with member functions PUSH, POP and DISPLAY. The Program should show implementation of Stack class with at least three different objects.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> #include <stdlib.h> template <class T> class Cstack{ private: T arr[10]; int ptr; public: void init(){ ptr = -1; } void push(T, int); T pop(int &); void clear(); void display(int); }; template <class T> void Cstack<T> :: push(T elm_push, int t_ptr){ if(t_ptr >= 9) cout<<"\n STACK OVERFLOW."; else arr[++t_ptr] = elm_push; } template <class T> T Cstack<T> :: pop(int &t_ptr){ T elm_pop; if(t_ptr <= -1) cout<<"\n STACK UNDERFLOW."; else{ elm_pop = arr[t_ptr--]; cout<<"\n POPPED value is : "<<elm_pop; return elm_pop; } return 0; } template <class T> void Cstack<T> :: clear(){ ptr = -1; cout<<"\n STACK Cleared."; } template <class T> void Cstack<T> :: display(int t_ptr){ cout<<"\n STACK: "; if(t_ptr == -1) cout<<" Empty"; else for(int i=0; i<=t_ptr; i++) cout<<arr[i]<<" "; } int _menu(){ int _ch; cout<<"\n STACK Type:- \n"; cout<<"\n 1 -> Integer."; cout<<"\n 2 -> Float."; cout<<"\n 3 -> Character."; cout<<"\n Enter your choice: "; cin>>_ch; return (_ch); } void main(){ int ch, i_ptr = -1, f_ptr = -1, c_ptr = -1; int i_num; float f_num; char c_num; Cstack<int> Oint; Cstack<float> Ofloat; Cstack<char> Ochar; Oint.init(); while(1){ clrscr(); cout<<"\n STACK - OPERATION"; cout<<"\n ~~~~~~~~~~~~~~~~~ \n"; cout<<"\n 1 -> PUSH"; cout<<"\n 2 -> POP"; cout<<"\n 3 -> CLEAR"; cout<<"\n 4 -> DISPLAY"; cout<<"\n 5 -> EXIT"; cout<<"\n Enter your choice: "; cin>>ch; switch(ch){ case 1: // PUSH ch = _menu(); switch(ch){ case 1: cout<<"\n Enter an Integer Value to push: "; cin>>i_num; Oint.push(i_num, i_ptr); i_ptr++; break; case 2: cout<<"\n Enter a Float Value to push: "; cin>>f_num; Ofloat.push(f_num, f_ptr); f_ptr++; break; case 3: cout<<"\n Enter a Character to push: "; cin>>c_num; Oint.push(c_num, c_ptr); c_ptr++; break; default: cout<<"\n Wrong Choice. No Operation Done."; } break; case 2: // POP ch = _menu(); switch(ch){ case 1: Oint.pop(i_ptr); i_ptr--; break; case 2: Ofloat.pop(f_ptr); f_ptr--; break; case 3: Ochar.pop(c_ptr); c_ptr--; break; default: cout<<"\n Wrong Choice. No Operation Done."; } break; case 3: // CLEAR ch = _menu(); switch(ch){ case 1: Oint.clear(); i_ptr = -1; break; case 2: Ofloat.clear(); c_ptr = -1; break; case 3: Ochar.clear(); c_ptr = -1; break; default: cout<<"\n Wrong Choice. No Operation Done."; } break; case 4: // DISPLAY cout<<"\n\n Integer Stack:-"; Oint.display(i_ptr); cout<<"\n\n Float Stack:-"; Ofloat.display(f_ptr); cout<<"\n\n Character Stack:-"; Ochar.display(c_ptr); break; case 5: // EXIT cout<<"\n\n You chose to EXIT"; getch(); exit(1); default: cout<<"\n\n Enter an appropriate choice."; } // end of switch. getch(); } // end of while. }// end of main.
Output:
STACK – OPERATION
1 -> PUSH
2 -> POP
3 -> CLEAR
4 -> DISPLAY
5 -> EXIT
Enter your choice: 1
STACK Type:-
1 -> Integer.
2 -> Float.
3 -> Character.
Enter your choice: 1
Enter an Integer Value to push: 43
STACK – OPERATION
1 -> PUSH
2 -> POP
3 -> CLEAR
4 -> DISPLAY
5 -> EXIT
Enter your choice: 4
Integer Stack:-
STACK: 43 119 23
Float Stack:-
STACK: 34.549999 78.900002
Character Stack:-
STACK: a n r
C++ Program to implement Generic Classes for user defined maths library – Q25
Q25. Program to implement Generic Classes for user defined maths library:
Create a user defined maths class for implementing following functions using the concept of generic classes – SUM, N_ROOT, PRODUCT, POWER
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <math.h> template <class T> class Cmath{ private: T n1, n2, n3; public: void getData(); void Sum(); void nRoot(); void Product(); void Power(); }; template <class T> void Cmath<T> :: getData(){ cout<<"\n Enter 1st Number : "; cin>>n1; cout<<"\n Enter 2nd Number : "; cin>>n2; } template <class T> void Cmath<T> :: Sum(){ n3 = n1 + n2; cout<<"\n Sum = "<<n3; } template <class T> void Cmath<T> :: nRoot(){ n3 = sqrt(n1); cout<<"\n Sq Root of "<<n1<<" = "<<n3; n3 = sqrt(n2); cout<<"\n Sq Root of "<<n2<<" = "<<n3; } template <class T> void Cmath<T> :: Product(){ n3 = n1 * n2; cout<<"\n Product = "<<n3; } template <class T> void Cmath<T> :: Power(){ n3 = pow(n1, n2); cout<<"\n Power = "<<n3; } void main(){ int ch; Cmath<int> Obj_i; Cmath<float> Obj_f; while(1){ clrscr(); cout<<"\n Generic Classes"; cout<<"\n ~~~~~~~~~~~~~~~\n"; cout<<"\n 1 -> Sum."; cout<<"\n 2 -> Root."; cout<<"\n 3 -> Product."; cout<<"\n 4 -> Power."; cout<<"\n 5 -> Exit."; cout<<"\n\n Enter your choice: "; cin>>ch; switch(ch){ case 1: // Sum. cout<<"\n Integer:- "; Obj_i.getData(); Obj_i.Sum(); cout<<"\n Floating:- "; Obj_f.getData(); Obj_f.Sum(); break; case 2: // Root. cout<<"\n Integer:- "; Obj_i.getData(); Obj_i.nRoot(); cout<<"\n Floating:- "; Obj_f.getData(); Obj_f.nRoot(); break; case 3: // Root. cout<<"\n Integer:- "; Obj_i.getData(); Obj_i.Product(); cout<<"\n Floating:-"; Obj_f.getData(); Obj_f.Product(); break; case 4: // Power. cout<<"\n Integer:- "; Obj_i.getData(); Obj_i.Power(); cout<<"\n Floating:-"; Obj_f.getData(); Obj_f.Power(); break; case 5: // Exit. exit(1); } // end of switch. getch(); } // end of while. } // end of main.
Output:
Generic Classes
1 -> Sum.
2 -> Root.
3 -> Product.
4 -> Power.
5 -> Exit.
Enter your choice: 1
Integer:-
Enter 1st Number : 23
Enter 2nd Number : 32
Sum = 55
Floating:-
Enter 1st Number : 23.22
Enter 2nd Number : 32.45
Sum = 55.669998
Generic Classes
1 -> Sum.
2 -> Root.
3 -> Product.
4 -> Power.
5 -> Exit.
Enter your choice: 4
Integer:-
Enter 1st Number : 2
Enter 2nd Number : 5
Power = 32
Floating:-
Enter 1st Number : 23.44
Enter 2nd Number : 4.5
Power = 1461535.25
C++ Program to implement Virtual Functions – Q24
Q24. Program to implement Virtual Functions:
Draw the various geometrical figures using the concept of virtual functions along with following class hierarchy:
The base class shapes should contain a virtual function draw().
All the derived classes should contain the function to calculate and display the area of the respective figure.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <graphics.h> #include <stdlib.h> #include <conio.h> #include <math.h> class Cshapes{ protected: int x1, y1; public: Cshapes(){} virtual void Draw() = 0; virtual void Area() = 0; }; class Cline : public Cshapes{ private: int x2, y2; public: Cline(){} Cline(int _x1, int _y1, int _x2, int _y2){ x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; } void Draw(){ line(x1, y1, x2, y2); } void Area(){ cout<<"\n No Area, its a Line."; } }; class Ctrng : public Cshapes{ private: int x2, y2, x3, y3; public: Ctrng(){} Ctrng(int _x1, int _y1, int _x2, int _y2, int _x3, int _y3){ x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; x3 = _x3; y3 = _y3; } void Draw(){ line(x1, y1, x2, y2); line(x2, y2, x3, y3); line(x3, y3, x1, y1); } void Area(){ unsigned int s1, s2, s3; float s, ar; s1 = sqrt( pow((x2-x1), 2) + pow((y2-y1), 2) ); s2 = sqrt( pow((x3-x2), 2) + pow((y3-y2), 2) ); s3 = sqrt( pow((x3-x1), 2) + pow((y3-y1), 2) ); s = (s1+s2+s3)/2; ar = sqrt(s * (s-s1) * (s-s2) * (s-s3)); gotoxy(1, 24); cout<<" Area of Triangle: "<<ar; } }; class Crect : public Cshapes{ private: int x2, y2; public: Crect(){} Crect(int _x1, int _y1, int _x2, int _y2){ x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; } void Draw(){ line(x1, y1, x1, y2); line(x1, y2, x2, y2); line(x2, y2, x2, y1); line(x2, y1, x1, y1); } void Area(){ int ln, bd; unsigned long ar; ln = abs(x2 - x1); bd = abs(y2 - y1); ar = ln * bd; gotoxy(1, 24); cout<<" Area of Rectangle: "<<ar; } }; void main(){ int ch, _x1, _y1, _x2, _y2, _x3, _y3; int gd=DETECT, gm; initgraph(&gd, &gm, "\\tc"); Cline Oline; Ctrng Otrng; Crect Orect; while(1){ clrscr(); cleardevice(); cout<<"\n 1 -> LINE"; cout<<"\n 2 -> TRIANGLE"; cout<<"\n 3 -> RECTANGLE"; cout<<"\n 4 -> EXIT"; cout<<"\n Enter your choice: "; cin>>ch; switch(ch){ case 1: // LINE. cout<<"\n Enter 1st Cordinates: "; cin>>_x1>>_y1; cout<<"\n Enter 2nd Cordinates: "; cin>>_x2>>_y2; Cline Oline(_x1, _y1, _x2, _y2); clrscr(); cleardevice(); Oline.Draw(); Oline.Area(); break; case 2: // TRIANGLE. cout<<"\n Enter 1st Cordinates: "; cin>>_x1>>_y1; cout<<"\n Enter 2nd Cordinates: "; cin>>_x2>>_y2; cout<<"\n Enter 3rd Cordinates: "; cin>>_x3>>_y3; clrscr(); cleardevice(); Ctrng Otrng(_x1, _y1, _x2, _y2, _x3, _y3); Otrng.Draw(); Otrng.Area(); break; case 3: // RECTANGLE. cout<<"\n Enter 1st Cordinates: "; cin>>_x1>>_y1; cout<<"\n Enter 2nd Cordinates: "; cin>>_x2>>_y2; clrscr(); cleardevice(); Crect Orect(_x1, _y1, _x2, _y2); Orect.Draw(); Orect.Area(); break; case 4: // EXIT closegraph(); exit(1); default: cout<<"\n Enter Appropriate Choice."; } // end of switch. getch(); } // end of while. } // end of main.
Output:
DRAW – MAIN MENU
1 -> LINE
2 -> TRIANGLE
3 -> RECTANGLE
4 -> EXIT
Enter your choice: 1
Enter 1st Cordinate: 100 100
Enter 2nd Cordinate: 300 400
No Area, its a Line.
C++ Program to implement Virtual Class by using Multipath Inheritance – Q23
Q23. Program to implement Virtual Class by using Multipath Inheritance:
Issue the books to various members of library.
– Member-Membername, Age, Address
– Faculty (derived from member classs)- Department and integer type array for issuing books with upperlimit 10.
– Student (derived from member class)- Class, Rollno and integer type array for issuing books with upperlimit 4.
Daily Transaction (derived from Faculty and Student classes)- date of issue, date of return (calculated from date of issue as 15 days for students and 45 days for a faculty).
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> #include <string.h> #include <stdlib.h> #include <dos.h> class CDATE{ private: int dd; int mm; int yy; public: struct date d; CDATE(){ getdate(&d); dd = d.da_day; mm = d.da_mon; yy = d.da_year; } CDATE(int d, int m, int y){ dd = d; mm = m; yy = y; } }; class CMember{ protected: char m_name[20], m_add[30]; int m_age; public: CMember(){} CMember(char *Mnam, char *Madd, int Mage){ strcpy(m_name, Mnam); strcpy(m_add, Madd); m_age = Mage; } }; class CFaculty : public virtual CMember{ protected: char f_dept[20]; int f_bk_issue; public: CFaculty(){} CFaculty(char *Fdept, int Fiss){ strcpy(f_dept, Fdept); f_bk_issue = Fiss; } }; class CStudent : public virtual CMember{ protected: char s_clas[10]; int s_roll, s_bk_issue; public: CStudent(){} CStudent(char *Sclas, int Sroll, int Siss){ strcpy(s_clas, Sclas); s_roll = Sroll; s_bk_issue = Siss; } }; class CTrans: public CFaculty , public CStudent{ private: CDATE dt; int dayStu; int dayFac; char bk_name[20]; public: CTrans(){ dayStu = 15; dayFac = 45; } CTrans(char *Tnam, char *Tadd, int Tage, char *Tdept, int Tiss, char *Tbk_nam, CDATE Tdoi) : CMember(Tnam, Tadd, Tage), CFaculty(Tdept, Tiss){ strcpy(bk_name, Tbk_nam); dt = Tdoi; } CTrans(char *Tnam, char *Tadd, int Tage, char *Tclas, int Troll, int Tiss, char *Tbk_nam, CDATE Tdoi) : CMember(Tnam, Tadd, Tage), CStudent(Tclas, Troll, Tiss){ strcpy(bk_name, Tbk_nam); dt = Tdoi; } void Fac_Show(){ cout<<"\n Faculty Details"; cout<<"\n Name: "<<m_name; cout<<"\n Address: "<<m_add; cout<<"\n Age: "<<m_age; cout<<"\n Department: "<<f_dept; cout<<"\n Total Books to Issue: "<<f_bk_issue; cout<<"\n Book name: "<<bk_name; } void Stu_Show(){ cout<<"\n Student Details"; cout<<"\n Name: "<<m_name; cout<<"\n Address: "<<m_add; cout<<"\n Age: "<<m_age; cout<<"\n Class: "<<s_clas; cout<<"\n Total Books to Issue: "<<s_bk_issue; cout<<"\n Book name: "<<bk_name; } }; void main(){ int ch; char book_name[20]; CTrans ObjLib; while(1){ clrscr(); cout<<"\n LIBRARY MANAGEMANT"; cout<<"\n ~~~~~~~~~~~~~~~~~~"; cout<<"\n 1 -> Issue Books to Faculty."; cout<<"\n 2 -> Issue Books to Student."; cout<<"\n 3 -> Show Details of Faculty."; cout<<"\n 4 -> Show Details of Student."; cout<<"\n 5 -> EXIT."; cout<<"\n Enter your choice: "; cin>>ch; switch(ch){ case 1: cout<<"\n Enter Book Name: "; cin>>book_name; CDATE dt1(5, 10, 2004); CTrans Olib1("Manoj", "Dwarka", 23, "Computer", 15, book_name, dt1); break; case 2: cout<<"\n Enter Book Name: "; cin>>book_name; CDATE dt2(5, 10,2004); CTrans Olib2("Nitin", "Dwarka", 24, "MCA", 214, 5, book_name, dt2); break; case 3: ObjLib.Fac_Show(); break; case 4: ObjLib.Stu_Show(); break; default: exit(1); } // end of switch. getch(); } // end of while. } // end of main.
Output:
LIBRARY MANAGEMANT: –
~~~~~~~~~~~~~~~
1 -> Issue Books to Faculty.
2 -> Issue Books to Student.
3 -> Show Details of Faculty.
4 -> Show Details of Student.
5 -> EXIT.
Enter your choice: 2
Name: Manoj
Address: Dwarka
Age: 23
Course: MCA
Enter Book Name: c plus plus
LIBRARY MANAGEMANT: –
~~~~~~~~~~~~~~~
1 -> Issue Books to Faculty.
2 -> Issue Books to Student.
3 -> Show Details of Faculty.
4 -> Show Details of Student.
5 -> EXIT.
Enter your choice: 4
Student Details: –
Name: Manoj
Address: Dwarka
Age: 23
Course: MCA
Total Books to Issue: 4
Book name: CPP
C++ Program to implement Multiple Inheritance – Q22
Q22. Program to implement Multiple Inheritance:
Display the digital clock using multiple inheritance. Using a time and date class, for deriving the class digiclock. Use appropriate functions to increment date and time.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> #include <dos.h> class CDate{ protected: int dd, mm, yy; struct date d; public: void initDate(){ getdate(&d); dd = d.da_day; mm = d.da_mon; yy = d.da_year; } void showDate(){ initDate(); cout<<dd<<" / "<<mm<<" / "<<yy; } }; class CTime{ protected: int hrs, min, sec; struct time t; public: void initTime(){ gettime(&t); hrs = t.ti_hour; min = t.ti_min; sec = t.ti_sec; } void showTime(){ initTime(); cout<<hrs<<" : "<<min<<" : "<<sec; } }; class CDigiClock : public CDate, public CTime{ public: CDigiClock(){ showDate(); cout<<"\t"; showTime(); } }; void main(){ clrscr(); cout<<" Current Date and Time is : - \n"; while(!kbhit()){ gotoxy(5, 5); CDigiClock(); } }
Output:
Current Date and Time is : –
18 / 11 / 2004 21 : 55 : 51