C++ Object-Oriented Flashcards

1
Q

Expression

A

An entity with a value that can possibly change that state of memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Plus

A

+expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Minus

A

-expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

size of

A

sizeof (expr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

multiplication

A

expr * expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Division

A

expr / expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

remainder

A

expr % expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

addition

A

expr + expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

subtraction

A

expr - expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

simple assignment

A

variable = expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

comp. assignment

A

variable op=expr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Literals

A

Constant values, such as numbers or strings
int num = 10; // 10 is a literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Variables

A

Named memory locations whose values can change during program execution
int x = 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Operators

A

Symbols that represent operations to be performed on operands

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Arithmetic operators

A

*
/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Assignment operator

A

=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Relational operators

A

==
!=
<
>
<=
>=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Logical operators

A

&&
||
!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Parentheses

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Function calls

A

Can be part of expressions if they return values

int sum = add(x, y);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Type conversion

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Expression evaluation

A

Based on the precedence and associativity of operators.
1. Parentheses
2. Unary operators
3. Multiplicative
4. Additive
5. Relational/equality
6. Logical
7. Assignment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Expression evaluation

A

Based on the precedence and associativity of operators.
1. Parentheses
2. Unary operators
3. Multiplicative
4. Additive
5. Relational/equality
6. Logical
7. Assignment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Primary expression

A

Basic unit of expressions. Can represent a value or operation in an expression.

Example:
Literal
“Hello” // string literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Parenthetical Expression

A

Allows us to change an expression of lower precedence to a primary expression.

(x + 3) * 5 // add x with 3, multiple result by 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Identifier

A

Serves as a name for various identities within the program. It can serve as a name for variables, objects, functions etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Object (object-oriented programming)

A

An instance of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Object Name

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Class (object-oriented programming)

A

A blueprint for creating objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Class Name

A

The identifier can be used to name a class, defining the properties and behaviors of objects of that type.

class my class {
// Class definition
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Namespace

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Program with Parenthetical Expression

A

include <iostream></iostream>

using namespace std;
int main ()
{
// Variable declaration
int x = 4;
// Printing the first expression with and without parentheses
cout &laquo_space;“Value with parentheses:” &laquo_space;(x + 3) * 5 &laquo_space;endl;
cout &laquo_space;“Value without parentheses:” &laquo_space;x + 3 * 5 &laquo_space;endl &laquo_space;endl;
// Printing the second expression with and without parentheses
cout &laquo_space;“Value with parentheses:” &laquo_space;12 / ( x + 2) &laquo_space;endl;
cout &laquo_space;“Value without parentheses:” &laquo_space;12 / x + 2;
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Unary Expressions

A

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 (~)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Multiplicative Expressions

A

Multiplicative expressions involve multiplication (*), division (/), and modulus (%) operations. These expressions evaluate to the product, quotient, or remainder of two operands

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Additive Expressions

A

Additive expressions involve addition (+) and subtraction (-) operations. These expressions evaluate to the sum or difference of two operands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Assignment Expressions

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Implicit Type Conversion

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Explicit Type Conversion (Casting)

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Operator Precedence

A

determines the order in which operators are evaluated within an expression. Higher precedence is evaluated before lower precedence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Associativity

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Program with Unary Expressions

A

/*************
* 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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

sizeof expression

A

Finds the size of an expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

sizeof (type)

A

Finds the size of a type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Multiplication Expression Program

A

/*************
* Shows effects of multiplicative expressions
*
**
***************/
#include <iostream>
using namespace std;
int main ()
{
// Multiplication
cout << "Testing multiplication operator" << endl;
cout << "Value of 3 * 4 = " << 3 * 4 << endl;
cout << "Value of 2.4 * 4.1 = " << 2.4 * 4.1 << endl;
cout << "Value of −3 * 4 = " << −3 * 4 << endl << endl;
// Division
cout << "Testing division operator" << endl;
cout << "Value of 30 / 5 = " << 30 / 5 << endl;
cout << "Value of 4 / 7 = " << 4 / 7 << endl;
// Remainder
cout << "Testing remainder operator" << endl;
cout << "Value of 30 % 5 = " << 30 % 5 << endl;cout << "Value of 30 % 4 = " << 30 % 4 << endl;
cout << "Value of 3 % 7 = " << 3 % 7 << endl;
return 0;
}
Run:
Testing multiplication operator
Value of 3 * 4 = 12
Value of 2.4 * 4.1 = 9.84
Value of -3 * 4 = -12
Testing division operator
Value of 30 / 5 = 6
Value of 4 / 7 = 0
Testing remainder operator
Value of 30 % 5 = 0
Value of 30 % 4 = 2
Value of 3 % 7 = 3</iostream>

45
Q

Compound Assignment

A

Shorthand notation in programming languages, including C++, to perform an operation and assignment in a single step.

variable op= expression;

46
Q

What happens when arithmetic operators are applied to Boolean data types?

A

Boolean data types are subject to implicit type conversion. True is treated as 1 and false is treated as 0.
True + true = 2
False + false = 0
True * true = 1
False * false = 0

47
Q

When using binary operators, what happens if the
operands are of different types? What is the type
of the return value?

A

the compiler typically performs implicit type conversion to make the types compatible for the operation.

The result type is typically the “largest” or “widest” type among the operand types.

48
Q

What happens when arithmetic operators are applied to character data types?

A

Characters are treated as their ASCII or UTF-8 integer equivalents.
‘A’ + ‘B’ = value ‘A’ + value ‘B’
‘A’ * ‘B’ = value ‘A’ * value ‘B’

49
Q

test of data type

A

tool to test the
type of any expression
typeid (expression).name()

50
Q

Implicit Conversion Type Hierarchy

A

int -> unsigned int -> long -> unsigned long -> double -> long double
(lowest to highest)

51
Q

What happens if we try to store a number
larger than the maximum value or smaller than
the minimum value for a type.

A

The result is overflow or underflow.

52
Q

Signed and Unsigned Integers

A

Two different data types to represent whole numbers in C++.

53
Q

Signed Integers

A

Can represent both positive and negative numbers.
int
short
long
long long

54
Q

Unsigned Integers

A

Can only represent zero to a maximum positive value based on number of bits allocated for their storage.

55
Q

Overflow

A

when the result of an arithmetic operation exceeds the maximum representable value for the data type being used.

56
Q

Underflow

A

when the result of an arithmetic operation falls below the minimum representable value for the data type being used. (Not applicable to Unsigned Integers since they can’t represent negative values)

57
Q

Floating-point Overflow and Underflow

A

where the result of a calculation exceeds the range of representable values or becomes too close to zero to be accurately represented

58
Q

Floating-point Overflow

A

the result of a calculation exceeds the maximum finite value that can be represented by the floating-point data type.
The result may become “infinite” or “infinity”, or NaN (not a number)

59
Q

Floating-point Underflow

A

the result of a calculation is too close to zero to be accurately represented by the floating-point data type.
The result may be rounded to zero or represented as a denormalized number.

60
Q

Statement

A

a complete instruction that performs a specific action. Define the behavior and flow of execution within the program.

61
Q

What are Manipulators used for?

A

modify or control the formatting of data being outputted to the console or other output streams. They allow for customization of aspects such as field width, precision, alignment, and output format.

62
Q

Name some common output manipulators in C++

A

std::setw(int n)
std::setprecision(int n)
std::fixed
std::scientific
std::setfill(char c)
std::left and std::right
std::boolalpha
std::hex, std::oct, std::dec

63
Q

What are input manipulators in C++ used for?

A

modify or control the formatting of data being inputted from the console or other input streams. They allow for customization of aspects such as skipping whitespace, converting case, and input base.

64
Q

Name some common input manipulators in C++

A

std::ws
std::skipws
std::noskipws
std::uppercase
std::nouppercase
std::hex, std::oct, std::dec

65
Q

How can output manipulators be used in C++?

A

include <iostream></iostream>

#include <iomanip></iomanip>

int main() {
int x = 123;
std::cout &laquo_space;“Decimal: “ &laquo_space;std::dec &laquo_space;x &laquo_space;std::endl;
std::cout &laquo_space;“Hexadecimal: “ &laquo_space;std::hex &laquo_space;x &laquo_space;std::endl;
std::cout &laquo_space;“Octal: “ &laquo_space;std::oct &laquo_space;x &laquo_space;std::endl;
return 0;
}

Output:
Decimal: 123
Hexadecimal: 7b
Octal: 173

66
Q

What are some examples of no-argument manipulators for output streams?

A

endl
noboolalpha, boolalpha
dec, oct, hex
noshowbase, showbase
fixed, scientific
noshowpoint, showpoint
noshowpos, showpos
nouppercase, uppercase
left, right, internal

67
Q

What manipulators are used to output integers in different bases?

A

dec (base 10)
oct (base 8)
hex (base 16)

68
Q

What manipulators control whether the base of the number is shown as a prefix?

A

noshowbase: Does not show the base of the number (default).
showbase: Shows the base of the number with a prefix (0 for octal, 0x for hexadecimal).

69
Q

What is the purpose of the boolalpha manipulator?

A

It is used to output boolean values as “true” or “false” instead of 1 or 0, respectively.

70
Q

Declaration Statement

A

used to declare a variable or a function, specifying its type and optionally initializing it with a value. A modifier
(called extern) can be added to postpone the definition to some
other part of the program.

Needs a semicolon (;)

71
Q

Single Declaration Statement

A

declares a single variable of a specific type.

Example:
short test;
int sum;
double average;

72
Q

Multiple Declaration Statement

A

declares multiple variables of the same type in a single line.

Example:
int first, second, third;
double average, mean;
char ch;

73
Q

Initialization

A

assigns an initial value to a variable at the time of declaration.

Example:
int first = 0;
double average = 0.0, mean = 0.0;
char ch = ‘a’;

74
Q

Constant Declaration Statement

A

declares a named constant with a specific value that cannot be changed during program execution.

Example:
const int FOOT_TO_INCH = 12;
const double TAX_RATE = 8.5;
const double PI = 3.1415926536;

75
Q

Expression Statement

A

consists of expression followed by semicolon (;). When program encounters expression statement, it evaluates expression then discards result (if any), treating statement as standalone operation

Example:
num *= 10;

76
Q

Null Statement

A

a semicolon (;) by itself, with no other code or operation. It represents an empty or “do nothing” statement

77
Q

Compound Statement

A

AKA a block, is a sequence of statements enclosed within a pair of curly braces {}. Allows multiple statements to be grouped together and treated as a single statement.

Example:
{
int x = 10;
cout &laquo_space;“Value of x: “ &laquo_space;x &laquo_space;endl;
}

78
Q

Return Statement

A

used to terminate execution of function and return a value to caller of function. Used in non-void return type functions, expected to produce a result.

79
Q

Less

A

expr < expr

80
Q

Less or equal

A

expr <= expr

81
Q

Greater

A

>

82
Q

Greater or equal

A

expr >= expr

83
Q

Equal

A

expr == expr

84
Q

Not equal

A

expr != expr

85
Q

Pitfall

A

Avoid using either of the equality operators with floating-point values (precision of value in memory storage unknown)

86
Q

One-Way Selection

A

if (Condition)
{
Statement(s)
}

87
Q

Printing Absolute Value

A

/*************
* Using if-statement to print the absolute value of a number *
**
***************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration
int number;
// Getting input
cout << "Enter an integer:";
cin >> number;
// Finding the absolute value
if (number < 0)
{
number = −number;
}
// Printing the absolute value
cout << "Absolute value of the number you entered is:" << number;</iostream>

return 0;
}
Run:
Enter an integer: 25
Absolute value of the number you entered is: 25
Run:
Enter an integer: −17
Absolute value of the number you entered is: 17

88
Q

Gross Payment of an Employee

A

/*************
* Use of if-statement to find gross payment of an employee *
**
***************/
#include <iostream>
#include <iomanip>;
using namespace std;
int main ()
{
// Declaration;
double hours;
double rate;
double regularPay;
double overPay;
double totalPay;
// Input
cout << "Enter hours worked:";
cin >> hours;
cout << "Enter pay rate:";
cin >> rate;</iomanip></iostream>

// Calculation that does not depend on decision
regularPay = hours * rate;
overPay = 0.0;
// Calculation that is skipped if hours worked is not more than 40
if (hours > 40.0)
{
overPay = (hours − 40.0) * rate * 0.30;
} // End if
// Rest of the calculation
totalPay = regularPay + overPay;
// Printing output
cout &laquo_space;fixed &laquo_space;showpoint;
cout &laquo_space;“Regular pay = “ &laquo_space;setprecision (2) &laquo_space;regularPay &laquo_space;endl;
cout &laquo_space;“Over time pay = “ &laquo_space;setprecision (2) &laquo_space;overPay &laquo_space;endl;
cout &laquo_space;“Total pay = “ &laquo_space;setprecision (2) &laquo_space;totalPay &laquo_space;endl;
return 0;
}

Run:
Enter hours worked: 30
Enter pay rate: 22.00
Regular pay = 660.00
Over time pay = 0.00
Total pay = 660.00
Run:
Enter hours worked: 45
Enter pay rate: 25.00
Regular pay = 1125.00
Over time pay = 37.50
Total pay = 1162.50

89
Q

Pitfalls: Assignment versus Equality

A

if (x = 0) // This statement is always false
{
statement; // The statement is never executed
}

90
Q

Pitfalls: Forgetting Braces

A

if (x == 0)
statement1; // Statement1 is executed if x is 0
statement2; // Statement2 is always executed

91
Q

Pitfalls: Extra Semicolon

A

if (x == 0); // Semicolon makes the body of if-statement empty
{
statement1; // Statement1 is always executed
statement2; // Statement2 is always executed
}

92
Q

Two Way Selection

A

if (boolean-expression)
{
statement1;
}
else
{
statement2;
}

93
Q

Nested if-else Statement

A

if(expr1)
{
if(expr2)
{
statement1;
}
else
{
statement2;
}
}
else
if(expr3)
{
statement3;
}
else
{
statement4;
}
}

94
Q

Dangling else Problem

A

An else is always paired with the most recent unpaired if

Avoid this issue by using block delimiters

95
Q

Multi-way Selection

A

// Compact
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else if (condition 4)
statement 4;
else
statement 5;

96
Q

logical not

A

! expr

97
Q

logical or

A

expr || expr

98
Q

logical and

A

expr && expr

99
Q

NOT expression

A

The NOT operator flips the value of a logical expression

100
Q

Check for Leap Year

A

leapYear = (divisibleBy400) || (divisibleBy4 && !(divisibleBy100))

To check for a leap year, we need to check three conditions. If a year is
divisible by 400, it is definitely a leap year.
Otherwise, if the year is divisible by 4, but not by 100, it is also a leap year.

100
Q

Removing NOT Operator

A

!(x < 7) -> (x >= 7)
!(x > 7) -> (x <= 7)
!(x == 7) -> (x !
= 7)

!(x && y) -> (!x || !y)

101
Q

side effects

A

the observable changes or interactions that occur beyond the direct output or return value of a function or expression. These changes can affect the state of the program, such as modifying variables, altering data structures, or performing input/output operations

102
Q

short-circuit behavior (software)

A

When evaluating logical expressions involving && (logical AND) or || (logical OR), the language sometimes doesn’t need to evaluate the entire expression to determine its truth value. Instead, it stops evaluating the expression as soon as it can deduce the outcome.

Example:
In the expression (3 < 2) && (x = 2), the first condition (3 < 2) is false, so the whole expression is false. Since the first condition is false, the second condition (x = 2) doesn’t even need to be evaluated. So, x remains unchanged.

103
Q

switch statement (multi-way decision)

A

the decision is based on specific values

104
Q

break; statements

A

termination of execution after switch statements

105
Q

default case

A

a fallback option in case none of the preceding case conditions match the value being evaluated. It provides a block of code to execute when none of the other case conditions are met.

e.g. a user doesn’t make a choice between A or B so the program must output a “default” choice C.

106
Q

Combine Cases

A

If the task performed by several cases is the same, we
can combine the cases.

int main ()
{
// Declaration
char grade;
// Input
cout &laquo_space;“Enter a grade (A, B, C, D, F): “;
cin&raquo_space; grade;
// Decision section using switch statement
switch (grade)
{
case ‘A’:
case ‘B’:
case ‘C’: cout &laquo_space;“Grade is pass”;
break;

107
Q

conditional expression (decision making)

A

Only ternary expression in C++

condition ? expression1 : expression2