Вы находитесь на странице: 1из 3

CHENNAI PUBLIC

SCHOOL
Anna Nagar  Chennai -600 101
Worksheet – 3
Classes and Objects
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);
}

b) class Eval
{
char Level;
int Point;
public:
void Init(){Level='E';Point=0;}
void Sink(int L)
{
Level-=L;
}
void Float(int L)
{
Level+=L;
Class XII Computer Science Worksheet – 3 Page 1 of 3
Point++;
}
void Show()
{
cout<<Level<<"#"<<Point<<endl;
}
};
void main()
{
Eval E;
E.Init();
E.Sink(3);
E.Show();
E.Float(7);
E.Show();
E.Sink(2);
E.Show();
}

c) class Stock
{
long int ID;
float Rate;
int Date;
public:
void Init_Stock(){ID=1001;Rate=200;Date=1;}
void RegCode(long int I,float R)
{
ID=I;
Rate=R;
}
void Change(int New,int DT)
{
Rate+=New;
Date=DT;
}
void Show()
{
cout<<"Date :"<<Date<<endl;
cout<<ID<<"#"<<Rate<<endl;
}
};
void main()
{
Stock A,B,C;
A.Init_Stock();
B.Init_Stock();
C.Init_Stock();
A.RegCode(1024,150);
B.RegCode(2015,300);
B.Change(100,29);
C.Change(-20,20);
A.Show();
B.Show();
C.Show();
Class XII Computer Science Worksheet – 3 Page 2 of 3
}
4 a) Define a class STORE with the following members in it:
Product_No – an integer
Product_Name – a string of 30 characters
Discount_type – single character – ‘F’ – Festival/’D’ – Non-Festival
Discount_price – float
List_price – long
A function ASSIGN() that assigns List_price based on the product name and also
calculate discount.
Product_Name List_price
Ipad Air 28000
Mac Mini 34770
Watch Sport 32800
IPod 15390
The discount is 10% on the list price which is given during non-festival season.
During the festival season, additional 5% discount is given.
Public members
A member function ASSIGN2() that receives PNo, PName,DType that are input by
the user and passed as parameters. The values in parameters are assigned to the
data members ProductNo, ProductName and DiscountType in the function.
A function Print() to print only the Product No, Name and the net price(List Price –
Discount Price).

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
void input();// to input hrs, min ,sec
void print();// to print hrs, min,sec
void diff(TIME,TIME); // finds difference between two times

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)

Class XII Computer Science Worksheet – 3 Page 3 of 3