0
813views
Introduction to Programming in C ++
1 Answer
0
2views

Program Style The C++ programming language is a free form language: it has no requirements about where program elements must be placed on the line or on the page. Consequently the programmer has complete freedom of program style. But experienced programmers know that the tasks of writing, debugging, and maintaining successful software are greatly facilitated by using a consistent, readable programming style. Moreover, others will find your programs easier to read if you conform to standard style conventions. Here are some simple rules that most C++ programmers follow:

  • Put all your #include directives at the beginning of your file.
  • Put each statement on a new line.
  • Indent all statements within a block.
  • Leave a space on either side of an operator, like this: n = 4.

Another worthwhile convention to follow is to choose your variable names carefully. Use short names to minimize the chances for typographical errors. But also pick names that describe what the variable represents. This is called self-documenting code.

The Semicolon

In C++, the semicolon is used as a statement terminator. Every statement must end with a semicolon. Note that lines that begin with the pound symbol # such as #include do not end with a semicolon because they are not statements; they are preprocessing directives.

Simple Arithmetic Operators

An operator is a symbol that “operates” on one or more expressions, producing a value that can be assigned to a variable. Some of the simplest operators are the operators that do arithmetic: +, -, *, /, and %. These five operators are summarized in the following table and illustrated in the example below.

Operator Use Example Result
+ To add two numbers i=3+1 4
- For Subtraction i=3-1 2
* For multiplication i=3*1 3
/ For Division i=3/1 3
$\%$ For Modular Division i= $3 \% 1$ 0

Variable Declaration

C++ requires every variable to be declared with its type before its first use. This informs the compiler the size to reserve in memory for the variable and how to interpret its value.

Syntax:

int area; //integer variable declaration.

float num; //float variable declaration.

Keyword Description Example
int An integer type 5, 15, 968
float A real number type 12.005, 96.36

Input Operator

In C++, input is analogous to output. But instead of data flowing out to the output stream tout, we have data flowing in from the input stream tin (pronounced “see-in”). The name stands for “console input.”

The symbol $\gt \gt$ is the extraction operator, also called the input operator. It is usually used with the tin input stream, which is usually the user’s keyboard.

Output Operator

The symbol $\lt \lt$ is called the insertion operator or the output operator. It inserts objects into the output stream named on its left. We usually use the tout output stream, which ordinarily refers to the computer screen. So cout $\lt \lt$ 6 6 would display the number 66 on the screen.

if Statement

The if statement allows conditional execution. Its syntax is

if (condition) Statement;

where condition on is an integer expression and statement is any executable statement. The statement t will be executed only if the condition on has a nonzero value. (Whenever an integer expression is being evaluated as a condition, a nonzero value is interpreted to mean “true” and a zero value to mean “false.“)

if……else Statement The if……else statement executes one of two alternative statements, according to the value of the specified condition. It has the syntax.

if (condition) statement;

else statement 2;

where condition is an integer expression, and statement 1 and statement 2 are any statements. The statement1 is executed if the condition has a non zero value, and statement2 is executed if the condition has a zero value.

Libraries

A software library for a programming language is a collection of software components that can be used in any program written in that language. These components contain definitions of constants, classes, objects, and functions that can be used as if they were part of the definition of the language itself. For example, the < iostream.h> header file is a component of the Standard C++ Library. It defines the tout object that we use for output in C++ programs.

Following are the some standard files,

< iostream >: several standard stream objects

< conio.h >: used mostly by MS-DOS compilers to provide console input/output.

< graphics.h >: used for graphical output

< math.h >: declares a set of functions to compute common mathematical operations and transformations.

Functions of graphics.h

Function Declaration Description
circle circle(int x, int y, int radius); Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle.
ellipse ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively.
line line(int x1, int y1, int x2, int y2); line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line.The code given below draws a line.
putpixel putpixel(int x, int y, int color); if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw circles, lines and ellipses using various algorithms.
setcolor setcolor(int color); In Turbo Graphics each color is assigned a number. Total 16 colors are available. Strictly speaking number of available colors depends on current graphics mode and driver.For Example :- BLACK is assigned 0, RED is assigned 4 etc. setcolor function is used to change the current drawing color.e.g. setcolor(RED) or setcolor(4) changes the current drawing color to RED. Remember that default drawing color is WHITE.
rectangle rectangle(int left, int top, int right, int bottom); rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

Graphics Mode Initialization – initgraph() Function

First of all we call the initgraph() function that will initialize the graphics mode on the computer. The method initigraph() has the following prototype.

int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode,”C:\TC\BGI”);

Please log in to add an answer.