0% found this document useful (0 votes)
5 views3 pages

C++ Programming Answers

The document provides a detailed overview of C++ programming concepts, differentiating between Object-Oriented and Procedural-Oriented approaches. It explains the concept of classes, library functions, preprocessor directives, as well as the roles of constructors and destructors. Additionally, it discusses loops in C++ with examples for each type.

Uploaded by

Tushar Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

C++ Programming Answers

The document provides a detailed overview of C++ programming concepts, differentiating between Object-Oriented and Procedural-Oriented approaches. It explains the concept of classes, library functions, preprocessor directives, as well as the roles of constructors and destructors. Additionally, it discusses loops in C++ with examples for each type.

Uploaded by

Tushar Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Programming Questions - Detailed Answers

1. Differentiate between Object-Oriented and Procedural-Oriented Approach


 Procedural-Oriented Programming (POP):

• Follows a top-down approach.


• Program is divided into functions or procedures.
• Focus is on writing functions that operate on data.
• Data is not secure as it is shared among functions.
• Less reusability.
• Examples: C, Pascal

 Object-Oriented Programming (OOP):

• Follows a bottom-up approach.


• Program is divided into objects that combine data and functions.
• Focus is on creating reusable classes and objects.
• Data is secure using access specifiers.
• Promotes reusability, modularity, and easy maintenance.
• Examples: C++, Java, Python

2. Explain the Concept of Class in C++


A class is a user-defined data type in C++ that contains both data members and member
functions. It acts as a blueprint for creating objects.

Syntax:

class ClassName {
public:
// data members
// member functions
};

Example:

class Car {
public:
string brand;
void showBrand() {
cout << "Brand: " << brand;
}
};
3. Define Any Three Library Functions and Any Three Preprocessor Directives in
C++
 Library Functions:

1. strlen(str) – Returns the length of a string.


2. sqrt(num) – Returns square root of a number.
3. toupper(ch) – Converts a lowercase character to uppercase.

 Preprocessor Directives:

1. #include <iostream> – Includes a standard header file.


2. #define PI 3.14 – Defines a constant or macro.
3. #ifndef – Checks if a macro is not defined.

4. Discuss the Role of Constructor and Destructor in C++ with Example


Constructor:

• Special function with same name as the class.


• Automatically called when an object is created.
• Used to initialize objects.
• No return type.

Destructor:

• Same name as class but with a tilde (~) prefix.


• Automatically called when an object is destroyed.
• Used to free memory or resources.

Example:

class Demo {
public:
Demo() {
cout << "Constructor called" << endl;
}
~Demo() {
cout << "Destructor called" << endl;
}
};

int main() {
Demo obj; // Constructor is called here
return 0; // Destructor is called automatically
}
5. Explain Loops in C++ with Suitable Example
Loops are used to execute a block of code repeatedly until a condition is false.

1. For Loop:
for(int i = 1; i <= 5; i++) {
cout << i << " ";
}

2. While Loop:
int i = 1;
while(i <= 5) {
cout << i << " ";
i++;
}

3. Do-While Loop:
int i = 1;
do {
cout << i << " ";
i++;
} while(i <= 5);

You might also like