Title: Introduction to Programming in C
1Introduction to Programming in C
- Class 18
- 03/31/2003
- Rohit Valsakumar
2Operator Overloading Continued
- Rules on overloading operators
- Some special constructors
- Overloading ltlt and gtgt
- Some rules of class design
3Rules on operator overloading
- At least one argument of the overloaded operator
should be of a class type - An overloaded operator can be friend of a class
or its member function or an ordinary function - Only existing operators such as ,-,/, etc. can
be overloaded, new operators cannot be created - We cannot change the number of arguments the an
operator takes e.g., always two arguments
whereas takes one argument only - We cannot change the precedence of the operator
- The following operators cannot be downloaded
viz., (.), (), operator ., ? - Assignment operator () has to be overloaded in a
special manner - Operators and -gt also need to be overloaded in
a special manner
4Constructors for automatic type conversion
- class complex
-
- public
- complex ()
- complex (double re, double im)
- complex (int x)
- complex operator (complex c1, complex c2)
-
- private
- double re_part, im_part
-
- void main()
-
- complex c1, c2
- c2 c1 34
- In the main() function we notice that we are
trying to add an integer with a complex object - Ideally this will succeed only if the
operator is overloaded to allow such an operation - However, if the operator is not overloaded we
still find that the operation succeeds - This is because C compiler checks for and finds
a constructor that takes a single integer value
as argument to construct the object - It uses that constructor to convert that integer
value into a complex object and then adds the two
complex objects together - If such a constructor did not exist then the
operation will not be carried out and the
compiler will signal an error
5Overloading Unary Operators
- Unary operators can be overloaded just like
binary operators - Unary operators like -, , -- are
frequently overloaded - Eg., with respect to complex class
- complex operator-(complex c1)
-
- complex c2
- c2.re_part -c1.re_part
- c2.im_part -c1.im_part
- return c2
6Overloading Unary Operators
- The single argument in the function definition
indicates to the compiler that it is an unary
operator overload and not a binary operator
overload - Now following operations can be carried out
- complex obj1, obj2(4.5, 6.7)
- obj1 -obj2
- Similarly other unary operators can also be
overloaded
7Overloading ltlt and gtgt
- The insertion operator ltlt is used with cout
or any other ostream object to send streams to
the output - It can also be overloaded like any other binary
operator - However there is a special technique to do it
- This is because in the case of the ltlt operator
we dont know what to return
8Overloading operator ltlt
- ostream operator ltlt (ostream outs, const
complex c1) -
- outs ltlt c1.re_part ltlt i ltlt c1.im_part
- return outs // remember this
9Overloading operator ltlt
- Always remember to have as return type ostream
when you overload the ltlt operator - This means the function returns a reference to an
ostream object - This is the only way the operato ltlt overload
function will work
10Overloading operator gtgt
- istream operator ltlt (ostream ins, complex c1)
-
- ins gtgt c1.re_part gtgt c1.im_part
- return ins // remember this
-
11Overloading operator gtgt
- The extraction operator (gtgt) is overloaded using
most of the same rules as the insertion operator
overload - Only one thing changes the const modifier is
removed form the parameter list - Why do you think that is done?
- Please read chapter 8 thoroughly
12Strings
- String is a sequence of characters
- There are two types of strings in C
- An array of characters terminated by a null
character - A class string is the second string type
13C-string Values and C-String Variables
- In C strings are represented as an array of
characters terminated by a null character - A null character is represented as \0
- E.g., the string Program will be represented as
eight indexed consecutive characters as shown
below - P r o g r a m \o
- The terminating null character is the compilers
way of keeping track of the end of the string
14C-string Values and C-String Variables
- A C-String variable is just an array of
characters - char str10
- The above variable can be used to store a string
of maximum 9 characters and one null characters - Let us try to store the previous string Program
in str10 - Str0 P str1 r str2 o str3
g str4 r str5 a str6 m
str7 \0 - As seen above we need to occupy only eight of the
ten available locations, the remaining two
locations viz, str8 and str9 will be
uundefined
15C-string Values and C-String Variables
- A C string variable can be initialized at the
point of declaration - E.g.,
- char str20 hello world
- There will be a null character in the 12th
location - The last 8 locations of the above array will be
undefined - When defining at the point of declaration we need
not specify the size inside the square braces - E.g.,
- char str1 abc
- The size of the array will be automatically
determined to be 4 characters - However char str2 a, b, c will not
have the effect of automatically introducing a
null character at the end - Thus size of str2 will be three and str2 is not
same as str1