C++
C++
Introduction to C++
9. Define a variable. What are the rules for naming variables in C++?
A variable is a named storage location for data.
Naming rules:
• Must start with a letter or underscore.
• Can contain letters, digits, and underscores.
• Case-sensitive.
• Cannot use reserved keywords (e.g., int, class).
10. What are the different data types in C++? Give examples.
Operators
• Addition (+): 5 + 3 = 8
• Subtraction (-): 7 - 4 = 3
• Multiplication (*): 6 * 2 = 12
• Division (/): 10 / 2 = 5
• Modulus (%): 7 % 3 = 1
Control Flow
16. What is the purpose of if, else if, and else statements?
These are conditional statements used for decision-making.
Loops
20. What is the syntax of a for loop? How does it differ from a while loop?
Syntax:
Functions
28. Define and explain the concept of attributes and methods in a class.
29. What are access specifiers in C++? Explain public, private, and
protected.
class Car {
public:
string brand;
};
int main() {
Car myCar;
myCar.brand = "Toyota";
cout << myCar.brand;
}
Miscellaneous Concepts