Programming with C++
Hello World Program Keyword
Function
Object
Header File
#include Main () { cout<< “Hello, World!”; }
End of function
Hello.cpp
Standard Output Stream cout<< “Hello, World!”; Object
String Being sent
- cout is the object displays the string on the console.
Standard Output Stream #include Main () { int amount = 123; cout << amount; } Here as amount is a variable, there are no “ ” used.
Standard Output Stream #include Main () { int amount = 123; cout << “The value of amount is ”; cout << amount; cout << ‘.’; }
The value of amount is 123.
Excersies Write a Program to display your name. #include Main () { cout << “My name is ”; }
Several Outputs in One Statement #include Main () { int amount = 123; cout << “The value of amount is ” << amount << ‘.’; }
Standard Input Stream #include Main () { int amount; cout << “Enter an amount…”; cin >> amount; cout << “The amount you entered was ” << amount; }
Here cin is used to read an integer from the keyboard.
Datatypes Description Datatype Range Integer int -32768 to +32767 Floating Point float 3.4 e-38 to 3.4 e+38 Character char -127 to 127 Double precision double 1.7 e-308 to 1.7 e+308 Void void Logical bool
Datatypes
Integer Type : Integers are whole numbers with a machine dependent range of values. 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. The long and unsigned integers are used to declare a longer range
Datatypes
Floating Point Types : Floating point number represents a real number with 6 digits precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision. To extend the precision further we can use long double which consumes 80 bits of
Datatypes
Character Type : A single character can be defined as a defined as a character type of data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to char. While unsigned characters have values between 0 and 255, signed characters have
Datatypes
Void Type : Using void data type, we can specify the type of a function. It is a good practice to avoid functions that does not return any values to the calling function.
Formatted Output iostream class system includes a set of manipulators with the output stream. #include Main () { int amount = 123; cout << dec << amount << ‘ ’<< oct << amount << ‘ ’<< hex << amount; }
Output will be
123
Exercise Write a program to take multiple inputs as 2. Name of the student 3. Age of the student 4. Percentage of Marks 5. Favorite subject and Display the summary after input.
C++ Comments Comment
is the instruction which is not the part of execution. Provides information about the code to the reader. Makes the code meaningful and readable. Good practice to have for a successful programmer. Double-slash ( // ) sequence is used to mark comment.
C++ Comments #include Main () { char name[20]; //declare a new string cout << “Enter a name…”; //request a name cin >> name; // read the name // ………………………… Display the name cout << “The name you entered was ” << name; }
Good Night Good Night That’s all for this lecture