Header

Thursday 5 September 2013

IGNOU BCA 3rd sem Solved Assignment - 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) Abstraction (b) Encapsulation (c) Operator Overloading (d) Static Member

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)                Abstraction
(b)               Encapsulation
(c)                Operator Overloading
(d)               Static Member

Ans
C++ 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.

What is Abstraction
The concept of abstraction relates to the idea of hiding data that is 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.
Encapsulation in C++
Encapsulation is one of the major cores of OOP. It is the mechanism that binds code and data together and keeps them safe and away from outside inference and misuse. In short, it isolates a data from outside world. Encapsulation can be called as the core concept of OOP because it is encapsulation that reduces the maintenance burden, and limiting your exposure to vulnerabilities. The programmer has to design a well-defined interface to control the access of a particular code and data. A class defines the data and code which is shared by a set of objects. A class is a logical construct and an object is a physical construct which exists in reality that is like a chair or a table. A class comprises of code and data which are collectively called members of the class

term Operator Overloading in c++
In C++ the overloading principle applies not only to functions, but to operators too. That is, of operators can be extended to work not just with built-in types but also classes. A programmer can provide his or her own operator to a class by overloading the built-in operator to perform some specific computation when the operator is used on objects of that class. Is operator overloading really useful in real world implementations? It certainlly can be, making it very easy to write code that feels natural (we'll see some examples soon). On the other hand, operator overloading, like any advanced C++ feature, makes the language more complicated. In addition, operators tend to have very specific meaning, and most programmers don't expect operators to do a lot of work, so overloading operators can be abused to make code unreadable. But we won't do that. 
An Example of Operator Overloading
Complex a(1.2,1.3);     //this class is used to represent complex numbers
Complex b(2.1,3);       //notice the construction taking 2 parameters for the real and imaginary part
Complex c = a+b;        //for this to work the addition operator must be overloaded
The addition without having overloaded operator + could look like this
Complex c = a.Add(b);
This piece of code is not as readable as the first example though--we're dealing with numbers, so doing addition should be natural. (In contrast to cases when programmers abuse this technique, when the concept represented by the class is not related to the operator--ike using + and - to add and remove elements from a data structure. In this cases operator overloading is a bad idea, creating confusion.

term Static Member in c++
I've run across the term Class Static Object a number of times without code or context to clarify if this is the author's term for something I don't know or an just another term forclass (scoped) static member (variable or function).
class m1 {
public:
    static int x;
};

// with x being the "class static "object"
// which I just call a "class static member".
int m1::x;
Or, does the term only apply to member variables that are actually user defined objects?
class m2 {
public:
    int y;
};

class m3 {
public:
    static m2 z;
};

// with z really being an object - the "class static object"?
m2 m3::z;
Because static member functions are not attached to a particular object, they can be called directly by using the class name and the scope operator. Like static member variables, they can also be called through objects of the class type, though this is not recommended.
Static member functions have two interesting quirks worth noting. First, because static member functions are not attached to an object, they have no this pointer! This makes sense when you think about it — the this pointer always points to the object that the member function is working on. Static member functions do not work on an object, so the this pointer is not needed.
Second, static member functions can only access static member variables. They can not access non-static member variables. This is because non-static member variables must belong to a class object, and static member functions have no class object to work with

No comments:

Post a Comment