The document discusses the basics of C++ programming, including:
- The structure of a C++ program includes header files, main functions, comments, and input/output instructions.
- There are two types of comments in C++: single-line comments using // and multi-line comments using /* */.
- Variables are used to store data and have a type, name, value, and size. Keywords are reserved words with special meaning. Common data types include int, float, double, and char. Constants cannot be modified after definition.
- The standard input stream cin is used to take input and the output stream cout is used to display output. Basic commands like endl move the cursor
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
45 views18 pages
Chapter 2
The document discusses the basics of C++ programming, including:
- The structure of a C++ program includes header files, main functions, comments, and input/output instructions.
- There are two types of comments in C++: single-line comments using // and multi-line comments using /* */.
- Variables are used to store data and have a type, name, value, and size. Keywords are reserved words with special meaning. Common data types include int, float, double, and char. Constants cannot be modified after definition.
- The standard input stream cin is used to take input and the output stream cout is used to display output. Basic commands like endl move the cursor
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18
Chapter 2
Basics of C++ Programming
2.1 Structure of a C++ Program • Any C++ program will have a look like this: #include<iostream.h> Header file main() Main function { /*create variables Comments Input instructions Processing instructions Output instructions */ } 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC Example 1: Write a C++ program to display your personal information. Header file # include<iostream.h> # include<iostream> # include<conio.h> using namespace std ; Main function declaration int main() int main() Open brace of the body of the program { {
clrscr(); cout<<“ My name is Amal ”;
cout<<“ My name is Amal ”; cout<<“ I’m studying at Ibri VC”; body of the program cout<<“ I’m studying at Ibri VTC”; cout<<“ I’m studying in Elect. Dept.”; cout<<“ I’m studying in Elect.Dept.”; getch(); Value return statement return 0; return 0; Close brace of the body of the program } }
12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC
2.2 comments • Comments in c++ are notes by the programmer documenting the program so that the program is clear and easy to read and they are ignored by the compiler. • Types of comments : there are two types of comments , namely: • Single line comment : use this symbol “ // comments” ex. // this is a single line comment • Mutiple line comment : use this symbol “ /* comments ……….. */” ex. /* this is a multiple line comment */ • Note: C ++ is CASE SENSITIVE. • i.e. A small letter is not the same thing as a capital letter. By default the C++ function names will use small letters. 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC 2.3 Variables, Keywords, Data Types and Constants 2.3.1 Variables: Places to store things • Variables are locations in memory for storing data and contain different values at different times. • Rules for writing the variable name: • They must begin with a letter or underscore (_). • Uppercase and lowercase are significant. • It should not be a keyword • White space is not allowed. • it should not contain special character( #,&,$) • Example: • Valid variable names: x1, T_ raise, distance, A, B, a, b, _man • Invalid variable names: 123 %, (area), 3an , sum&avg, int • In a program every variable has Type , Name , Value and Size • Assignment operator(=) is used to assign a value to a variable. • Declare a variable : before using any variable name weDraftsman 12/16/2023 haveandtoElectronics declare thatYearvariable - Academic 2018 - 2019 S1with - Ibri VC 2.3.2 Keywords or reserved words • Keyword or reserved word is one that has special meaning to C++ Programming language. • Example • int, float, main, auto, return, case, switch, do, static, else, for, while, do etc.
12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC
2.3.3 Data Types: • While doing programming in any programming language, you need to use various variables to store various information. You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. • Some of these types are as follows: • int : used to store integers ( a whole number) e.g 5 , 65, 7200 • float: is a 32bit floating point number e.g. 12.52 , 15.3 • double: is similar to the float, but is a 64 bit representation • char : used to represent a single character e.g 'V', 'g', '60' 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC 2.3.4 Constants • Constants refer to fixed values that the program may not alter and they are called literals. • Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating Point Numerals, Characters, Strings and Boolean Values. • Again, constants are treated just like regular variables except that their values cannot be modified after their definition. • Defining Constants: There are two simple ways in C++ to define constants: • Using #define preprocessor. Ex. #define Length 30 CONST1.CPP
• Using const keyword.
Ex. const int Length= 30 ; CONST2.CPP
12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC
2.4 Basic Input and Output Commands • The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared. • 2.4.1 The Output Stream (cout): • The cout is an output command that sends data given to it to the standard output display device(console). • Adding \n or <<endl within any cout command causes the cursor move to the next line. • Ex. cout<<“The area is : ”<< area ; // we have to use the insertion operator “<<“ with cout. • 2.4.2 The Input Stream (cin) • The cin command reads in information from the keyboard via the standard input stream. • Ex. cin>>x>>y ; // we have to use the extraction operator “>>” with cin command.
12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC
Exercises • 1. Write a C++ program to display the following message: “welcome to the world of C++” . Note: Add the following comment at the beginning: “My first program in C++” • The Answer // My first program in C++ #include <iostream.h> #include<conio.h> void main() { clrscr(); // Clear Screen cout<<“welcome to the world of C++” ; getch(); // Wait to take a character from the user } 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC 2. Write a C++ program input three numbers and display the sum. The Answer #include <iostream.h> #include<conio.h> void main() { clrscr(); int FirstNum , SecondNum , ThirdNum , Sum ; cout<<“Please Enter The First Number : \n” ; cin>>FirstNum; cout<<“Please Enter The Second Number : \n” ; cin>>SecondNum; cout<<“Please Enter The Third Number :\n” ; cin>>ThirdNum; Sum = FirstNum + SecondNum + ThirdNum ; cout<<“The sum of three numbers is : ” << Sum ; 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC getch(); • 3. Make some modification in the above code to get the following output: X=5 Y=6 Z=3 The sum of 5, 6 and 3 is 14 The Answer #include <iostream.h> #include<conio.h> void main() { clrscr(); int X , Y , Z , Sum ; cout<<“Please Enter Three Numbers : \n” ; cin>> X >>Y >> Z ; // Enter 5 , 6 and 3 Sum = X + Y + Z ; cout<<“ X=“<<X << endl; cout<<“ Y=“<<Y << endl; cout<<“ Z=“<<Z << endl; cout<<“The sum of “ << X <<“ , “ << Y << “and ”<< Z <<“ is ” << Sum ; getch(); 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC 4. Write the output of the following program: /* output using cout */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int x=7; cout<<”the result is:”; cout<<456; cout<<x; getch(); } The Answer the result is:4567 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC • 5. Revise the above program so that new output will be: The result is: 456 7 The Answer #include<iostream.h> #include<iostream.h> void main() { clrscr(); int x=7; cout<<”the result is: \n”; cout<<456 <<endl ; cout<<x; getch(); } 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC • 7. Write a C++ program to find the area of: • Circle • Triangle • Square • rectangle The Answer : C++ program to find the area of a Circle #include <iostream.h> #include<conio.h> void main() { clrscr(); const float PI = 3.14 ; float r , Area ; cout<<“Please Enter The radius of a circle :” ; cin>> r ; Area = r * r * PI ; cout<<“The area of a circle is : “ << Area ; getch(); 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC } C++ program to find the area of a Triangle #include <iostream.h> #include<conio.h> void main() { clrscr(); float b , h , Area ; cout<<“Please Enter The Base and Height a Triangle :” ; cin>> b >> h ; Area = 0.5 * b * h ; cout<<“The area of a Triangle is : “ << Area ; getch(); } 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC C++ program to find the area of a Square #include <iostream.h> #include<conio.h> void main() { clrscr(); float S , Area ; cout<<“Please Enter The Side length of a Square :” ; cin>> S ; Area = S * S ; cout<<“The area of a Square is : “ << Area ; getch(); } 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC C++ program to find the area of a Rectangle #include <iostream.h> #include<conio.h> void main() { clrscr(); float L , W , Area ; cout<<“Please Enter The Length and Width of a Rectangle :” ; cin>> L >> W ; Area = L * W ; cout<<“The area of a Rectangle is : “ << Area ; getch(); } 12/16/2023 Draftsman and Electronics - Academic Year 2018 - 2019 S1 - Ibri VC