Advertisements

Archive

Posts Tagged ‘Cpp Inheritance’

C++ Program to implement Virtual Class by using Multipath Inheritance – Q23

January 23, 2010 Leave a comment

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


Advertisements

C++ Program to implement Multiple Inheritance – Q22

January 22, 2010 Leave a comment

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


C++ Program to implement Hierarchical Inheritance (Part 2) – Q21

January 21, 2010 Leave a comment

Q21. Program to implement Hierarchical Inheritance (Part 2):

A university is associated with three different kinds of students Undergraduate , Post-Graduate and Doctoral.
– Undergraduate students have grades awarded to the for individual subjects.
– Postgraduate students sit for 8 papers and deliver a seminar. They are awarded grades for these.
– Doctoral students sit for 4 papers and write a thesis. They are awarded grades for the papers but for the thesis the evaluation is only whether the thesis is accepted or not.

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

enum boolean {false, true};

class Cstud{
	protected:
		char name[20];
	};

class Cunderg : public Cstud{
	private:
		int papers[5];
		char grade;
	public:
		int tot, avg;
		void getdata(){
			cout<<"\n Enter Name: ";
			cin>>name;
			cout<<"\n Enter Marks in 5 Subjects: -"<<endl;
			tot = 0;
			for(int i=0; i<5; i++){
				cin>>papers[i];
				tot += papers[i];
				}
			avg = tot / 5;
			if(avg >= 80 && avg < 100) grade = 'A';
			else if(avg > 60 && avg < 80) grade = 'B';
			else if(avg > 40 && avg < 60) grade = 'C';
			else grade = 'D';
			}
		void showdata(){
			cout<<"\n Name: "<<name;
			cout<<"\n Total Marks: "<<tot;
			cout<<"\n Grade: "<<grade;
			}
	};

class Cpostg : public Cstud{
	private:
		int papers[8], sem;
		char grade;
	public:
		int tot;
		void getdata(){
			cout<<"\n Enter Name: ";
			cin>>name;
			cout<<"\n Enter Marks in 8 Subjects: -"<<endl;
			tot = 0;
			for(int i=0; i<8; i++){
				cin>>papers[i];
				tot += papers[i];
				}
			cout<<"\n Enter Marks of Seminar: ";
			cin>>sem;
			if(sem >= 80 && sem < 100) grade = 'A';
			else if(sem > 60 && sem < 80) grade = 'B';
			else if(sem > 40 && sem < 60) grade = 'C';
			else grade = 'D';
			}
		void showdata(){
			cout<<"\n Name: "<<name;
			cout<<"\n Total Marks in 8 Papers: "<<tot;
			cout<<"\n Seminar Score: "<<sem;
			cout<<"\n Grade: "<<grade;
			}
	};

class Cdoctor : virtual public Cstud{
	private:
		int papers[4];
		boolean thesis;
		char grade;
	public:
		int tot, avg;
		char ch;
		void getdata(){
			cout<<"\n Enter Name: ";
			cin>>name;
			cout<<"\n Enter Marks in 4 Subjects: -"<<endl;
			tot = 0;
			for(int i=0; i<4; i++){
				cin>>papers[i];
				tot += papers[i];
				}
			avg = tot / 4;
			if(avg >= 80 && avg < 100) grade = 'A';
			else if(avg > 60 && avg < 80) grade = 'B';
			else if(avg > 40 && avg < 60) grade = 'C';
			else grade = 'D';
			cout<<"\n Is the Thesis Accepted (y/n): ";
			cin>>ch;
			if(ch == 'y') thesis = true;
			else thesis = false;
			}
		void showdata(){
			cout<<"\n Name: "<<name;
			cout<<"\n Total Marks: "<<tot;
			cout<<"\n Grade: "<<grade;
			cout<<"\n Thesis: ";
			if(thesis) cout<<"Accepted";
			else cout<<"Not Accepted";
			}

	};

void main(){
	int ch;
	while(1){
		clrscr();
		cout<<"\n UNIVERSITY";
		cout<<"\n ~~~~~~~~~~";
		cout<<"\n 1 -> Under Graduates.";
		cout<<"\n 2 -> Post Graduates.";
		cout<<"\n 3 -> Doctoral.";
		cout<<"\n 4 -> Exit.";
		cout<<"\n Enter your choice: ";
		cin>>ch;
		switch(ch){
			case 1:		// Under-Graduates.
				Cunderg Oug;
				Oug.getdata();
				Oug.showdata();
				break;
			case 2:		// Post-Graduates.
				Cpostg Opg;
				Opg.getdata();
				Opg.showdata();
				break;
			case 3:		// Doctoral.
				Cdoctor Od;
				Od.getdata();
				Od.showdata();
				break;
			default:
				exit(1);
			} // end of switch.
		getch();
		} // end of while.
	} // end of main.

 

Output:

UNIVERSITY
1 -> Under Graduates.
2 -> Post Graduates.
3 -> Doctoral.
4 -> Exit.
Enter your choice: 1

Enter Name: Nitin

Enter Marks in 5 Subjects: –
200
300
400
350
150

Name: Nitin
Total Marks: 1400
Grade: D

UNIVERSITY
1 -> Under Graduates.
2 -> Post Graduates.
3 -> Doctoral.
4 -> Exit.
Enter your choice: 2

Enter Name: Manoj

Enter Marks in 8 Subjects: –
200
300
250
350
150
400
200
100

Enter Marks of Seminar: 400

Name: Manoj
Total Marks in 8 Papers: 1950
Seminar Score: 400
Grade: D


C++ Program to implement Hierarchical Inheritance (Part 1) – Q20

January 20, 2010 Leave a comment

Q20. Program to implement Hierarchical Inheritance (Part 1):

Imagine a publishing company that markets books as well as audicassette. Create a class publication that stores the title and price. From this class derive two classes as follows:
– Book, which adds a Pagecount, AuthurName, Edition
– Tape, which adds Playnigtime in minutes, SpeakerName, Date of Recording
Each of these classes should have a getdata( ) and putdat()
Functions to read and to display the data reapectively.

… from College notes (BCA/MCA assignments):

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

class CPubs{
	protected:
		char title[20];
		float price;
	};

class CBooks : public CPubs{
	private:
		char aname[20];
		int pcount, edition;
	public:
		void getdata(){
			cout<<"\n Enter Title of Book: ";
			cin>>title;
			cout<<"\n Enter Price: ";
			cin>>price;
			cout<<"\n Enter Author Name: ";
			cin>>aname;
			cout<<"\n Enter Page Count: ";
			cin>>pcount;
			cout<<"\n Enter Edition: ";
			cin>>edition;
			}
		void showdata(){
			cout<<"\n\t Book Title: "<<title;
			cout<<"\n\t Price: "<<price;
			cout<<"\n\t Author Name: "<<aname;
			cout<<"\n\t Page Count: "<<pcount;
			cout<<"\n\t Edition no.: "<<edition;
			}
	};

class CTapes : public CPubs{
	private:
		char sname[20];
		int ptime;
		int dd, mm, yy;
	public:
		void getdata(){
			cout<<"\n Enter Title of Cassette: ";
			cin>>title;
			cout<<"\n Enter Price: ";
			cin>>price;
			cout<<"\n Enter Speaker Name: ";
			cin>>sname;
			cout<<"\n Enter Play Time: ";
			cin>>ptime;
			cout<<"\n Enter Date of Recording (dd mm yy): ";
			cin>>dd>>mm>>yy;
			}
		void showdata(){
			cout<<"\n\t Cassette Title: "<<title;
			cout<<"\n\t Price: "<<price;
			cout<<"\n\t Speaker Name: "<<sname;
			cout<<"\n\t Play Time: "<<ptime;
			cout<<"\n\t Date of Recording: " 
    <<dd<<":"<<mm<<":"<<yy;
			}
	};

void main(){

	CBooks Obook;
	CTapes Otape;

	clrscr();

	cout<<"\n Publishing Company";
	cout<<"\n ~~~~~~~~~~~~~~~~~~";

	cout<<"\n Enter the Information of BOOKS:-"<<endl;
	Obook.getdata();
	cout<<"\n Enter the Information of TAPES:-"<<endl;
	Otape.getdata();

	clrscr();

	cout<<"\n Information of BOOKS & TAPES : -";
	cout<<"\n\n BOOKS : -"<<endl;
	Obook.showdata();
	cout<<"\n\n TAPES : -"<<endl;
	Otape.showdata();

	getch();

	}

 

Output:

Publishing Company

Enter the Information of BOOKS:-

Enter Title of Book: cplusplus

Enter Price: 250

Enter Author Name: stroustrup

Enter Page Count: 700

Enter Edition: 4

Enter the Information of TAPES:-

Enter Title of Cassette: bacardiblast

Enter Price: 125

Enter Speaker Name: ricky

Enter Play Time: 90

Enter Date of Recording (dd mm yy): 10 03 2000

 

Information of BOOKS & TAPES : –

BOOKS : –
Book Title: cplusplus
Price: 250
Author Name: stroustrup
Page Count: 700
Edition no.: 4

TAPES : –
Cassette Title: bacardiblast
Price: 125
Speaker Name: ricky
Play Time: 90
Date of Recording: 10:3:2000