Question: Write a Line class to represent a line in a 2D plane. The class has two data members defining a line, a slope and an

Write a Line class to represent a line in a 2D plane. The class has two data members defining a line, a slope and an intercept. It must not have any other data members. It should have the following public methods:

  • A constructor taking a slope and an intercept value to make a new line
  • A constructor that takes a set of point coordinate values x1, y1, x2, y2, which define a line passing through the points (x1,y1) and (x2,y2)
  • A method double distanceTo(Point p) which returns the distance between the line and a given point object p
  • A method Point intersection(Line ln) which returns the intersection point of this line and a given line ln.

Use the Point class given below for the Point type needed in the last two methods (put it in your code before your Line class). Do not modify this Point class.

class Point { private: double x, y; public: Point(double _x, double _y) : x(_x), y(_y) {} double _x() { return x; } double _y() { return y; } double distanceTo(const Point & p) { return sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y)); } };

Note: you may use the fabs() and sqrt() methods from the standard maths library.

Write a function (not a method of a class), that uses the functionality already provided by your Line class, to solve any pair of simultaneous equations of the form:

a1 x + b1 y + c1 = 0

a2 x + b2 y + c2 = 0

Your function should have the following form:

Point solve(double a1, double b1, double c1, double a2, double b2, double c2) { /* use Line objects here to find the answer - don't rewrite any code unnecessarily */}

Test your method using the following code in main() :

Point sol = solve(2.5, -2.0, 3.0, 1.5, -3.0, -4.1); cout << "Equations solution is x = " << sol._x() << " and y = " << sol._y() << endl;

which should print out

Equations solution is x = -3.82222 and y = -3.27778

Note: Aim to have no more than 3 or 4 lines of code in your function.

Note: In the next three questions you will create a set of classes that are related by inheritance. It is recommended that you read all three questions before writing any code. Sketching out a class diagram as you read the questions will help you to get a picture of the required structure of the classes (you do not need to submit this diagram).

Write an abstract class to represent a Student. A student has an ID, a first name and a last name. The ID should be an integer and the first and last names should be C++ strings. The class must have the following methods:

  • A constructor that takes a student ID value, a first name and a last name
  • A int getID() method which returns the student's ID value
  • A pure virtual method string getType() which, if implemented in any class derived from Student, would be expected to return the type of student as a string
  • A virtual void printInfo() method which prints out the student's details. This method should call the getType() method to print out the student type as the first line of the printout.

Write a concrete class called ResearchStudent which inherits from your Student class of question 3. A research student has a supervisor name (as a single C++ string) and a thesis title (as a C++ string). The class must also have the following methods:

  • A constructor taking the student's ID, first and last names, supervisor name and thesis title. This constructor calls the parent class constructor.
  • A void printInfo() method that calls the parent class's printInfo() method to print generic student information and also prints details specific to a research student.
  • A method that provides an implementation (overrides) the base class pure virtual function getType(). This method should simply return the string "Research Student".

Write a concrete class called TaughtStudent which also inherits from your Student class. A taught student has an array of module names for which they are registered. This array can have up to 10 module names. A module name is a C++ string. Additionally, the class has a member numRegisteredModules which stores the number of modules for which the student is currently registered. The class must have the following methods:

  • A constructor taking the student's ID, first and last name. The constructor should set numRegisteredModules to 0.
  • A method bool registerFor(const string & moduleName) which adds a module to the student's modules. If the student is already registered for 10 modules then this method should return false, otherwise it returns true.
  • A void printInfo() method that calls the parent class printInfo() method and also prints details specific to a taught student, including their list of registered modules.
  • An override for the base class pure virtual function that returns the string "Taught Student".

Write a function (not a method of a class) void studentTester() which contains code to test the classes you have developed in the previous three questions. When this function is called it should:

  • Create two research students.
  • Create two taught students, and register them for three modules each.
  • Make an array of pointers to Student and point the elements of the array at your four student objects.
  • Run a for loop which calls each student's printInfo() method, using the array of pointers.

Write a class StudentRegister which contains a list of students, where students are objects of the classes you wrote in questions 3, 4 and 5. The class should have an array of pointer to Student to store student objects. It should be possible to store up to 1000 students in the register. The class should have a data member that records the number of students currently in the register. The class should have the following data members:

  • A constructor that takes no parameters and initializes the number of students to 0
  • A method add(const Student & student) that allows a student object to be added to the register (to the array of pointers). This method should make a copy of the student passed to the method, using the new operator.
  • A destructor which frees all memory that has been allocated using new
  • A void printInfoForID(int id) method that prints out information for the student with the given id value.

Your class should be generic, that is, it should only refer to Student objects and not to the derived classes ResearchStudent and TaughtStudent. To achieve this objective you may need to add a method to the classes of questions 3, 4 and 5. If you do this, state what code you have added and why.

Your class must operate correctly when called using the following code in main():

TaughtStudent s1(10001, "Bill", "Murray"); s1.registerFor("EE200"); s1.registerFor("EE201"); ResearchStudent s2(20001, "Clever", "Cloggs", "Prof. Knowalot", "Hard Sums"); StudentRegister studentRegister; studentRegister.add(s1); studentRegister.add(s2); int studentID = 20001; cout << "Searching for student : " << studentID << endl; studentRegister.printInfoForID(studentID);

Write a class Employee. An employee has a number and a name and the following additional features:

  • The first employee created is automatically assigned the number 1001, the second 1002 and so on. Implement this feature using a static data member nextEmpolyeeNumber.
  • Also keep track of the total number of employee objects using another static data member totalNumberEmployees. Note that this number should be decremented when an Employee object is destroyed.
  • Add a static member function called numEmpolyees() which returns the current number of employee objects.

Using the following code in main(), explain why this code "leaks memory".

cout << "Number of employees " << Employee::numEmployees() << endl; { Employee* e1 = new Employee("John Smith"); } cout << "Number of employees " << Employee::numEmployees() << endl;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!