Sie sind auf Seite 1von 10

CHAPTER-4

Classes and Objects

LONG ANSWER QUESTIONS


SHORT QUESTION ANSWERS
Write a program to handle 10 account holders. The program should use the class as defined in question 9 of TYPE B.
1.
Make necessary changes in the class definition if required.
Ans. Same as Question no. 14 of Short Answer Question.
Define a class Applicant in C++ with the following description:
2.
Private Members:

A data member ANo (Admission Number) of type long

A data member Name of type string

A data member Agg (Aggregate Marks) of type float

A data member Grade of type char

A member function GradeMe() to find the Grade as per the Aggregate Marks obtained by a student.
Equivalent Aggregate Marks range and the respective Grades are shown as follows:
Aggregate Marks
Grade
>=80
A
less than 80 and >=65
B
less than 65 ad >=50
C
less than 50
D
Public Members:

A function ENTER() to allow user to enter values for Ao, Name, Agg and call function GradeMe() to find the
Grade.

A fuction RESULT() to allow user to view the content of all the data members.
Ans. #include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Applicant
{
long ANO;
char NAME[25];
float Agg;
char Grade;
void GradeMe()
{
if(Agg>=80)
Grade='A';
else if(Agg>=65)
Grade='B';
else if(Agg>=50)
Grade='C';
else if(Agg>=33)
Grade='D';
else
Grade='E';
}
public:
void ENTER()
{
cout<<"Enter Admission No:";
cin>>ANO;
cout<<"Enter Name of the Applicant :";
gets(NAME);
cout<<"Enter Aggregare Marks obtained by the Candidate :";

http://cbsecsnip.in

Page 1 of 10

cin>>Agg;
GradeMe();

3.

Ans.

}
void RESULT()
{
cout<<"*******Applicat Details***********"<<endl;
cout<<"Admission No: "<<ANO<<endl;
cout<<"Name of the Applicant: "<<NAME<<endl;
cout<<"Admission NoAggregare Marks obtained by the Candidate:
"<<Agg<<endl;
cout<<"Grade Obtained: "<<Grade<<endl;
}
};
void main(){
clrscr();
Applicant a1;
a1.ENTER();
a1.RESULT();
getch();
}
Write a class to represent a vector (1-D numeric array). Include member functions:

for vector creation

for modification of a given element

for displaying the largest value in the vector.

for displaying the entire vector

Write a program using this class.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int const size=50;
class vector
{
float d[size];
int s;
public:
void create(void);
void modify(void);
void largest(void);
void display(void);
};
void vector :: create(void)
{
cout<<"\n\nEnter of Array you want to create:-";
cin>>s;
cout<<"Enter "<<s<<" Real Numbers\n";
for(int i=0;i<s;i++)
cin>>d[i];
}
void vector :: modify(void)
{
int mfy_value;
float with;
cout<<"\nEnter Location of array at which value is to be modified:-";
cin>>mfy_value;

http://cbsecsnip.in

Page 2 of 10

cout<<"Enter Value with which you want to Replace:-";


cin>>with;
d[mfy_value]=with;
}
void vector :: largest(void)
{
int larg_no=d[0];
for(int i=0;i<s;i++)
{
if(d[i]>larg_no)
larg_no=d[i];
}
cout<<"The largest number of vector is: "<<larg_no<<endl;
}
void vector :: display(void)
{
cout<<"\n\nDisplay of Array\n";
cout<<"(";
for(int i=0;i<s;i++)
{
cout<<d[i];
if(i!=s-1)
cout<<",";
}
cout<<")";
}
void main()
{
clrscr();
vector o1;
int choice;
do
{
cout<<"\n\nChoice List\n";
cout<<"1)
To Create Vector Array\n";
cout<<"2)
To Modify Array\n";
cout<<"3)
To find largest value\n";
cout<<"4)
To Display\n";
cout<<"5)
EXIT\n";
cout<<"Enter your choice:-";
cin>>choice;
switch(choice)
{
case 1:
o1.create();
break;
case 2:
o1.modify();
break;
case 3:
o1.largest();
break;
case 4:
o1.display();
break;
case 5:
goto end;
}

http://cbsecsnip.in

Page 3 of 10

}while(1);
end:
getch();
}
Write a C++ program to implement a circle class. Each object of this class will represent a circle, storing its radius
and the x and y coordinate of its center as floats. Iclude two access functions for:
i.
calculating area of circle.
i.
calculating the circumference of circle.
Ans. #include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define pi=3.14;
class circle
{
public:
float x,y,area,cir;
4.

void calarea(float radius)


{
area=3.14*radius*radius;
cout<<endl<<"Area of circle = "<<area;
}
void calcir(float radius)
{
cir=2*3.14*radius;
cout<<endl<<"circumference of circle = "<<cir;
}

5.

};
void main()
{
clrscr();
circle a1;
float x,y,radius;
cout<<"Enter x-cordinate : ";
cin>>x;
cout<<"Enter y-cordinate : ";
cin>>y;
cout<<"Enter radius : ";
cin>>radius;
cout<<endl<<"****************************"<<endl;
cout<<endl<<"x-cordinate is: "<<x;
cout<<endl<<"y-cordinate is: "<<y;
cout<<endl<<"Radius is: "<<radius;
a1.calarea(radius);
a1.calcir(radius);
getch();
}
Imagine a ticket selling booth at a fair. People passing by are requested to purchase a ticket. A ticket is priced as Rs.
2.50/-. The booth keeps track of the number of people that have visited the booth, and of the total amount of
money collected.
Model this ticket selling booth with a class called ticbooth including following members:
Data Members:

number of people visited

total amount of money collected.

http://cbsecsnip.in

Page 4 of 10

Ans.

Member functions

To assign initial values (assign 0 to both data members)

To increment only people total in case ticket is not sold out

To increment people total as well as amount total if a ticket is sold out

To display the two totals

To display the number of tickets sold out (a ticky one)


Include a program to test this class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class ticbooth
{
int PeopleVisted;
float TotalAmount;
public:
ticbooth()
{
PeopleVisted = 0;
TotalAmount = 0.0;
}
void IncrementPeopleCounter()
{
PeopleVisted++;
cout<<endl<<"After increment total people: "<<PeopleVisted;
}
void Incre_people_amount()
{
TotalAmount=PeopleVisted*2.50;
cout<<endl<<"After increment total amount: "<<TotalAmount;
}
void ShowDetails()
{
cout << "People Visited : " << PeopleVisted;
cout<< "\nAmount Collected : " << TotalAmount ;
}
int TotalTicketsSold()
{
int TicketCount = (int)TotalAmount /2.5 ;
cout << "\n Total Tickets Sold : " << TicketCount ;
return TicketCount;
}
};
int main()
{
clrscr();
char ch;
int choice;
ticbooth t1;
l1:cout<<"\n\n\n\n1.Increment only person total or person and
amount both"<<endl;
cout<<"2.Display no. of people and the amount collected till
now"<<endl;
cout<<"3.Display no. of tickets sold"<<endl;
cout<<"4.Exit"<<endl;

http://cbsecsnip.in

Page 5 of 10

cout<<"Enter your choice:";


cin>>choice;
switch(choice)
{
case 1:
cout<<"is ticket sold?(Y/N):";
cin>>ch;
if(ch=='N')
{
t1.IncrementPeopleCounter();
}
else
{
t1.IncrementPeopleCounter();
t1.Incre_people_amount();
}
goto l1;
break;
case 2:
t1.ShowDetails();
goto l1;
break;
case 3:
t1.TotalTicketsSold();
goto l1;
break;
case 4:
exit(0);
break;
}
return 0;
getch();
6.

Ans.

}
Write a C++ program to perform various operations on a string class without using language supported built-in
string functions. The operations on a class are:
a)
Read a string
b)
Display the string
c)
Reverse the string
d)
Copy the string into an empty string
e)
Concatenate two strings
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
class string
{
char str1[20];
char str2[20];
public:
void read()
{
cout<<"\nEnter string :=> ";
cin>>str1;
}
void Display()
{
cout<<endl<<"The entered string is: "<<str1;
}

http://cbsecsnip.in

Page 6 of 10

void reverse();
void copy();
void concatenate();
};
void string::reverse()
{
int i,l,j=0;
cout<<"\nEnter string :=> ";
cin>>str1;
l=strlen(str1);
for(i=l-1;i>=0;i--)
{
str2[j]=str1[i];
j++;
}
str2[j]='\0';
cout<<"Reverse is: "<<str2;
}
void string::copy()
{
int i;
cout<<"\nEnter string :=> ";
cin>>str1;
for(i=0;i<=strlen(str1);i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
cout<<"Copy is: "<<str2;
}
void string::concatenate()
{
int i,l;
cout<<"\nEnter string1 :=> ";
cin>>str1;
cout<<"\nEnter string2 :=> ";
cin>>str2;
l=strlen(str1);
for(i=0;i<=l;i++)
{
str1[l+i]=str2[i];
}
cout<<"Copy is: "<<str1;
}
int main()
{
clrscr();
int choice;
string s1;
l1:cout<<"\n\n\n\n1.Read the string"<<endl;
cout<<"2.Display the string "<<endl;
cout<<"3.Reverse the string"<<endl;
cout<<"4.Copy the string"<<endl;
cout<<"5.Concatenate two strings"<<endl;

http://cbsecsnip.in

Page 7 of 10

cout<<"6.Exit"<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
s1.read();
goto l1;
break;
case 2:
s1.Display();
goto l1;
break;
case 3:
s1.reverse();
goto l1;
break;
case 4:
s1.copy();
goto l1;
break;
case 5:
s1.concatenate();
goto l1;
break;
case 6:
exit(0);
break;
}
return 0;
getch();
7.

Ans.

}
Define a class named ADMISSION in C++ with the following description:
Private Members:
AD_NO
integer (Ranges 10 - 2000)
NAME
Array of characters (String)
CLASS
Character
FEES
Float
Public Members:

Function Read_Data() to read an object of ADMISSION type.

Function Display() to display the details of an object.

Function Draw-Nos() to choose 2 students randomly.

And display the details. Use random function to generate admission nos. to match with AD_NO.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
class ADMISSION
{
private:
int AD_NO;
char NAME[50];
char CLASS[5];
float FEES;
public:
void Read_Data();
void Display();
void Draw_Nos();
};
void ADMISSION::Read_Data()

http://cbsecsnip.in

Page 8 of 10

{
cout<<"Enter Admission No (10 - 2000): ";
cin>>AD_NO;
cout<<"Name: ";
gets(NAME);
cout<<"Class: ";
gets(CLASS);
cout<<"Fees: ";
cin>>FEES;

8.

Ans.

}
void ADMISSION::Display()
{
cout<<"Admission No:"<<AD_NO<<endl;
cout<<"Name: "<<NAME<<endl;
cout<<"Class: "<<CLASS<<endl;
cout<<"Fees: "<<FEES<<endl;
}
void ADMISSION::Draw_Nos()
{
int Arr[5];
int no1,no2,i;
randomize();
no1=random(1991)+10;
//Generate a radom no between 10-2000
no2=random(1991)+10;
//Generate a radom no between 10-2000
cout<<"Random no-1: "<<no1<<" Random no-2: "<<no2;
for(i=0;i<100;i++)
{
if((Arr[i]==no1)||(Arr[i]==no2))
Display();
}
}
void main()
{
clrscr();
ADMISSION a1;
a1.Read_Data();
a1.Display();
a1.Draw_Nos();
getch();
}
Define a class named HOUSING in C++ with the following description:
Private Members:
REG_NO
integer (Ranges 10 - 2000)
NAME
Array of characters (String)
TYPE
Character
COST
Float
Public Members:

Function Read_Data() to read an object of HOUSING type.

Function Display() to display the details of an object.

Function Draw-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 nos. to match with REG_NO from the
array.
#include<iostream.h>
#include<conio.h>

http://cbsecsnip.in

Page 9 of 10

#include<stdio.h>
#include<stdlib.h>
class HOUSING{
private:
int REG_NO;
char NAME[50];
char TYPE[20];
float COST;
public:
void Read_Data();
void Display();
void Draw_Nos();
};
void HOUSING::Read_Data()
{
cout<<"Enter Reg No (10 - 1000): ";
cin>>REG_NO;
cout<<"Name: ";
gets(NAME);
cout<<"Type: ";
gets(TYPE);
cout<<"Cost: ";
cin>>COST;
}
void HOUSING::Display()
{
cout<<"Reg No:"<<REG_NO<<endl;
cout<<"Name: "<<NAME<<endl;
cout<<"Type: "<<TYPE<<endl;
cout<<"Cost: "<<COST<<endl;
}
void HOUSING::Draw_Nos()
{
int Arr[5];
int no1,no2,i;
randomize();
no1=random(991)+10;
//Generate a radom no between 10-1000
no2=random(991)+10;
//Generate a radom no between 10-1000
cout<<"Random no-1: "<<no1<<" Random no-2: "<<no2;
for(i=0;i<100;i++)
{
if((Arr[i]==no1)||(Arr[i]==no2))
Display();
}
}
void main(){
clrscr();
HOUSING a1;
a1.Read_Data();
a1.Display();
a1.Draw_Nos();
getch();
}
SHORT QUESTION ANSWERS

http://cbsecsnip.in

Page 10 of 10

Das könnte Ihnen auch gefallen