Lab-2.1.docx.pdf
Document Details
Tags
Full Transcript
Classes and Objects: 1. Define a class Complex with variables real and imaginary along with following: (a) void setComplex (float, float) to initialize object values. (b) void displayComplex() to show the complex number (c) Pass and return objects to calculate s...
Classes and Objects: 1. Define a class Complex with variables real and imaginary along with following: (a) void setComplex (float, float) to initialize object values. (b) void displayComplex() to show the complex number (c) Pass and return objects to calculate sum of two complex numbers. Display the sum. Similarly, implement functions for subtraction and multiplication of two complex variables. Also, implement a function to return the conjugate complex number of the number passed as parameter. In continuation to this, define overloaded +, - , and * operators for more readable syntax for addition, subtraction and multiplication of two complex numbers. 2. Create a class that imitates part of the functionality of the basic data type int. Call the class Int (note different capitalization). The only data in this class is an int variable. Include member functions to initialize an Int to 0, to initialize it to an int value, to display it (it looks just like an int ), and to add two Int values. Write a program that exercises this class by creating one uninitialized and two initialized Int values, adding the two initialized values and placing the response in the uninitialized value, and then displaying this result. 3. Define a Vector class to store a vector of n dimension. Define a copy constructor to copy one vector to the other on its creation. Write functions for addition, subtraction and dot product of two vectors. Also write operator overloading function for + , - , * (dot product). 4. Define a Matrix class to store a matrix of n x m dimension. Define a copy constructor to copy one matrix to the other on its creation. Write functions for addition, subtraction and multiplication of two matrices. Also write operator overloading function for + , - , *. 5. Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected. Model this tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both of these to 0. A member function called payingCar() increments the car total and adds 0.50 to the cash total. Another function, called nopayCar() , increments the car total but adds nothing to the cash total. Finally, a member function called display() displays the two totals. Make appropriate member functions const. Include a program to test this class. This program should allow the user to push one key to count a paying car, and another to count a nonpaying car. Pushing the Esc key should cause the program to print out the total cars and total cash and then exit. 6. Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. A main() program should create two initialized time objects (should they be const ?) and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally it should display the value of this third variable. Make appropriate member functions const. 7. Create a date class. Its member data should consist of three integers: month , day , and year. It should also have two member functions: getdate() , which allows the user to enter a date in 12/31/02 format, and showdate() , which displays the date. 8. Create an employee class. The member data should comprise an int for storing the employee number and a float for storing the employee’s compensation. Member functions should allow the user to enter this data and display it. Write a main() that allows the user to enter data for three employees and display it. Extend the employee class to include a date class and an etype enum. An object of the date class should be used to hold the date when the employee was hired. The etype variable should hold the employee’s type: laborer, secretary, manager, and so on. These two items will be private member data in the employee definition, just like the employee number and salary. You’ll need to extend the getemploy() and putemploy() functions to obtain this new information from the user and display it. These functions will probably need switch statements to handle the etype variable. Write a main() program that allows the user to enter data for three employee variables and then displays this data.