[go: up one dir, main page]

0% found this document useful (0 votes)
120 views9 pages

Practice Tasks

The document contains 18 programming tasks involving concepts like inheritance, classes, constructors, static members, pointers, arrays, and more. The tasks range from basic programs calculating sales tax or age-based messages to more complex programs involving classes for dates, vehicles, invoices, distances, savings accounts, calculators, cylinders, books, and 3D points.

Uploaded by

Shuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views9 pages

Practice Tasks

The document contains 18 programming tasks involving concepts like inheritance, classes, constructors, static members, pointers, arrays, and more. The tasks range from basic programs calculating sales tax or age-based messages to more complex programs involving classes for dates, vehicles, invoices, distances, savings accounts, calculators, cylinders, books, and 3D points.

Uploaded by

Shuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Task 1

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:

• If age less then 6 then prints, “What a nice child!”


• If age is between 6 and 9 then prints, “That’s a good age!”
• If age is between 9 and less than 20 then prints, “Ah! In the prime of life”
• If age between 20 and less than 30 then prints, “Watch out, the younger ones are gaining on you.”
• More than 30 then it prints, “Well, have fun, and don’t look back.”

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

Compile and run the program and see what it does.

#include <iostream>

using namespace std;

int main () {

int x =10 ; int *yptr ; yptr =


&x;
*yptr += 3; yptr += 3;
return 0;

}
Task 5

Compile and run the program and see what it does.

#include <iostream>
using namespace std;

int main () {

int num;
char c;
int count;
float salary;

// Declaring bunch of pointers that point to nothing (point to null)


int *numptr; // declare a pointer variable to an integer
char *cptr; // declare a pointer variable to a character
int *countptr; // declare a pointer variable to an integer
float *salaryptr; // declare a pointer variable to a float

// Making them point somewhere


numptr = &num; //numptr is pointing to num
cptr = &c; //cptr is pointing to c
countptr = &count; //countptr is pointing to count
salaryptr = &salary; //salaryptr is pointing to 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;

// Assign values to the locations where the pointers point


*numptr = 2;
*cptr = 'A';
*countptr = 100;
*salaryptr = 3200;

// Display the content of what pointers variable point to


cout << "The value pointed by numptr = " << *numptr << endl;
cout << "The value pointed by cptr = " << *cptr << endl;
cout << "The value pointed by countptr = " << *countptr << endl;
cout << "The value pointed by salaryptr= " << *salaryptr << endl;

++*numptr;
++*cptr;
++*countptr;
++*salaryptr;

// Display the content of what pointers variable point to


cout << "The value pointed by numptr = " << *numptr << endl;
cout << "The value pointed by cptr = " << *cptr << endl;
cout << "The value pointed by countptr = " << *countptr << endl;
cout << "The value pointed by salaryptr= " << *salaryptr << endl;

// Display the contents of those memory locations


cout << "The value of num = " << num << endl;
cout << "The value of c = " << c << endl;
cout << "The value of count = " << count << endl;
cout << "The value of salary= " << salary << endl;

++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.

Write a test program that demonstrates class Date’s capabilities.

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”.

Write a program to demonstrate MotorVehicle’s capabilities.

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.

Write a driver program to demonstrate Invoice’s capabilities.

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’’.

Write a test program that demonstrates class Distance’s capabilities.

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

Create a Calculator class that has following methods:

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.

Task 16: Cylinder Inherits Circle

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++.

You might also like