Hamid

   
 
  CS-72
Course Code : CS 72
Course Title : C++ and Object Oriented Programming
Assignment Number : BCA (6)-72/Assignment/ 09
Maximum Marks : 25
Last Date of Submission : 30th April, 2009/30th October, 2009

There are eight questions in this Assignment. Answer all the questions.  You may use illustrations and diagrams to enhance your explanations.  Answer to each part of the question should be confined to about 300 words.

Q.1
Explain four major advantages of Object Oriented Programming with the help of an
example each.

Ans.
Advantages of Object Oriented Programming :

I - Inharitance.

Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.

Features or Advantages of Inheritance:

*  Reusability:

    Inheritance helps the code to be reused in many situations. The base class is defined and once it is compiled, it need not be reworked. Using the concept of inheritance, the programmer can create as many derived classes from the base class as needed while adding specific features to each derived class as needed.

*  Saves Time and Effort:

    The above concept of reusability achieved by inheritance saves the programmer time and effort. Since the main code written can be reused in various situations as needed.

*  Increases Program Structure which results in greater reliability.


General Format for implementing the concept of Inheritance:
    class derived_classname: access specifier baseclassname


Inheritance Example 1:.

    class exforsys
    {
    public:
    exforsys(void) { x=0; }
    void f(int n1)
    {
    x= n1*5;
    }

    void output(void) { cout<<x; }

    private:
    int x;
    };

    class sample: public exforsys
    {
    public:
    sample(void) { s1=0; }

    void f1(int n1)
    {
    s1=n1*10;
    }

    void output(void)
    {
    exforsys::output();
    cout << s1;
    }

    private:
    int s1;
    };

    int main(void)
    {
    sample s;
    s.f(10);
    s.output();
    s.f1(20);
    s.output();
    }

The output of the above program is

    50
    200


example 2 :.
#include<iostream.h>
#include<conio.h>
class base
{
    public:
    void display()
    {
        cout<<"This is my base class.n"
    }
};
class derived : public base
{
    public:
    void demo()
    {
        cout<<"This is my derived class.n"
    }
};
void main()
{
    derived obj;
    clrscr();
    obj.display();
    obj.demo();
    getch();
}


II - Polymorphism :

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.


Below is a simple example of the above concept of polymorphism:
    6 + 10
The above refers to integer addition.
The same + operator can be used with different meanings with strings:
    "Exforsys" + "Training"
The same + operator can also be used for floating point addition:
    7.15 + 3.78


Features and Advantages of the concept of Polymorphism:
Applications are Easily Extendable:

Once an application is written using the concept of polymorphism, it can easily be extended, providing new objects that conform to the original interface. It is unnecessary to recompile original programs by adding new types. Only re-linking is necessary to exhibit the new changes along with the old application. This is the greatest achievement of C++ object-oriented programming. In programming language, there has always been a need for adding and customizing. By utilizing the concept of polymorphism, time and work effort is reduced in addition to making future maintenance easier.

    * Helps in reusability of code.
    * Provides easier maintenance of applications.
    * Helps in achieving robustness in applications.

Types of Polymorphism:
C++ provides three different types of polymorphism.
    * Virtual functions
    * Function name overloading
    * Operator overloading
In addition to the above three types of polymorphism, there exist other kinds of polymorphism:
    * run-time
    * compile-time
    * ad-hoc polymorphism
    * parametric polymorphism

example.
#include<iostream.h>
#include<conio.h>
class overloading
{
    public:
    int funt(int x)
     {
        return(x);
    }
    float fun(float x, int y)
    {
        return(x+y);
    }
    double fun(int x, float y, double z)
    {
        return(x+y+z);
    }
};
void main()
{
    overloading ol;
    clrscr();
    int a;
    float b;
    double c;
    a=ol.fun(10);
    b=ol.fun(15.24,15)
    c=ol.fun(10,40.45,1000.125)
    cout<<"integer = "<<a<<endl;
    cout<<"float = "<<b<<endl;
    cout<<"Double = "<<c<<endl;
    getch();
}
 


III. Encapsulation

Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions present inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The concept of restricted access led programmers to write specialized functions or methods for performing the operations on hidden members of the class. Attention must be paid to ensure that the class is designed properly.

Neither too much access nor too much control must be placed on the operations in order to make the class user friendly. Hiding the implementation details and providing restrictive access leads to the concept of abstract data type. Encapsulation leads to the concept of data hiding, but the concept of encapsulation must not be restricted to information hiding. Encapsulation clearly represents the ability to bundle related data and functionality within a single, autonomous entity called a class.

Features and Advantages of the concept of Encapsulation:
* Makes Maintenance of Application Easier:

    Complex and critical applications are difficult to maintain. The cost associated with maintaining the application is higher than that of developing the application properly. To resolve this maintenance difficulty, the object-oriented programming language C++ created the concept of encapsulation which bundles data and related functions together as a unit called class. Thus, making maintenance much easier on the class level.

* Improves the Understandability of the Application

* Enhanced Security:

    There are numerous reasons for the enhancement of security using the concept of Encapsulation in C++. The access specifier acts as the key strength behind the concept of security and provides access to members of class as needed by users. This prevents unauthorized access. If an application needs to be extended or customized in later stages of development, the task of adding new functions becomes easier without breaking existing code or applications, there by giving an additional security to existing application.

In order to benefit from the powerful feature of encapsulation in object-oriented programming language C++, the programmer must use encapsulation properly. To maximize the benefits of encapsulation, the user must minimize the implementation details in external interfaces as needed.

Example :
For example, a class called LoggerFile might bundle data members such as the log file's path; a buffer of data waiting to be written to that file; and member functions that open, read, write, and close that file, as in the following code listing:

class LogFile
{
public:
 int Open();
 int Read(char *buff);
 int Write(const char *buff);
 int Close();
private:
 FILE *logfile;
 bool _open;
//...additional data members
};


IV - Abstraction :

Abstraction is one of the most powerful and vital features provided by object-oriented C++ programming language. Modularity is very important in any programming language, it provides flexibility to users for using the programming language. This aspect is well achieved with high performance by the concept of abstraction in C++. In object-oriented programming language the programmer can abstract both data and code when needed.

The concept of abstraction relates to the idea of hiding data that are not needed for presentation. The main idea behind data abstraction is to give a clear separation between properties of data type and the associated implementation details. This separation is achieved in order that the properties of the abstract data type are visible to the user interface and the implementation details are hidden. Thus, abstraction forms the basic platform for the creation of user-defined data types called objects. Data abstraction is the process of refining data to its essential form. An Abstract Data Type is defined as a data type that is defined in terms of the operations that it supports and not in terms of its structure or implementation.

In object-oriented programming language C++, it is possible to create and provide an interface that accesses only certain elements of data types. The programmer can decide which user to give or grant access to and hide the other details. This concept is called data hiding which is similar in concept to data abstraction.

Advantages of Abstraction
Flexibility in approach::
By hiding data or abstracting details that are not needed for presentation, the programmer achieves greater flexibility in approach.

Enhanced Security::
Abstraction gives access to data or details that are needed by users and hide the implementation details, giving enhanced security to application.

Easier Replacement:
With the concept of abstraction in object-oriented programming language, it is possible to replace code without recompilation. This makes the process easier and saves time for users.

Modular Approach:
In object-oriented programming language C++, the abstraction concept helps users to divide the project application into modules and test each of them separately. Then all modules are integrated and ultimately tested together. This approach makes the application development easier.


Example : Class :
class stack
{
 public:
  void init(); //initialise stack
  void push(int elem);
  int pop();
  int isempty(); //returns 0 if not
 private:
  int store[100];
  int *tos; //stack top pointer
};


Q. 2
Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each.
(a)    Single Inheritance
(b)    Abstraction
(c)    Encapsulation
(d)    Function Overloading

(a)    Single Inheritance
     
A single inheritance have or else it will be created by a single base class or single derived class
      Or
      A Single derived class created from a base class.

Syntax :
class base
{
 
}
Class drived : visibility_scope base
{

}

Example :
#include<iostream.h>
#include<conio.h>
class base
{
  public:
  void display()
  {
    cout<<"This is my base class"<<endl;
  }
};
class drived : pulic base
{
  public:
  void demo()
  {
    cout<<"This is my drived class."<<endl;
  }
};
void main()
{
  drived obj;
  clrscr();
  obj.display();
  obj.demo();
  getch();
}

(b)    Abstraction



(c)    Encapsulation




(d)    Function Overloading

    Function overloading is the practice of declaring the same function with different signatures. The same function name will be used with different number of parameters and parameters of different type. But overloading of functions with different return types are not allowed.

   For example let us assume an AddAndDisplay function with different types of parameters.

   //Sample code for function overloading
    void AddAndDisplay(int x, int y)
    {
        cout<<" C++ Tutorial - Integer result: "<<(x+y);
    }

    void AddAndDisplay(double x, double y)
    {
        cout<< " C++ Tutorial - Double result: "<<(x+y);
    }

    void AddAndDisplay(float x, float y)
    {
        cout<< " C++ Tutorial - float result: "<<(x+y);

    }


Q. 3
Explain the concept of late binding and dynamic binding with the help of at least two examples.  Are these concepts related to polymorphism? Explain your answer.







Q. 4
What is the need of UML? Explain the use of any two diagrams used in UML with the help of an example of each.







Q. 5
Explain the usage of the following C++ operators with the help of an example program.
(a)    sizeof operator
(b)    bitwise AND operator
(c)    ++ as post increment operator
(d)    Dereferencing operator







Q. 6
Write a function swap that exchanges the values of two variables using a third variable, by using the:
(a)    Pointers
(b)    & (address of) operator
Explain the two functions that you have written.







Q. 7
Create a class hierarchy for the Worker and Supervisor using C++. The Worker class should include details like worker ID, Name, address of the worker, department ID and Supervisor ID. The classes should have necessary functions to input the required information and a polymorphic function to print the details of the workers or supervisors. For the worker’s this print function should output the worker ID, Name and Supervisor ID. Whereas for the supervisor’s this function should out put the Name and department ID of the supervisor. Please note that a supervisor is also a worker. Make and state suitable assumptions, if any.







Q.8
Write a template class “stack” in C++. The class should have functions for push, pop, stack full and stack empty. Use this template to create a stack of integer and a stack of character. Make suitable assumptions, if any.







Q.9
Create a class IntStr that stores a positive decimal integer as a string (the maximum number of decimal digits in the string can be assumed to be only 5 digits, so the maximum value that can be stored is 99999, whereas minimum value that can be stored is 00000.). The class should have a copy constructor, and an overloaded + operator. The overloaded + operator should perform the addition. On addition using this + operator, if the length of result exceeds 5 digit then the class should give the result as 00000. Write the necessary main() function to demonstrate the use of the class.
Login
 
Username:
Password:
Donate
 
You can help this website
by donate or
you can click an
advertisement.

IGNOU Students
 
Sample Synopsis

Time Table BCA 2009
Projects (Vb n ASPnet) : Download
Synopsis Form

Training Letter

Guide Remuneration Form

Certificates of Originality

Java KeyWords - PDF

Java KeyWords - Wikipedia


Click Here / Click Here

Edit Plus (Use it as Java editor) Download

How to configure EditPlus to compile JAVA codes click here


Advertisement
 
PC Games (Full, Rip, Compressed)
 
 
© 2007 - 2009 HamidRaza - Today, there have been 6 visitors (20 hits) on this page!
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free