COMPUTER SCIENCE CLASS AND OBJECTS

2.b) Define a class Student for the following specifications.

Private members of the Student are:
roll_no          integer
name            array of characters of size 20
class_st       array of characters of size 8
marks           array of integers of size 5,
Percentage  float
Calculate( ) that calculates overall percentage marks and returns the percentage

Public Members of the Student are:

Readmarks reads mark and invoke thecalculate function
Displaymarks prints the data.

Ans:

class Student
{ int roll_no;
char name[20];
char class_st[8];
int marks[5];
float percentage;
float calculate( )
{ percentage=(marks[0]+marks[1]+marks[2]+ marks[3]+marks[4])/5;
return percentage;
}
public:
void Readmarks( )
{ cout<<”\nEnter any 5 subject marks;
cin>>marks[0]>>marks[1]>>marks[2]>> marks[3]>>marks[4];
calculate( );
}
void Displaymarks( )
{ cout<<”\nThe Roll Number of the Student: “<<roll_no;
cout<<”\nThe Name of the Student:” <<name;
cout<<”\nThe class of the Student: “ <<class_st;
cout<<”\n5 subject marks of the student…\n”;
cout<<marks[0]<<”\t”<<marks[1]<<”\t”<<marks[2]<<”\t”;cout<<marks[3]<<”\t”<<marks[4]<<”\n”;
cout<<”Percentage =”<<percentage;
}
};

2001:

2.b) Declare a class to represent bank account of 10 customers with the following data members. Name of the depositor, account number, type of account (S for Savings and C for Current), Balance amount. The class also contains member functions to do the following:

(i)To initialize data members.
(ii) To deposit money
(iii)To withdraw money after checking the balance (minimum balance is Rs.1000)
(iv) To display the data members.

[Note:You are also required to give detailed function definitions.]

class Bank
{ char name[15];
int acc_no;
char acc_type;
float bal_amount;
public:
void readData( )
{ cout<<”\nEnter the name: “;
gets(name);
cout<<”\nEnter the account number: “;
cin>>acc_no;
cout<<”\nEnter the account type: “;
cin>>acc_type;
cout<<”\nEnter the amount to deposit: “;
cin>>bal_amount;
}
void deposit( )
{ float deposit;
cout<<”\nEnter your account number: “;
cin>>acc_no;
cout<<”\nEnter the amount to deposit: “;
cin>>deposit;
bal_amount=bal_amount + deposit;
}
void withdraw( )
{ float w_amount;
cout<<”\nEnter your account number:“;
cin>>acc_no;
cout<<”\nEnter amount to withdraw”;
cin>>w_amount;
if((bal_amount-w_amount)<1000)
cout<<”\nWithdraw is not possible”;
else
{ bal_amount=bal_amount-w_amount;
cout<<”\nThe balance is“<<bal_amount-w_amount;
}
}
void display( )
{ cout<<”\nName of the depositor: “<<name;
cout<<”\nAccount Number: “<<acc_no;
cout<<”\nAccount Type: “<<acc_type;cout<<”\nThe balance amount is“<<bal_amount;
}
};

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )