Archive

Archive for January, 2010

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


C++ Program to implement Operator Overloading (>>, <<) – Q19

January 19, 2010 Leave a comment

Q19. Program to implement Operator Overloading (>>, <<):

Write a class which contains a string as private data member. Add a member function IsPalindrome that returns TRUE/FALSE depending upon whether the str is palindrome or not. Overload the <> operator to insert the string and print the string respectively along with appropriate message.

… from College notes (BCA/MCA assignments):

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

enum bool {false, true};

class Cpal{
	private:
		char *str;
	public:
		Cpal(){}

		bool IsPalindrome();

		friend ostream& operator<<(ostream &, const Cpal &);

		friend istream& operator>>(istream &, const Cpal &);
	};

bool Cpal :: IsPalindrome(){
	int len, i, j;
	bool valid;

	len = strlen(str);

	for(i=0, j=len-1; i<len/2; i++, j--){
		if(str[i] == str[j])
			valid = true;
		else{
			valid = false;
			break;
			}
		}
	return valid;
	}

istream& operator>>(istream &istr, const Cpal &Opal){
	return istr.get(Opal.str, 80);
	}

ostream& operator<<(ostream &ostr, const Cpal &Opal){
	return ostr<<Opal.str;
	}

void main(){
	Cpal Opal;
	clrscr();

	cout<<"\n Enter a String: ";
	cin>>Opal;

	cout<<"\n The String: ";
	cout<<Opal;

	if(Opal.IsPalindrome())
		cout<<"\n\t is a Palindrome.";
	else
		cout<<"\n\t is not a Palindrome.";

	getch();
	}

 

Output:

Enter a String: able was i i saw elba

The String: able was i i saw elba
is a Palindrome.

 

Enter a String: my name is Manoj

The String: my name is Manoj
is not a Palindrome.


Categories: Cpp Tags: ,

C++ Program to implement Operator Overloading for String Comparison with relational op (+, , ==) – Q18

January 18, 2010 Leave a comment

Q18. Program to implement Operator Overloading for String Comparison with relational op (+, , ==):

Implement the operator overloading for following relational operators for two different strings
– Concatenate two strings using +
– Compare two strings with following overloaded operator >, <, ==

… from College notes (BCA/MCA assignments):

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

class CStrings{
	private:
		char str[20];
	public:
		void getdata();
		friend CStrings operator+(CStrings&, CStrings&);
		friend int operator<(CStrings&, CStrings&);
		friend int operator>(CStrings&, CStrings&);
		friend int operator==(CStrings&, CStrings&);
	};

void CStrings :: getdata(){
	cout<<"\n Enter a String: ";
	cin>>str;
	}

CStrings operator+(CStrings &s1, CStrings &s2){
	CStrings Os;
	strcpy(Os.str, s1.str);
	strcat(Os.str, s2.str);
	cout<<"\n Concatenation of Str1 & str2: "<<Os.str;
	return Os;
	}

int operator<(CStrings &s1, CStrings &s2){
	if(strcmp(s1.str, s2.str) < 0)
		return 1;
	else
		return 0;
	}

int operator>(CStrings &s1, CStrings &s2){
	if(strcmp(s1.str, s2.str) > 0)
		return 1;
	else
		return 0;
	}


int operator==(CStrings &s1, CStrings &s2){
	if(strcmp(s1.str, s2.str) == 0)
		return 1;
	else
		return 0;
	}

void main(){
	clrscr();
	CStrings Ostr, Ostr1, Ostr2;

	cout<<"\n Enter the Information:-"<<endl;
	Ostr1.getdata();
	Ostr2.getdata();

	cout<<"\n String Concatenation:-"<<endl;
	Ostr = Ostr1 + Ostr2;

	cout<<"\n String Comparison:-"<<endl;
	if(Ostr1 < Ostr2)
		cout<<endl<<"\t str1 is less than str2";
	else if(Ostr1 > Ostr2)
		cout<<endl<<"\t str1 is greater than str2";
	else if(Ostr1 == Ostr2)
		cout<<endl<<"\t str1 and str2 are equal.";

	getch();
	}

 

Output:

Enter the Information:-

Enter a String: manoj

Enter a String: pandey

String Concatenation:-

Concatenation of Str1 & str2: manojpandey

String Comparison:-

str1 is less than str2


Categories: Cpp Tags: ,

C++ Program to implement Operator Overloading with binary arithmetic op (*,+,-) – Q17

January 17, 2010 Leave a comment

Q17. Program to implement Operator Overloading -with binary arithmetic op (*,+,-):

Define a class complex with two data members real & imag.
Perform the following operations:
– Find the sum of two complex numbers
– Find the difference of two complex numbers
– Find the Product of two complex numbers
– Find the Product of a complex number with a constant

… from College notes (BCA/MCA assignments):

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

class Ccomplex{
	private:
		double real, img;
	public:
		Ccomplex(){}
		Ccomplex(double r, double i){
			real = r;
			img = i;
			}
		friend Ccomplex operator+(Ccomplex &, Ccomplex &);
		friend Ccomplex operator-(Ccomplex &, Ccomplex &);
		friend Ccomplex operator*(Ccomplex &, Ccomplex &);
		friend Ccomplex operator*(Ccomplex &, double);
		friend ostream& operator<<(ostream &, Ccomplex &);
		friend istream& operator>>(istream &, Ccomplex &);
	};

Ccomplex operator+(Ccomplex &c1, Ccomplex &c2){
	return Ccomplex(c1.real + c2.real, c1.img + c2.img);
	}

Ccomplex operator-(Ccomplex &c1, Ccomplex &c2){
	return Ccomplex(c1.real - c2.real, c1.img - c2.img);
	}

Ccomplex operator*(Ccomplex &c1, Ccomplex &c2){
	return Ccomplex(c1.real * c2.real, c1.img * c2.img);
	}

Ccomplex operator*(Ccomplex &c1, double num){
	return Ccomplex(c1.real * num, c1.img * num);
	}

ostream& operator<<(ostream &Mout, Ccomplex &Oc){
	return Mout<<"\n Real: "<<Oc.real<<"\n Img: "<<Oc.img;
	}
 
void main(){

	Ccomplex Oc;
	Ccomplex Oc1(3.1, 4.1);
	Ccomplex Oc2(4.2, 5.7);

	double num= 10;

	clrscr();

	cout<<"\n Sum of two Complex No's:- ";
	Oc = Oc1 + Oc2;
	cout<<Oc;

	cout<<"\n\n Difference of two Complex No's:- ";
	Oc = Oc1 - Oc2;
	cout<<Oc;

	cout<<"\n\n Product of two Complex No's:- ";
	Oc = Oc1 * Oc2;
	cout<<Oc;

	cout<<"\n\n Product of Complex No with Constant:-";
	Oc = Oc1 * num;
	cout<<Oc;

	getch();
	}

 

Output:

Sum of two Complex No’s:-
Real: 7.3
Img: 9.8

Difference of two Complex No’s:-
Real: -1.1
Img: -1.6

Product of two Complex No’s:-
Real: 13.02
Img: 23.37

Product of Complex No with Constant:-
Real: 31
Img: 41


Categories: Cpp Tags: ,