C++ Object-Oriented Flashcards
Expression
An entity with a value that can possibly change that state of memory
Plus
+expr
Minus
-expr
size of
sizeof (expr)
multiplication
expr * expr
Division
expr / expr
remainder
expr % expr
addition
expr + expr
subtraction
expr - expr
simple assignment
variable = expr
comp. assignment
variable op=expr
Literals
Constant values, such as numbers or strings
int num = 10; // 10 is a literal
Variables
Named memory locations whose values can change during program execution
int x = 5;
Operators
Symbols that represent operations to be performed on operands
Arithmetic operators
*
/
Assignment operator
=
Relational operators
==
!=
<
>
<=
>=
Logical operators
&&
||
!
Parentheses
Used to group expressions and alter the order of operations. Can be used to explicitly define the order of evaluation.
int result = (x + y) * 2;
Function calls
Can be part of expressions if they return values
int sum = add(x, y);
Type conversion
When a value is changed from one data type to another in an expression. C++ may perform type conversion when operands of different types are used together in an expression.
int result = 5 + 3.5; // 5 is implicitly converted to a floating-point number
Expression evaluation
Based on the precedence and associativity of operators.
1. Parentheses
2. Unary operators
3. Multiplicative
4. Additive
5. Relational/equality
6. Logical
7. Assignment
Expression evaluation
Based on the precedence and associativity of operators.
1. Parentheses
2. Unary operators
3. Multiplicative
4. Additive
5. Relational/equality
6. Logical
7. Assignment
Primary expression
Basic unit of expressions. Can represent a value or operation in an expression.
Example:
Literal
“Hello” // string literal
Parenthetical Expression
Allows us to change an expression of lower precedence to a primary expression.
(x + 3) * 5 // add x with 3, multiple result by 5
Identifier
Serves as a name for various identities within the program. It can serve as a name for variables, objects, functions etc.
Object (object-oriented programming)
An instance of a class
Object Name
The identifier can be used to name an object, representing a sped instance of a class with its own set of properties and behaviors
MyClass myObject;
Class (object-oriented programming)
A blueprint for creating objects
Class Name
The identifier can be used to name a class, defining the properties and behaviors of objects of that type.
class my class {
// Class definition
};
Namespace
A declarative region that provides a scope for the identifiers declared within it. Identifiers can be used to name namespaces to organize code and prevent conflict.
namespace MyNamespace {
// Namespace contents
}
Program with Parenthetical Expression
include <iostream></iostream>
using namespace std;
int main ()
{
// Variable declaration
int x = 4;
// Printing the first expression with and without parentheses
cout «_space;“Value with parentheses:” «_space;(x + 3) * 5 «_space;endl;
cout «_space;“Value without parentheses:” «_space;x + 3 * 5 «_space;endl «_space;endl;
// Printing the second expression with and without parentheses
cout «_space;“Value with parentheses:” «_space;12 / ( x + 2) «_space;endl;
cout «_space;“Value without parentheses:” «_space;12 / x + 2;
return 0;
}
Unary Expressions
Unary expressions are operations on a single operand. These operations can be** prefix** or postfix, such as increment (++), decrement (–), unary plus (+), unary minus (-), logical negation (!), and bitwise complement (~)
Multiplicative Expressions
Multiplicative expressions involve multiplication (*), division (/), and modulus (%) operations. These expressions evaluate to the product, quotient, or remainder of two operands
Additive Expressions
Additive expressions involve addition (+) and subtraction (-) operations. These expressions evaluate to the sum or difference of two operands.
Assignment Expressions
Assignment expressions involve assigning a value to a variable. They use the assignment operator (=) to assign the value on the right-hand side to the variable on the left-hand side.
Implicit Type Conversion
AKA automatic type conversion, occurs automatically by the compiler when operands of different data types are used in an expression. Does not require any explicit action from the programmer.
Explicit Type Conversion (Casting)
AKA type casting, is performed explicitly by the programmer using special syntax. This allows the programmer to control the type conversion explicitly when necessary.
static_cast <type> (expression</type>
Operator Precedence
determines the order in which operators are evaluated within an expression. Higher precedence is evaluated before lower precedence.
Associativity
determines the order in which operators of the same precedence are evaluated when they appear consecutively. Operators can be left-associative (left to right) or right-associative (right to left). Addition (+) and subtraction (-) are left-associative, they are evaluated from left to right.
Program with Unary Expressions
/*************
* The program shows the use of plus/minus expressions *
*****************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration and initialization
int x = 4;
int y = −10;
// Applying plus and minus operator on variable x
cout << "Using plus operator on x:" << +x << endl;
cout << "Using minus operator on x:" << -x << endl;
// Applying plus and minus operator on variable y
cout << "Using plus operator on y:" << +y << endl;
cout << "Using minus operator on y:" << -y;
return 0;
}</iostream>
sizeof expression
Finds the size of an expression
sizeof (type)
Finds the size of a type