Posts

Showing posts from May, 2012

Inheritence

Inheritence: In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes , classes can inherit attributes and behavior from pre-existing classes called base classes , superclasses , parent classes or ancestor classes . The resulting classes are known as derived classes , subclasses or child classes . The relationships of classes through inheritance gives rise to a hierarchy . In prototype-based programming , objects can be defined directly from other objects without the need to define any classes, in which case this feature is called differential inheritance

Constructors and Destructors

Constructors and Destructors Objects generally need to initialize variables or assign dynamic memory during their process of creation to become operative and to avoid returning unexpected values during their execution. For example, what would happen if in the previous example we called the member function area() before having called function set_values() ? Probably we would have gotten an undetermined result since the members x and y would have never been assigned a value. In order to avoid that, a class can include a special function called constructor , which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void . We are going to implement CRectangle including a constructor: // example: class constructor #include <iostream.h> class CRectangle { int width, height; public : CRectangle ( int , int );//Parameterized Cons

Introduction to Classes in C++

Class : A class is an extension to the structure Data Type: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are generally declared using the keyword class , with the following Syntax: class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names;//This is optional Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers. All is very similar to the declaration of a structure, except that we can now include also functions and members, but also this new thing called access specifier . An access specifier is one of the following three keywords: private , public or prot