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.




Comments on this page:
Comment posted by a80yjv( syluwhgmail.com ), 06/21/2017 at 10:03pm (UTC):
<a href=http://invelevoria.com>invelevoria</a>
j7ncirla8mph5elcp6

Comment posted by Davpoicync( posmentry-rx.com ), 06/11/2017 at 9:29pm (UTC):
Viagra Ohne Rezept Berlin <a href=http://bestlevi.com/buy-levitra-20mg-usa.php>Buy Levitra 20mg Usa</a> Productos Con Finasteride Comprar Propecia Amoxicillin And Vitamin <a href=http://low-cost-generic-kamagra.kamagorder.com>Low Cost Generic Kamagra</a> Vendo Cialis Original Y Generico Potenzmittel Viagra Zink <a href=http://leviprix.com/low-price-levitra.php>Low Price Levitra</a> Viagra Italiano Clomid Provames <a href=http://cheapcial.com/cialis-online-pharmacy.php>Cialis Online Pharmacy</a> Sale Discount Bentyl Real Kamagra Mg <a href=http://levicost.com>cheapest brand name levitra</a> Acheter Viagra Montreal Priligy Generico En Venezuela <a href=http://cial40mg.com/cheap-cialis-40mg.php>Cheap Cialis 40mg</a> Compra Segura Viagra Zithromax Where Can I Buy <a href=http://comprar-propecia-online.propecpills.com>Comprar Propecia Online</a> Cialis 20 Mg Preis Zithromax Abscess Tooth <a href=http://dprixe.com>viagra</a> Generic Valtrex Overnight Delivery Viagra Tarif Pharmacie <a href=http://cialbuy.com>online pharmacy</a> Cheap 80 Mg Vardenafil Hcl Vendita Viagra Cialis <a href=http://bestlevi.com/vardenafil.php>Vardenafil</a> Prix Cialis 20 Dflucan Without A Pres <a href=http://cialcheap.com/cialis-prices.php>Cialis Prices</a> Amoxil Dosing Chart Dutasteride Duagen Bph Want To Buy In Internet <a href=http://propecia-where-to-buy.propecpills.com>Propecia Where To Buy</a> Online Pharmacy Thailand Cialis No Rx <a href=http://cheap-cialis-and-viagra.BuyCial.com>Cheap Cialis And Viagra</a> Baclofene Intrathecal Achete Viagra <a href=http://costofvia.com/fast-shipping-viagra.php>Fast Shipping Viagra</a> Comprar Cialis Andorra Forum Viagra Pour Fille <a href=http://dprixe.com>viagra</a> Remeron Medication From Canada <a href=http://leviplus.com/levitra-low-cost.php>Levitra Low Cost</a> Buy Valtrex Canada Online Buy Propecia 1mg <a href=http://viacheap.com>viagra</a> Baclofene Angers Which Country Has Cheap Doxycycline <a href=http://staminamen.com/buy-tadalafil.php>Buy Tadalafil</a> Acheter Baclofen Des

Comment posted by WilbSwateRef( milotry-rx.com ), 06/04/2017 at 6:30pm (UTC):
Sildenafil Tablets 100mg <a href=http://cheap-cheap-viagra.viapill.com>Cheap Cheap Viagra</a> Cheapest Viagra Prices Licensed Pharmacies Viagra E Varicocele <a href=http://purchase-kamagra.kamagpills.com>Purchase Kamagra</a> Cialis E Viagra Effetti Collaterali Keflex Dosin <a href=http://cialusa.com>cialis</a> Isotretinoin izotek from canada direct low price overseas Buy Amoxicillin Uk No Prescription <a href=http://cialbuy.com>cialis</a> Cialis Cura Disfunzione Erettile Orlistat 120mg <a href=http://priligy-buy.priliorder.com>Priligy Buy</a> buy generic accutane uk Uk Buy Albenza Oral <a href=http://propecia-pharmacy.propecpills.com>Propecia Pharmacy</a> Amoxicillin Clav Er Fedex Shipping Isotretinoin Tablets Low Price <a href=http://fast-shipping-viagra.via100mg.com>Fast Shipping Viagra</a> Where Can I Buy Thyroxine In N.Z Where Cam I Buy Proponalol <a href=http://newisotretinoin.com/buy-accutane-online.php>Buy Accutane Online</a> Cheap Viagra Us Companies Only Renacidin Propecia <a href=http://viacheap.com>viagra</a> Availability Of Cafergot Amoxicillin Generic <a href=http://natural-propecia.propecorder.com>Natural Propecia</a> Cheap Viagra 100mg To Buy In Canada Prix Cialis 10 Mg <a href=http://bestlevi.com/levitra-buy.php>Levitra Buy</a> Acquista Cialis Generic Viagra Cheap Free Shipping <a href=http://sildenafil-citrate.viapill.com>Sildenafil Citrate</a> Cual Es La Mejor Viagra Primolut To Buy Online <a href=http://viacheap.com>viagra</a> Purchasing Generic Propecia Allopurinol 300 Mg Australia <a href=http://brandcial.com/cialis-professional.php>Cialis Professional</a> Methotrexate And Amoxicillin Interactions Cialis Generico In Italia <a href=http://buylevitraeufast.com/map.php>Levitra Vardenafil</a> Stendra Vs Viagra Buy Plavix Cheap <a href=http://brandcial.com/cialis-online-pharmacy.php>Cialis Online Pharmacy</a> Cephalexin For Oral Suspension Kamagra Oral Jelly Pictures <a href=http://prices-cialis.cial5mg.com>Prices Cialis</a> Canadian Meds Cialis 5mg No Prescription <a href=http://levipill.com/affordable-levitra.php>Affordable Levitra</a> Canadain Drugs Venta Kamagra El Mundo <a href=http://buy-cheap-priligy-online-uk.priliorder.com>Buy Cheap Priligy Online Uk</a> Cialis Wirkung Dosis Cheap Progesterone No Physician Approval Website <a href=http://bestlevi.com/levitra-price.php>Levitra Price</a> Lasix Deshydratation Cialis Langzeittherapie <a href=http://brandcial.com/cialis-online-prices.php>Cialis Online Prices</a> Buy Doxycycline Forum Order Prescription From Canada <a href=http://costofvia.com/viagra-online-usa.php>Viagra Online Usa</a> Buy Amlodipine Without Prescription Amoxicillin 500mg Capsule <a href=http://cheap-propecia-for-sale.propecorder.com>Cheap Propecia For Sale</a> Canada Healthcare Mall Where Can I Order 20 Gm Retin A Tubes <a href=http://viagra-alternative.via100mg.com>Viagra Alternative</a> Danni Propecia



Add comment to this page:
Your Name:
Your Email address:
Your website URL:
Your message:

Login
 
Username:
Password:
Advertisement
 
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 229 visitors (1436 hits) on this page!
=> Do you also want a homepage for free? Then click here! <=