Practice Tasks
Practice Tasks
Write a program that computes the total sales tax on a $95 purchase. Assume the state sales tax is 6.5 percent
and the county sales tax is 2 percent. Display the purchase price, state tax, county tax, and total tax amounts
on the screen.
Task 2
Write a program, which takes age as input from user and prints appropriate message depending upon the following
conditions:
Task 3
Write a program that asks the user for a positive integer value and that uses a loop to validate the input. The
program should then use a second loop to compute the sum of all the integers from 1 up to the number entered.
For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50.
Task 4
#include <iostream>
int main () {
}
Task 5
#include <iostream>
using namespace std;
int main () {
int num;
char c;
int count;
float salary;
cout << "The address stored in numptr = " << numptr << endl;
cout << "The address stored in cptr = " << cptr << endl;
cout << "The address stored in countptr = " << countptr << endl;
cout << "The address stored in salaryptr= " << salaryptr << endl;
++*numptr;
++*cptr;
++*countptr;
++*salaryptr;
++numptr;
cout << "The address stored in numptr = " << numptr << endl;
cout << "The value pointed by numptr = " << *numptr << endl;
return 0;
Task 6
Declare a dynamic array of integer type, size taken from user. Define the following functions:
1. Fill_Array: this function will take the array as input and fill the array with random numbers using rand
( ) function.
2. Maximum: this function will returns the maximum value in the array.
3. Minimum: this function will returns the minimum value in the array.
Task 7
The arrays array1 and array2 each hold 25 integer elements. Populate the arrays with some valued. Write code
that swap the contents of array1 and array2.
Task 8
Write a modular program that accepts up to 20 integer test scores in the range of 0 to 100 from the user and stores
them in an array. Then main function should report how many perfect scores were entered (i.e., scores of 100).
Demonstrate the working of the developed function.
Task 9
Create a class called Date that includes three pieces of information as data members; a month (type int), a day
(type int), and a year (type int). Your class should have three constructors that initialize the three data members.
For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that
the month value in the range 1-12; if it isn’t, set the month to 1.
Provide setter and getter methods for each data member. Provide a member function displayDate() that displays
the month, day and year in the provided format, i.e. day – month – year.
Task 10
Create a class called MotorVehicle that represents a motor vehicle using data members: make (type string),
fuelType (type string), yearOfManufacture (type int), color (type string), and engineCapacity (type int). Your
class should have three constructors (default, argument & copy) that initialize all data members. Provide setter
and getter methods for each data member. Add a member function called displayCardDetails( ) that displays the
five data members in five separate lines in the form “member name: member value”.
Task 11
Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store.
An Invoice should include six data members – a part number (type string), a part description (type string), a
quantity of the item being purchased (type int), a price per item (type int) a value-added tax (VAT) rate as a
decimal (type double), and a discount rate as a decimal (type double).
Your class should have default constructor as well as parameterized constructor that initializes the six data
members. The constructor should initialize the first four data members with values from parameters and the last
two data members to default values of 0.20 percent and zero respectively.
Provide setters and getters methods for all data members. In addition, provide a member function named
getInvoiceAmount( ) that calculates the invoice amount (i.e., multiplies the quantity by the price per item and
applies the tax and discount amounts), then returns the amount. Perform validity checks on parameters of setters,
if a parameter value is not positive, it should be left unchanged.
Task 12
Create a class called Distance that includes two pieces of information as data members - feet (type int), inches
(type int). Your class should have three constructors which initialize the two data members.
Provide setter and getter methods for each data member. Provide a member function displayDistance( ) that
displays the feet and inches in the form of 5’6’’.
Task 13
Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual interest rate
for each of the savers. Each member of the class contains a private data member savingsBalance indicating the
amount the saver currently has on deposit. Provide member function calculateMonthlyInterest that calculates
the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be
added to savingsBalance.
Provide a static member function modifyInterestRate that sets the static annualInterestRate to a new value.
Write a main function to test SavingsAccount class. Instantiate two different objects of class SavingsAccount,
saver1 and saver2, with balances of $2000 and $3000, respectively. Set the annualInterestRate to 3 percent. Then
calculate the monthly interest and print the new balances for each of the savers.
Then set the annualInterestRate to 4 percent, calculate the next month's interest and print the new balances for
each of the savers.
Task 14
Write program to count the number of objects created and destroyed for a class using static data members and
static member functions. You can develop any class of your choice.
Task 15
1. sum,
2. multiply
3. divide
4. modulus
5. sin
6. cos
7. tan
The user should be able to call these methods without creating an object of Calculator class. Demonstrate working
of your class.
See the UML class diagram of circle & cylinder and implement inheritance relationship of classes in C++.
You need to implement following items for each class;
1. Constructors
2. Setters
3. Getters
4. Member Methods
Task 17: Book, EBook, AudioBook, PaperBook
See the UML class diagram of Book, ElectronicBook, AudioBook,& PaperBook and implement inheritance
relationship of classes in C++.
You need to implement following items for each class;
1. Constructors
2. Setters
3. Getters
4. Member Methods
Following class diagrams are showing complete detail of above mentioned classes.
Task 18: Point & Point3D
See the UML class diagram of point & point3d and implement inheritance relationship of classes in C++.