Class Assignment5 Xi
Class Assignment5 Xi
Methods (Chap 8)
Question 1
A class Cabfare has been defined to calculate the fares for cabs as declared by a travel agency.
Class name Cabfare
Data members/instance variables:
dist to store the distance
fare to store the calculated fare
Member functions/methods:
Cabfare(int d) parameterized constructor
void calcfare( ) to compute and display the amount to be paid by the
customer on the following basis:
Base fare: Rs 30
Rs 6 per km upto a distance of 10 km
Rs 12 per km for the distance above 10 km and upto
20 km
Rs 9.50 per km for the distance above 20 km
Specify the class Cabfare giving details of the constructor and member methods.
Question 2
A class Wages has been defined to compute the monthly wages for a worker. Its details are:
Some of the other members are:
Class name Wages
Data members/instance variables:
name to store name of the worker
basic to store basic pay in decimals
hrs to store hours worked during overtime
rate to store rate per hour
wage to store overall wages of the worker
Member functions/methods:
Wages(....) parameterized constructor
double overtime( ) computes and returns overtime amount as: (hours x
rate)
void display( ) calculates the wages using the formula:
Wage = overtime amount + basic pay and displays it
with other details
Specify the class Wages giving details of the constructor and member methods.
Question 3
A class Composite has been defined to find the sum of composite numbers. A number is
composite if it has more than two factors. For e.g. 6, 15, 21, and so on.
Some of the other members are:
Class name Composite
Data members/instance variables:
lim to store limit as integer
Member functions/methods:
CPP XI – SR Page 1
Composite (int m) constructor to initialize lim by m
boolean iscomp(int x) returns true if x is a composite number else
returns false
void sumcomposite( ) prints the sum of all composite numbers from 1 to
lim
Specify the class Composite giving details of the constructor and member methods.
Question 4
A class Primorial has been defined to find the primorial of a number. The primorial of a number
p is defined as the product of all prime numbers less than or equal to p. For example,
3# = 2 x 3 = 6
5# = 2 x 3 x 5 = 30
Some of the other members are:
Class name Primorial
Data members/instance variables:
num to store an integer
Member functions/methods:
Primorial(int nn) parameterized constructor
int primorial( ) finds and returns num#
void display( ) to display the number and its primorial
Specify the class Primorial giving details of the constructor and member methods.
Question 5
A class MyNum has been defined as:
Class name MyNum
Data members/instance variables:
num to store a number
sum stores the sum of the first and last digit of a number
Member functions/methods:
MyNum(int n) constructor to assign n to num and 0 to sum
void digitFrequency( ) displays the frequency of each digit of num
void firstlast( ) prints the sum of the first and last digit of num
Specify the class MyNum giving details of the constructor and methods.
Question 6
A library issues books on rental basis at a 2% charge on the cost price of the book per day.
As per the rules of the library, a book can be retained for 7 days without any fine. If the
book is returned after 7 days, a fine will also be charged for the excess days as per the chart
given below:
Number of excess days Fine per day (in Rs. )
Up to 5 2.00
Above 5 up to 10 3.00
Above 10 days 5.00
Design a class Library to perform the task. Details of the class are given below:
Class name Library
Data members / instance variables:
name name of the book
author author of the book
CPP XI – SR Page 2
p price of the book in decimals
d number of days taken in returning the book
f stores the fine.
Member functions / methods:
Library (…) parameterized constructor to assign values to data
members
void fine( ) calculates the fine for the excess days
void display( ) displays the book details along with the number of
days, fine and the total amount to be paid
Total amount is calculated as:
(2% of price of book total no. of days) + fine.
Specify the class Library giving details of the constructor, void fine ( ) and void display ( ).
Question 7
A spy number is a number in which sum of its digits is equal to the product of its digits. For
example: 123 (1 + 2 + 3 = 1 x 2 x 3)
Class name SpyNum
Data members/instance variables:
num to store a number
Member functions/methods:
SpyNum (int nn) parameterized constructor to initialize ‘num’
int sumdigits( ) returns the sum of the digits of ‘num’
int prodigits( ) returns the product of the digits of ‘num’
void check( ) checks and displays whether the number is spy or
not
Specify the class SpyNum giving details of the constructor and methods.
Question 8
A lead number is a number if the sum of its even digits is equal to the sum of its odd digits. For
example: 6369 (3 + 9 = 6 + 6), 1452 (1 + 5 = 4 + 2)
Class name Lead
Data members/instance variables:
num to store a number
Member functions/methods:
Lead (int nn) parameterized constructor to initialize ‘num’
boolean isLead( ) returns true if the number is lead else returns false.
The function extracts digits without using % operator
void check( ) checks and displays whether the number is lead or
not
Specify the class Lead giving details of the constructor and methods.
Question 9
Fibonacci sequence is given as 1, 2, 3, 5, 8, 13......
A class Fiboprime has been defined as:
Class name Fiboprime
Data members/instance variables:
lim stores the limit in integer
Member functions/methods:
Fiboprime (int r) constructor to assign r to lim
CPP XI – SR Page 3
boolean isprime(int n) returns true if n is prime, otherwise returns false
void display( ) displays Fibonacci primes number less than the
given limit
Specify the class Fiboprime giving details of the constructor and methods.
Question 10
Design a class Weight to calculate the charge of a parcel.
Class name Weight
Data members/instance variables:
int w to store the weight of the parcel in grams
int charge to store the total charge of the parcel
Member functions/methods:
Weight ( ) default constructor
void getWeight ( ) to input the weight of the parcel in grams
void calculate ( ) to calculate the total charge according to the
following criteria:
If weight ≤ 1000 gram, then charge = Rs. 30
For every additional 500 grams or part thereof Rs.5
will be charged. For e.g. if weight of parcel is 1700
grams the charge will be Rs. 40.
Specify the class Weight giving details of the constructor and methods.
Question 11
A trunk number is a number whose reverse is larger than itself. For example: 155
Class name Trunk
Data members/instance variables:
num to store a number
Member functions/methods:
Trunk (int nn) parameterized constructor to initialize ‘num’
int reverse(int n) returns reverse of ‘n’. The function extracts digits
without using % operator
void check( ) checks and displays whether the number is trunk or
not
Specify the class Trunk giving details of the constructor and methods.
CPP XI – SR Page 4