Академический Документы
Профессиональный Документы
Культура Документы
SCHOOL
Anna Nagar Chennai -600 101
Worksheet – 3
Classes and Objects - Key
COMPUTER SCIENCE – CLASS XII
1) Predict the output for the following code fragments:( Assume header files
already included)
a) class X
{
int a,b,c;
void count()
{a++;}
public:
int x;
void init(int,int,int);
void print(int);
};
void X::init(int i, int j, int k)
{
a=i;
b=j;
c=k;
}
void X::print(int c)
{
count();
cout<<"a="<<a<<"b="<<b<<"c="<<c<<"\n";
}
void main()
{
X Ob1,Ob2;
Ob1.init(0,1,2);
Ob2.init(2,3,4);
Ob1.print(10);
Ob2.print(20);
}
Ans:
a=1b=1c=10
a=3b=3c=20
b) class Eval
{
char Level;
int Point;
public:
void Init(){Level='E';Point=0;}
void Sink(int L)
{
b)
Define a class STUDENT with the following specifications:
Private members
Rollno of type int
Name of type string
Marks of type float
Public members
void Input() ;// that inputs details
void Print();// that prints topper’s details
Write a program that defines the above class and determine the topper of the
class by calling the required functions.
c) Write a program to find the difference between two times implemented as classes
and objects. The class TIME contains the following members.
hrs,min,sec – of int type
public members
d)
Write a program to find the sum of weights of two products entered as objects of a
class ITEM having following specifications:
Kgs as float, gms as int
Public members
void Input();// Input kgs and gms
void Print();// Print kgs and gms
void sum(ITEM, ITEM);// find sum of 2 ITEMS in kgs and gms
Note: 1000 gms = 1kg(to be added to existing kgs)