Friday, September 18, 2009

Object Oriented Programming

Struct is use to define data types and group them into one object. Struct does not contain methods or functions, and its data members are all public. Struct are useful when we want sets of element with different data types.  Struct is a group of data elements grouped under one name. Those data elements are know as members and can have differnet type and different lengths.  Struct creates a new type. Once a struct is created a new type with the name of struct is created. It can be used later on in the program to create objects of struct.

A Class have both data members and functions/methods associated with it. A class can contain sever variables and functions/methods, those are called members of class. Bydefault all its members are private.

Class can define its member as private, protected, public.  Protected has a special meaning to inheritance. Protected members are accessible in the class that defines them as well as in the classes that inherit from that base class or friend of it.
 Class can contain special member functions called as constructors or destructors. Constructors cannot be explicitly called like regular member functions. They are automatically executed when new object of class is created. 

In principal, private and protected members of a class cannot be accessed from outside the scope of the same class in which they are declared.  However this rule does not affect friends. Friendship is no transitive and it is not bidirectional.

Inheritance is a key feature of C++ class. It allows to create a class which is derived from other classes, so it can automatically include/inherit members of base class.  When class inhertis from another class the members of derived class can access only the public/protected members of  base class, but cannot access private members of base class.
In principal, a derived class inherits every member of base class except its constructor, destructor, and its friends.

Virtual function/member: A member of class can be redefined in its derived classes is know as a virtual member.
When the type of pointer is pointer to base class but it is pointing to an object of th derived class, virtual keyword in-front of member functions allows a member function of a derived class with the same name as one in base class to be called from base class pointer to derived class object.

A class that declares or inherits a virtual function/member  is called a polymorphic class.

Abstract base class is a class that lacks implementation of atleast one member. Therefore, we cannot create object of that class (cannot create object of ABC - Abstract Base Class). These are the main differences between abstract class and a regular polymorphic class.   The function which lacks implementation is called as pure virtual function. for example, virtual int function_name() = 0; However, pointers of ABC (Abstract Base Class) can be used to point to objects of derived classes.

 Templates are special types that can operate with generic types. Templates allows us to create a function template whose functionality can be adapted to more than one type without repeating the entire code. That is, we don't need to write the same functionality code for different types such as int, float, char, etc. This allows generic programming, bocz it access any type object.

No comments:

Post a Comment