You are on page 1of 6

Punjab Technical University Lab Assignment

C++ (should be done on Dev-C++ on Windows OR g++ on Linux) A Recap of C 1) Write a Program to display the following pattern : * * * * * * * * * * * * * Write a program to convert a decimal integer to any of the following : Binary, Octal, Hexadecimal. Use Switch-case and individual recursive functions for each conversion. Write a Program to display the Transpose of a matrix. Write a Program to calculate the product of two user-given matrices. Keep the necessary check conditions. Write a Program to reverse your name in the following ways : a) Using a library function b) Without using a library function Write a Program to find a substring within a String. Do not use a library function. Write a program to find the presence of a String within an array of Strings : a) Using Linear search b) Using Binary search(Sort the array in alphabetic order) Assume an integer array of size 10. Insert values in it. Write a function to delete the 5 th value and then to shift the values from 6th to 10th by 1 place towards left. Display the entire array after the operation is over. Access the array only by using Pointer. Write a Program to create the following Structure : Structure name Student fields Sname : alphanumeric Scourse : alphanumeric Sroll : number Sfeespaid : real number upto 2 decimal point Write a function make() to create a collection of 10 such records. Also write a display() function. Write a function to calfeespaid() to return the total fees paid(ref.to Q9).

2)

3) 4)

5)

6) 7)

8)

9)

10)

Classes and Objects 11) Design a class A with 2 real type data members. Keep constructor so that you can initialize them from main() with user given values. Write a member function sumMem() to add up member values. Write showSum() and a suitable main().

12)

Design a class Time with 3 member variables hour, minute and second. Keep member functions like setData(), addTime() and showTime() to initialize, add two Times and display object in a formatted way respectively. Write a suitable main(). Design a class Student using the assignment-9. Use setVal() method to initialize the data members. Keep a showVal() method. There should be a friend function calFeesPaid().Keep a static member variable count to keep track with the number of objects created. Write an appropriate main() to deal with at least 5 student records. Create two classes DM and DB which store the value of distances. DM stores distances in meters & centimeters and DB in feet & inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results may be a DM object or DB object, depending on the units in which the results are required. The display should be in the format of feet and inches or meters & centimeters depending on the object on display. Rewrite main() for the class Student(ref.Q9 & Q13) to store 10 objects of class Student in a file. Implement this with an array of Student objects. Define a destructor for Student class with a suitable message to show the value of count.

13)

14)

15)

15A) Create a class called time that has separate int member data for hours, minutes and seconds. One constructor should initialize these data to 0 and another should initialize them to some 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() should create two initialized time objects and another that is not 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 function const. 15B) Create a class that includes a data member that holds a serial number for each object created from the class. That is, the first object created will be numbered 1, the second 2, and so on. To do this, you will be need another data member that records a count of how many objects have been created so far. Then as each object is created, its constructor can examine this count member variable to determine the appropriate serial number for the new object. Add a member function that permits an object to report its own serial number. Then write main() that creates three objects and queries each one about its serial number. They should respond I am object number 2... and so on. Create a class Alpha having an integer member data. Create another class Beta with a member of same name. Moreover, class Beta should have a member function funTwo() which is friendly to Alpha. Now, this method is to add up the values of data, return and store it into an instance of Beta in main(). Write a suitable display() function.

15C)

Inheritance : Extending Classes 16) Create a class X with a protected variable x. A public method setX() is present to initialize the variable. Derive a class Y in public mode with a protected member variable y. A public method setY() is present to initialize the variable. Derive a class Z again in public mode from Y having a protected member z and a public method setZ(). Now write the main() to show the values of x, y and z from Z. Try the same thing by changing both the derivation in protected mode. Create a class Base1 with a public method display() having a output message such as We are in Base1. Derive a class Base2 with a public method display() with a similar message like We are in Base2.

17) 18)

Now derive a class Derived in public way from both Base1 and Base2. Derived is also having a method display() having a message like We are in Derived. Now write a suitable main() so as to invoke the display() methods of Base1 and Base2 one after another with an instance of Derived. 19) Imagine a publishing Co. that markets both book and audio cassette version of its works. Create a class Publication that stores the title(string) and price(float) of a publication. From here, derive two classes : Book, which adds a page count(type int) and Tape which adds a playing time in minutes(float). Each of these three classes should have a getData() function to get its data from the user at the keyboard, and a putData() function to display its data. Refer to the assignment 19. Add a base class Sales that holds an array of three floats so that it can record the dollar sales of a particular publication for the last three months. Include a getData() function to get three sales amounts from the user, and a putData() function to display the sales figures. Alter the Book and Tape classes so they are derived from both publications and sales. An object of class Book or Tape should input and output sales data along its other data. Write a main() function to create a book object and a tape object and exercise input/output capabilities. Start with the Publication, Book and Tape classes of 19A. Suppose want to add the date of publication for both Book and Tape. From Publication class, derive a new class called Publication2 that includes this member data. Then change Book and Tape so they are derived from Publication2 instead of Publication. Make all the necessary changes in member functions so the user can input and output dates along with the other data. For the dates, you can use a Date class which stores date as three integers, for month, day and year. Create a base class called Shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called Triangle and Rectangle from the base Shape. Add to the base class, a member function getData() to initialize base class data members and another member function displayArea() to compute and display the area of figures. Make displayArea() as a virtual function and redefine this function in the derived classes to suit their requirements. Using these three classes, design a program that will accept dimensions of a Triangle or a Rectangle interactively and display the area. Remember the two values given as input will be treated as lengths of two sides in case of Rectangles and as base & height in the case of Triangles and used as follows : Area of rectangle = x*y Area of triangle = 0.5*x*y Assume that a bank maintains two kinds of accounts for customers, one called as savings account and the other as current account. The savings account provides compound interest and withdrawal facilities but no cheque book facility. The current account provides cheque book facility but no interest. Current account holders should also maintain a minimum balance and if the balance falls below this level, a service charge is imposed. Create a class Account that stores customer name, account number and type of account. From this derive the classes Cur_acct and Sav_acct to make them more specific to their requirements. Include necessary member functions in order to achieve the following tasks: (a) Accept deposit from a customer and update the balance. (b) Display the balance. (c) Compute and deposit interest. (d) Permit withdrawal and update the balance. (e) Check for the minimum balance, impose penalty, necessary, and update the balance. Do not use any constructors. Use member functions to initialize the class members.

19A)

19B)

20)

21)

22)

Consider the following class network. The class master derives information from both account and admin classes which in turn derive information from the class person. Define all the four classes and write a program to create, update and display the information contained in master objects.

person name code

account

admin
experience

pay

master name code experience pay

Operator Overloading and Type Conversions 23) Create a class FLOAT that contains one float data member. Overload all the four arithmetic operators so that they operate on the objects of FLOAT. Create a class X having an integer data member. Now overload the operator to add data members of two objects of X. Write a program to do the addition operation of either two int type or two float type values using add(). Create a matrix class MAT of size m n. Define all possible matrix operations for MAT type objects. e.g. i) + : addition ii) - : subtraction iii) * : multiplication iv) = : assigning one MAT type to another v) == : check equivalence of two MAT type vi) % : transpose MAT Define a class String. Use a overloaded == operator to compare two strings. Also use a + operator to concatenate two strings. Create a class Myclass having two integer data members x and y. Now write three member methods for Myclass of same name swap() in order to swap these two variables. Prototypes of the functions should look like following : i) swap(int, int) ii) swap(int &, int &) iii) swap(int *, int *)

23A)

23B) 24)

25)

26)

Write a suitable display() method for ii) & iii) cases.

Polymorphism 27) Design a class Parent and derive a class Child from it. Now put a display() function in both the classes. Implement the invocation the display() of Child with the help of a pointer of the Parent class. Extend the Problem 20 to display the area of circles. This requires addition of a new derived class Circle that computes the area of a circle. Remember, for a circle we need only one value, its radius, but the getData() function in the base class requires two values to be passed.[Make the 2nd argument of getData() as a default one with zero value]. Run the above program with the following modifications : (a) Remove the definition of displayArea() from one of the derived classes. (b) In addition, to the above change, declare the displayArea() as virtual in the base class Shape.

28)

29)

30)

Design any suitable class and a derived class from it to exhibit function overriding. Now make a suitable function as virtual to show that reference of the appropriate object can call the member of it in context of inheriting classes.

Streams and Files 31) Write a suitable example of overloading the >> and << operators in order to facilitate cin and cout respectively in such a way so that they can accept the values of the data member of an object directly. Write a program that emulates the DOS copy command. That is, it should copy the contents of a text file(such as any .CPP file) to another file. Invoke the program with two command-line arguments the source file and the destination file like this c:\> ocopy <srcfile>.cpp <destfile>.cpp Write a program in C++ that reads a text fileand creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space. A file contains a list of telephone numbers in the following form : John 23456 Ahmed 9876 ... ... The names contain only word and the names and telephone numbers are separated by white spaces. Write a program to read the file and output the list in two columns. The names should be left-justified and the numbers right-justified. Write a program that will create a data file containing the list of Telephone numbers given in 33A. Use a class object to store each set of data. Write an interactive, menu-driven program that will access the file created in Exercise 33B and implement the following tasks. i) Determine the Telephone number of the specified person ii) Determine the name if a Telephone number is known. Iii) Update the telephone number, whenever there is a change.

32)

33)

33A)

33B)

34)

Data Structure 35) Create class LinkedList with suitable data members. Provide the mehods required to operate on them viz. InsertList(), displayList(), deleteList(). Write main() for it. Extend the class DblList from LinkedList to implement a doubly linked list. Keep suitable members and methods for it. Design a class Stack with a character data member. Keep push() and pop() methods to operate on it. Write the main() to revert your name using the stack. Implement a class Queue having suitable data members as front and rear. Keep necessary methods for en-queuing and de-queuing.

36)

37)

38)