Input And Output of C++
Input And Output of C++
Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout << "Hello World\n"; Sends string "Hello World" to display, & escape sequence "\n", skipping to next line cout << "Hello World" << endl; Same result as above
Input Using cin cin for input Differences: ">>" (extraction operator) Object name "cin" used instead of "cout" No literals allowed for cin Must input "to a variable" cin >> num; Waits on-screen for keyboard entry Value entered at keyboard is "assigned" to num
First C++ program Greeting.cpp Preprocessor directives // Program: Display greetings #include <iostream> #include <string> Comments Function named main() indicates start of program int main() { cout << "Hello world!" << endl; return 0; } Ends executions of main() which ends program Insertion statement
Output
C++ Data Types Data types are means to identify the type of data and associated operations of handling it. C++ provides a predefined set of data types for handling the data it uses. When variables are declared of a particular data type then the variable becomes the place where the data is stored and data types is the type of value(data) stored by that variable. Data can be of many types such as character, integer, real etc. since the data to be dealt with are of many types A programming language must provide different data types.
FUNDAMENTAL DATA TYPES DATA TYPES DATA TYPE MODIFIERS DERIVED DATA TYPES USER DEFINED DATA TYPES
INTEGER CHARACTER WIDE CHARACTER FUNDAMENTAL DATA TYPES FLOAT FUNDAMENTAL DATA TYPES ARE THOSE THAT ARE NOT COMPOSED OF OTHER DATA TYPES DOUBLE BOOL VOID
INTEGER DATA TYPE.. 2 bytes Integers are whole numbers with a machine dependent range of values. A good programming language as to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms. A short int requires half the space than normal integer values. Unsigned numbers are always positive and consume all the bits for the magnitude of the number. T he long and unsigned integers are used to declare a longer range of values.
FLOAT DATA TYPE 4 bytes A number having fractional part is a floating- point number. An identifier declared as float becomes a floating-point variable and can hold floatingpoint numbers. floating point variables represent real numbers. They have two advantages over integer data types:- They can represent values between integers. They can represent a much greater range of values. they have one disadvantage also, that is their operations are usually slower.
DOUBLE DATA TYPE 8 bytes The data type double is also used for handling floatingpoint numbers. It is treated as a distinct data type because, it occupies twice as much memory as type float, and stores floatingpoint numbers with much larger range and precision. It is slower than type float.
VOID DATA TYPE 1byte in GCC Compiler otherwise depends upon compiler It specifies an empty set of values. It is used as the return type for functions that do not return a value. No object of type void may be declared. It is used when program or calculation does not require any value but the syntax needs it.
Bool DATA TYPE 1 byte it is an additional data type for representing a Boolean value. A variable associated with a bool data type may be assigned an integer value 1 to the literal true or a value 0 to the literal false.
WIDE CHARACTER DATA TYPE.2 or 4 bytes Wide character is a computer character data type It has generally size more than 8- bit character The increased size allows use of larger code character sets
Area.cpp #include <iostream> int main() { // Extract length and width cout << "Rectangle dimensions: "; float Length; float Width; cin >> Length >> Width; // Compute and insert the area } float Area = Length * Width; cout << "Area = " << Area << " = Length " << Length << " * Width " << Width << endl; return 0;
INTEGER TYPE MODIFIERS DATA TYPE MODIFIERS CHARACTER TYPE MODIFIERS THEY CHANGE SOME PROPERTIES OF THE DATA TYPE FLOATING-POINT MODIFIERS
INTEGER TYPE MODIFIERS C++ offers three types of integer data type:- 1:- short integer- at least two bytes. 2:- int integer at least as big as short. 3:- long integer-at least four bytes. the prefix signed makes the integer type hold negative values also. Unsigned makes the integer not to hold negative values.
TYPE APPROXIMATE SIZE MINIMAL RANGE SHORT 2-32768 to 32767 UNSIGNED SHORT 2 0 to 65,535 SIGNED SHORT 2 Same as short INT 2-32768 to 32767 UNSIGNED INT 2 0 to 65,535 SIGNED INT 2 Same as int LONG 4-2,147,483,648 TO 2,147,483,647 UNSIGNED LONG 4 0 to 4,294,967,295 SIGNED LONG 4 Same as long
CHARACTER TYPE MODIFIER The char can also be signed or unsigned. unlike int,char is not signed or unsigned by default. It is later modified to best fit the type to the hardware properties. TYPE APPROXIMATE SIZE MINIMAL RANGE CHAR 1-128 to 127 UNSIGNED CHAR 1 0 to 255 SIGNED CHAR 1 Same as char
FLOATING POINT TYPE MODIFIERS There are three floating-point types: float, double, and long double. These types represent minimum allowable range of types. Note:- don t use commas in numeric values assigned to variables. TYPE APPROXIMATE SIZE FLOAT 4 7 LONG DOUBLE 8 15 LONG DOUBLE 10 19 DIGITS OF PRECISION
Name Description Size* Range* char Character or small integer. 1byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer. 2bytes int Integer. 4bytes long int (long) Long integer. 4bytes signed: -32768 to 32767 unsigned: 0 to 65535 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits) long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
ARRAYS FUNCTIONS DERIVED DATA TYPES POINTERS REFERENCES CONSTANTS
ARRAYS An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. For example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier. Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [elements]; NOTE: The elements field within brackets [ ] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution.
VALID OPERATIONS WITH ARRAYS billy[0] = a; billy[a] = 75; b = billy [a+2]; billy[billy[a]] = billy[2] + 5;
PROGRAM: ARRAYS EXAMPLE #include <iostream> using namespace std; int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; int main () { for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0; } OUTPUT:- 12206
POINTERS Variable that points to a memory location It holds the address of another variable The general form of declaring the pointer is type*ptr; Example : int *ptr;
REFERENCES It is an alternative name for an object. A reference variable provides an alias for a previously defined variable. It s declaration consists of a base type, an &(ampersand), a reference variable name equated to a variable name. the general form of declaring is:- type &ref-var = var-name; Example : int x ; int & r = x;
CLASS STRUCTURE USER DEFINED DERIVED DATA TYPES UNION ENUMERATION
CLASS Class: A class is a collection of variables and function under one reference name. it is the way of separating and storing similar data together. Member functions are often the means of accessing, modifying and operating the data members (i.e. variables). It is one of the most important features of C++ since OOP is usually implemented through the use of classes.
CLASS Classes are generally declared using the keyword class, with the following format: class class_name { access_specifier_1: member1; access_specifier_2: member2;... } object_names;
STRUCTURES A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures are declared in C++ using the following syntax: struct structure_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3;.. } object_names; where structure_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name.
STRUCTURES Structure is different from an array in the sense that an array represents an aggregate of elements of same type whereas a structure represents an aggregate of elements of arbitrary types..
UNION Unions allow one same portion of memory to be accessed as different data types, since all of them are in fact the same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally different: union union_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; } object_names;
UNION All the elements of the union declaration occupy the same physical space in memory. Its size is the one of the greatest element of the declaration. All of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent of each other. One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. The exact alignment and order of the members of a union in memory is platform dependent. Therefore be aware of possible portability issues with this type of use.
ENUMERATION An enumeration is a set of named integer constants that specify all the permissible values that can be assigned to enumeration variables. These set of permissible values are known as enumerators. Declaring an enum type enum country {US, UN, India, China}; In this statement, an enumeration data-type country (country is a tag name), consisting of enumerators US, UN and so on, is declared. Note that these enumerators represent integer values, so any arithmetic operation can be performed on them.
ENUMERATION By default, the first enumerator in the enumeration data type is assigned the value zero. The value of subsequent enumerators is one greater than the value of previous enumerator. Hence, the value of US is 0, value of UN is 1 and so on. However, these default integer values can be overridden by assigning values explicitly to the enumerators As shown here. enum country {US, UN=3, India, china} ; In this declaration, the value of US is O by default, the value of UN is 3, India is 4 and soon.
ENUMERATION Once an enum type is declared, its variables can be declared using this statement. country countryl, country2; These variables countryl, country2 can be assigned any of the values specified in enum declaration only. For example, consider these statements. countryl = India; // valid country2 = Japan; // invalid Though the enumerations are treated as integers internally in C++, the compiler issues a warning, if an int value is assigned to an enum type. For example, consider these statements. country1 = 3; //warning country1 = UN; / /valid country1 = (country) 3; / /valid
ENUMERATION C++ also allows creating special type of enums known as anonymous enums, that is, enums without using tag name as shown in this statement. enum {US, UN=3, India, China}; The enumerators of an anonymous enum can be used directly in the program as shown here. int count = US;
C++ Statements C++ stmts Exp stmt Labelled stmt Guarding stmt Control stmt Selection stmt Iteration stmt Jump stmt If If else switch while do for break contin ue return