subject: c++ class:x name of the student: introduction to...

16
1 DELHI PUBLIC SCHOOL JALANDHAR SUBJECT: C++ CLASS:X NAME OF THE STUDENT: _______________________ INTRODUCTION TO C++ Concept of Programming A program is a sequence of instructions (called programming statements), executing one after another - usually in a sequentialmanner. • Computers require both hardware and software to operate. Software consists of instructions thatcontrol the hardware. • At the lowest level, the instructions for a computer program can be represented as a sequence of zerosand ones. • Application software can be written largely without regard to the underlying hardware. A tool called compiler translates the higher-level, abstract language into the machine language required by theHardware. • Programmers develop software using tools such as editors, compilers, debuggers, and profilers. • C++ is a higher-level programming language. • An IDE is an integrated development environment—one program that provides all the tools those developersneeds to write software. A Brief History of C++ C++ was developed by BjarneStroustrup of AT&T Bell Laboratories in the early 1980's, and is based on the C language. The "++" is a syntactic construct used in C (to increment a variable), and C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled (i.e. converted into a series of low- level instructions that the computer can execute directly) using a C++ compiler.

Upload: dangdan

Post on 21-Mar-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

1

DELHI PUBLIC SCHOOL JALANDHAR

SUBJECT: C++ CLASS:X

NAME OF THE STUDENT: _______________________

INTRODUCTION TO C++

Concept of Programming

A program is a sequence of instructions (called programming statements), executing one after another - usually in a sequentialmanner. • Computers require both hardware and software to operate. Software consists of instructions thatcontrol the hardware. • At the lowest level, the instructions for a computer program can be represented as a sequence of zerosand ones. • Application software can be written largely without regard to the underlying hardware. A tool called compiler translates the higher-level, abstract language into the machine language required by theHardware. • Programmers develop software using tools such as editors, compilers, debuggers, and profilers. • C++ is a higher-level programming language. • An IDE is an integrated development environment—one program that provides all the tools those developersneeds to write software.

A Brief History of C++

C++ was developed by BjarneStroustrup of AT&T Bell Laboratories in the early 1980's, and is based on the C language. The "++" is a syntactic construct used in C (to increment a variable), and C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled (i.e. converted into a series of low-level instructions that the computer can execute directly) using a C++ compiler.

Page 2: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

2

Classes and Objects

A class is a blueprint of an object. You can think of a class as a concept, and the object is the embodiment of that concept. You need to have a class before you can create an object.

So, let's say you want to use a person in your program. You want to be able to describe the person and have the person do something. A class called 'person' would provide a blueprint for what a person looks like and what a person can do. To actually use a person in your program you need to create an object.

"Object" refers to a particular instance of a class. You use the person class to create an object of the type 'person.' Now you can describe this person and have it do something.

Classes are very useful in programming. Consider the example of where you don't want to use just one person but 100 people. Rather than describing each one in detail from scratch, you can use the same person class to create 100 objects of the type 'person.' You still have to give each one a name and other properties, but the basic structure of what a person looks like is the same.

Page 3: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

3

Concept of Object Oriented Programming

Object oriented programming is a programming style that is associated with the concept of OBJECTS, having datafields and related member functions. C++ can be said to be as C language with classes. In C++ everything revolves around object of class, which has their methods & data members.

Forexample: We consider human body as a class, we do have multiple objects of this class, with variable as color, hair etc. And methods as walking, speaking etc.

Page 4: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

4

C++ PROGRAM STRUCTURE: Let us look at a simple code that would print the words Hello World.

#include<iostream.h>

void main()// main() is where program execution begins.

{

Cout<<"This is my first C++ program";// prints Hello World

}

Let us look various parts of the above program:

The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream.h> is needed.

The linemain() is the main function where program execution begins.

The next line // main() is where program execution begins. Is a single-line comment available in C++. Single-line comments begin with // and stop at the end of the line.

The next line cout<< "This is my first C++ program."; causes the message "This is my first C++ program" to be displayed on the screen.

Page 5: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

5

INPUT AND OUTPUT IN C++ Input via cin>>

cin>>firstInt; We then use "cin>>firstInt" to read the user input from the keyboard and store the value into variablefirstInt. cin is known as the standard input device (or Console Input), i.e., keyboard, and >> is known as stream extraction operator.

Output via cout<<

In C++, output to the display console is done via "cout" and the stream insertion (or put-to) operator <<. You can print as many items as you wish to cout, by chaining the items with the << operator. For example, Cout<< “hello “<<”world”<<endl;

Page 6: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

6

C++ TERMINOLOGY

1. Statement: A programming statement performs a piece of programming action. It must be terminated by a semicolon (;)

2. Preprocessor Directive: The #include is a preprocessor

directive and NOT a programming statement. A preprocessor directive begins with hash sign (#). It is processed before compiling the program. A preprocessor directive is NOT terminated by a semicolon - take note of this unusual rule.

3. Block: A block is a group of programming statements enclosed by

braces { }. This group of statements is treated as one single unit. There is one block in this program, which contains the body of the main() function. There is no need to put a semicolon after the closing brace.

4. Comments: A multi-line comment begins with /* and ends

with */, which may span more than one line. An end-of-line comment begins with // and lasts till the end of the line. Comments are NOT executable statements.

5. Case Sensitivity: C++ is case sensitive - a ROSE is NOT a Rose,

and is NOT a rose.

Page 7: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

7

C++ BASIC ELEMENTS

Programming language is a set of rules, symbols, and special words used to construct programs. There are certain elements that are common to all programming languages. Now, we will discuss these elements in brief:

C++ CHARACTER SET

Character set is a set of valid characters that a language can recognize.

Letters A-Z, a-z

Digits 0-9

Special Characters

Space + - * / ^ \ () [] {} = != <> ‘ “ $ , ; : % ! & ? _ # <= >= @

Formatting characters

backspace, horizontal tab, vertical tab, form feed, and carriage return

TOKENS

A token is a group of characters that logically belong together. The programmer can write a program by using tokens. C++ uses the followingtypes of tokens. Keywords,Identifiers, Literals, Punctuators, Operators etc

Page 8: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

8

KEYWORDS

Keywords are words reserved as part of the language. They cannot be used by the programmer to name things i.e. they cannot be used as identifiers.. They have special meaning to the compiler

C++ TOKENS

Page 9: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

9

DATA TYPES IN C++

Data type is defined as the type of data a variable will have in it i.e. They are used to define type of variables and contents used. Data types define the way you use storage in the programs you write. The following are 4 basic data types used in c++

DATATYPE SIZE

Char ( 1 byte )

Int ( 2 bytes )

Float ( 4 bytes )

Double ( 8 bytes )

Example :

Char a = 'A'; // character type Inta = 1; // integer type Float a = 3.14159; // floating point type Double a = 6e-4; // double type (e is for exponential)

Page 10: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

10

VARIABLES

Variable are used in C++, where we need storage for any value, which will change in program. Variable is the name of memory location allocated by te compiler depending upon the data type of the variable.

Declaration and Initialization

Variable must be declared before they are used. Usually it is preferred to declare them at the starting of the program, but in C++ they can be declared in the middle of program too, but must be done before using them.

Example :Inti; // declared but not initialized

Inti, j, k; // Multiple declaration

Initialization means assigning value to an already declared variable,

Inti; // declaration

I = 10; // initialization

Initialization and declaration can be done in one single step also,

Int i=10; //initialization and declaration in same step

Inti=10, j=11;

Page 11: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

11

CONSTANTS

Constants refer to fixed values that the program may not alter. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Constants are also called literals.

Definition: "A constant value is the one which does not change during the execution of a program."

1. Integer Constants-Integer constants consist of one or more digits

such as 0,1,2,3,4 or -115.

2. Real Constants-Floating point constants contain a decimal point

such as 4.15, -10.05.

3. Single Character Constants-Character constants specify the

numeric value of that particular character such as ‘a’ is the value

of a.

4. String Constants-String constants consist of characters enclosed in double quotes such as“Hello, World”

Page 12: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

12

IDENTIFIERS

An identifier is needed to name a variable (or any other entity such as a function or a class). C++ imposes the following rules on identifiers: An identifier is a sequence of characters, of up to a certain length

comprising uppercase and lowercase letters (a-z, A-Z), digits (0-9), and underscore "_".

White space (blank, tab, new-line) and other special characters (such as +, -, *, /, @, &, commas, etc.) are not allowed.

An identifier must begin with a letter or underscore. It cannot begin with a digit. Identifiers beginning with an underscore are typically reserved for system use.

An identifier cannot be a reserved keyword or a reserved literal (e.g.,int, double, if, else, for).

Page 13: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

13

OPERATORS IN C++ Operators are special type of functions that takes one or more arguments and produces a new value. For example: addition (+), subtraction (-), multiplication (*) etc, are all operators. Operators are used to perform various operations on variables and constants.

Typesof operators

Arithmetical operators

Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation.

Operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

You can use the operators +, -, *, and / with both integral and floating-point data types. Modulus or remainder % operator is used only with the integral data type.

Binary operators

Operators that have two operands are called binary operators.

Unary operators

Page 14: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

14

C++ provides two unary operators for which only one variable is required. For Example a = - 50; a = + 50; Here plus sign (+) and minus sign (-) are unary because they are not used between two variables.

Relational operators

The relational operators are used to test the relation between two values. All relational operators are binary operators and therefore require two operands. A relational expression returns zero when the relation is false and a non-zero when it is true. The following table shows the relational operators.

Relational Operators Meaning

< Less than

<= Less than or equal to

== Equal to

> Greater than

>= Greater than or equal to

! = Not equal to

Page 15: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

15

Logical operators

The logical operators are used to combine one or more relational expression. The logical operators are

Operators Meaning

|| OR

&& AND

! NOT

Assignment operator

The assignment operator '=' is used for assigning a variable to a value. This operator takes the expression on its right-hand-side and places it into the variable on its left-hand-side. For example: m = 5; The operator takes the expression on the right, 5, and stores it in the variable on the left, m. x = y = z = 32; This code stores the value 32 in each of the three variables x, y, and z.

in addition to standard assignment operator shown above, C++ also support compound assignment operators.

Compound Assignment Operators

Operator Example Equivalent to

+ = A + = 2 A = A + 2

Page 16: SUBJECT: C++ CLASS:X NAME OF THE STUDENT: INTRODUCTION TO C++dpsjalandhar.in/UserSpace/MessageDetails/90/Class X , C NOTES... · NAME OF THE STUDENT: _____ INTRODUCTION TO C++

16

- = A - = 2 A = A - 2

% = A % = 2 A = A % 2

/= A/ = 2 A = A / 2

*= A * = 2 A = A * 2

Increment and Decrement Operators

C++ provides two special operators viz '++' and '--' for incrementing and decrementing the value of a variable by 1. The increment/decrement operator can be used with any type of variable but it cannot be used with any constant. Increment and decrement operators each have two forms, pre and post.

Thesyntax of the increment operator is: Pre-increment:++variable Post-increment:variable++ The syntax of the decrement operator is: Pre-decrement:––variable Post-decrement: variable–– In Prefix form first variable is first incremented/decremented, then evaluated In Postfix form first variable is first evaluated, then incremented/decremented int x, y; int i = 10, j = 10; x = ++i; //add one to i, store the result back in x y = j++; //store the value of j to y then add one to j cout<< x; //11 cout<< y; //10