Chapter: Object Oriented Programming and Data Structure : Data Abstraction & Overloading

Initializion

An initial value specified as part of a declaration is called an initializer.

Initializion

 

An initial value specified as part of a declaration is called an initializer.

 

The names used for variables, functions, types, constants, and so forth are collectively known as identifiers. In C++, the rules for identifier formation are

 

1. The name must start with a letter or the underscore character (_).

 

2. All other characters in the name must be letters, digits, or the underscore. No spaces or other special characters are permitted in names.

3. The name must not be one of the reserved keywords

 

Most variables are declared with the body of a function. Such variables are called local variables. The scope of a local variable extends to the end of the block in which it is declared. The lifetime of a local variable is the time during which that function is active. When the function is called, space for each local variable is allocated for the duration of that function call. When the function returns, all its local variables disappear. If a variable declaration appears outside any function definition, that declaration introduces a global variable. The scope of a global variable is the remainder of the file in which it is declared. Its lifetime continues throughout the entire execution of a program. Global variables are therefore able to store values that persist across function calls.

 

A data type is defined by two properties: a domain, which is the set of values that belong to that type, and a set of operations,which defines the behavior of that type. The type int, which corresponds to the standard representation of an integer on the computer system you are using.

 

C++ defines three integer types— shortint, and long

 

The internal size of an integer cannot decrease as you move from short to into long. A compiler designer for C++ could, for example, decide to make short and int the same size but could not make intsmaller than short.

 

 The maximum value of type intmust be at least 32,767 (215–1).

 The maximum value of type long must be at least 2,147,483,647 (231–1).

 

Numbers that include a decimal fraction are called floating-point numbers, which are used to approximate real numbers in mathematics. As with integers, C++ defines three different floating-point types: floatdouble, and long double.

 

The most primitive elements of text data are individual characters, which are represented in C++ using the predefined data typechar.

 

Characters are most useful when they are collected together into sequential units. In programming, a sequence of characters is called a string.

 

it is often necessary to test a particular condition that affects the subsequent behavior of your code. Typically, that condition is specified using an expression whose value is either true or false. This data type—for which the only legal values are true and false—is called Boolean data, In C++, the Boolean type is called bool and its domain consists of the values true and

 

false. all input and output operations—which are often referred to collectively as I/O operations—are performed by calling functions provided as part of a library.

 

int main() {

cout<< "This program averages three numbers." <<endl; cout<< "1st number: ";

 

double n1 = GetReal(); cout<< "2nd number: "; double n2 = GetReal(); cout<< "3rd number: "; double n3 = GetReal();

double average = (n1 + n2 + n3) / 3;

cout<< "The average is " << average <<endl; return 0;

 

}


 In C++, an expression is composed of terms and operators. An operator is a character (or sometimes a short sequence of characters) that indicates a computational operation.

 

The point of listing all the operators in a single table is to establish how they relate to one another in terms of precedence,which is a measure of how tightly an operator binds to its operands in the absence of parentheses.

 

It is, however, important to note that the  operator occurs in two forms. When it is written between two operands, it is abinary operator representing subtraction. When it is written in front of a single operand, as in is -b, it is a unary operatorrepresenting negation. If two operators have the same precedence, they are applied in the order specified by Their associativity, which indicates whether that operator groups to the left or to the right.

 

Most operators in C++ are left-associative, which means that the leftmost operator is evaluated first. A few operators—primarily the assignment operator, which is discussed in more detail later in this chapter—are right-associative, which mean that they are evaluated from right to left.

 

Mixing types in an expression

 

If C++ encounters an operator whose operands are of different numeric types, the compiler automatically converts the operands to a common type by determining which of the two operand types appears closest to the top

 

The operation of discarding a decimal fraction is called truncation.

 

 

Type casts

 

a unary operator that consists of the desired type followed by the value you wish to convert in parentheses

 

The assignment operator

In C++, assignment of values to variables is built into the expression structure. The = operator takes two operands, just like +or *. The left operand must indicate a value that can change, which is typically a variable name. When the assignment operator is executed, the expression on the right-hand side is evaluated, and the resulting value is then stored in the variable that appears on the left-hand side.

 

Assignments that are written as part of larger expressions are called embedded assignments.

 

n1 = (n2 = (n3 = 0));

The expression n3 = 0 is evaluated, which sets n3 to 0 and then passes 0 along as the value of the assignment expression. That value is assigned to n2, and the result is then assigned to n1. Statements of this sort are called multiple assignments. C++ allows you to combine assignment with a binary operator to produce a form called a shorthand assignment. For any binary operator op, the statement

 

variable op= expression;

Increment and decrement operators

 

Beyond the shorthand assignment operators, C++ offers a further level of abbreviation for two particularly common programming operations—adding or subtracting 1 from a variable. Adding 1 to a variable is called incrementing it subtracting 1 is called decrementingit. To indicate these operations in an extremely compact form, C++ uses the operators ++ and --. For example, the statement x++;

 

The first form, in which the operator follows the operand, is called the postfix form, the second, the prefix form.

 

x++ Calculates the value of x first, and then increments it. The value returned to the surrounding expression is the original valuebeforethe increment operation is performed. ++x Increments the value of x first, and then uses the new value as the value of the ++operation as a whole.

 

Procedures and functions enable you to divide a large programming problem into smaller pieces that are individually easy to understand. The process of dividing a problem into manageable pieces, called decomposition, is a fundamental programming strategy.

 

The use of reference parameters makes it possible for functions to change values in the frame of their caller. This mechanism is referred to as call by reference.

C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).

 

When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Data Abstraction & Overloading : Initializion |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.