Object-Oriented Programming in C++ Vocabulary Flashcards

Introduction to Object-Oriented Programming and C++ Overview

  • Purpose and Evolution of C++:

    • C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey.

    • It was originally named C with Classes as an enhancement to the C programming language (which is one of the most powerful programming languages).

    • It was officially renamed C++ in 1983.

    • The primary purpose of C++ was to add object-oriented programming (OOP) capabilities to the C language.

  • Language Classification and Characteristics:

    • Statically Typed: Types are checked at compile-time.

    • Compiled: Source code is translated directly into machine code by a compiler.

    • General-Purpose: Suitable for writing applications across diverse domains.

    • Case-Sensitive: Distinguishes between uppercase and lowercase letters (e.g., Manpower and manpower are two distinct identifiers).

    • Free-Form: Program layout and indentation do not dictate code structure or block boundaries.

    • Middle-Level Language: Comprises a combination of both high-level and low-level language features.

    • Multi-Paradigm: Fully supports procedural, object-oriented, and generic programming.

  • Four Pillars of Object-Oriented Development Supported by C++:

    • Encapsulation

    • Data Hiding

    • Inheritance

    • Polymorphism

  • Standard Libraries of C++:

    • Core Language: Provides fundamental building blocks including variables, data types, literals, and control structures.

    • C++ Standard Library: Offers a rich collection of built-in functions for manipulating files, strings, stream operations, etc.

    • Standard Template Library (STL): Provides a rich set of templated algorithms and container methods for data structure manipulation.

  • The ANSI Standard:

    • The ANSI standard ensures portability of C++ source code across various compilers and hardware architectures.

    • Source code compliant with ANSI C++ compiles seamlessly without errors on Microsoft compilers, Mac compilers, UNIX environments, Windows machines, or Alpha platforms.

    • The ANSI standard remains stable and is universally supported by major C++ compiler manufacturers.

Core Concepts of Object-Oriented Programming

  • Object:

    • The fundamental basic unit of object-oriented programming.

    • Represents a real-world physical or logical entity that possesses states (attributes/properties) and behaviors (methods/actions).

    • An object is a concrete instance of a class created at runtime.

    • Examples:

    • A car object has states such as color and number of doors, and behaviors such as accelerate and brake.

    • A dog object has states such as color, name, and breed, and behaviors such as wagging, barking, and eating.


Objects Overview
  • Class:

    • A template or blueprint that defines and describes the behaviors and states that objects of its type support.

    • Does not define actual data directly, but specifies what a class name means—defining what an object of the class consists of and what operations can be performed on it.

  • Methods:

    • Behaviors associated with a class or object.

    • Functions defined within a class where executable logic is written, data is manipulated, and actions are executed.

  • Instance Variables:

    • Unique sets of variables possessed by each distinct object instance.

    • An object's specific state is defined by the values assigned to its instance variables.

  • Data Abstraction:

    • The process of providing only essential information to the outside world while hiding underlying implementation details.

    • Represents required information in a program without revealing internal details (e.g., a database system hiding low-level storage mechanism details).

  • Encapsulation:

    • The mechanism of wrapping and binding data members and the functions operating on that data into a single unit or object framework.

  • Inheritance:

    • The process of forming a new class (derived class) from an existing class (base class).

    • Promotes code reusability and significantly reduces overall program code size.

  • Polymorphism:

    • Derived from the Greek words poly (many) and morphs (forms).

    • The ability to use an operator or function in different ways depending on usage context.

  • Overloading:

    • A branch of polymorphism where an existing operator or function is defined to operate on new or user-defined data types.

C++ Program Structure, Syntax, and Lexical Elements

  • Basic Program Execution Model:

    • A C++ program is defined as a collection of objects that communicate by invoking each other's methods.

  • Sample C++ Program Structure:

#include <iostream>
#include <conio.h>
using namespace std;

// main() is where program execution begins.
int main() {
   cout << "Hello World"; // prints Hello World
   getch();
}
  • Detailed Breakdown of Program Components:

    • #include <iostream>: Preprocessor directive including the standard input/output stream header file required for output/input operations.

    • #include <conio.h>: Console input/output header file used for screen manipulation functions like getch().

    • using namespace std;: Directive instructing the compiler to use the standard (std) namespace, which contains identifiers like cout and cin (alternatively accessible via std::cout and std::cin).

    • // main() is where program execution begins.: Single-line comment starting with // and terminating at the end of the line.

    • int main(): The primary entry-point function where program execution begins.

    • cout << "Hello World";: Stream insertion statement outputting the string literal to the display screen.

    • getch(): Console input function that reads a single character from the keyboard, effectively pausing screen execution until a key is pressed.

  • C++ Identifiers:

    • Names used to identify variables, functions, classes, modules, or user-defined items.

    • Must start with an uppercase letter (AA to ZZ), lowercase letter (aa to zz), or an underscore (_), followed by zero or more letters, underscores, and digits (00 to 99).

    • Punctuation characters such as @, $, and % are explicitly forbidden within identifiers.

    • Case-sensitive language: mohd, zara, abc, move_name, a_123, myname50, _temp, j, a23b9, retVal are valid examples.

    • A variable or identifier name is significant up to 3131 characters; characters beyond 3131 are ignored by the compiler.

  • C++ Reserved Keywords:

    • Reserved words that cannot be used as constant, variable, or identifier names:





Asm

else

new

This

Auto

enum

operator

Throw

Bool

explicit

private

True

Break

export

protected

Try

Case

extern

public

typedef

Catch

false

register

typeid

Char

float

reinterpret_cast

typename

Class

for

return

union

Const

friend

short

unsigned

const_cast

goto

signed

using

Continue

if

sizeof

virtual

Default

inline

static

Void

Delete

int

static_cast

volatile

Do

long

struct

wchar_t

Double

mutable

switch

while

dynamic_cast

namespace

template


  • C++ Trigraph Sequences:

    • A trigraph is a three-character sequence representing a single character, always starting with two question marks (??).

    • Expanded anywhere they appear, including string literals, character literals, comments, and preprocessor directives.

    • Not supported by all compilers and generally discouraged due to confusing syntax.


Trigraph Replacement KeywordsTrigraph Replacement Table HeaderTrigraph Mapping Table

Trigraph

Replacement Character

??=

#

??/

\

??'

^

??(

[

??)

]

??!

|

??<

{

??>

}

??-

~

  • Whitespace Handling in C++:

    • Blank lines containing only whitespace (or comments) are completely ignored by the compiler.

    • Whitespace characters (spaces, tabs, newlines) separate tokens; at least one whitespace character is required between keywords/types and identifiers (e.g., int age;).

    • Whitespace surrounding operators is optional and used for readability (e.g., fruit = apples + oranges;).

  • Comments in C++:

    • Explanatory text ignored by the C++ compiler.

    • Multi-line Comments: Start with /* and end with */ (e.g., /* This is a comment */).

    • Single-line Comments: Start with // and extend to the end of the line (e.g., // prints Hello World).

Data Types, Variable Declaration, and Scope

  • C++ Basic Primitive Data Types:

    • Variables represent named reserved memory locations storing values.


Primitive Data Types Table

Type

Keyword

Boolean

bool

Character

char

Integer

int

Floating point

float

Double floating point

double

Valueless

void

Wide character

wchar_t

  • Type Modifiers:

    • Modify the storage length or sign representation of basic types: signed, unsigned, short, long.

  • Determining Type Sizes with sizeof() Operator:

#include <iostream>
#include <conio.h>
using namespace std;

int main() {
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   getch();
}
  • endl: Inserts a newline character and flushes the output stream buffer.

  • <<: Stream insertion operator passing multiple values to the console.

  • sizeof(): Unary operator returning the memory allocation size (in bytes) of a data type or variable.

    • Variable Definition and Initialization:

  • Definition Syntax:

    • data_type variable_name;

    • data_type variable_name1, variable_name2;

    • Examples: int a, b, c;, char letter;, float area;, double d;

  • Initialization Syntax:

    • data_type variable_name = value;

    • Examples: char letter = 'A';, float area = 26.5;

    • Rules for Declaring Variables:

  • Variable names consist of uppercase letters (AA--ZZ), lowercase letters (aa--zz), digits (00--99), and underscores (_).

  • The first character must be a letter or an underscore.

  • Blank spaces and special characters (#, $, etc.) are prohibited.

  • C++ reserved keywords cannot be used as variable names.

  • Variable names are strictly case-sensitive.

  • Significant length is up to 3131 characters.

  • Valid underlying types include bool, char, int, float, double, void, or wchar_t.

    • Variable Scope Rules:

  • Local Variables:

    • Declared inside a function or code block {}.

    • Accessible only to statements inside that specific function or block.

    • Unknown and inaccessible to external functions.

  • Global Variables:

    • Defined outside of all functions, typically at the top of the program file.

    • Retain values throughout the entire lifetime of the program.

    • Accessible by any function anywhere within the program after declaration.

    • Global and Local Variable Demonstration Code:

#include <iostream>
#include <conio.h>
using namespace std;

// Global Variable declaration
int x, y;
float f;

int main() {
   // Local variable declaration inside nested block
   {
      int tot;
      x = 10;
      y = 20;
      tot = x + y;
      cout << tot << endl;
   }
   f = 70.0 / x;
   cout << f << endl;
   getch();
}
  • Note: Uninitialized local variables contain garbage values, while global variables are automatically initialized to zero by default.

Constants and Operators

  • Defining Constants:

    • The #define Preprocessor Directive:

    • Syntax: #define CONSTANTNAME value

    • Examples: #define PI 3.142, #define LENGTH 20, #define WIDTH 5

    • The const Keyword:

    • Syntax: const datatype CONSTANTNAME = value;

    • Examples: const int LENGTH = 10;, const int WIDTH = 5;

    • Best Practice: Constant identifiers should be written in ALL CAPITALS.

  • C++ Operators Classification:

    • 1. Arithmetic Operators (Assuming A=10A = 10, B=20B = 20):


Arithmetic Operators Table

Operator

Description

Example

+

Addition

A + B results in 30

-

Subtraction

A - B results in -10

*

Multiplication

A * B results in 200

/

Division

B / A results in 2

%

Modulus (remainder after integer division)

B % A results in 0

++

Increment operator (increases integer value by 1)

A++ results in 11

--

Decrement operator (decreases integer value by 1)

A-- results in 9

  • 2. Relational Operators (Assuming A=10A = 10, B=20B = 20):


Relational Operators Table

Operator

Description

Example

==

Equivalent to

(A == B) is false

!=

Not equivalent to

(A != B) is true

>

Greater than

(A > B) is false

<

Less than

(A < B) is true

>=

Greater than or equal to

(A >= B) is false

<=

Less than or equal to

(A <= B) is true

  • 3. Logical Operators (Assuming A=1A = 1 [true], B=0B = 0 [false]):


Logical Operators Table

Operator

Description

Example

&&

Logical AND (True if both operands are non-zero)

(A && B) is false

||

Logical OR (True if any operand is non-zero)

(A || B) is true

!

Logical NOT (Reverses logical state)

!(A && B) is true

  • 4. Bitwise Operators:

    • Perform bit-by-bit operations on integral types (char, int, long). Cannot be used on floating-point types.

    • Operators: Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^), Bitwise NOT (~).


Bitwise Operators Truth Table

pp

qq

~p

p & q

p | q

p ^ q

00

00

11

00

00

00

00

11

11

00

11

11

11

11

00

11

11

00

11

00

00

00

11

11

  • 5. Assignment Operators:

    • Assigns right-hand side value (rvalue) into left-hand side memory location (lvalue).

    • The assignment operator is the only operator that can be overloaded but cannot be inherited.


Assignment Operators Table

Operator

Description

Example

=

Simple assignment operator

C = A + B assigns A + B to C

+=

Add AND assignment operator

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator

C %= A is equivalent to C = C % A

&=

Bitwise AND assignment operator

C &= A is equivalent to C = C & A

^=

Bitwise exclusive OR assignment operator

C ^= A is equivalent to C = C ^ A

|=

Bitwise inclusive OR assignment operator

C |= A is equivalent to C = C | A

  • Operator Precedence:

    • Determines expression evaluation grouping order.

    • Example: x = 7 + 3 * 2; assigns 13 to x (not 20), because multiplication (*) holds higher precedence than addition (+).

Basic Input/Output Streams and Header Files

  • C++ I/O Stream Concept:

    • C++ I/O occurs in streams (sequences of bytes).

    • Input Operation: Byte flow from device (keyboard, disk, network) into main memory.

    • Output Operation: Byte flow from main memory to device (display screen, printer, disk, network).

  • Core I/O Header Files:

    • #include <iostream>: Fundamental header required for standard stream objects (cin, cout, cerr, clog).

    • #include <iomanip>: Required for stream manipulators (e.g., formatting output).

    • #include <fstream>: Required for file stream input/output operations.

  • Standard Stream Objects in <iostream>:

    • cout (Standard Output Stream):

    • Connected to standard output display screen.

    • Uses stream insertion operator << (overloaded for built-in types: int, float, double, strings, pointers).

    • Syntax: `cout <<


  • Purpose and Evolution of C++:

    • C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey.

    • It was originally named C with Classes as an enhancement to the C programming language.

    • It was officially renamed C++ in 1983.

    • The primary purpose of C++ was to add object-oriented programming (OOP) capabilities to the C language.

  • Language Classification and Characteristics:

    • Statically Typed: Types are checked at compile-time.

    • Compiled: Source code is translated directly into machine code by a compiler.

    • General-Purpose: Suitable for writing applications across diverse domains.

    • Case-Sensitive: Distinguishes between uppercase and lowercase letters (e.g., Manpower and manpower are two distinct identifiers).

    • Free-Form: Program layout and indentation do not dictate code structure or block boundaries.

    • Middle-Level Language: Comprises a combination of both high-level and low-level language features.

    • Multi-Paradigm: Fully supports procedural, object-oriented, and generic programming.

  • Four Pillars of Object-Oriented Development Supported by C++:

    • Encapsulation

    • Data Hiding

    • Inheritance

    • Polymorphism

  • Standard Libraries of C++:

    • Core Language: Provides fundamental building blocks including variables, data types, literals, and control structures.

    • C++ Standard Library: Offers a rich collection of built-in functions for manipulating files, strings, stream operations, etc.

    • Standard Template Library (STL): Provides a rich set of templated algorithms and container methods for data structure manipulation.

  • The ANSI Standard:

    • The ANSI standard ensures portability of C++ source code across various compilers and hardware architectures.

    • Source code compliant with ANSI C++ compiles seamlessly without errors on Microsoft compilers, Mac compilers, UNIX environments, Windows machines, or Alpha platforms.

  • Possible Exam Questions & Answers:

    • Q: Explain why C++ is considered a middle-level language.
      A: C++ combines high-level language features (such as object-oriented concepts, abstraction, and rich standard libraries) with low-level language capabilities (such as direct memory access and manipulation using pointers).

    • Q: Who developed C++, in what year, and what was its original name?
      A: C++ was developed by Bjarne Stroustrup in 19791979 at Bell Labs, and it was originally named "C with Classes".

    • Q: List the four pillars of object-oriented development supported by C++.
      A: Encapsulation, Data Hiding, Inheritance, and Polymorphism.

Core Concepts of Object-Oriented Programming
  • Object:

    • The fundamental basic unit of object-oriented programming.

    • Represents a real-world physical or logical entity that possesses states (attributes/properties) and behaviors (methods/actions).

    • An object is a concrete instance of a class created at runtime.

    • Examples:

    • A car object has states such as color and number of doors, and behaviors such as accelerate and brake.

    • A dog object has states such as color, name, and breed, and behaviors such as wagging, barking, and eating.

  • Possible Exam Questions & Answers:

    • Q: Differentiate between a class and an object.
      A: A class is a blueprint or template that defines attributes and methods, whereas an object is a concrete runtime instance of a class that holds actual data values.

    • Q: Define state and behavior in the context of an object.
      A: State refers to the attributes or properties of an object (e.g., color, size), while behavior refers to the methods or actions an object can perform (e.g., drive, bark).

C++ Program Structure, Syntax, and Lexical Elements
  • Basic Program Execution Model:

    • A C++ program is defined as a collection of objects that communicate by invoking each other's methods.

  • Sample C++ Program Structure:

  #include <iostream>
  #include <conio.h>
  using namespace std;

  int main() {
      cout << "Hello World";
      getch();
  }
  • Detailed Breakdown of Program Components:

    • #include <iostream>: Preprocessor directive including standard input/output stream header.

    • #include <conio.h>: Console input/output header file used for functions like getch().

    • using namespace std;: Directive instructing compiler to use the standard namespace.

    • int main(): Primary entry-point function where execution begins.

    • cout << "Hello World";: Stream insertion statement outputting string literal.

    • getch(): Reads single character from keyboard, pausing screen execution.

  • C++ Identifiers:

    • Names used to identify variables, functions, classes, modules, or user-defined items.

    • Must start with a letter (AA to ZZ, aa to zz) or underscore (_), followed by letters, underscores, or digits (00 to 99).

    • Characters beyond 3131 characters are ignored by the compiler.

  • Possible Exam Questions & Answers:

    • Q: What are the rules for defining valid identifiers in C++? Give two valid and two invalid examples.
      A: Identifiers must start with a letter or underscore, cannot contain punctuation like @ or $, and are case-sensitive. Valid: _temp, myVar1. Invalid: 123var, my@var.

    • Q: Explain the function of #include <iostream> and using namespace std;.
      A: #include <iostream> imports standard I/O stream declarations like cout and cin. using namespace std; allows using these identifiers directly without the std:: prefix.

Data Types, Variable Declaration, and Scope
  • C++ Basic Primitive Data Types:

    • bool, char, int, float, double, void, wchar_t.

    • Type modifiers: signed, unsigned, short, long.

  • Variable Scope Rules:

    • Local Variables: Declared inside function/block {}; accessible only within that block.

    • Global Variables: Defined outside all functions; accessible throughout program lifetime. Uninitialized global variables default to zero, while local variables contain garbage values.

  • Possible Exam Questions & Answers:

    • Q: Compare local and global variables in terms of scope, lifetime, and default initial values.
      A: Local variables are scoped within their enclosing block, exist during block execution, and contain garbage values if uninitialized. Global variables are accessible program-wide, exist for the lifetime of the program, and automatically default to zero if uninitialized.

    • Q: What is the difference between call by value and call by reference?
      A: Call by value passes a copy of the argument, so modifications inside the function do not affect the original variable. Call by reference passes the variable's memory address, so modifications affect the original variable directly.

Constants and Operators
  • Defining Constants:

    • #define Preprocessor Directive: #define PI 3.142

    • const Keyword: const int LENGTH = 10;

  • Operators Classification:

    • Arithmetic, Relational, Logical, Bitwise, Assignment, and Misc Operators.

  • Possible Exam Questions & Answers:

    • Q: How is the expression x=7+3Ă—2x = 7 + 3 \times 2 evaluated in C++?
      A: Due to operator precedence, multiplication (*) has higher priority than addition (+), so 3Ă—2=63 \times 2 = 6 is evaluated first, followed by 7+6=137 + 6 = 13. Thus, x=13x = 13.

    • Q: Differentiate between preprocessor constants (#define) and const variables.
      A: #define is a text-replacement preprocessor directive without type safety or scope restriction, whereas const defines typed variables that observe standard C++ scoping and compiler type checking.

Basic Input/Output Streams and Header Files
  • Standard Stream Objects:

    • cout (Standard Output Stream)

    • cin (Standard Input Stream)

    • cerr (Standard Un-buffered Error Stream)

    • clog (Standard Buffered Log Stream)

  • Possible Exam Questions & Answers:

    • Q: Differentiate between cout, cerr, and clog.
      A: cout handles standard output stream; cerr handles standard un-buffered error output for immediate display; clog handles standard buffered error/logging output.

    • Q: What is the purpose of endl and sizeof()?
      A: endl inserts a newline character and flushes the output buffer. sizeof() returns the memory size