DCA1203-Object Oriented Programming – C++ Id
DCA1203-Object Oriented Programming – C++ Id
INTERNAL ASSIGNMENT
SET-I
C++ offers a variety of data types to handle different kinds of data efficiently. These can be
categorized into primitive and user-defined types:
1. Integral Types:
o int: Represents integer values, typically 4 bytes.
o char: Represents single characters, 1 byte.
o bool: Represents Boolean values (true or false), 1 byte.
o wchar_t: Wide characters, 2 or 4 bytes.
2. Floating-Point Types:
o float: Single-precision floating-point, 4 bytes.
o double: Double-precision floating-point, 8 bytes.
o long double: Extended-precision floating-point, 8, 12, or 16 bytes.
3. Size Modifiers:
o short: Short integer, typically 2 bytes.
o long: Long integer, 4 or 8 bytes.
o long long: Larger integer, 8 bytes.
o unsigned: Non-negative integers.
Void Type:
void: Represents the absence of type, mainly used for functions that do not return a
value.
2. What is the difference between the do-while and the while statements?
The do-while and while statements in C++ are both looping constructs used to repeatedly
execute a block of code as long as a specified condition is true. However, they differ in how
they evaluate the condition and when the code block is executed. Here are the key
differences:
cpp
while (condition) {
// Code to execute
}
oBehavior: If the condition is false at the outset, the code block inside the while
loop is never executed.
do-while Loop: The condition is evaluated at the end of each iteration.
cpp
do {
// Code to execute
} while (condition);
o Behavior: The code block inside the do-while loop is always executed at least
once, regardless of whether the condition is true or false initially.
2. Use Cases
while Loop: Used when the number of iterations is not known beforehand and the
loop may not need to execute at all if the condition is false from the start.
o Example: Reading input until a valid input is provided.
cpp
int num;
std::cout << "Enter a positive number: ";
std::cin >> num;
while (num <= 0) {
std::cout << "Enter a positive number: ";
std::cin >> num;
}
do-while Loop: Suitable when the loop body must be executed at least once,
regardless of the condition.
o Example: Displaying a menu to the user at least once before deciding to exit.
cpp
int choice;
do {
std::cout << "Menu:\n";
std::cout << "1. Option 1\n";
std::cout << "2. Option 2\n";
std::cout << "3. Exit\n";
std::cin >> choice;
} while (choice != 3);
Summary
Choosing between the two depends on whether the initial execution of the loop body is
necessary.
Class:
A class is a blueprint for creating objects. It defines a data type by bundling data and methods
that operate on the data into a single unit. Classes encapsulate data for the object and provide
member functions to operate on that data.
Syntax:
cpp
class ClassName {
public:
// Access specifier
// Data members (attributes)
int dataMember;
Access Specifiers: Control the visibility of the class members. Common specifiers
include public, private, and protected.
Data Members: Variables that hold the state of the class.
Member Functions: Functions that define the behavior of the class.
Object:
An object is an instance of a class. When a class is defined, no memory is allocated until an
object of that class is created. Objects are used to access the data members and member
functions of the class.
Syntax:
cpp
Example:
cpp
class Car {
public:
std::string brand;
int year;
void displayInfo() {
std::cout << "Brand: " << brand << ", Year: " << year << std::endl;
}
};
int main() {
Car myCar; // Create an object of Car
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.displayInfo(); // Output: Brand: Toyota, Year: 2020
return 0;
}
Class Definition: Car class with brand and year data members, and displayInfo method.
Object Creation: myCar object of Car class, accessing its members.
In summary, classes and objects are the cornerstones of OOP in C++, with classes defining
the structure and behavior, and objects representing instances of those classes to perform
specific tasks.
SET-II
In C++, an exception is an event that occurs during the execution of a program and disrupts
the normal flow of instructions. Exception handling is a mechanism to handle runtime errors,
allowing a program to continue its execution rather than terminating abruptly.
C++ provides a robust mechanism for handling exceptions using three keywords: try, catch,
and throw.
cpp
throw exception;
cpp
try {
// Code that may throw an exception
}
cpp
Example:
cpp
#include <iostream>
int divide(int a, int b) {
if (b == 0) {
throw std::runtime_error("Division by zero error");
}
return a / b;
}
int main() {
int x = 10, y = 0;
try {
int result = divide(x, y);
std::cout << "Result: " << result << std::endl;
} catch (const std::runtime_error& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
return 0;
}
Explanation:
try Block: Encapsulates the code that might throw an exception.
throw Statement: Throws an exception when a runtime error (division by zero) is
detected.
catch Block: Catches and handles the exception, preventing program termination.
In this way, exception handling in C++ allows for graceful error management and improves
program robustness.
The Standard Template Library (STL) in C++ provides a collection of template classes and
functions designed to simplify data management and algorithms. The key components of STL
are:
1. Containers
Containers store objects and manage data. They are classified into:
2. Algorithms
Algorithms perform operations on containers, such as sorting and searching. They are
implemented as template functions that work with various container types.
3. Iterators
These components collectively provide a powerful framework for efficient data management
and manipulation in C++.
In C++, files are handled using the <fstream> library, which provides three primary classes for
file operations: ifstream, ofstream, and fstream. Each class offers methods to open files, either via
constructors or the open() method.
Constructor Method: Open a file directly by passing the filename to the constructor.
cpp
ifstream file("filename.txt");
cpp
ifstream file;
file.open("filename.txt", ios::in); // Open for reading
Constructor Method: Open a file directly by passing the filename to the constructor.
cpp
ofstream file("filename.txt");
cpp
ofstream file;
file.open("filename.txt", ios::out); // Open for writing
cpp
fstream file("filename.txt");
cpp
fstream file;
file.open("filename.txt", ios::in | ios::out); // Open for both reading and writing
File modes can be specified with flags such as ios::in, ios::out, ios::app, and ios::trunc.