Search This Blog

Friday, December 10, 2010

A very first program in c++ with the concept of class

include
using namespace std;

class person
{
private:
   char name[20];
   int age;

public:
    void getdata(void);
    void dislpay(void);
};
void person :: getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void person :: display(void)
{
cout<<"\nName: "<
cout<<"\nAge: "<
}
int main()
{
  person p;

  p.getdata();
  p.display();

  return 0;
}

Differences between object-oriented programming (OOP) and procedure-oriented programming (POP)



                          POP
                             OOP

1. Emphasis on doing things (procedure).

1. Emphasis on data rather than procedure.
2. Programs are divided into what are            known as functions.
2. Programs are divided into what are known as objects.
3. Data move openly around the system from function to function.
3. Data is hidden and cannot be access by external functions.
4. Employs top down approach in the program design.
4. Employs bottom up approach in program design.

Monday, December 6, 2010

Copy constructor

Copy constructor is a type of constructor which constructs an object by copying the state from another object of the same class. Whenever an object is copied, another object is created and in this process the copy constructor gets called. If the class of the object being copied is x, the copy constructor’s signature is usually x::x (const x&).
Let’s take an example to illustrate this:
class Complex
{
int real, img;
public:
Complex (int, int);
Complex (const Complex& source); //copy constructor
};
Complex:: Complex (const Complex& source)
{
this.real = source.real;
this.img = source.img;
}
main ()
{
Complex a (2, 3);
Complex b = a; // this invokes the copy constructor
}
A copy constructor is called whenever an object is passed by value, returned by value or explicitly copied.

Saturday, November 27, 2010

enum Types in C++



Introduction

In C and C++, enum types can be used to set up collections of named integer constants. (The keyword enum is short for ``enumerated''.)The traditional C way of doing this was something like this:
#define SPRING   0
#define SUMMER   1
#define FALL     2
#define WINTER   3
An alternate approach using enum would be
enum { SPRING, SUMMER, FALL, WINTER };

enum declarations

There are two kinds of enum type declarations. One kind creates a named type, as in
enum MyEnumType { ALPHA, BETA, GAMMA };
If you give an enum type a name, you can use that type for variables, function arguments and return values, and so on:
enum MyEnumType x;  /* legal in both C and C++ */
MyEnumType y;       // legal only in C++
The other kind creates an unnamed type. This is used when you want names for constants but don't plan to use the type to declare variables, function arguments, etc. For example, you can writeenum { HOMER, MARGE, BART, LISA, MAGGIE };

Values of enum constants

If you don't specify values for enum constants, the values start at zero and increase by one with each move down the list. For example, given
enum MyEnumType { ALPHA, BETA, GAMMA };
ALPHA has a value of 0, BETA has a value of 1, and GAMMA has a value of 2.If you want, you may provide explicit values for enum constants, as in
enum FooSize { SMALL = 10, MEDIUM = 100, LARGE = 1000 };

C++ enum type conversion rules

These rules apply to C++ only. The rules for C are different.There is an implicit conversion from any enum type to int. Suppose this type exists:
enum MyEnumType { ALPHA, BETA, GAMMA };
Then the following lines are legal:
int i = BETA;      // give i a value of 1
int j = 3 + GAMMA; // give j a value of 5
On the other hand, there is not an implicit conversion from int to an enum type:
MyEnumType x = 2;    // should NOT be allowed by compiler
MyEnumType y = 123;  // should NOT be allowed by compiler 
Note that it doesn't matter whether the int matches one of the constants of the enum type; the type conversion is always illegal. 

Saturday, November 6, 2010

Destructor

The symmetric operation is the destruction of objects. This is required as soon
as the object dynamically allocates other objects.
The special method defined to do that is called the destructor, and is called as
soon as the compiler need to deallocate an instance of the class. There is only
one destructor per class, which return no value, and has no parameter. The
name of the destructor is the class name prefixed with a ~.
We can now re-write our matrix class :

class Matrix {
int width, height;
double *data;
public:
Matrix(int w, int h) {
width = w; height = h;
data = new double[width * height];
}
~Matrix() { delete[] data; }
double getValue(int i, int j) {
if((i<0) || (i>=width) || (j<0) || (j>=height)) abort();
return data[i + width*j];
}
void setValue(int i, int j, double x) {
if((i<0) || (i>=width) || (j<0) || (j>=height)) abort();
data[i + width*j] = x;
}
};

Constructors

The C++ syntax defines a set of special methods called constructors. Those
methods have the same name as the class itself, and do not return results. They
are called when the variable of that type is defined :

#include
#include
class NormalizedVector
{
double x, y;
public:
NormalizedVector(double a, double b) {
double d = sqrt(a*a + b*b);
x = a/d;
y = b/d;
}
double getX() { return x; }
double getY() { return y; }
};
int main(int argc, char **argv) {
NormalizedVector v(23.0, -45.0);
cout << v.getX() << ’ ’ << v.getY() << ’\n’;
NormalizedVector *w;
w = new NormalizedVector(0.0, 5.0);
cout << w->getX() << ’ ’ << w->getY() << ’\n’;
delete w;
};

The same class can have many constructors :
#include
#include
class NormalizedVector {
double x, y;
public:
NormalizedVector(double theta) {
x = cos(theta);

y = sin(theta);
}
NormalizedVector(double a, double b) {
double d = sqrt(a*a + b*b);
x = a/d;
y = b/d;
}
double getX() { return x; }
double getY() { return y; }
};

Wednesday, November 3, 2010

Compatibility of C and C++


The C and C++ programming languages are closely related. C++ grew out of C, as it was designed to be source-and-link compatible with C. Due to this, C code is often developed with C++ IDEs, integrated with C++ code, and compiled in C++ compilers. While most C source code will compile as C++ code without any changes, certain language differences prevent C++ from being a strict superset of C.
Likewise, C++ introduces many features that are not available in C and in practice almost all code written in C++ is not conforming C code. This article, however, focuses on differences that cause conforming C code to be ill-formed C++ code, or to be conforming/well-formed in both languages but to behave differently in C and C++.
Bjarne Stroustrup, the creator of C++, has suggested that the incompatibilities between C and C++ should be reduced as much as possible in order to maximize inter-operability between the two languages. Others have argued that since C and C++ are two different languages, compatibility between them is useful but not vital; according to this camp, efforts to reduce incompatibility should not hinder attempts to improve each language in isolation. The official rationale for the 1999 C standard (C99) "endorse[d] the principle of maintaining the largest common subset" between C and C++ "while maintaining a distinction between them and allowing them to evolve separately," and stated that the authors were "content to let C++ be the big and ambitious language."
Several additions of C99 are not supported in C++ or conflict with C++ features, such as variadic macros, compound literals, designated initializers, variable-length arrays, and native complex-number types. The long long int datatype and restrict qualifier defined in C99 are not included in the current C++ standard, but some compilers such as the GNU Compiler Collection provide them as an extension. The long long datatype along with variadic templates, with which some functionality of variadic macros can be achieved, will be in the next C++ standard, C++0x. On the other hand, C99 has reduced some other incompatibilities by incorporating C++ features such as // comments and mixed declarations and code.

Constructs valid in C but not C++

One commonly encountered difference is that C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; this idiomappears often in C code using malloc memory allocation. For example, the following is valid in C but not C++:
void* ptr;
int *i = ptr;       /* Implicit conversion from void* to int* */
or similarly:
int *j = malloc(sizeof(int) * 5);     /* Implicit conversion from void* to int* */
In order to make the code compile in both C and C++, one must use an explicit cast:
void* ptr;
int *i = (int *) ptr;
int *j = (int *) malloc(sizeof(int) * 5);
Another portability issue from C to C++ are the numerous additional keywords that C++ introduced. This makes C code that uses them as identifiers invalid in C++. For example:
struct template 
{
    int new;
    struct template* class;
};
is valid C code, but is rejected by a C++ compiler, since the keywords "template", "new" and "class" are reserved.
C++ compilers prohibit using goto or switch from crossing an initialization, as in the following C99 code:
void fn(void)
{
  goto flack;
  int i = 1;
flack:
  ;
}
There are many other C syntaxes which are invalid or behave differently in C++ :
  • The comma operator can result in an "l-value" (a quantity that can be used for the left-hand side of an assignment) in C++, but not in C.
  • C does not allow a given typedef to be duplicated in the same scope, whereas C++ allows repeated typedefs.
  • Enumeration constants (enum values) are always of type int in C, whereas they are distinct types in C++ and may have size different from that of int.
  • C++ identifiers are not allowed to contain two or more consecutive underscores in any position. C identifiers are not allowed to start with two or more consecutive underscores, but may contain them in other positions.
  • C++ also changes some C standard library functions to add additional const qualifiers, e.g. strchr returns char* in C and const char* in C++.
  • In both C and C++ one can define nested struct types, but the scope is interpreted differently (in C++, a nested struct is defined only within the scope/namespace of the outer struct).
  • Non-prototype ("K&R"-style) function declarations are not allowed in C++, although they have also been deprecated in C since 1990. Similarly, implicit function declarations (using functions that have not been declared) are not allowed in C++, but have also been deprecated in C since 1999.
  • C allows structunion, and enum types to be declared in function prototypes, whereas C++ does not.
  • structunion, or enum declaration in C++ usually implies an implicit typedef of the same name, while in C it does not.
  • In C, a function prototype without arguments, e.g. int foo();, implies that the parameters are unspecified. Therefore it is legal to call such a function with one or more arguments, e.g. foo(42, "hello world"). In contrast, in C++ a function prototype without arguments means that the function takes no arguments, and calling such a function with arguments is ill-formed. In C, the correct way to declare a function that takes no arguments is by using 'void', as in int foo(void);.


Constructs that behave differently in C and C++

There are a few syntactical constructs that are valid in both C and C++, but produce different results in the two languages.
For example, character literals such as 'a' are of type int in C and of type char in C++, which means that sizeof 'a' will generally give different results in the two languages: in C++ it will be 1in C it will be sizeof(int) which on architectures with 8 bit wide char will be at least 2. As another consequence of this type difference, in C 'a' will always be a signed expression, regardless of whether or not char is a signed or unsigned type, whereas for C++ this is compiler implementation specific.
The static keyword is used in C to restrict a function or global variable to file scope (internal linkage). This is also valid in C++, although C++ deprecates this usage in favor of anonymous namespaces (which are not available in C). Also, C++ implicitly treats any const global as file scope unless it is explicitly declared extern, unlike C in which extern is the default. Conversely,inline functions in C are of file scope whereas they have external linkage by default in C++.
Several of the other differences from the previous section can also be exploited to create code that compiles in both languages but behaves differently. For example, the following function will return different values in C and C++:
extern int T;
 
int size(void)
{
    struct T {  int i;  int j;  };
 
    return sizeof(T);
    /* C:   return sizeof(int)
     * C++: return sizeof(struct T)
     */
}
This is due to C requiring struct in front of structure tags (and so sizeof(T) refers to the variable), but C++ allowing it to be omitted (and so sizeof(T) refers to the implicit typedef). Beware that the outcome is different when the extern declaration is placed inside the function: then the presence of an identifier with same name in the function scope inhibits the implicit typedef to take effect for C++, and the outcome for C and C++ would be the same. Observe also that the ambiguity in the example above is due to the use of the parenthesis with the sizeof operator. Using sizeof T would expect T to be an expression and not a type, and thus the example would not compile with C++.
Both C99 and C++ have a boolean type bool with constants true and false, but they behave differently. In C++, bool is a built-in type and a reserved keyword. In C99, a new keyword, _Bool, is introduced as the new boolean type. In many aspects, it behaves much like an unsigned int, but conversions from other integer types or pointers always constrained to 0 and 1. Other than for other unsigned types, and as one would expect for a boolean type, such a conversion is 0 if and only if the expression in question evaluates to 0 and it is 1 in all other cases. The header stdbool.hprovides macros booltrue and false that are defined as _Bool1 and 0, respectively.


Linking C and C++ code

While C and C++ maintain a large degree of source compatibility, the object files their respective compilers produce can have important differences that manifest themselves when intermixing C and C++ code. Notably:
  • C compilers do not name mangle symbols in the way that C++ compilers do.
  • Depending on the compiler and architecture, it also may be the case that calling conventions differ between the two languages.
For these reasons, for C++ code to call a C function foo(), the C++ code must prototype foo() with extern "C". Likewise, for C code to call a C++ function bar(), the C++ code for bar() must be declared with extern "C".
A common practice for header files to maintain both C and C++ compatibility is to make its declaration be extern "C" for the scope of the header:
/* Header file foo.h */
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
 
/* These functions get C linkage */
void foo();
 
struct bar { /* ... */ };
 
#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif
Differences between C and C++ linkage and calling conventions can also have subtle implications for code that uses function pointers. Some compilers will produce non-working code if a function pointer declared extern "C" points to a C++ function that is not declared extern "C".
For example, the following code:
void my_function();
extern "C" void foo(void (*fn_ptr)(void));
 
void bar()
{
   foo(my_function);
}
Using Sun Microsystems' C++ compiler, this produces the following warning:
$ CC -c test.cc
"test.cc", line 6: Warning (Anachronism): Formal argument fn_ptr of type
extern "C" void(*)() in call to foo(extern "C" void(*)()) is being passed
void(*)().
This is because my_function() is not declared with C linkage and calling conventions, but is being passed to the C function foo().

Saturday, September 18, 2010

Overloading the [ ] subscript operator

The last operator that we will overload is the [ ] array subscript operator. In C++, the [ ] is considered a binary operator for the overloading purposes. The [ ] can be overloaded only by a member function. Therefore the general form of a member operator[ ]( ) function is as shown here
type class-name::operator[ ](int index)
{
// body ...
}
Technically, the parameter does not have to be of type int, but operator[ ]( ) function is typically used to provide array subscript and as such an integer value is generally used.
To understand how the [ ] operator works, assume that an object colled O is indexed as shown here:
O[9]
This index will translate into the following call to the operator[ ]( ) function:
O.operator[ ](9)

That is, the value of the expression within the subscript operator is passed to the operator[ ]( ) function in its explicit parameter. The this pointer will point to O, the object that generates the call.
In the following program, arraytype declares an array of five integers. Its constructor function initialises each member of the array. The overloaded operator[ ]( ) function returns the value of the element specified by its parameter.
#include
using namespace std;
const int SIZE = 5;
class arraytype {
int a[SIZE];
public:
arraytype( ) {
int i;
for (i=0;i
}
int operator[ ] (int i) { return a[i]; }
};
int main( ) {
arraytype ob;
int i;
for (i=0; i<< ob[i] << " ";
return 0;
}
This program displays the following output:
0 1 2 3 4
It is possible to design the operator[ ]( ) function in such a way that the [ ] can be used on both the left and right sides of an assignment statement. To do this return a reference to the element being indexed,
#include
using namespace std;
const int SIZE = 5;
class arraytype {
int a[SIZE];
public:

arraytype( ) {
int i;
for (i=0;i
}
int &operator[ ] (int i) { return a[i]; }
};
int main( ) {
arraytype ob;
int i;
for (i=0; i<< ob[i] << " ";
cout << "\n";
// add 10 to each element in the array
for (i=0; i
ob[i] = ob[i] + 10; // [ ] on left of =
for (i=0; i<< ob[i] << " ";
return 0;
}
This program displays:
0 1 2 3 4
10 11 12 13 14
As you can see this makes objects of arraytype act like normal arrays.

A closer look at the assignment operator



As you have seen, it is possible to overload the assignment operator relative to a class. By default, when the assignment operator is applied to an object, a bitwise copy of the object on the right side is put into the object on the left. If this is what you want there is no reason to provide your own operator=( ) function. However, there are cases in which a strict bitwise copy is not desirable (e.g. cases in which object allocates memory). In these types of situations, you will want to provide a special assignment operator. Here is another version of strtype class that overload the = operator so that the point p is not overwritten by the assignment operation.
#include
#include
#include
using namespace std;
class strtype {
char *p;
int len;
public:
strtype(char *s); // constructor
∼strtype( ) { // destructor
cout << "Freeing " << (unsigned) p << "\n";
delete [ ] p;
}
char *get( ) { return p; }
strtype &operator=(strtype &ob);
};
// Constructor
strtype::strtype(char *s) {
int l;
l = strlen(s) + 1;
p = new char [l];
if (!p) {
cout << "Allocation error\n";
exit(1);
}
len = 1;
strcpy(p, s);
}
// Assign an object
strtype &strtype::operator=(strtype &ob) {
// see if more memory is needed
if (len < ob.len) {// need to allocate more memory
delete [ ] p;
p = new char [ob.len];
if (!p) {
cout << "Allocation error\n";
exit(1);
}
}
len = ob.len;
strcpy(p, ob.p);

return *this;
}
int main( ) {
strtype a("Hello"), b("there");
cout << a.get( ) << "\n";
cout << b.get( ) << "\n";
a = b; // now p is not overwritten
cout << a.get( ) << "\n";
cout << b.get( ) << "\n";
return 0;
}
Notice two important features about the operator=( ) function:
• It takes a reference parameter (prevent a copy of the object on the right side from being made).
• It returns a reference, not an object (prevent a temporary object from being created).

Using friend operator functions

As mentioned before, it is possible to overload an operator relative to a class by using a friend rather than a member function. As you know, a friend function does not have a this pointer. In the case of a binary operator, this means that a friend operator function is passed both operands explicitly. For unary operators, the single operand is passed. All other things being equal, there is no reason to use a friend rather than a member operator function, with one important exception, which is discussed in the examples.
Remember, you cannot use a friend to overload the assignment operator. The assignment operator can be overloaded only by a member operator function.
Here operator+( ) is overloaded for the coord class by using a friend function:
//Overload the + relative to coord class using a friend.
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
friend coord operator+(coord ob1, coord ob2);
};
// Overload + using a friend.
coord operator+(coord ob1, coord ob2) {
coord temp;
temp.x = ob1.x + ob2.x;

temp.y = ob1.y + ob2.y;
return temp;
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; //add to objects
// this calls operator+( )
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
return 0;
}
Note that the left operand is passed to the first parameter and the right operand is passed to the second parameter.
Overloading an operator by using a friend provides one very important feature that member functions do not. Using a friend operator function, you can allow objects to be used in operations involving build-in types in which the built-in type is on the left side of the operator:
ob1 = ob2 + 10; // legal
ob1 = 10 + ob2; // illegal
The solution is to make the overloaded operator functions, friend and define both possible situations.
As you know, a friend operator function is explicitly passed both operands. Thus, it is possible to define one overloaded friend function so that the left operand is an object and the right operand is the other type. Then you could overload the operator again with the left operand being the built-in type and the right operand being the object. For example,
// Use friend operator functions to add flexibility.
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
friend coord operator+(coord ob1, int i);

friend coord operator+(int i, coord ob1);
};
// Overload for obj + int.
coord operator+(coord ob1, int i) {
coord temp;
temp.x = ob1.x + i;
temp.y = ob1.y + i;
return temp;
}
// Overload for int + obj.
coord operator+(int i, coord ob1) {
coord temp;
temp.x = ob1.x + i;
temp.y = ob1.y + i;
return temp;
}
int main( ) {
coord o1(10, 10);
int x, y;
o1 = o1 + 10; // object + integer
o1.get_xy(x, y);
cout << "(o1+10) X: " << x << ", Y: " << y << "\n";
o1 = 99 + o1; // integer + object
o1.get_xy(x, y);
cout << "(99+o1) X: " << x << ", Y: " << y << "\n";
return 0;
}
As a result of overloading friend operator functions both of these statements are now valid:
o1 = o1 + 10;
o1 = 99 + o1;
If you want to use friend operator function to overload either ++ or -- unary operator, you must pass the operand to the function as a reference parameter. This is because friend functions do not have this pointers. Remember that the increment or decrement operators imply that the operand will be modified. If you pass the operand to the friend as a reference parameter, changes that occur inside the friend function affect the object that generates the call. Here an example,
// Overload the ++ relative to coord class using a

// friend.
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
friend coord operator++(coord &ob);
};
// Overload ++ using a friend.
coord operator++(coord &ob) {
ob.x++;
ob.y++;
return ob;
}
int main( ) {
coord o1(10, 10);
int x, y;
++o1; //o1 is passed by reference
o1.get_xy(x, y);
cout << "(++o1) X: " << x << ", Y: " << y << "\n";
return 0;
}
With modern compiler, you can also distinguish between the prefix and the postfix form of the increment or decrement operators when using a friend operator function in much the same way you did when using member functions. For example, here are the prototypes for both versions of the increment operator relative to coord class:
coord operator++(coord &ob); // prefix
coord operator++(coord &ob, int notused); // postfix

Overloading a unary operator

Overloading a unary operator is similar to overloading a binary operator except that there is one operand to deal with. When you overload a unary operator using a member function, the function has no parameters. Since, there is only one operand, it is this operand that generates the call to the operator function. There is no need for another parameter.
The following program overloads the increment operator ++ relative to the class coord.
// overload the ++ relative to coord class
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
coord operator++( );
};
// Overload ++ operator for coord class
coord coord::operator++( ) {
x++;
y++;
return *this;
}
int main( ) {
coord o1(10, 10);
int x, y;
++o1; //increment an object
o1.get_xy(x, y);
cout << "(++o1) X: " << x << ", Y: " << y << "\n";
return 0;
}


In early versions of C++ when increment or decrement operator was overloaded, there was no way to determine whether an overloaded ++ or -- preceded or followed its operand (i.e. ++o1; or o1++; statements). However in modern C++, if the difference between prefix and postfix increment or decrement is important for you class objects, you will need to implement two versions of operator++( ). The first is defined as in the preceding example. The second would be declared like this:
coord coord::operator++(int notused);
If ++ precedes its operand the operator++( ) function is called. However, if ++ follows its operand the operator++(int notused) function is used. In this case, notused will always be passed the value 0. Therefore, the difference between prefix and postfix increment or decrement can be made.
In C++, the minus sign operator is both a binary and a unary operator. To overload it so that it retains both of these uses relative to a class that you create: simple overload it twice, once as binary operator and once as unary operator. For example,
// overload the - relative to coord class
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
coord operator-(coord ob2); // binary minus
coord operator-( ); // unary minus
};
// Overload binary - relative to coord class.
coord coord::operator-(coord ob2) {
coord temp;
temp.x = x - ob2.x;
temp.y = y - ob2.y;
return temp;
}
// Overload unary - for coord class.
coord coord::operator+( ) {
x = -x;
y = -y;
return *this;
}
int main( ) {
coord o1(10, 10), o2(5, 7);

int x, y;
o1 = o1 - o2; // subtraction
// call operator-(coord)
o1.get_xy(x, y);
cout << "(o1-o2) X: " << x << ", Y: " << y << "\n";
o1 = -o1; // negation
// call operator-(int notused)
o1.get_xy(x, y);
cout << "(-o1) X: " << x << ", Y: " << y << "\n";
return 0;
}

Overloading the relational and logical operators



It is possible to overload the relational and logical operators. When you overload the relational and logical operators so that they behave in their traditional manner, you will not want the operator functions to return an object of the class for which they are defined. Instead, they will return an integer that indicates either true or false. This not only allows the operators to return a true/false value, it also allows the operators to be integrated into larger relational and logical expressions that involves other type of data.
Note if you are using a modern C++ compiler, you can also have an overloaded relational or logical operator function return a value of type bool, although there is no advantage to doing so.
The following program overloads the operators == and &&:
// overload the == and && relative to coord class
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
int operator==(coord ob2);
int operator&&(int i);
};
// Overload the operator == for coord
int coord::operator==(coord ob2) {
return (x==ob2.x) && (y==ob2.y);
}
// Overload the operator && for coord
int coord::operator&&(coord ob2) {
return (x && ob2.x) && (y && ob2.y);
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3(10, 10), o4(0, 0);

if (o1==o2) cout << "o1 same as o2\n";
else cout << "o1 and o2 differ\n";
if (o1==o3) cout << "o1 same as o3\n";
else cout << "o1 and o3 differ\n";
if (o1&&o2) cout << "o1 && o2 is true\n";
else cout << "o1 && o2 is false\n";
if (o1&&o4) cout << "o1 && o4 is true\n";
else cout << "o1 && o4 is false\n";
return 0;
}

Overloading binary operators

When a member operator function overloads a binary operator, the function will have only one parameter. This parameter will receive the object that is on the right side of the operator. The object on the left side is the object that generates the call to the operator function and is passed implicitly by this.
It important to understand that operator functions can be written with many variations. The examples given illustrate several of the most common techniques.
The following program overloads the + operator relative to the coord class. This class is used to maintain X, Y co-ordinates.
// overload the + relative to coord class
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }

coord operator+(coord ob2);
};
// Overload + relative to coord class.
coord coord::operator+(coord ob2) {
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; //add to objects,
// this calls operator+()
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
return 0;
}
The reason the operator+ function returns an object of type coord is that it allows the result of the addition of coord objects to be used in larger expressions. For example,
o3 = o1 + o2;
o3 = o1 + o2 + o1 + o3;
(o1+o2).get_xy(x, y);
In the last statement the temporary object returned by operator+( ) is used directly. Of course, after this statement has executed, the temporary object is destroyed.
The following version of the preceding program overloads the - and the = operators relative to the coord class.
// overload the +, - and = relative to coord class
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
coord operator+(coord ob2);
coord operator-(coord ob2);
coord operator=(coord ob2);
};

// Overload + relative to coord class.
coord coord::operator+(coord ob2) {
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}
// Overload - relative to coord class.
coord coord::operator-(coord ob2) {
coord temp;
temp.x = x - ob2.x;
temp.y = y - ob2.y;
return temp;
}
// Overload = relative to coord class.
coord coord::operator=(coord ob2) {
x = ob2.x;
y = ob2.y;
return *this; // return the object that is assigned
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; // add two objects,
// this calls operator+()
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
o3 = o1 - o2; //subtract two objects
o3.get_xy(x, y);
cout << "(o1-o2) X: " << x << ", Y: " << y << "\n";
o3 = o1; //assign an object
o3.get_xy(x, y);
cout << "(o3=o1) X: " << x << ", Y: " << y << "\n";
return 0;
}
Notice that to correctly overload the subtraction operator, it is necessary to subtract the operand on the right from the operand on the left. The second thing you should notice is that the function returns *this. That is, the operator= function returns the object that is being assigned to. The reason for this is to allow a series of assignment to be made. By returning *this the overloaded assignment operator allows objects of type coord to be used in a series of assignment,
o3 = o2 = o1;

Here another example where the + operator is overloaded to add an integer value to a coord object.
// overload the + for obj+int and as well as obj+obj
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
coord operator+(coord ob2); // obj + obj
coord operator+(int i); // obj + int
};
// Overload + relative to coord class.
coord coord::operator+(coord ob2) {
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}
// Overload + for obj + int.
coord coord::operator+(int i) {
coord temp;
temp.x = x + i;
temp.y = y + i;
return temp;
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; // add two objects,
// calls operator+(coord)
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
o3 = o1 + 100; // add object + int
// calls operator+(int)
o3.get_xy(x, y);
cout<< "(o1+100) X: "<< x << ", Y: "<< y << "\n";
return 0;
}
You can use a reference parameter in an operator function. For example,
// Overload + relative to coord class using reference.
coord coord::operator+(coord &ob2) {
coord temp;
temp.x = x + ob2.x;

temp.y = y + ob2.y;
return temp;
}
One reason for using a reference in an operator function is efficiency. Another reason is to avoid the trouble caused when a copy of an operand is destroyed.
There are many other variations of operator function overloading.

The basics of operator overloading



Operator overloading resembles function overloading. In fact, operator overloading is really just a type of function overloading. However, some additional rules apply. For example, an operator is always overloaded relatively to a user defined type, such as a class. Other difference will be discussed as needed.
When an operator is overloaded, that operator loses none of its original meaning. Instead, it gains additional meaning relative to the class for which it is defined.
To overload an operator, you create an operator function. Most often an operator function is a member or a friend of the class for which it is defined. However, there is a slight difference between a member operator function and a friend operator function.
The general form of a member operator function is shown here:
return-type class-name::operator#(arg-list)
{
// operation to be performed
}
The return type of an operator function is often the class for which it is defined (however, operator function is free to return any type). The operator being overloaded is substituted for #. For example, if the operator + is being overloaded, the operator function name would be operator+. The contents of arg-list vary depending upon how the operator function is implemented and the type of operator being overloaded.
There are two important restrictions to remember when you are overloading an operator:
• The precedence of the operator cannot be change.
• The number of operands that an operator takes cannot be altered.
Most C++ operators can be overloaded. The following operators cannot be overload:
.  ::  .*  ?


Also, you cannot overload the pre-processor operators (.* is highly specialised and is beyond the scope of this course).
Remember that C++ defines operators very broadly, including such things as the [ ] subscript operator, the ( ) function call operators, new and delete, and the dot and arrow operator. However, we will concentrate on overloading the most commonly used operators.
Except for the =, operator functions are inherited by any derived class. However, a derived class is free to overload any operator it chooses (including those overloaded by the base class) relative to itself.
Note, you have been using two overloaded operators: << and >>. These operators have been overloaded to perform console I/O. As mentioned, overloading these operators does not prevent them from performing their traditional jobs of left shift and right shift.
While it is permissible for you to have an operator function perform any activity, it is best to have an overloaded operator's actions stay within the spirit of the operator's traditional use.

Monday, August 16, 2010

C++ program to add two matrices using classes and objects


#include  stdio.h 
#include 
  conio.h 
#include    math.h 
class matrix
{
int a[4][4],b[4][4],i,j,x,y;
public:
void getvalues();
void displaysum();
};
void matrix::getvalues()
{
cout<<"Enter the size of the row and column ";
cin>>x>>y;
if(x!=y)
{
cout<<"The size of the row and column should be equal";
getch();
exit(0);
}
cout<<"Enter values for the matrix A\n";
for(i=0;i
for(j=0;j
cin>>a[i][j];
cout<<"Enter the values for matrix b\n";
for(i=0;i
for(j=0;j
cin>>b[i][j];
}
void matrix::displaysum()
{
cout<<"The sum of matrix A and B is\n";
for(i=0;i
{
for(j=0;j
cout<<<"\t";
cout<
}
}
int main()
{
clrscr();
matrix m;
m.getvalues();
m.displaysum();
getch();
}