Monday, August 18, 2008

String Class in cpp(Standard cpp strings)

Here is how to test the string class already present in cpp. To use simple strings u have to include <iostream>. For example consider the program, #include <iostream> #include <conio.h> using namespace std; int main() {     string word="hello";     cout<<word;     getch();     } There are various functions and operators in this string class. These operators are present in string - =        assignment + , +=  concatenation...

Stack ADT

Stacks are an elementary Data Structure where all interaction with the data is done through the looking at the first element. Stacks are last in first out (LIFO) data structures. It is named on the basis of how the elements are removed from it. Opposite to stack is queue, in which the last element is removed, the element which was most old item, and hence it is FIFO or first in first out data structure. Stack Operations There are two main operations...

Thursday, August 14, 2008

Miscellaneous cases with printf

Strings 1. printf(5+Fascimile); O mile 2. printf(5+"Good Morning"); O Morning 3. printf("%c", abcdefg[4]); O. e...

Strings

static char s[]= "Oinks Grunts and Guffaws"; Now we see some results on this string: 1. *(&s[2])=s[2]=*(s+2)=*(2+s ) = n 2. strlen(s) = length of the string so strlen(s)+s= last character of the string = \0 , whose ascii value is 0. So when %c is used , \0 is printed and when %d is used 0 is printed. 3. when is printed with %d we get the base address of the string. 4. s+5 with %s prints ^Grunts and Guffaws, that is base address + 5 and rest of the string. Now let i=0; ch = s[++i]; // ch = s[1] i.e. i ch=s[i++];  // ch=s[1] but i=2 ch...

Method Overriding

Suppose bc and dc have same functions: In case of java for eg. Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not, then the two methods are simply overloaded. For example, consider this modified version of the preceding example: // Methods with differing type signatures are overloaded – not overridden. class A {int i, j;A(int a, int b) {i = a;j = b;}// display i and jvoid show() {System.out.println("i and j: " + i + " " + j);}} // Create a subclass by extending class A. class B...

Access specification

This topic tells when is contructor called and when not 1.formation of object(lolz) 2.passing objects to the function class X{    int i;public:    X(int b)    {            cout<<"love";         i=b;     }    void  set_i(int b){s=b;}    put_i()     {         cout<<"i="<<     }    X()//destructor...

Access in cpp classes

When a base class is inherited as private, all public and protected members of that class become private members of the derived class. However, in certain circumstances, you may want to restore one or more inherited members to their original access specification. For example, you might want to grant certain public members of the base class public status in the derived class even though the base class is inherited as private.  In Standard C++, you have two ways to accomplish this. First, you can use a using statement, which is the preferred...

Wednesday, August 13, 2008

Escape characters and printf

Escape Sequence Character represented \n newline \t tab \\ backslash itself \' quote ``''' \" double quote ``"'' For example, printf("\t\"My name is \'%s\'\"\n", "Jim"); Which prints "My name is 'Jim...

I/O in c without printf and scanf.

Problem I/O in c without printf and scanf. Method 1 - getchar and putchar int getchar(); int putchar(int value); Sample Program #include <stdio .h> int main(void) { int c; while ((c = getchar()) != EOF) { putchar(c); } return 0; } Note the use of parentheses. c=gethar()!=EOF implies whether c = 0 or 1 depending on whether character get is not EOF or is EOF. EOF is 257 , so use int rather than char. Method 2 - gets and puts gets(name of string) ; int puts(name of string); for puts - It terminates the line...

Printf with %

 Format Specifiers cCharactera d or iSigned decimal integer392 e EScientific notation (mantise/exponent) using e character3.9265e+2 3.9265E+2 fDecimal floating point392.65 gUse the shorter of %e or %f392.65 GUse the shorter of %E or %f392.65 oSigned octal610 sString of characterssample uUnsigned decimal integer7235 xUnsigned hexadecimal integer7fa XUnsigned hexadecimal integer (capital letters)7FA pPointer addressB800:0000 nNothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored. %A...

Friday, August 8, 2008

C preprocessor

The C preprocessor (cpp) is the preprocessor for the C programming language. In many C implementations, it is a separate program invoked by the compiler as the first part of translation. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if). The language of preprocessor directives is not strictly specific to the grammar of C, so the C preprocessor can also be used independently to process other types of files. The transformations it makes on its input form the first...