Showing posts with label OOP's. Show all posts
Showing posts with label OOP's. Show all posts

Sunday, 14 October 2012

How to Change Color of Output screen in C

Hello friends, whenever u run any C program, you finds that the color of output screen is black! you can change its color. Its very easy. you have to follow only few steps.......

Open the FILE menu


Click on Dos-shell


Now type COLOR ?


Now type the color combination (example-"color 1c")

  • Now type exit

    now the color of output screen has been changed.........
  • Tuesday, 11 September 2012

    C++ ( Tutorials XIV )


    Defination of C++ :-

    C is much more efficient than BASIC, a new concept called Object Oriented Programming seemed better than C. OOP was the basis of C++(which was initially called ‘C with classes’).C++ was developed by Bjarne Strastroup.In object oriented programming the programmer can solve problems by breaking them down into real-life objects (it presented the programmer with an opportunity to mirror real life). What is an object? This topic is dealt with extensively in the chapter on ‘Objects and Classes’ but a brief introduction is provided here.
    Consider the category of cars. All cars have some common features (for example all cars have four wheels, an engine, some body colour, seats etc.). Are all cars the same? Of course not. A Fiat and a Ford aren’t the same but they are called as cars in general. In this example cars will form a class and Ford (or Fiat) will be an object.

    C++ ( Tutorials XIII )

    Compiler:-

    Before providing a technical explanation a compiler in layman’s language is a program which will read through the C++ program code (code refers to the program written by the programmer) line by line. The compiler will read each and every character that has been typed in the program code. After reading the entire code, the compiler will convert the code into machine level format (i.e. 0s and 1s). The compiler is very dedicated and hard working since it will meticulously read each and every character.

    Technically speaking, the microprocessor in a computer can understand only binary numbers (only 0s and 1s). It is not possible for us to write programs in binary numbers.Compilers will convert the coding for a program into machine understandable form (i.e. it will convert the instructions we type into machine understandable form). The instructions we type (in something similar to English), is known as the source code. The compiler converts the source code into the object code (which the microprocessor understands and we cannot understand).

    C++ ( Tutorials XII )

     Saving and compiling the program: -

    There are many C++ compilers available in the market. Some of them are freeware (meaning that they are free to use) while others have to be paid for. Turbo C++ compiler might be the simplest to use (but it is not freeware). Simply choose "New File" and type out the program coding. Suppose you are using some other compiler, click on File and choose "New". Some compilers may have the option of creating a C++ source file while other compilers may require a new project to be created. Whatever the method you will ultimately come to the step of creating a C++ source file. After typing the code in the compiler, save the file by giving it some name. The "Save As" option will appear under the "File" menu. Give a name (for example: first). Now the file is saved as first.cpp. All C++ source files are saved in *.cpp format. Just like *.doc represents a Word document file, *.cpp denotes a C++ (C Plus Plus) source file. In the compiler program there will be an option called ‘Compile’ in the menu bar. Select the compile option and the compiler will do its work. It will compile the program (or in other words, it will read whatever has been typed) and in case there are any errors, the compiler will point out the line where the error was detected. Check whether the program has been typed exactly as given earlier. Even if a semi-colon is missing, it will lead to errors. If the compiler says no errors (or if the message "compiled successfully" appears), then you can go to the next stage: building the *.exe file.

    *.exe file extension stands for executable files. A *.cpp file cannot be run directly on the computer. This has to be converted into a *.exe file and to do so select the "Make or Build exe" option in the compiler. The file ‘first.exe’ will be created. Now the program can be executed from the DOS prompt by typing ‘first’. But instead of running the program every time from DOS, it will be convenient to run the program from the compiler itself and check the output. In the compiler there will be another option called "Run". Just click on this and the program will run from the compiler itself (you needn’t switch back and forth between DOS and the compiler screen)

    C++ ( Tutorials XI )

    The following topics are covered in this section: 
    Introduction , Integer, Floating Type, Double, Character, Boolean determining the range, More on Binary Numbers.
    1.)Integer
    An integer can contain only digits (numbers) from 0 to 9. Examples of integers are:
    0, 10, 345, 6789, 33, -23, -600

    It includes positive and negative numbers but the numbers have to be whole numbers. It does accept the decimal point. Hence the following numbers are not integer data types:
     3.5
     4.8
     0.23
    These numbers come under the second category (floating point type) and not under integers. If the program has to accept such values from the user do not declare the variable as an integer. If a variable is declared as an integer and the user enters a value of 2.3, the program will assign 2 as the value for that integer variable. Similarly, if the user enters 3.2, the program will assign 3 to the integer variable.

    Once a variable is declared as an integer, it will only store whole numbers (if the user types a value with the decimal point, the program will ignore everything that is typed after the decimal point).

    How to declare a variable as belonging to the type integer? The syntax is:

    int variable-name;

    2.)Floating Types

    Floating type data include integers as well as numbers with a decimal point. It can also have an exponent. Exponent means 10 to the power of some integer value (whole number). 20000 = 2 x 10^4 = 2e4 = 2E4. 
    If you specify decimal numbers, floating point data type will store up to a precision of 6 digits after the decimal point. Suppose 0.1234567 is assigned to a floating-point variable, the actual value stored would be 0.123457 (it will round up to the sixth digit after the decimal place). Valid floating-point numbers are:
    a.) 0.1276
    b.) 1.23
    c.) 1.0
    d.) 10.2
    e.) 35
    f.) 2e5 (this will be typed in your code as 2e5)

    Do not use an exponent with a decimal point. For example: 2e2.2 is an invalid floating point because the exponent has to be an integer.Floating point numbers use 4 bytes of memory and has a much greater range than integers because of the use of exponents. They can have values up to 10^38 (in positive and negative direction). The same qualifiers used for an integer can be applied to floating point numbers as well. To declare a floating variable, the syntax is:
    float variable-name;

    3.)Double

    This is similar to the floating-point data type but it has an even greater range extending up to 10308. The syntax to declare a variable of type double is:

    double variable-name;

    Beware: Visual C++ (VC++) usually uses its default as ‘double’ instead of ‘float’. Suppose we type:
    float x=31.54;
    you will get a warning message saying that a ‘double’ (i.e. 31.54) is being converted into a floating point. It is just to warn you that you are using a ‘float’ and not a ‘double’. (Even if there are warnings, there won’t be any problem in running your program).

    4.)Character

    A character uses just one byte of memory. It can store any character present on the keyboard (includes alphabets and numbers). It can take numbers from 0 to 9 only. The following are valid characters:

    a.)A
    b.) B
    c.) 3
    d.) a
    e.) :
    f.) ‘
    g.) /

    If the number 13 is entered as the value for a character, the program will only store 1 (i.e it will store the first character that it encounters and will discard the rest). A character is stored in one byte (as a binary number). Thus whatever the user enters is converted into a binary number using some character set to perform this conversion. Mostly all computers make use of the ASCII (American Standard Code for Information Interchange). For example, according to the ASCII coding, the letter ‘A’ has a decimal value of 65 and the letter ‘a’ has a value of 97.

    There is another form of coding called the EBCDIC (Extended Binary Coded Decimal Information Code) which was developed by IBM and used in IBM computers. However, ASCII remains the most widely used code in all computers. The table at the end of the book gives a listing of the ASCII values and their equivalent characters. The syntax to declare a variable which can hold a character is: 

    char variable-name;

    5.)Determining the range:

    You might be wondering why the ranges are from -128 to 127 or from -32768 to 32767? Why not from –128 to 128? We’ll take the case of signed characters to understand the range. A character occupies one byte (8 bits). In a signed character the MSB (i.e. the 7th bit is used to denote the sign; if the MSB is 1 then the number is negative else it is positive). The binary number: 0111 1111 represents +127 in decimal value (we’ve seen about conversion from binary to decimal numbers in the first chapter). The number 0000 0000 represents 0 in decimal. But what about 1000 0000? Since the MSB denotes a negative number, does this stand for –0? Since there is no point in having a –0 and a +0, computers will take 1000 0000 as –128. 0000 0000 is taken to be zero. Thus in the negative side we have a least value of –128 possible while in the positive side we can have a maximum of only +127.

    Another point to note is that negative number in computers are usually stored in 2’s complement format. For example: 1000 0001actually stands for –1 but if this number is stored in 2’s complement format then it stands for –127. Similarly 1000 0010 would appear to be –2 but is actually –126. To understand 2’s complement you should know about 1’s complement. Let us say we have a signed binary number: 1000 0001. The 1’s complement of this number is 1111 1110 (i.e. to find the 1’s complement of a binary number just change the 1s to 0s and 0s to 1s but do not change the sign bit). To find the 2’s complement just add 1 to the 1’s complement. Hence the 2’s complement of 1000 0001 is 1111 1111 (or –127). In the same way the 2’s complement of 1111 1111 (-127) is 1000 0001 (or – 1). Thus the computer instead of storing 1111 1111 (for –127) will store the 2’s complement of the number (i.e. it will store 1000 0001).

    6.)More on Binary Numbers:

    Everything in the computer is stored in binary format. Even if we ask the computer to store an octal number or a hexadecimal number in a variable, it will still be stored in binary format (we’ll see this later) because computers only understand 0s and 1s. How does a computer perform calculations? Yes, it has to do it in binary format.

    C++ ( Tutorials X )

    1.) Identifiers:-

    Identifiers are very important in C++ and are used in all programs. What is an identifier?In the previous program ‘check’ was declared as a character variable. The identifier in this case is ‘check’. Basically identifiers are names that are given to variables, constants or functions. In the previous program there are two identifiers: ‘check’ and ‘i’. 


    C++ permits the use of a huge variety of names for identifiers like:

    test, Test, int_test, var1, var123, c_b_f, _var

    Almost any name you can think of can be used as an identifier but there are some restrictions. An identifier should not start with a number. The first character of an identifier should be an alphabet or an underscore ( _ ).
    After the first character, numbers can be used in the identifier. Remember the following:

    a.) Never start an identifier with anything other than a letter or an underscore.
    b.) It is better to use a letter than starting with underscores.
    c.) Do not use keywords as identifiers (ex: do not name an identifier as int).
    d.) Uppercase and Lowercase identifiers are different (Check is different from check).
    e.) Be careful when using characters that look similar to each other. For example ‘1’ (the number one) and ‘l’ (the lowercase alphabet L) look alike.
    f.) Use names that are easy to understand (if you are storing the salary of a person, name the variable as ‘salary’ instead of declaring it as ‘x’.
    g.) Do not use very long names.
    h.) Do not name many variables with similar names (avoid using identifiers like: count, counter, counting etc.)

    C++ ( Tutorials IX )

    Defination of Keyword:-

    Keywords are reserved words in C++ programming. They should not be used as identifiers and all keywords should be in lower case. The 63 keywords are tabulated below.
    Asm, Auto, bool, break, case, Catch, char, Class, Const, const_cast, continue, default, Delete, do, Double, Dynamic_cast, else, enum, explicit, Export,extern, False, Float, For, friend, goto, If, inline,Int, Long, mutable,namespace, new, Operator, private,Protected, Public, register,reinterpret_cast, return, Short, signed,Sizeof, Static, static_cast, struct, switch, Template, this,Throw, True, Try, typedef, typeid, typename, union,Unsigned, Using, virtual, void, volatile, wchar_t while..

    C++ ( Tutorials VIII )

    File Naming Convention :- 

    A few points regarding file nomenclature are:

     1.) In MSDOS a filename cannot have more than 8 characters whereas in Windows there is no such restriction.
    2.) Use meaningful names (depending on what the program performs).
    3.) Avoid using underscores in the file names.
    4.) Be careful when using characters that look similar to each other. For example ‘1’ (the number one) and ‘l’ (the lowercase alphabet L) look alike.

                           File Type                          Extension used

                           C++ source file                    *.cpp
                          
                           C++/C header file                 *.h
                           C source file                         *.c
                           Executable file                      *.exe
                           Object Code                         *.obj
                           Text file                               *.txt
                           Word document                     *.doc
                           Image/graphics file                *.jpg/ *.gif/ *.bmp

    C++ ( Tutorials VII )

    Constant Mean :-

    Constants are used in expressions and their values cannot be changed. They are used in the program code without using any variable name to refer to them. For example consider the statements:
    int x = 5;
    float y = 3.33;

    In this, 5 is an integer constant and 3.33 is a floating type constant. You cannot change 5 or 3.33 but you can assign these values to variables (the variables in the above code fragment are ‘x’ and ‘y’).


    Constants can be classified based on their data type as follows:

    1.) Numeric type constants (includes integers, floating-point etc.)
    2.) Character and String Constants.

    1.) Numeric constants:-

     Numeric constants consist of a series of digits. Integer constants can be written in different number systems:
    hexadecimal (base 16), octal (base 8) or in decimal (base 10).

    a.) A decimal integer constant is any normal whole number (can consist of digits from 0 to 9):
    2,34, 100, 900, 1456 etc.

    b.) An octal integer constant can contain digits from 0 to 7 only and it should start with a zero (so that the computer will know it is an octal number). For example:
    023, 0567, 0214 etc.

    c.) A hexadecimal integer constant should start with 0x or 0X and can consist of digits from 0 to 9 and A to F (uppercase and lowercase letters are allowed). For example:
    0x10, 0x1FF etc.

    d.) Floating point constants will have the decimal point in the number.

    2.) Character and String Constants:-

     a.) Character constants are single characters enclosed within two single quotes (or between two apostrophes). For example:
         ‘a’
         ‘b’
         ‘x’ 
         ‘1’
         ‘A’
         ‘*’

    A single character enclosed within single quotes is a character constant. All character constants will have an integer value (determined from the ASCII table). A C++ statement:
    char ch = ‘B’;
    will assign the character constant ‘B’ to the character variable ch.

    b.) String constants consist of a series of characters enclosed within double quotes. For example:
          "hello"
           "This is a string"
           "x" Even "x" is a string because it is enclosed in double quotes. "x" is different from ‘x’ (this is a character constant). The reason is because "x" actually consists of ‘x’ and ‘\0’ (the null character which will be explained later).

    c.) Constants are also called as ‘literal’. You might come across the terms character literal, integer literal, string literal etc.

    C++ ( Tutorials VI )

    Operator Overloading :-

    Operator overloading means giving additional meaning to normal operators when they are applied to user defined data types. How will the compiler recognize this? The compiler identifies that it is an overloaded operator by looking at the data type of the operand (i.e. if it is a user defined data type then the compiler knows it is an overloaded operator; if it is a built-in data type then the compiler will do the normal operation).
    You have to remember that objects are defined by you (or the programmer). You choose the data that comes under an object. The data type that you use will be in-built data types like integer or character but the object as a whole is user defined. Hence it is not possible to add two objects (even though they belong to the same class) using the + sign. The addition operator is designed to work only on in-built data types like integers, float etc. It cannot add two objects. But using operator overloading you could use the same + sign to
    add two objects. Operator overloading is a form of polymorphism(for example, the + operator once overloaded in a class can be used in different ways depending on the operands. If the operands are objects then the overloaded + is used. If the operands are fundamental data types then the normal + operation is carried out).
     

    Syntax: 
    return-data-type operator symbol-of-operator (parameters)
    {
    //body of the function
    }

    C++ ( Tutorials V )


    Manipulators
    There are many stream-formatting functions available but it is a little tedious to use them.Manipulators duplicate these functions and make it easier to carry out stream-formatting operations. Usually it is the output (what we display on the screen) that needs to be formatted to suit our needs. In this case, ‘cout’ is the stream. One of the most commonly used manipulators is ‘endl’. This is used to end the current line and go to the next line. You cannot use ‘endl’ separately. All the manipulators have to operate on streams. Manipulators that do not require any arguments are defined in the iostream.h header (manipulators with arguments are defined in the iomanip.h header file).
    Let's check out an example program.

    # include<iostream.h>
    int main( )
    {
    cout<< "Hi, this is a test program"<<endl;
    cout<<endl;
    cout<<endl<<"You should see three empty lines above this";
    return 0;
    }


    The output will be:

    Hi, this s a test program

    Monday, 10 September 2012

    C++ ( Tutorial IV )

    Creating New Manipulators (Custom Manipulators)

    You can create your manipulators to work along with ‘cout’ (output stream object) or with ‘cin’ (input stream object). Let us consider an example of a manipulator dealing with ‘cout’. The syntax for creating a manipulator is:

    ostream& name (ostream &str)
    {
    //code to be executed;
    }


    In the case of manipulators to be used with input stream, only one modification is necessary.
     

    istream& name (istream &str)
    {
    //code to be executed;
    }


    #include <iostream.h>
    ostream& divider (ostream &str)
    {
    str<<"--------------------------------"<<endl;
    return str;
    }
    int main( )
    {
    int x;
    cout<<divider<<"Enter an integer value : ";
    cin>>x;
    cout<<divider<<"The value you entered is : ";
    cout<<x;
    return 0;
    }


    The output is:

    --------------------------------
    Enter an integer value : 98
    --------------------------------
    The value you entered is : 98