C++ Question Bank- Module 1
C++ Question Bank- Module 1
MODULE-I
Inheritance:
Inheritance allows a class (called a derived class) to inherit properties and behaviors
(methods) from another class (called the base class). It promotes code reuse and
establishes a parent-child relationship between classes.
Encapsulation:
Encapsulation is the bundling of data and methods that operate on that data within
a class. It restricts direct access to some of the object's components, which is
essential for protecting the object's integrity. It allows for data hiding by using
access specifiers like private, protected, and public.
2. Bring out the difference between object oriented and structured oriented
programming.
Code
Organized around objects and classes. Organized around functions or procedures.
Organization
Data and
Data and functions are combined in
Function Data is often separate from functions.
objects.
Relationship
High reusability through inheritance and Reusability is limited to functions but lacks
Reusability
polymorphism. inheritance and polymorphism.
Data is encapsulated and accessed via Less focus on data security, data can be
Data Security
methods, ensuring better security. easily modified by any function.
Examples of
Java, C++, Python, C#, Ruby, Swift C, Pascal, Fortran, BASIC
Languages
int main()
{
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 15;
myObj.myString = "Some text";
// Print attribute values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;}
class ClassName
{
// Data members (attributes)
public:
// Member functions (methods)
void display()
{
cout << "Hello, World!" << endl;
}
};
int main()
{
// Creating an object of ClassName
ClassName obj; // Declare object obj of class ClassName
// Accessing member function using the object
obj.display(); // Calls the display() function of the object
return 0;
}
6. Explain the basic C++ syntax with an example?
#include<iostream>
using namespace std;
//main is where the program execution begins
int main()
{
cout<<”Hello World”; //prints Hello World
return 0;}
Line 1: #include<iostream> is a header file library that lets us use input and output
objects like cin and cout. Header files add functionality to C++ Programs.
Line 2: using namespace std means that we can use names for objects and variables
from the standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more
readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is
called a function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion
operator (<<) to output/print text. In our example it will output "Hello World!".
Line 6: return 0 ends the main function.
Line 7: Adding the closing curly bracket } is to actually end the main function.
7. What is the abstract class? What is the need of abstract class in C++? Discuss
with an example.
Abstract classes in C++ are a key concept in object-oriented programming that
provide a way to define interfaces and enforce a contract for derived classes. An
abstract class is a class that cannot be instantiated on its own and is typically used
as a base class for other classes.
1. Contains Pure Virtual Functions: An abstract class contains at least one pure
virtual function. A pure virtual function is declared by assigning 0 in its declaration,
indicating that it must be overridden in derived classes.
3. Enforces a Contract: Derived classes must implement all pure virtual functions
from the abstract class. This enforces a consistent interface across different derived
classes.
Code Reusability: An abstract class can implement some functionality that can be
shared across derived classes.