A Quick Guide for C++ #1
Review of C++ language
○OPP : OOP emphasizes on data
The ideology here is to unite both the data and function that operate on
that data into a single unit called as "object".
○OOP view any problem as object rather than as a procedure
For example,
we can say 'mobile' is an object and its characteristics are color, weight, display,size etc.
Its features include price, voice call, video call, memory size etc.
Here OOP considers the characteristics as data and features as functions.
○Another important concept with respect to OOP is the 'Class'. A class as plan or blueprint that specifies what data and what functions should be included in the objects of that class.
OOP characteristics
The characteristics of OOP are:1) Abstraction
2) Data encapsulation
3) Inheritance
4) Polymorphism
5) Dynamic binding
6) Message Passing
□ Modularity
Modularity is a concept where given problem is divided into sub-problems and the sub-problems are further divided. Each sub-problem is solved by writing asubprogram. Each subprogram is called a module.
□ Abstraction
Abstraction is an act which represents the essential features of an entity without including explanations or any background details about it.OOP implements abstraction using classes as a list of abstract attributes.
□ Data Encapsulation
The binding of data and functions into a single unit called as the class is known as encapsulation.The concept of insulating the data from direct access by the program is called data hiding
□ Inheritance
Inheritance is the process by which objects of one class acquires the properties of the objects of another class□ Polymorphism
Polymorphism means taking more than one form. Polymorphism is the ability for a message to be processed in more than one form.The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
Using a single function name to perform different types of tasks is known as function overloading
Polymorphism allows objects to have internal structures to share the same external interface. It supports to implement inheritance to a great extent
□ Dynamic binding
Binding means linking of a function call to the code to be executed when the function is called.Dynamic Binding means that the code associated with a function call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
□ Message Passing
The objects of a class can communicate with eachThe objects communicate with each other by sending and receiving messages. A message means a request for the execution of a function for an object.Therefore, a
message invokes a function in the form of an object to generate the desired output.
For example,
consider the message:
stack1.push();
Here, we are invoking a function push of an object stack1. stack1 is a object of the class stack.(This will be more clear when we see the structure of C++ )
Fundamentals of C++
Bjarne Stroustrup developed C++ at AT & T Bell Laboratories in Murray Hill, New Jersey. He developed this language in early 1980's and was very muchconcerned in making C++ more efficient.
C++ character set
The following are the character set in C++.Alphabets A, B, ...., Za, b, ...., 2
Numerals 0, 1, 2, ...., 9
Special characters: +- * % / 1.<>,= _@!^&- etc
Tokens
A token is a smallest individual unit in a program or a lexical unit.The most fundamental elements of a C++ program are "tokens". These element help us to construct statements, definitions, declarations, and so on, which in turn helps us to construct complete programs. C++ has following tokens:
1) Identifiers
2) Keywords
3) Constants
4) Punctuators
5) Operators
□Identifiers
An identifier is a name given to the programming elements such as variables ,arrays,functions etc. It can contain letter, digits and underscore. C++ is case sensitive (meaning it treats uppercase and lowercase characters differently)Exampe:
Student STUDENT
amount AMOUNT
marks
RANKS
ad12
total score
□ Keywords
Keyword is a predefined word that gives special meaning the compilerThey are reserved words which can be used for their intended purpose and should not be used as normal identiner. Some of the keywords are given below
bool
and
asm
char
auto
break
case
□Constants or Literals
Constant is an identifier whose value is fixed and does not change during execution of the program.
□ Integer constants:
Integer constants are numbers that has no fractional part or exponent. It refers to the whole numbers. Integers always begin with a digit or or -
We can specify integer constants in decimal, octal, or hexadecimal form
□Unsigned constants:
To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix.
Example: 328u , Ox7FFFFFL , 0776745ul
□Floating point constants
Floating-point constants are also called as real constants. These values contain decimal points (.) and can contain exponents. They are used to represent values that will have a fractional part and can be represented in two forms - fractional form and exponent form.
In the fractional form, the fractional number contains integer part and fractional part. A dot (.) is used to separate the integer part and fractional part.
Example: 65.05 , 125.5 , -125.75
In the exponential form, the fractional number contains constants a mantissa and exponent. Mantissa contains the value of the number and the exponent contains the magnitude of the number. The exponent, if present, specifies the magnitude of the number as a power of 10.
Example: 7.6: 23,46e0 // 23.46 x 10° - 23.46 x 1 - 23.46
23.46e1 // 23.46 x 10- 23.46 x10 - 234.6
□Character constants
Character constants are specified as single character enclosed in pair of quotation marks. Single character constants are internally represented as ASCII codes.
For example: 'A' 'p' '5'
There is another class of characters which cannot be displayed on the screen (non-graphic characters) but they have special meaning and are used for special purpose. Such character constants are called
constants are called as escape sequences.
For example: \' , \" , \\ , \0 , \a , \t
String constants
A string zero or more characters enclosed by double quotation Marks (") is called a string or string constant.
String constants are treated as an array of char. By default, the compiler adds a special character called the 'null character' ('\o') at the end of the string to mark the end of the string.
For example:
"Empress College"
"Tumkur"
"C++ Programming\0"
Punctuators
Punctuators are symbols in C++ and have syntactic and semantic meaning to the compiler. But do not by themselves specify an operation that yields a value
Some punctuators can be either alone or in combination. The following characters are considered as punctuators:
For example: ! % & * ( ) -
C++ Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.The operators can be either 'unary' or 'binary' or even 'ternary' operators.
□Unary operators
Unary operations have only one operand.
For example: ! , & , ~ , * , ++ , -- , + , -
□ Binary operators:
The binary operators are those operators that operate on two operands. These operators can be arithmetic operators, relational, logical operators, bitwise, assignment operators.
□ Arithmetic Operators:
The arithmetic operators are:
+ , - * , / , %
These operators are used in arithmetic expressions
□ Relational Operators:
The relational operators are: == != > < >= <=These operators are used in relational operations. The relational operations always give 0 (or False) or 1 (or True)
□ Logical Operators
The logical operators supported by C++ are: &&
|| and !
logical operators are used in logical expressions. Such expressions always
give 0 (or False) or 1 (or True)
□ Bitwise Operators
Bitwise operator works on bits. The bitwise operators are: &
| ^ ~ << >>
□ shorthand operators
We combine the assignment operators with arithmetic operators and bitwise
operators.
Such operators are called as shorthand operators.
The shorthand operators are:
+= , -= , * = ,%=
&= , |= ^=
□ Assignment Operator
It is an operator used to assign a value to a variable.
The syntax is variable = value or expression;
Example: n = 10;
sum = n +
35;
□ Special Operators
Some special operators are there in C++. They are
sizeof() comma(,) dot(.)
pointer(*)
□Ternary operators
The ternary operators are those operators that operate on three or more
operands. Ternary operator is also called as conditional operator.
? is the ternary operator. This operator can be used as an alternative to
if-else statement.
Precedence of operators or Hierarchy of operators
If an expression contains multiple operators, the order in which
operations are to be carried out is called hierarchy.
Example : x = 7 + 3 * 2;
Here x is assigned 13. not 20 because operator * has higher precedence
than +
First multiply 3 and 2 and then adds into 7 and assign it to the variable
x.
Type Conversion
Converting an expression of a given type into another type is known as
type conversion
Type conversions are of two types, they
☆Implicit conversion
☆Explicit conversion
Explicit conversion will be performed by the user. The user can convert an expression from one type to another type. Such conversions are called a explicit conversion or type casting.
Type conversions are of two types, they
☆Implicit conversion
☆Explicit conversion
Implicit conversions do not
require any type conversion operator. They are automatically performed
when a value is copied to a compatible type.The C++ compiler will
implicitly does conversion.
Explicit conversion will be performed by the user. The user can convert an expression from one type to another type. Such conversions are called a explicit conversion or type casting.
Comments
Post a Comment