केंद्रीय विद्यालय जोशीमठ

ग्रीष्मकालीन अवकाश गृहकार्य कक्षा- 12

कंप्यूटर सांइस

HOLIDAY HOMEWORK FOR CLASS XII

COMPUTER SCIENCE

Find the errors in the following programs and rewrite them

1) #include<iostream.h

void main()

{

const MAX=0;

inta,b;

cin<a>b;

if(a>b) MAX=a;

for(x=0,x<MAX;x++) cout<x;

}

2) #include<iostream.h

Main()

{

intch=9,sch=90;

char S[2,2];

ifch<=9

coutch;

for(int x=0;x<2;x++)

for(int y=0;y<2;y++)

{

if(y==0) S[x][y]=’A’;

else

cout>S[x][y];

}

getch(); }

class X

{

public:

inta,b;

voidint(void)

{

a=b=0;

}

int sum(void);

int square(void);

};

int sum(void)

{

returna+b;

}

int square(void)

{

return sum() *sum();

}

3) include<iostream.h

void main()

{

int R; W=90;

while W>60

{

R=W-50;

switch(W)

{20:cout<”Lower range”<endl;

30: cout<”Middle Range”<endl;

20: cout<”Higher Range”<endl;

}

}

}

4) #include<iostream.h

main()

{

int x[5],*y,z[5];

for(i=0;i<5;i++)

{

x[i]=I;

z[i]=i+3;

y=z; x=y;

}}

class ABC

{

int x=10;

float y;

ABC(){ y=5; }

~AB?C() {}

};

void main()

{

ABC a1,a2;

}

void main()

{

constinti=20;

constint *const ptr=&i;

(*ptr)++;

int j=15;

ptr =&j;

}

5) #include(iostream.h)

void main()

{

int X[]={60,50,30,40},Y; count=4;

cin>Y;

for(i=count-1;i>=0;i--)

switch(i)

{

case 0;

case 2: cout<Y*Y[i]<endl; break;

case 1:;

cae 3: cout>Y+X[i];

}

}

struct group

{

int x1,x2;

}

void main()

{

g1,g2 group;

cin>g1.x1<g2.x2;

g2=g1;

cout<g2.x2<g2.x1<endl;

2+=g1.x1;

cout<g1.x1<g1.x2;

}

structureswimmingclub

{

intmem number;

charmamname[20]

char memtype[]=”LIG”;

};

void main()

{

swimmingclub per1,per2;

cin<”Member Number”;

cin>memnumber.per1;

cout<”\n Member name”;

cin>per1.membername;

per1.memtype=”HIG”;

per2=per1;

cin<”\n Member number ”<per2.memnumber;

cin<”\n Member name “<per2.memname;

cin<”\n Member number “<per2.memtype;

}

6) #include<iostream.h

CLASS STUDENT

{

intadmno;

float marks;

public :

STUDENT()

{

admno=0;

marks=0.0;

}

void input()

{

cinadmno;

cin>marks;

}

void output()

{

coutadmno;

cout<marks;

}

}

void main()

{

STUDENT s;

input(s);

}

7) #include<iostream.h

void main()

{

struct STUDENT

{

charstu_name[20];

charstu_sex;

intstu_age=17;

}student;

gets(stu_name);

gets(stu_sex);

}

Classes & constructor

  1. Answer the questions(i) and (ii) after going through the following class :

class Exam

{

int year;

public :

Exam(int y) { year=y; }

Exam(Exam &t);

}

(i)Create an object, such that it invokes constructor 1.

(ii)Write complete definition for constructor 2.

  1. Define a class named Housing in C++ with the following descriptions :

private members

reg_nointegers (Ranges 10-1000)

namechar array

typecharacter

costfloat

public members

  • functionRead_data() to read an object of housing type.
  • function display() to display the details of an object.
  • functiondraw_nos() to choose and display the details of 2 houses selected randomly from an array of 10 objects of type Housing. Use random function to generate the registration no with reg_no from the array.
  1. Define a class named Cricket in C++ with the following descriptions :

private members

Target_scopeint

Overs_bowledint

Extra_timeint

Penaltyint

cal_panalty() a member function to calculate penalty as follows :

ifExtra_time <=10 , penalty =1

ifExtra_time >10 but <=20, penalty =2

otherwise, penalty =5

public members

a function extradata() to allow user to enter values for target_score,overs_bowled,extra_time.

a function dispdata() to follow user to view the contents of all data members.

  1. Define a class named Directory in C++ with the following descriptions :

private members

docunamesstring (documents name in directory)

freespacelong (total number of bytes available in directory )

occupiedlong (total number of bytes available in directory)

public members

newdocuentry()a function to accept values of docunames,freespace & occupied from user

retfreespace()a function that return the value of total kilobytes available. (1 KB=1024 b)

showfiles()a function that displays the names of all the documents in directory.

  1. Define a class named Publisher in C++ with the following descriptions :

private members

Id long

title40 char

author40 char

price ,stockqtydouble

stockvaluedouble

valcal()A function to find price*stockqty with double as return type

Public members

  • a constructor function to initialize price , stockqty and stockvalue as 0
  • Enter() function to input the idnumber , title and author
  • Takestock() function to increment stockqty by N(where N is passed as argument to this function) and call the function valcal() to update the stockvalue().
  • sale() function to decrease the stockqty by N (where N is sale quantity passed to this function as argument) and also call the function valcal() to update the stockvalue
  • outdata() function to display all the data members on the screen.
  1. Define a class named Serial in C++ with the following descriptions :

private members

serialcodeint

title20 char

durationfloat

noofepisodesinteger

Public members

  • a constructor function to initialize duration as 30 and noofepisodes as 10.
  • Newserial() function to accept values for serialcode and title.
  • otherentries() function to assign the values of duration and noofepisodes with the help of corresponding values passed as parameters to this function.
  • dispdata() function to display all the data members on the screen.
  1. Considering the following specifications :

Structure namedatatypesize

Namefirstchar array40

midchar array40

lastchar array60

Phoneareachar array4

exchchar array4

numbchar array6

Class nameDataType

P_recnameName

phonePhone

Member functions:

  • Define constructor (outside the class P_rec) that accepts the values of data members from the user.
  • Define the display_rec (outside the class P_rec) that shows the current values .

Declare structures in C++ for Name and Phone . Declare the class P_rec.

  1. Define a class Competition in C++ with the following descriptions:

Data Members

Event_no integer

Description char(30)

Score integer

qualified char

Member functions

A constructor to assign initial values Event_Nonumber as 101,Description as “State level” Score is

50 , qualified ‘N’.

Input() To take the input for event_no,description and score.

Award(int) To award qualified as ‘Y’, if score is more than the cutoffscore passed as

argument to the function else ‘N’.

Show()To display all the details.

9. Declare a class bank to represent bank account of 10 customers with thefollowing data members: name of depositor, account number, type of account(s for savings and c for current account), balance amount. The class also contains thefollowing member functions:

(a)To initialize data members.

(b)To deposit money

(c)To withdraw money after checking minimum balance (say 1000)

(d)To display the data members on screen.

Inheritance

10.

(a) Answer the questions (i) to (iv) based on the following code:

class Medicines

{

char Category[lO];

charDate_of_manufacture[lO];

char Company[20];

public:

Medicines();

voidentermedicinedetails();

voidshowmedicinedetails();

} ;

class Capsules: public Medicines

{

protected:

charcapsule_name[30];

charVolume_label[20];

public:

float Price;

Capsules();

voidentercapsuledetails();

voidshowcapsuledetails();

};

class Antibiotics: public Capsule

{

intDosage_units;

charSide_effects[20];

intUse_within_days;

public:

Antibiotics() ;

voidenterdetails();

voidshowdetails();

};

(i) How many bytes will be required by an object of class Medicines and an objectof class Antibiotics respectively?

(ii) Write names of all the member functions accessible from the object of classAntibiotics.

(iii) Write names of all the members accessible from member functions of classCapsules.

(iv) Write names of all the data members, which are accessible from objects of classAntibiotics.

11.

(a) Answer the questions (i) to (iv) based on the following code :

class Trainer

{

charTNo [5], TName [20], Specialisation [10];

int Days;

protected :

float Remuneration;

voidAssignRem (float);

public :

Trainer ( ) ;

voidTEntry ( );

voidTDisplay ( );

};

class Learner

{

charRegno [10], LName [20], Program [10];

Protected :

int Attendance, Grade;

public:

Learner ( );

voidLEntry ( );

voidLDisplay ( );

};

class Institute : public Learner, public Trainer

{

charICode[10], IName [20];

public:

Institute ( );

voidIEntry ( );

voidIDisplay ( );

};

(i) Which type of Inheritance is depicted by the above example?

(ii) Identify the member function(s) that cannot be called directly from the

objects of class Institute from the following

TEntry( )

LDisplay()

IEntry()

(iii) Write name of all the member(s) accessible from member functions of

class Institute.

(iv) If class Institute was derived privately from class Learner and privately

from class Trainer, then, name the member function(s) that could be

accessed through Objects of class Institute.

12.

(a) Answer the questions (i) to (iv) based on the following:

classFaceToFace

{

charCenterCode[10];

public:

void Input( );

void Output( );

};

class Online

{

char website[50];

public:

voidSiteIn( );

voidSiteOut( );

};

class Training: public FaceToFace, private online

{

longTcode;

float charge;

int period;

public:

void Register( );

void show( );

};

(i) Which type of inheritance is shown in the above example?

(ii) Write names of all the member functions accessible from Show( ) function ofclass Training.

(iii) Write name of all the member accessible through an object of class Training.

(iv) Is the function Output( ) accessible inside the function SiteOut( )? Justifyyour answer?

13.

(a) Answer the questions (i) to (iv) based on the following :

class Chairperson

{

long CID ; //Chairperson Identification Number

charCName[20] ;

protected :

char Description[40] ;

void Allocate( ) ;

public :

Chairperson( ) ;

void Assign( ) ;

void Show( ) ;

} ;

class Director

{

int DID ; //Director ID

charDname[20] ;

protected :

char Profile[30] ;

public :

Director( ) ;

void Input( ) ;

void output( ) ;

} ;

class Company : private Chairperson, public Director

{

int CID ; //Company ID

char City[20] , Country[20] ;

public :

Company( ) ;

void Enter( ) ;

void Display( ) ;

} ;

(i) Which type of Inheritance out of the following is specifically is illustrated in theabove C++ code ?

(a) Single Level Inheritance

(b) Multi Level Inheritance

(c) Multiple Inheritance

(ii) Write the names of data members, which are accessible by objects of class typeCompany.

(iii) Write the name of all the member functions, which are accessible by objects ofclass type Company.

(iv) Write the names of all members, which are accessible from member functions ofclass Director.

14.

(a) Answer the question (i) to (iv) based on the following:

class Student

{

intRno;

char Name[20];

float Marks;

protected:

void result( );

public:

Student ( );

void Register ( ); void Display( );

};

class Faculty

{

longFCode;

charFName [20];

protected:

float Pay;

public:

Faculty ( );

void Enter( );

void Show( );

};

class Course: public Student, private Faculty

{

longCCode [10]; char CourseName [50];

charStartDate [8], EndDate [8];

public:

Course( );

void Commence ( );

voidCDetail ( );

};

(i) Which type of inheritance is illustrated in the above C++ code?

(ii) Write the names of all the data members, which is /are accessible from

member function Commence of class Course.

(iii) Write the names of member functions, which are accessible from objects ofclass Course.

(iv) Write the names of all the members, which are accessible from objects of

class faculty.

Data File Handling

15. (a) Observe the program segment given below carefully and fill the blanks marked as

Line 1 and Line 2 using fstream functions for performing the required task.

#include<fstream.h

class Stock

{

longIno; // Item Number

char Item[20]; // Item Name

int Qty; // Quantity

public:

void Get(int);

Get(int);// Function to enter the content

void Show( ); // Function to display the content

void Purchase(intTqty)

{

Qty+ = Tqty; // Function to increment in Qty

}

longKnowIno( )

{

returnIno;

}

};

voidPurchaseitem(long PINo, intPQty)

// PINo -> Info of the item purchased

// PQty -> Number of items purchased

{

fstream file;

File.open(“ITEMS.DAT”,ios::binary|ios::in|ios::cut);

int Pos=-1;

Stock S;

while (Pos== -1 & File.read((char*)&S, sizeof(S)))

if (S.KnowInc( ) == PINo)

{

S.Purchase(PQty); // To update the number of items

Pos = File.tellg()- sizeof(S);

//Line 1 : To place the file pointer to the required position

______;

//Line 2 : To write the objects on the binary file

______;

}

if (Pos == -1)

cout<“No updation done as required Ino not found...”;

File.close( );

} 2009

16.(a) Observe the program segment given below carefully and fill the blanks

marked as Statement 1 and Statement 2 using seekg( ), seekp( ), tellp( ) and tellg()

functions for performing the required task.

# include <fstream.h

class PRODUCT

{

intPno;

charpname [20];

int qty;

public :

voidModifyQty();// The function is to modify quantity of a PRODUCT

};

void PRODUCT :: ModifyQty ( )

{

fstreamFil;

Fil.open(“PRODUCT.DAT”,ios::binary | ios::in | ios::out);

intMPno;

cout<“Product No to modify quantity :”;

cinMPno;

while(Fil.read((char*) this, sizeof (PRODUCT)))

{

if (MPno ==Pno)

{

cout<“present quantity:” <Qty<endl;

cout<“changed quantity:”;

cin>Qty;

int position = ______; //Statement 1

______; //Statement 2

Fil.write ((char*) this, sizeof (PRODUCT));

// Re-writing the Record

}

}

Fil.close( );

} 2011

17 (a) Assuming that a text file named FIRST.TXT contains some text written into it,

write a function named vowelwords( ), that reads the file FIRST.TXT and creates

a new file named SECOND.TXT, to contain only those words from the file

FIRST.TXT which start with a lowercase vowel (i.e. with ‘a’, ‘e’, ‘i’, ‘o’, ‘u’). For

example if the file FIRST.TXT contains

Carry umbrella and overcoat when it rains

Then the file SECOND.TXT shall containumbrella and overcoat it

2004

(b) Write a function COUNT_DO( ) in C++ to count the presence of a word „do‟ in a

text file “MEMO.TXT”.

Example :

If the content of the file “MEMO.TXT” is as follows:

I will do it, if yourequest me to do it.

It would have been done muchearlier.

The function COUNT_DO( ) will display the following message:

Count of -do- in flie: 2009

18.

(a) Write a function in C++ to count the no. of “Me” or “My” words present in a

text file “DIARY.TXT”.

If the file “DIARY.TXT” content is as follows :

My first book was Me and Myfamily. It gave me chance to beknown to the world.

The output of the function should be Count of Me/ My in file :

2011

19.

(a) Assuming the class EMPLOYEE given below, write functions in C++ to perform following:

(i) Write the objects of EMPLOYEE to a binary file.

(ii) Read the objects of EMPLOYEE from binary file and display them on screen.

class EMPLOYEE

{

int ENO;

char ENAME[10];

public :

void GETIT()

{

cin > ENO;

gets (ENAME);

}

void SHOWIT()

{

cout <ENO < ENAME ;

coutendl;

}

};

(b) Write a function in C++ to search for a laptop from a binary file

“LAPTOP.DAT” containing the objects of class LAPTOP (as defined below).

The user should enter the Model No and the function should search and

display the details of the laptop.

class LAPTOP

{

longModelNo;

float RAM, HDD;

char Details[120];

public:

voidStockEnter ( )

{

cin>Model>No>RAM>HDD;

gets(Details);

}

voidStockDisplay( )

{coutModelNo<RAM<HDD<Details<endl;

}

longReturnModelNo ( )

{

returnModelNo ;

}

}; 2011

(c) Write a function in C++ to read and display the detail of all the users whose status

is „A‟ (i.e. Active) from a binary file “USER.DAT”. Assuming the binary file

“USER.DAT” is containing objects of class USER, which is defined as follows:

class USER

{

intUid; // User Id

charUname[20]; // User Name

char Status; // User Type: A Active I Inactive

public:

void Register( ); // Function to enter the content

void show( ); // Function to display all data members

charGetstatus( )

{

return Status;

}

}; 2009

(d) Given a binary file STUDENT.DAT, containing records of the following class

Student type.

class Student

{

charS_Admno[lO]; //Admission number of student

charS_Name[30]; //Name of student

int Percentage; //Marks Percentage of student

public:

voidEnterData()

{

gets(S_Admno);

gets(S_Name);

cin>Percentage;

}

voidDisplayData()

{

coutsetw(12)<S_Admno;

coutsetw(32)<S_Name;

coutsetw(3)<Percentage<endl;

}

intReturnPercentage()

{

return Percentage;

}

};

Write a function in C++, that would read contents of file STUDENT.DAT and display

the details of those Students whose Percentage is above 75. 2005