C++ Concepts and Definitions
C++ Concepts and Definitions
a. Define array.
An array is a collection of elements, all of the same data type, stored in contiguous memory
locations. It allows random access to elements using an index.
Example:
int arr[5] = {1, 2, 3, 4, 5};
d. What is `cin`?
`cin` is an object in C++ used for taking input from the standard input device (keyboard). It
is part of the <iostream> library.
Example:
int x;
cin >> x;
g. Define literal.
A literal is a constant value assigned directly in the code. Examples include integer literals
(10), floating-point literals (3.14), character literals ('a'), and string literals ("hello").
h. Define object.
An object is an instance of a class in object-oriented programming. It encapsulates data
(attributes) and functions (methods).
Example:
class MyClass { public: int x; };
MyClass obj;
j. Define destructor.
A destructor is a special member function in a class that is executed when an object is
destroyed. It is used to release resources.
Example:
class MyClass { ~MyClass() { cout << "Destructor called"; } };
k. Define pointer.
A pointer is a variable that stores the memory address of another variable.
Example:
int x = 10;
int *ptr = &x; // ptr points to x
l. Define abstraction.
Abstraction hides implementation details and shows only the essential features of an object.
It is achieved using abstract classes or interfaces.
Example:
class Shape { virtual void draw() = 0; };
n. Define subclass.
A subclass is a derived class that inherits properties and methods from a parent (base)
class.
Example:
class Parent {};
class Child : public Parent {};
o. Define friend function.
A friend function is not a member of a class but has access to its private and protected
members.
Example:
class MyClass {
private:
int x;
public:
MyClass(int val) : x(val) {}
friend void display(MyClass obj);
};
void display(MyClass obj) {
cout << obj.x;
}