C++ Programming Technique II Object Oriented Programming Past Year Question Problems (Advance FileOperation Question)

Given Program 3 below which is intended to read a list of cars from a binary file and calculate the total price of all the cars. The user will need to enter the file’s name that contains the list of cars. Write the correct C++ code to perform the following tasks. Write your answer in Table 1.
a. Specify an appropriate parameter for the method fileCheck.
 This method will take a file as its parameter.          (1 mark)
 b. Write the condition to check whether the file is opened successfully.                    (1 mark)
 c. The file’s name used in your computer system is in small letters. However, the user might be entering the name in capital letters or mixed cases.  Thus, convert the file’s name to small letters.          (3 marks)
 d. Open the file as a binary input file.                                                                        (2 marks)
e. Invoke the method fileCheck to check the input file has been opened successfully.  (2 marks)
 f. Determine the size of the input file in bytes.                                                       (3 marks)
g. Determine the size of each Car object.                                                                (1 mark)
 h. Determine how many cars in the input file.                                                            (1 mark)
i. Read all the Car objects from the input file into the array  cars                     (4 marks)
 j. Calculate the total price.   
                                                                                     (2 marks)



Answer

a)static void fileCheck(fstream &file)
b)(!file.fail()) @ (!file.open)
c)For (int i=0;i< fileName.size();i++){
 fileName[i] =tolower(fileName[i])}
d)fin.open(fileName.c_str(),ios::in|ios binary) @ fin.open(fileName.data(),ios::in|ios binary)
e).Car::fileCheck(fin)//static mathode
f)fin.seekg(0,ios::end)//move the cursor to the end
   fileSize=fin.tellg()//tell the number of character in file
g)objectSize=sizeof(Car);//sizeof memang reserve words dlm library
h)nCars=fileSize/objectSize;
i)fin.seekg(0,ios::beg);
  in.read((char*)Cars,nCars*sizeof(Car));//seekg only for input file only,used to go to specific                                                                                  location in file
                                                                  //set to read position at 0 bytes from beginning of file
j) for (int i=0;i<nCars,i++)
       totalprice +=Cars[i].getPrice();

Question 1                               [15 Marks]
 a. Object-Oriented Programming (OOP) comes with many principles. Choose only FOUR (4) of them and briefly describe the key concept of each principle with an appropriate example.                                                                                                             (10 Marks

Inheritance :the ability of one class to extend the capabilities of another
Polymorphism : is the ability of objects performing the same actions diffrently
Data hiding: ensured methods should not directly access instances attributed in a class other than their own.
Encapsulation :is combining attributes and methods in one package.
Objects:Each object that is created from a class is called an isntance of the class.
             A program is a collection of objects that interact with each other to accomplish goal
Classes:A class is a template  from which objects are actually made.It encapsulates  the attributes and actions that characterize a certain type of object

b. Design a UML class diagram to model an employee of a company.  Each employee has a name and salary rate (given as per hour). You must use proper notations for each element of the design including notations for private and public members and data types for each member data (attribute) and parameter. The class should also include the following operations:
 A default constructor.
 An appropriate overloaded constructor.
 A mutator for each attribute.
 An accessor for each attribute.
 A method to calculate the total salary earned. The method will take the total hours of work as a parameter and return the total salary. (5 marks)


Post a Comment

0 Comments