Sunday, 8 July 2012

Exercise on C++

 1. Design a small program without the class concept for the functionality of a cashier to accept cash and to accept money and behave accordingly. The cashier is expected to call the signature verify authority (In this case a unique id generated by a random number generator and stored in some global entity) to verify the signature of the customer. If he is a valid customer then the cashier should accept the cheque or the money and deposit it in a global variable.

2. List down the problem faced in designing such kind of problem???

3. Try down the same problem with the concept of class and object and design a small bank.

4. If you require efficiency for any simple functions, will you treat the function as normal function or not. What is the appropriate step you should take to such kind of problems (In the above case signature function).

5. Create a class with public, private data members and function members. Create an object of this  class and see what kind of compiler messages you get when you try to access all the class members.

6. Write a struct called Lib that contains three string objects a, b, and c. In main( ) create a Lib object called x and assign to x.a, x.b, and x.c. Print out the values. Now replace a, b, and c with an array of string s[3]. Show that your code in main( ) breaks as a result of the change. Now create a class called Libc, with private string objects a, b, and c, and member functions seta( ), geta( ), setb( ), getb( ), setc( ), and getc( ) to set and get the values. Write main( ) as before. Now change the private string objects a, b, and c to a private array of string s[3]. Show that the code in main( ) does not break as a result of the change.

7. Write two classes, each of which has a member function that takes a pointer to an object of the other class. Create instances of both objects in main( ) and call the aforementioned member function in each class.

8. Create a Hen class. Inside this, nest a Nest class. Inside Nest, place an Egg class. Each class should have a display( ) member function. In main( ), create an instance of each class and call the display( ) function for each one.

9. Create a stack class and try to bundle the data part along with the functionality part. The program should have the push, pop, display.

10. For the above stack class use the overloading methodologies so that the stack works with integer, double and character types.
Note down the problems which you incur during such kind of operation.

11. Write a program in which you try to (1) Create a reference that is not initialized when it is created. (2)
Change a reference to refer to another object after it is initialized. (3) Create a NULL reference.

12. Create two classes, A and B, with default constructors that announce themselves. Inherit a new class called C from A, and create a member object of B in C, but do not create a constructor for C. Create an object of class C and observe the results.

13. Create a class with two static member functions. Inherit  from this class and redefine one of the member functions. Show that the other is hidden in the derived class.

14. Create a class called SpaceShip with a fly( ) method. Inherit Shuttle from SpaceShip and add a land( )
method. Create a new Shuttle, upcast by pointer or reference to a SpaceShip, and try to call the land( )
method. Explain the results.

15 write a simple class and create a array of objects and initialize a  array of objects  like a primitive data type.

16  write a class and  create a objects using new operator and destroy the objects using delete operator. examine weather constructor  is calling or not when you create objects using  new operator and also check destructor ,when you delete objects . using same example create reference objects to invoke members of class.

17. Write a simple class without any pointers and try to do initialization, pass by value, return by value and see weather it is going to affect any part of code.
Note. Copy constructors should not be used

18. Create a class that contains a double*. The constructor initializes the double* by calling new double and assigning a value to the resulting storage from the constructor argument. The destructor prints the value
that’s pointed to, assigns that value to -1, calls delete for the storage, and then sets the pointer to zero. Now create a function that takes an object of your class by value, and call this function in main( ). What happens? Fix the problem by writing a copy-constructor.

19. Create a simple class without a copy-constructor, and a simple function that takes an object of that class by value.Now change your class by adding a private declaration (only) for the copy-constructor. Explain what happens when your function is compiled.

20 This exercise creates the design pattern called proxy. Start with a base class Subject and give it three functions: f( ), g( ), and h( ). Now inherit a class Proxy and two classes Implementation1 and Implementation2 from Subject. Proxy should contain a pointer to a Subject, and all the member functions for Proxy should just turn around and make the same calls through the Subject pointer. The Proxy constructor takes a pointer to a Subject that is installed in the Proxy (usually by the constructor). In main( ), create two different Proxy objects that use the two different implementations. Now modify Proxy so that you can dynamically change implementations

21. Create a class and a global friend function that manipulates the private data in the class. Create one more class and using one more friend function swap the private data of obj1 of class1 with obj2 of class2

22. Create 2 classes and make a member function of class1 as friend of class2 and access the private members in class2.

23. Create three classes. The first class contains private data, and grants friendship to the entire second class and to a member function of the third class. In main( ), demonstrate that all of these work correctly.

24. Create a class containing an array of int. Can you index through this array using a pointer to member?

25. Create a simple class with an overloaded operator++. Try calling this operator in both pre- and postfix form and see what kind of compiler warning you get.

26. Write a Number class that holds a double, and add overloaded operators for +, –, *, /, and assignment.
Choose the return values for these functions so that expressions can be chained together, and for efficiency.

27. Create a class that contains a pointer, and demonstrate that if you allow the compiler to synthesize the operator= the result of using that operator will be pointers that are aliased to the same storage. Now fix the problem by defining your own operator= and demonstrate that it corrects the aliasing. Make sure you check for selfassignment and handle that case properly.

28. Attempt to create a non-member operator= for a class and see what kind of compiler message you get.

29. Implement the conversion from a basic to class by overloading "=" operator.

30. Convert from one class to another class with the help of constructor

31.Modify the program  to accept a whole object by overloading ">>" operator.

32 Templatize the fibonacci( ) function on the type of value that it produces (so it can produce long, float, etc. instead of just int).

33. Try to create a simple version of Vector template (Try to mimic the properties of STL vector). Try to implement an iterator to the Vector. **.

34. Write a program to implement bubble sort to act upon different data types.

35. Implement a Template to take user defined data type as a parameter.

36. Create a class with member functions that throw exceptions. Within this class, make a nested class to use as an exception object. It takes a single char* as its argument; this represents a description string. Create a member function that throws this exception. (State this in the function’s exception
specification.) Write a try block that calls this function and a catch clause that handles the exception by printing out its description string. **

37. Create a destructor that throws an exception, and write code to prove to
yourself that this is a bad idea by showing that if a new exception is thrown
before the handler for the existing one is reached, terminate( ) is called.

38.  Modify the program one to accept a whole object by overloading ">>" operator.

39. Create a namespace without a name. Declare some variables inside that namespace. Determine the behavior of such kind of namespace.

40. Define 2 namespace with the same name space name. What will be the behavior of such kind of namespace? What can you conclude by such kind of namespaces?

No comments:

Post a Comment

Write your openion about my blog spot..To get automatic facebook updates like my Page ..It takes only 1 min to write the comment and to like the page.. Thanks.