Вы находитесь на странице: 1из 13

C++

1. Procedure Oriented 2. Object Oriented Characteristics of C++ 1. 2. 3. 4. 5. 6. Emphasis is on doing things. Large programs are divided into smaller one called functions. Most of the functions share global data. Data move openly around the system from function to function. Functions transform data from one form to another. Employs top-down approach in program design.

Characteristics of Object Oriented 1. 2. 3. 4. 5. 6. 7. 8. Emphasis is on data rather than procedures. Programs are divided into what are known as objects. Data structures are designed such that they characterize the objects. Functions that operate on data of an object are tried together in the data structure. Data is hidden and cannot be accessed by external functions. Objects communicate with each other through functions. New data and functions can be easily added. Follows bottom-up approach in program design.

Characteristics of OOPS 1. 2. 3. 4. 5. 6. 7. Objects Classes Data Abstraction & Encapsulation Inheritance - code reusability feature Polymorphism many forms Dynamic Finding Message Passing

Difference between C & C++ SL C SL C++ 1. It is designed in the year of 1972 by 1. It is designed in the year of 1983 by Dennis Ritchie at AT&T Bell Labs. Bjorn Stroustup AT&T Bell Labs. 2. It was derived from BCPL (Basic 2. It was derived from C with Classes. But Combined Programming Language). it did not become popular at that time. And as it is increment of C language, it was named as C++. 3. It divides the whole problems into number 3. It follows bottom up modular of sub-modules & executes each and programming approach, i.e., it simply every sub-module as an independent one collect or design the separate-separate and combines all the modules to get the modules for different operations & final result / goal. combine them to get the result. 4. It is a procedure oriented language, i.e., 4. It is a object oriented programming the programming style depends upon the language where the whole programming function and the code sequence. style depends on the objects rather than the code sequence. 5. In this approach, the problem is solved as 5. In this approach, the whole problem is a sequence of things. divided into number of objects. 6. Dynamic declaration is not possible. 6. Dynamic declaration is possible. 7. Data is open around the system from 7. Data is more close to the functions that function to function. operates on it & protects it from accidental modifications from outside the function. 8. More emphasis is on doing the things, i.e., 8. Emphasis is on data rather than the functions. procedures or functions. 9. Programs are divided into functions. 9. Programs are divided into objects. 10 Addition of data and function is very 10 New data and functions can be added . difficult in an existing system. . easily.

C++ Data C++ Data Types Types

User Defined User Defined

Fundamental Fundamental

Derived Derived

structure structure

Integral Integral

Arrays Arrays

enum enum

int int

Pointers Pointers

class class

char char

Functions Functions

union union

Floating Floating

float float

double double

TOKEN In C++ there is 6 tokens, which are as follows: 1. Identifiers Identifiers can be defined as the name of the variables and some other program elements using the combination of the following characters: Alphabets Numerals Underscore Special Characters : a .. z, A .. Z : 0 .. 9 :_ : All characters other than listed as alphabets, numerals and underscore, are treated as special characters.

2. Keywords The keywords are also identifiers but cannot be user defined since they are reserved words. The following words are reserved for use as keywords, which should not be used as variable or identifiers: asm auto brea k case catch char class const continue float new default for operator delete friend private do double else enum extern goto if inline int long protected public register return short signed sizeof static struct switch template this throw try typedef union unsigned virtual void volatile while

3. Constants There are three types of constants: a. String Constants A string constant is a sequence of alphanumeric character enclosed in double quotation marks whose maximum length is 255 characters. For ex: Your name is : or Rs.2000.00 etc. b. Numeric Constants Numeric constants are positive or negative numbers. There are four types of numeric constants: integer constant, floating point constant, hex constant and octal constant.

CONSTANT NAME Integer Constants

DESCRIPTION

EXAMPLE

Integer constants do not contain decimal points. int x,y; Variables can be declared as integers. short int x,y; long int x,y;Q
SL. 1. 2. 3. 4. 5. DATA TYPE int short int long int unsigned short int unsigned long int SIZE 16 or 32 bits 16 bits 32 bits 16 bits 32 bits RANGE 16 bit: -215 to 215 -1 32 bit : -231 to 231 -1 -32,768 to 32767 -2,147,483,648 to -2,147,483,647 Or -231 to 231 -1 0 to 65635 0 to 4,294,967,295

Floating Point Constants

Positive or negative numbers are represented in exponential form (similar to scientific notation). The floating point constant consists of an optionally signed integer or fixed point number (the mantissa) followed by the letter E and e and optionally signed integer (the exponent).
SL. DATA TYPE SIZE RANGE

float f; 9010E10 7810.11E-11 -10.990e8 -1.001e-1

float 4 double 8 long double 12 or 16 Hexadecimal numbers are integer numbers of base 16 0x0 and their digits are 0 to 9 and A to F (a to f). Any 0x3 hexadecimal number is characterized in the Hex 22x3 constants group, which is represented using the character x. Octal numbers are integer numbers of base 8 and their digits are 0 to 7. Any octal number is characterized in the octal constants group.

1. 2. 3.

(See chart in next page.)

Octal Constants

Hex Constants

c. Character Constants A character represented within single quotes denotes a character constant. For ex: A, a, :, ?, 9 etc.

Numeric Constants Numeric Constants Integer Integer Integer (int x,y;) Integer (int x,y;) Short Integer (short) (short int x,y;) Short Integer (short) (short int x,y;) Long integer (long) (long int x,y;) Long integer (long) (long int x,y;) Float Float Single precision (float) Single precision (float) Double precision (double) Double precision (double) Long double Long double Unsigned Unsigned Unsigned char Unsigned char Unsigned integer Unsigned integer Unsigned short integer Unsigned short integer Unsigned long integer Unsigned long integer Hex Hex Short hexadecimal Short hexadecimal Long hexadecimal Long hexadecimal Octal Octal Short octal Short octal Long octal Long octal

4. Operators Following operators are used in C++: (A) Arithmetic Operators Arithmetic operations are the basic and common operations performed using any computer programming. These are considered as basic operators and known as binary operators as they require two variables to be evaluated.
Operator Meaning

+ * / %

Addition Subtraction Multiplication Division Modulo (Remainder of an integer division) (B) Assignment Operators An assignment operator is used to assign back to a variable, a modified value of the present holding.

Operator Meaning

= += -= *= /= %= >>= <<= &= |= ~=

Assign right hand side value to the left hand side. Value of left hand side variable will be added to the value of right hand side and assign it back to the variable in left hand side. Value of right hand side variable will be subtracted from the value of left hand side and assign it back to the variable in left hand side. Value of left hand side variable will be multiplied by the value of right hand side and assign it back to the variable in left hand side. Value of left hand side variable will be divided by the value of right hand side and assign it back to the variable in left hand side. The remainder will be stored back to the left hand side after integer division is carried out between the left hand side variable and the right hand side variable. Right shift and assign to the left hand side. Left shift and assign to the left hand side. Bitwise AND operation and assign to the left hand side. Bitwise OR operation and assign to the left hand side. Bitwise complement and assigns to the left hand side. (C) Comparison and Logical Operators For program flow, the comparisons as well as the logical operations are required, which are: (i) Relational Operators (ii) Equality Operators (iii) Logical Operators

Operator Meaning Logical Operators Equality Operators Relational Operators

Description

< > <= >= == !=

Less than Greater than Less than or equal to Greater than or equal to Equal to Not equal to

Relational operators compare values to see if they are equal or if one of them is greater than the other and so on.

The operators are normally represented by using the two keys, namely, equal to by the operator = = and not equal to by !=.

&& || !

Logical AND Logical OR Not

The logical operators && and || are lower in precedence than the relational operators < and >, which in turn are lower than + and -. The operators && is higher in precedence than the operator.

(D)
Bitwise Operator

Bitwise Logical Operators


Meaning Example

(AND) & (OR) | (XOR) ^ (E)


Operator UNARY OPERAOR

This will be carried out between the two bit patterns of the x=6,y=3 two operands. x & y =2

Special Operators
Meaning

* & ! ~ ++ -type

Pointer Operator - Used to get the content of the address operator pointing to a particular memory element or cell. Address Operator - Used to get the address of the other variable in an indirect manner. Minus Sign Negation (0, if value # 0, 1, if value =0) Bitwise Complement Incrementer Decrementer Forced type of conversion.

sizeof ?:

Size of subsequent data type or type in byte. Called as Conditional Operator. It is called as Ternary because it uses three expressions. c = ( a > b ) ? A is big. : B is big.;

TERNARY OPERATORSOTHER OPERATORSNEW AND DELETE OPERATORSCOPE OPERATORCOMMA OPERATOR

This operator can be used in two ways, (i) as a separator in variable declaration and (ii)as an operator in an expression for loop

(::)

Used as scope resolution operator in C++. This is operator is also used in a class member function definition.

new delete

In C as the dynamic memory allocations and de-allocation are handled through library functions like malloc, alloc, calloc and free routines, in C++ methods like new and delete are used to carry out memory allocations.

() [] ->

( ) is used for grouping expressions. The precedence and associativity of each operator determines whether it takes effect before or after the next operator in an expression. For ex: (c*d) + a In C++ there are several kinds of variables used, containing a set of values rather than just one, namely, arrays, structures and unions. To represent these variables, membership operators are used, which are represented as [ ] ->.

Rules for Variable Naming 1. The length of a variable name depends upon the operating system where as incase of C language it is up to 32 characters. 2. Comment statements C++ allows only single line comment statements but it also supports multiple line comment statements like C language: // - Single line comment state in C++ /* Multiple line comment Statement in C */ C++ Programming Structure [Documentation] [Preprocessor Directives] [Class Declaration] [Class Definitions] [Global Functions] [Global Variables] main( ) { [Local Functions] [Local Variables] [Executable Statements] }

Standard I/O Operators C++ provides some standard operators to perform the operations which are declared in a header file called iostream.h (input and output stream).
SL. FUNC SYNTAX NAME DESCRIPTION EXAMPLE

01.

cout

02.

cin

cout << variable 1 << This function is used variable 2 << << to display an object variable n; onto the standard device, normally the video screen. The insertion operator (<<) is used along with the cout stream. cin >> variable 1 >> This function is used variable 2 >> to read a number, a variable n; character or a string of characters from a standard input device, normally the keyboard. The extraction operator (>>) is used along with the cin operation.

int x = 123; float y=2.2; cout << x << y; cout << x << \t <<y; cout << x = << x << \t; cout << y = << y << \t; int x = 123; float y=2.2; cout << Enter two numbers \n; cout >> x >> y; cout << x << \n <<y; (or) cout << x = << x << \n; cout << y = << y ;

Вам также может понравиться