You are on page 1of 4

Practice Question-1(Polymorphism)

Task 1:
 Create an abstract class called Account.
 It has data member:
o Private Account Number.
o Private Account Balance.
 Add suitable setter/getter for data.
 Add Debit(float), Credit(float) as member functions (Pure Virtual).
 Add Print() function (Virtual)
o Override Print Debit and Credit functions according to derived classes.

Task 2:
 Create a class called CurrentAccount i-e: CurrentAccount(is-a) Account
 It has data member:
o Service Charges (To be charged during credit if account balance is less than min
balance )
o Minimum Balance
 Override print() as created in above class which displays:
o Account Number, Account Balance, Minimum Balance, Service Charges
 Modify the definition of the print() so that it displays a suitable message containing above
info.
 Similarly override credit(float), debit(float) functions such that credit(float) simply add
amount to the Account Balance and debit(float) checks if the amount to be debited is within
the range of Account Balance.
 Further if the account balance after debit is less than min balance, standard charges would
also be deducted from account balance.

 Create a class called SavingAccount i-e: SavingAccount (is-a) Account


 It has data member:
o Interest Rate.
 Override print() as created in parent class which displays:
o Account Number, Account Balance, Interest Rate
 Modify the definition of the print() so that it displays a suitable message containing above
info.
 Similarly override credit(float), debit(float) functions such that credit(float) simply add
amount to the Account Balance and debit(float) checks if the amount to be debited is within
the range of Account Balance.

Taks 3:

 In main function:
o Create an array of Account type Pointers, of size 3. Assign Account object to index
0, CurrentAccount object to index 1 and SavingAccount object to index 2.
o Have you encountered any problem ? Report the problem in comment and change
the size and elements of the array accordingly
o Now Credit and Debit the CurrentAccount and SavingAccount in main function
after taking input from user.

Task 4:
Add destructors in all classes. Print a suitable message in each destructor.

Practice Question-2 (Composition)


Define and implement a class Point - Point.cpp. This class should provide:

 Two private integer data members x and y which will store the x and y coordinates of a point
 A default constructor which takes two parameters to initialize the x and y coordinates and
prints “Point() called” on the screen.
 A function print() which prints out the point on the screen in the format (x,y)
 A destructor which prints “~Point() called” on the screen.

Step 2:
Now define and implement a class Circle - Circle.cpp. This class should contain:

 A private data member center which will be an instance of the Point class
 A private float data member radius that will store the radius of the circle
 A constructor which takes three parameters (x and y coordinates of the center of the circle, and
the radius) and initializes the data members accordingly and also prints “Circle() called” on
the screen.
 A destructor which prints “~Circle() called” on the screen.
 A function print() which prints the information (center and radius) of the circle on the screen

To call the constructor of class Point from the constructor of class Circle, you can use the following
syntax.

Circle::Circle(int x, int y, float r): center(x,y) { … };

main() function:

void main()
{
Circle c (3,4,2.5);
c.print();
}

Step 3:
Define and implement a class Quadrilateral - Quadrilateral.cpp. This class should provide:

 Four private data members w, x, y and z (Point type) which will be indicating the four corners
of the quadrilateral.
 A constructor which takes eight parameters (x and y coordinates of the four corners) and
initializes the data members accordingly and prints “Quadrilateral () called” on the screen.
 A destructor which prints “~Quadrilateral called” on the screen.
 A function print () which prints out the information (i.e. the coordinates of its four corners) of
the quadrilateral object on the screen.

Step 4:
Modify the main() to instantiate an object of class Quadrilateral called obj with parameters for
points (1, 0) (0, 1), (1, 1) and (0, 0) and call its print function.

Practice Question-3 (Association)


Implement a class Teacher that has following members:
int EmployeeID
char* Name

Create another class Student with these data members:


Char * roll number
Char * name
Teacher* TeacherList; //List of all teachers who are teaching this student

Create constructors, destructors, Display function of both the classes such that Teachers are
associated with Students.
Note:
 Deallocate all dynamically allocated memory.
 Do not use any string class built-in functions except for strlen(), if required.
 Follow all the code indentation, naming conventions and code commenting guidelines.

Practice Question-4 (Templates)


Consider the following class template:

template <class T, int N>


class Sequence {
T memblock [N];
public:
void setmember (int x, T value);
T getmember (int x);
};

Sequence is a class that stores a sequence of elements. N is an integer. The member function setmember
sets the member at position x in the memblock with value and getmember returns the value at index x.

a. Implement the Sequence class w.r.t the following main

int main ()
{
Sequence <int,5> myints;
Sequence <double,5> myfloats;
myints.setmember (0,100);
myfloats.setmember (3,3.1416);
cout << myints.getmember(0) << '\n';
cout << myfloats.getmember(3) << '\n';
return 0;
}

You might also like