Question 1 Consider the following structure used to keep employee records:

Similar documents
Tutorial letter 202/2/2018

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Object Oriented Design

Tutorial letter 202/1/2015

Review Questions for Final Exam

CS242 COMPUTER PROGRAMMING

University of Swaziland Department OfComputer Science Main Examination December 2016

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Functions, Arrays & Structs

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

C++ (Non for C Programmer) (BT307) 40 Hours

Functions, Arrays & Structs

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Lecture 7. Log into Linux New documents posted to course webpage

Absolute C++ Walter Savitch

END TERM EXAMINATION

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

CS 247: Software Engineering Principles. ADT Design

PROGRAMMING EXAMPLE: Checking Account Balance

Short Notes of CS201

CSCE 110 PROGRAMMING FUNDAMENTALS

Name Section: M/W or T/TH. True or False (14 Points)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CS201 - Introduction to Programming Glossary By

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Object Oriented Programming 2012

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

AN OVERVIEW OF C++ 1

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

C++ Programming: From Problem Analysis to Program Design, Third Edition

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Lecture 18 Tao Wang 1

Integer Data Types. Data Type. Data Types. int, short int, long int

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Encapsulation in C++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Lab # 02. Basic Elements of C++ _ Part1

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Lecture #1. Introduction to Classes and Objects

Problem Solving with C++

Interview Questions of C++

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

CS250 Final Review Questions

Introduction to Programming using C++

VALLIAMMAI ENGINEERING COLLEGE

Review: C++ Basic Concepts. Dr. Yingwu Zhu

2 nd Week Lecture Notes

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSCE 110 PROGRAMMING FUNDAMENTALS

C++_ MARKS 40 MIN

Ch. 12: Operator Overloading

Name SECTION: 12:45 2:20. True or False (12 Points)

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE1303. B.Tech. Year - II

System Design and Programming II

Abstract Data Types. Different Views of Data:

Introduction Of Classes ( OOPS )

Objectives. In this chapter, you will:

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

CS250 Final Review Questions

Introduction to Classes

PIC 10A. Final Review: Part I

CE221 Programming in C++ Part 1 Introduction

C++ 8. Constructors and Destructors

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Friends and Overloaded Operators

Implementing an ADT with a Class

Friends and Overloaded Operators

CS2141 Software Development using C/C++ C++ Basics

Introduction to C++ Systems Programming

Abstraction in Software Development

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS201- Introduction to Programming Current Quizzes

Exam 3 Chapters 7 & 9

True or False (12 Points)

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CS250 Final Review Questions

Midterm Exam 5 April 20, 2015

1) You want to determine whether time has run out. The following code correctly implements this:

Use the dot operator to access a member of a specific object.

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CMSC 202 Midterm Exam 1 Fall 2015

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Chapter if (machine.history.lastserviced.year!= currentdate.year)

Chapter 15 - C++ As A "Better C"

22316 Course Title : Object Oriented Programming using C++ Max. Marks : 70 Time: 3 Hrs.

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

UEE1302 (1102) F10: Introduction to Computers and Programming

Transcription:

Question 1 Consider the following structure used to keep employee records: struct Employee string firstname; string lastname; float salary; } Turn the employee record into a class type rather than a structure type. The employee record class should have private member variables for all the data. Include public member functions for each of the following: a default constructor that sets the employee s first name and last name to a blank string, and his annual salary to 0; an overloaded constructor that sets the member variables to specified values; member functions to set each of the member variables to a value given as an argument to the function (i.e. mutators); member functions to retrieve the data from each of the member variables (i.e accessors); Embed your class definition in a test program. The test program should create two Employee 36

COS1512/101/3/2018 objects and display each object s annual salary. Use the overloaded constructor to initialise one of the Employee records as Joe Soap with an annual salary of R145600.00. Obtain the following input values for the other Employee record from the keyboard: Joanne Soape R154460.66 Now give each employee a 10% raise and display each Employee object s annual salary again. Question 2 a bit of theory and terminology (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) (k) (l) What is the purpose of the keywords public and private in the class declaration? What is the difference between a class and an object? What does it mean to instantiate an object? What is the purpose of a constructor? What is the difference between the default constructor and the overloaded constructor? What is the purpose of a destructor? What is the purpose of an accessor? What is the purpose of a mutator? What is the purpose of the scope resolution operator? What is the difference between the scope resolution operator and the dot operator? What is the difference between a member function and an ordinary function? What is an abstract data type (ADT)? (m) How do we create an ADT? (n) (o) (p) (q) (r) What are the advantages of using ADTs? What is separate compilation? What are the advantages of separate compilation? What is a derived class? What is the purpose of inheritance? Question 3 3 (a) Consider the following class declaration: class ExamType public: ExamType(); ExamType(string m, string v, int t, string d); 37

}; private: string module; string venue; int time; string date; Explain what is wrong with the following code fragment and include code to correct it: int main() ExamType exams[12]; for (int i = 0; i < 12; i++) if (exams.module[i] == "COS1512") cout << "COS1512 will be written on " << exams.date << " at " << exams.time; return 0; } Question 4 The questions below refer to the following class declaration: #include <iostream> #include <string> using namespace std; class Chequebook public: Chequebook(); Chequebook(float AccountBalance); void Deposit (float Amount); void WithDraw (float Amount); float CurrentBalance() const; void Adjust(); private: float Balance; }; 38 int main() cout << "Enter the Account balance:"; float amount; cin >> amount; Chequebook Chequebook1(amount); cout << Account balance: R << Chequebook1 << endl; cout << Enter amount to deposit: ; cin >> amount; Chequebook1.Deposit(amount); cout << Balance after deposit: R << Chequebook1 << endl; cout << Enter amount to withdraw: ; cin >> amount; Chequebook1.WithDraw(amount); cout << Balance after withdrawal: R << Chequebook1 << endl; ++Chequebook1; cout << Balance after adjusting: R << Chequebook1;

COS1512/101/3/2018 } return 0; (a) (b) (c) (d) (e) (f) (g) Give implementations of both the default constructor and the second constructor. Implement the Deposit() and WithDraw() member functions. The Deposit() member function should increment the member variable Balance with the amount deposited, and the WithDraw() member function should decrement the member variable Balance with the amount withdrawn. Implement the CurrentBalance() member function. It should return the current balance of the cheque book. The member function Adjust() should increment the member variable Balance by R100. Give an implementation for this member function. Overload the stream insertion operator as a friend function. It should write the balance of the account to the given output stream. The statement ++Chequebook1; should increment the member variable Balance of Chequebook1 by R100. Give three different implementations for the overloaded operator ++ to accomplish this: using the member function Adjust() implementing the overloaded operator ++ as a friend function implementing the overloaded operator ++ as a member function. Hint: See chapter 11 in the study guide, Tutorial letter 102 under Additional Resources on the COS1512 course website. Run your program three times (each time with a different version of the overloaded operator ++; comment the other two versions out during each run) with the following input: 400 200 300 Enrichment Exercise: Turn the Chequebook class into an ADT, so that separate files are used for the interface and implementation. Use separate compilation to compile the implementation separate from the application file that tests the ADT. PLEASE NOTE: The enrichment exercises do not form part of the assignment. It is for practice only. Question 5 Define a class Voter as an ADT that uses separate files for the interface and the implementation. This class represents one voter voting in an election. This class has three member variables: ID, a string that holds the ID of the voter nr_times_voted, an integer value that indicates the number of times the voter has voted in the past voted, a Boolean value to indicate whether the voter has voted in the current election or not. 39

In addition, the class should contain a default constructor that initializes ID to an empty string, nr_times_voted to 0, and voted to false. It should also contain an overloaded constructor that creates a new voter and sets ID to a specified value, nr_times_voted to 0 and voted to false. The destructor should not perform any action. Include accessor functions that returns the values stored in each of an object of class Voter s member variables respectively, as well as a mutator function called set_voted() that sets the voted member variable of a Voter to true. Overload the prefix increment operator++ (implemented as a friend function) to return the current instance of the class Voter after incrementing the nr_times_voted by 1. Use the following prototype: Voter operator++(voter& V); Overload the stream extraction operator >> (implemented as a friend function) so that it can be used to input values of type Voter. Overload the stream insertion << (implemented as a friend function) so that it can be used to output values of type Voter. Demonstrate the class in an application program (main()) that is used to obtain a voter s ID from the user, find the voter on the voters roll (kept in a file called VotersRoll.dat), check whether the voter has voted in the current election, and if not, prints a note that allows him/her to vote. If a voter has already voted, a message should indicate that the voter is not allowed to vote again. When the user votes, the operator++ is used to increment the number of times the voter has voted, and the set_voted() member function is used to indicate that the voter has now voted in the current election. While the voter s roll (file VotersRoll.dat) is processed, a new updated voters roll (another file called UpdatedVoters.dat) is created simultaneously. Take care to make sure that all the voters on the original voters roll also appear in the updated voters roll. Test your program with the following data: VotersRoll.dat: 19810102009 1 0 19792003008 2 0 19851010890 3 1 19900909897 2 0 19561812567 6 0 19682703345 7 1 Voters that want to vote: 19810102009 19792003008 19851010890 19561812567 19682703345 Question 6 Overload the stream extraction operator >> for the class Employee in Question 1 to read values for an object of class Employee from a file. Also overload the stream insertion operator << to print the data for an employee record either on the screen or to a file. 40

COS1512/101/3/2018 Use separate compilation and write a program that uses the overloaded extraction operator >> to read records for employees from a file named Employees.dat into an array. Assume that the file will never contain data for more than 20 employees. Use the array to determine the average salary, as well as the highest salary and the lowest salary. Display the average salary, the highest salary and the lowest salary on the screen. Increase each employee s salary by 10 % and use the overloaded stream insertion operator << to display the updated Employee objects on the screen and also to write the updated Employee objects to a new data file. Use the following data: Peter Pannier R134000 Lucas Radebe R75000 Albert Mokgaba R80000 Petrus Petersen R111120 Thabelo Tebogo R200453 Alexa Breda R231334 Jocasta Johnson R199181 Gian Grooteboom R60000 Question 7 Consider the following class: class Package public: Package(double the_cost, double the_weight, const string& the_sender, const string& the_receipient); double calculate_cost()const;//multiply weight by cost_per_kilogram string get_recipient() const; string get_sender() const; protected: double cost_per_kilogram; double weight; private: string sender; string recipient; }; (a) (b) Implement class Package. Test class Package in a driver program that does the following: instantiates an object of class Package, with the following details: sender: Charles Somerset receiver: Anne Barnard cost per kilogram: 12.75 weight: 1.25 use the accessor functions to display the names of the sender and receiver of the instantiated object on the console. 41

Use the member function calculate_cost() to determine the cost of the package represented by the instantiated object. Display the calculated cost also on the console. (c) (d) Derive and implement a new class TwoDayPackage that inherits the functionality of the class Package. TwoDayPackage should redefine calculate_cost(), where a fixed fee is added to the weight-based cost. For example, a TwoDayPackage that has a weight of 10kg, a cost of R3.00 per kilogram and charged a fixed fee of R5.00 would cost R35.00 (10*3.00 + 5.00) to deliver. The class should include a member variable to represent the fixed fee. TwoDayPackage has an additional member function Print(), which outputs the cost_per_kilogram, weight, sender, recipient and the total cost of delivery of the package. Test class TwoDayPackage in a driver program that does the following: instantiates an object of class TwoDayPackage, with the following details: sender: Charles Somerset receiver: Anne Barnard cost per kilogram: 12.75 weight: 1.25 use the accessor functions to display the names of the sender and receiver of the instantiated object on the console. Use the member function calculate_cost() to determine the cost of the two_day_ package represented by the instantiated object, then display the calculated cost also on the console. 42