Thursday, September 15, 2011

C++ Classes incliding Structures-Classes-Unions

Introduction to C++ Classes

Classes (also known as composite data types) are a collection of different data types grouped together to form one new self-defined data type which can be categorized in form of a structure, class or union. The variable types we have used so far have been primitive types because they are declared in a very simplified manner. Each variable represented an object in its whole ( i.e int car) but at times we want a group of values to be considered as one entity to represent an object (car).

For example, we may need to use one or more data types to represent the car object. In such a situation we can group the different data types into one data type of our liking and then use it the same way we have used other data types. In C++, there are three techniques of defining a new self-defined data type: a structure, a class, and a union.

Structures

A structure is an aggregate of data types that together form new self-defined data type. It is built using several types including the structs. We create a structure using a the struct keyword, followed by a name for the object and the body of the structure with a semicolon at the end. The naming of the struct must follow conventional rules of names in C++. To create a composite type at least one known data type should be used to declare a variable in the body of the structure. Below is an example of a car structure.Read more on C++ Classes

Understanding C++ Operator Overloading and User Defined Class Objects

In understanding c++ operator overloading we begin by defining the Concept. In C++ operator overloading means ‘providing different functionality to an operator under a different environment. Majority of the C++ built-in operators are already overloaded. Like functions, operators take operands (arguments) and return a value. operator overloading can be done on many operators for them to act in a specific manner on the argument or arguments and return a value.

To have a better understanding of c++ operator oveloading, let us look at this example. The plus (+) operator is overloaded to add two integers, increment an integer by one and concatenate a string. Likewise a minus (-) operator is overloaded to subtract two integers, and decrement an integer by one. Like the + and –, C++ provides built-in overloaded definitions for most operators to perform various functionalities directly but only on built-in types. However, C++ provides abilities for a programmer to overload built-in operators to act on user-defined types in a user-defined manner. Read the details onunderstanding C++ Operator Overloading

Introduction to C++ class inheritance

In C++, inheritance is a technique in which a C++ class known as the derived class absorbs, utilizes and enhances the data and behavior of another class known the base class . C++ inheritance enhances object-oriented programming in a way that it saves program development time, and supports reuse of debugged high quality software. In c++ inheritance allows an object oriented programmer to focus on the larger picture rather than specific cases. Because different classes sometimes require similar data members, inheritance enables avoidance of duplication of codes in the same project environment. The base class captures the most common members and then supplies them to the derived classes with the help of inheritance technique. Details