Object Oriented Programming - Nabia Khalid
Object Oriented Programming - Nabia Khalid
Marks 14 10 12 6 8 50
Assigned
Marks
Obtained
Page 1 of 4
Enrollment Number: ____________________________
a. Recall the inheritance concept and suppose you have an inheritance hierarchy with a
base class Animal and two derived classes Bird and Snake. Based on the
characteristics of animals in general and birds and snakes specifically, can you think
of possible virtual and pure virtual functions to place in Animal? Justify your answer.
b. Complete the following tasks:
1. Create a class named Teacher that holds a Teacher’s first and last names and the grade
level the Teacher teaches. Include a constructor function that uses default first and last
names, such as “ZZZZZZZ” and a default grade level, such as 0, when no arguments
are provided.
2. Create a class named Student that holds a Student’s first and last names. Include a
constructor function that uses default first and last names, such as “ZZZZZZZ,” when
no names are provided. Create a class named Classroom. A Classroom holds a Teacher
and an array of 35 Student objects.
3. Create a class named School. A School contains an array of 100 Classroom objects.
4. In a main () demonstration function, declare a pointer to an array of 500 School objects
and include code to handle any memory allocation errors that occur. In a loop, declare 500
array objects at a time, continuing to rewrite the main () function if necessary until you
create enough School objects to cause a memory allocation exception.
(Hint: You might want to display a count periodically to assure yourself that the
School objects are being created.)
When memory is exhausted, display an appropriate message and terminate the application.
};
Given the above declaration for a TestScore class, do the following tasks:
1. Write the implementation for the TestScore class. The constructor just assigns its
parameters to the private data members, and the observers simply return the
corresponding member.
2. Write a derived class declaration called IDScore that adds an integer student ID
number as a private member, and that supplies (a) a constructor whose parameters
correspond to the three member fields, and (b) an observer that returns the ID number.
3. Write the implementation for a class called Exam that uses composition to create an
array of 100 objects of class IDScore. The class can use the default constructor, and it
will have a function that assigns an IDScore object to a location in the array, given
Page 2 of 4
Enrollment Number: ____________________________
the object and the location as parameters. The class should also have an observer that
returns the IDScore object at the position specified by its parameter.
a. Create a class template for a class that holds an object and the number of data
elements in the object. For example, if an Employee class has two data elements, an
ID number and a salary, then the class template holds the number 2 and an Employee
object; if a Student class contains 12 data elements, then the class template holds 12
and a Student. Code a standard input function for the object that displays a message
on the screen—“You will be asked to enter X items”—where X is the number of data
elements. Write a main () function that tests your template class with an integer and
two programmer designed classes.
b. Write a program that allows a user to use the keyboard to input the first and last
names of 40 friends, up to 50 characters each. Display the names 15 at a time, pausing
to let the user press a key before the list continues.
Question # 5 (8 Marks)
(CLO-2)
Given the following declaration for a class that represents a computer in a company’s
inventory, extend a derived class declaration (for a class called InstallRecord) that adds (1)
a string field representing the location of the computer and (2) a field of class SimpleDate
that holds the installation date. The new class should provide observers for each of the new
fields. It should also re implement the Write function.
class Computer
{
public:
Computer (string newName, string newBrand, string newModel, int newSpeed, string
newSerial, int newNumber);
string GetName() const;
string GetBrand() const;
string GeModel() const;
int GetSpeed() const;
string GetSerial() const;
int GetNumber() const;
void Write() const;
private:
string name;
string brand;
int speed;
string model;
string serialNumber;
int inventoryNumbr;
Page 3 of 4
Enrollment Number: ____________________________
};
Best of Luck
Page 4 of 4