Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   The three major concepts of C++ (http://www.go4expert.com/articles/major-concepts-cpp-t2933/)

Sanskruti 9Feb2007 19:00

The three major concepts of C++
 
Three major concepts of C++ are Classes, Polymorphism, Inheritance.Let us discuss them in details.

Classes



A class is the implementation of an abstract data type. It defines attributes and methods which implement the data structure and operations of the abstract data type, respectively. Instances of classes are called objects. Consequently, classes define properties and behaviour of sets of objects.

Constructors



A constructor initializes an object. A default constructor is one that can be invoked without any arguments. If there is no user-declared constructor for a class, and if the class doesn't contain const or reference data members, C++ implicitly declares a default constructor for it. Such an implicitly declared default constructor performs the initialization operations needed to create an object of this type. Note, however, that these operations don't involve initialization of user-declared data members.

For example:

Code: CPP

class C
{
private:
 int n;
 char *p;
public:
 C() : n(0), p(0) {}
 virtual ~C() {}
};

C obj; // 1 user-defined constructor is invoked
 

Now the data members of the object obj are initialized because the user-defined constructor was invoked to create it. The user-defined constructor only initializes the data members n and p. When did the initialization of the virtual pointer take place? Here's the answer: The compiler arguments the user-defined constructor with additional code, which is inserted into the constructor's body before any user-written code, and performs the necessary initialization of the virtual pointer.

Destructors



A destructor destroys an object of its class type. It takes no arguments and has no return type (not even void). const and volatile qualities are not applied on an object under destruction; therefore, const, volatile, or const volatile objects may have a destructor. If no user-defined destructor exists for a class, the implementation implicitly declares one. A destructor is trivial if it's implicitly declared and if its direct-base classes and embedded objects have trivial destructors. Otherwise, the destructor is nontrivial. A destructor invokes the destructors of the direct base classes and member objects of its class. The invocation occurs in the reverse order of their construction.

Polymorphism



This ability of different objects to respond, each in its own way, to identical messages is called polymorphism. Polymorphism results from the fact that every class lives in its own name space. The names assigned within a class definition won't conflict with names assigned anywhere outside it. This is true both of the instance variables in an object's data structure and of the object's methods:

Just as the fields of a C structure are in a protected name space, so are an object's instance variables.
Method names are also protected. Unlike the names of C functions, method names aren't global symbols. The name of a method in one class can't conflict with method names in other classes; two very different classes could implement identically named methods.

Method names are part of an object's interface. When a message is sent requesting an object to do something, the message names the method the object should perform. Because different objects can have different methods with the same name, the meaning of a message must be understood relative to the particular object that receives the message.

The same message sent to two different objects could invoke two different methods.

The main benefit of polymorphism is that it simplifies the programming interface. It permits conventions to be established that can be reused in class after class. Instead of inventing a new name for each new function you add to a program, the same names can be reused. The programming interface can be described as a set of abstract behaviors, quite apart from the classes that implement them.

Inheritance



The easiest way to explain something new is to start with something old. The same is true if want to define a new kind of object; the description is simpler if it can start from the definition of an existing object.

With this in mind, object-oriented programming languages permit you to base a new class definition on a class already defined. The base class is called a superclass; the new class is its subclass. The subclass definition specifies only how it differs from the superclass; everything else is taken to be the same.

Nothing is copied from superclass to subclass. Instead, the two classes are connected so that the subclass inherits all the methods and instance variables of its superclass, If the subclass definition were empty , the two classes would be identical and share the same definition. It would be like explaining what a ``fiddle'' is by saying that it's exactly the same as a "violin". However, the reason for declaring a subclass isn't to generate synonyms, but to create something at least a little different from its superclass.

The easiest way to explain something new is to start with something old. The same is true if want to define a new kind of object; the description is simpler if it can start from the definition of an existing object.

rahul.mca2001 6Mar2008 13:43

Re: The three major concepts of C++
 
nice explanation


All times are GMT +5.5. The time now is 02:44.