You are on page 1of 10

2.

Class and Object


QUES2.1) write a program to define a class candidate with the following
specification

DATA MEMBERS:

 Admission no.of type long int


 Name of type character aaray
 Category of type integer
 Fees of type float

MEMBER FUNCTION:

 A private member function AFEES() to assign fees depending on


category that is

If category is G then fees is rs 5000

If category is F then fees is 7500

If category is S or T then fees is rs 2500

 A public member function INPUT() to accept admission no. , name and


category and to call the function AFEES() to assign fees
 A public member function OUTPUT() to display all the contents .

Ans) #include<iostream.h>

#include<conio.h>

class candidate

longintano;

char name[20];

char category;
float fees;

void AFEES()

if(category==’G’)

fees=5000;

else if(category==’f’)

fees=7500;

else if(category==’S’||category==’T’)

fees=2500;

public:

void INPUT()

{
cout<<”enter admission no. and category”;

cin>>ano>>category;

cout<<”enter name”;

cin>>name;

AFEES();

void OUTPUT()

cout<<name<<endl<<ano<<endl;
cout<<category<<endl<<fees;

};

void main()

candidate c;

c.INPUT();

c.OUTPUT();

getch();

QUES2.2) write a program to define a class RING in C++ with following

PRIVATE MEMBERS:

 Ring number of type int


 Radius of type float
 Area of type float
 calcarea()

PUBLIC MEMBERS:

 getarea() to input all the values and to call calcarea() to calculate


 showarea() to display all the values
Ans) #include<iostream.h>

#include<conio.h>

class RING

intrno;

floatr,a;

voidcalcarea()

a=3.14*r*r;

public:

voidgetarea()

cout<<”enter ring number and radius”;

cin>>rno;

cin>>r;

calcarea();

voidshowarea()

cout<<rno<<endl<<r<<endl<<a<<endl;

}
};

void main()

RING ri;

ri.getarea();

ri.showarea();l

getch();

QUES2.3) write a program to define a class worker with following specifications

PRIVATE MEMBERS:

 wno (int)
 wname (20 character array)
 hrwrk,wgrate (float) ( hour work and wage rate)
 totwage (float; hrwrk*wgrate)
 calwg() ( a function to calculate the totwage with float return type)
PUBLIC MEMBERS:

 takedata() [function to read wno, wname,hrwrk and call calwg() to


calculate total wage]
 showdata() [to display all data members on screen]

Ans)#include<iostream.h>

#include<conio.h>

class worker

int wno;

char wname[20];

float hrwrk,wgrate,totwage,ans;

float calwg();

public:

void takedata();

void showdata();

};

float worker::calwg()

totwage=hrwrk*wgrate;

return(totwage);

void worker::takedata()

{
cout<<”enter name”;

cin>>wname;

cout<<”worker no., hour worked and wage rate”;

cin>>wno>>hrwrk>>wgrate;

ans=calwg();

void worker::showdata()

cout<<wname<<endl<<wno<<endl<<hrwrk<<endl<<wgrate<<endl<<ans<<endl;

void main()

worker w;

w.takedata();

w.showdata();

getch();

}
QUES2.4)write a program to define a class TEST IN C++ with the following
specification

PRIVATE MEMBERS:

 test code (int)


 description (20 character)
 no. of candidates (int)
 center required (int)
 calcntr() [to calculate no. of center required as creq=no/100 +1]

PUBLIC MEMBERS:

 a function schedule() to take input of all values


 a function dispdata() to display all the values

ans) #include<iostream.h>

#include<conio.h>

class TEST

inttcode;

chardesc[20];

intno,creq;

voidcalcntr();

public:

void schedule();

voiddispdata();

};

void TEST::calcntr()
{

creq=no/100 +1;

void TEST::schedule()

cout<<” enter test code &no. of candidates”;

cin>>tcode;

cin>>no;

cout<<” enter description”;

gets(desc);

calcntr();

void TEST::dispdata()

cout<<tcode<<endl<<no<<endl<<desc<<endl<<” center required:-“<<creq;

void main()

TEST t;

t.schedule();

t.dispdata();

getch();
}

You might also like