0% found this document useful (0 votes)
2 views

C++ Concepts and Definitions

The document provides definitions and examples of various C++ concepts, including arrays, inline functions, static data members, and the cin object. It also explains control statements like switch and continue, as well as object-oriented programming concepts such as objects, destructors, pointers, abstraction, subclasses, and friend functions. Additionally, it lists basic arithmetic operators used in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Concepts and Definitions

The document provides definitions and examples of various C++ concepts, including arrays, inline functions, static data members, and the cin object. It also explains control statements like switch and continue, as well as object-oriented programming concepts such as objects, destructors, pointers, abstraction, subclasses, and friend functions. Additionally, it lists basic arithmetic operators used in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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};

b. What is an inline function?


An inline function is a function where the compiler replaces the function call with the
function's code to reduce overhead. It is defined using the inline keyword.
Example:
inline int square(int x) { return x * x; }

c. What are static data members?


Static data members are variables declared with the static keyword inside a class. They are
shared among all objects of the class and have a single memory location.
Example:
class MyClass { static int count; };

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;

e. What is the use of the `?:` operator?


The `?:` operator (ternary operator) is a shorthand for the `if-else` statement.
Syntax: condition ? expression1 : expression2;
Example:
int x = 10, y = 20;
int max = (x > y) ? x : y;

f. What is the function of the switch statement?


A `switch` statement allows executing one code block among multiple options based on the
value of a variable or expression.
Example:
switch (x) {
case 1: cout << "One"; break;
case 2: cout << "Two"; break;
default: cout << "Default";
}

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;

i. What is the function of the continue statement?


The `continue` statement skips the remaining code in the current iteration of a loop and
proceeds to the next iteration.
Example:
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
cout << i << " ";
}

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; };

m. List any four arithmetic operators.


1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)

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;
}

You might also like