BCS-031 Programming in C++Assignment
BCS-031 Programming in C++Assignment
Ans
In C++, constructors cannot be virtual because when a constructor is called, the object
is not yet fully created, so there is no object on which a virtual function call can operate.
However, there are scenarios where you want to create objects of different derived
classes depending on some condition at runtime. This is where the "virtual constructor"
concept comes in. It is implemented using the Factory Method or Prototype Design
Pattern.
Advantages:
Here’s an example to illustrate the virtual constructor pattern using the Factory Method:
Code in C++
#include <iostream>
// Base class
class Shape {
public:
// Pure virtual function
};
public:
};
public:
};
if (type == 1) {
} else if (type == 2) {
return new Square();
} else {
return nullptr;
int main() {
// Using polymorphism
if (shape1) shape1->draw();
if (shape2) shape2->draw();
// Clean up
delete shape1;
delete shape2;
return 0;
Explanation:
• Shape Class: The base class Shape declares a pure virtual function draw() and a
static method createShape() which acts as a virtual constructor.
• Circle and Square Classes: These are derived classes implementing the draw()
method.
Output:
In this example, the ‘Shape::createShape() method’ is responsible for creating objects
of Circle or Square based on the input provided. This demonstrates the concept of a
virtual constructor by allowing object creation dynamically at runtime.
Conclusion:
While C++ doesn’t support virtual constructors directly, the concept is effectively
implemented using design patterns like the Factory Method or Prototype Pattern. These
patterns provide flexibility and maintainability by enabling dynamic object creation,
which is essential for polymorphism in object-oriented programming.
Q2. What is a Virtual Function? How does it differ from a Pure Virtual Function? Explain
with examples.
A Virtual Function is a function in a base class that is declared with the virtual keyword.
The purpose of a virtual function is to ensure that the correct function is called for an
object, regardless of the type of reference (or pointer) used for function call. This is
essential in achieving polymorphism in C++.
When a derived class overrides a virtual function, and you call that function using a
pointer or reference to the base class, the function in the derived class is called. This
allows for dynamic binding or late binding.
Key Points:
Code C++
#include <iostream>
public:
};
public:
};
int main() {
b = &d;
return 0;
Output:
A Pure Virtual Function is a virtual function that has no implementation in the base
class and is meant to be overridden in derived classes. It is declared by assigning ‘0’ in
the base class. A class containing at least one pure virtual function is considered an
abstract class and cannot be instantiated.
Key Points:
• Must be overridden by derived classes; otherwise, the derived class will also
become abstract.
Code c++
#include <iostream>
class Shape {
public:
};
public:
};
public:
}
};
int main() {
delete s1;
delete s2;
return 0;
Output:
Drawing Circle
Drawing Square
Summary:
Q3. What is a Header File? Explain any 5 header files along with their functions.
Ans
A Header File in C and C++ is a file with a ‘.h’ extension (though in C++ they can also
have ‘.hpp’ or no extension in some cases) that contains declarations of functions,
macros, variables, and other identifiers. These files are included in your source code
files using the ‘#include’ preprocessor directive to provide the necessary information for
compilation.
Header files allow for the separation of interface and implementation. They enable code
reusability and modularity by allowing declarations to be shared across multiple source
files, ensuring that the same declarations are available wherever they are needed
without duplication.
1. <iostream>
o Common Components:
▪ std::cin: Standard input stream (for reading input from the user).
o Example:
Code C++
#include <iostream>
int main() {
return 0;
2. <vector>
o Common Components:
o Example:
Code C++
#include <iostream>
#include <vector>
int main() {
numbers.push_back(6);
return 0;
}
3. <string>
o Purpose: Provides the std::string class, which is used for handling and
manipulating sequences of characters.
o Common Components:
▪ Methods like length(), substr(), find(), append(), etc., are used for
string manipulation.
o Example:
Code C++
#include <iostream>
#include <string>
int main() {
greeting.append(", World!");
return 0;
4. <cmath>
o Common Components:
▪ Functions like sqrt(), pow(), sin(), cos(), log(), etc., are used for
performing mathematical computations.
o Example:
Code C++
#include <iostream>
#include <cmath>
int main() {
std::cout << "The square root of 25 is: " << result << std::endl;
return 0;
5. <algorithm>
o Common Components:
o Example:
Code C++
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::sort(numbers.begin(), numbers.end());
}
return 0;
Summary:
Header files are essential components in C and C++ programming, facilitating code
organization, modularity, and reusability. Common header files like <iostream>,
<vector>, <string>, <cmath>, and <algorithm> provide a wide range of functionalities
that are frequently used in programming tasks, making them indispensable in most C++
programs.