0% found this document useful (0 votes)
7 views

DCA1203-Object Oriented Programming – C++ Id

The document is an internal assignment for a BCA program, focusing on Object Oriented Programming in C++. It covers various topics including C++ data types, differences between do-while and while loops, classes and objects, exception handling, STL components, and methods to open files. Each section provides detailed explanations and examples relevant to C++ programming concepts.

Uploaded by

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

DCA1203-Object Oriented Programming – C++ Id

The document is an internal assignment for a BCA program, focusing on Object Oriented Programming in C++. It covers various topics including C++ data types, differences between do-while and while loops, classes and objects, exception handling, STL components, and methods to open files. Each section provides detailed explanations and examples relevant to C++ programming concepts.

Uploaded by

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

Centre for Distance & Online Education

INTERNAL ASSIGNMENT

Name Id Mahammd Ray


Roll-Number 2314520696
SESSION FEBRUARY/MARCH 2024
PROGRAM BCA
SEMESTER II
COURSE CODE & NAME DCA1203-Object Oriented Programming – C++

SET-I

1. Describe the various datatypes available in C++?

C++ offers a variety of data types to handle different kinds of data efficiently. These can be
categorized into primitive and user-defined types:

Primitive Data 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.

Derived Data Types:


1. Arrays: Collection of elements of the same type.
2. Pointers: Store the address of variables.
3. References: Alias for another variable.
4. Function Types: Define functions.dfdsv
User-Defined Data Types:

1. Structures (struct): Group variables of different types.


2. Unions (union): Store different data types in the same memory location.
3. Enumerations (enum): Define variables that can take one of a set of values.
4. Classes: Encapsulate data and functions, forming objects.

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:

1. Condition Evaluation Timing


 while Loop: The condition is evaluated at the beginning of each iteration.

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);

3. Syntax and Readability


 while Loop: More straightforward for simple conditions and when pre-checking the
condition makes logical sense.
 do-while Loop: More intuitive when the code block must run at least once, providing
better readability for scenarios that require an initial execution.

Summary

 while Loop: Evaluates condition first, may not execute at all.


 do-while Loop: Executes block first, then evaluates condition, ensuring at least one
execution.

Choosing between the two depends on whether the initial execution of the loop body is
necessary.

3. Brief about class and objects.

In C++, classes and objects are fundamental concepts of Object-Oriented Programming


(OOP), which help in organizing and structuring code for better modularity, reusability, and
maintainability.

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;

// Member functions (methods)


void memberFunction() {
// Function body
}
};

 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

ClassName objectName; // Create an object of ClassName


objectName.dataMember = 10; // Access data member
objectName.memberFunction(); // Call member function

 Instantiation: Creating an object from a class.


 Accessing Members: Using the dot operator (.) to access data members and member
functions.

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

4. Define exception. Explain exception handling mechanism.

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.

Exception Handling Mechanism:

C++ provides a robust mechanism for handling exceptions using three keywords: try, catch,
and throw.

1. throw: Used to signal the occurrence of an anomalous situation (exception).

cpp
throw exception;

2. try: Block of code where exceptions can occur.

cpp

try {
// Code that may throw an exception
}

3. catch: Block of code to handle the exception.

cpp

catch (exception_type variable) {


// Code to handle the exception
}

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.

5. List and explain the STL components.

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:

 Sequence Containers: Maintain the order of elements. Examples include:


o vector: Dynamic array with fast random access.
o list: Doubly linked list with efficient insertions/deletions.
o deque: Double-ended queue allowing fast insertions/removals at both ends.
 Associative Containers: Store data in a sorted manner. Examples include:
o set: Collection of unique elements.
o map: Collection of key-value pairs with unique keys.
o multiset: Collection of elements allowing duplicates.
o multimap: Collection of key-value pairs allowing duplicate keys.
 Unordered Containers: Store data in an unordered fashion, providing average
constant-time complexity for operations. Examples include:
o unordered_set: Unique elements with no specific order.
o unordered_map: Key-value pairs with unique keys, unordered.
o unordered_multiset: Elements allowing duplicates, unordered.
o unordered_multimap: Key-value pairs allowing duplicate keys, unordered.

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

Iterators provide a mechanism to traverse elements in containers. They act as pointers to


container elements, enabling iteration in a uniform way.

4. Function Objects (Functors)


Function objects are objects that can be called as if they were functions. They are used to
define custom operations for algorithms.

These components collectively provide a powerful framework for efficient data management
and manipulation in C++.

6. Explain the types of methods to open a file.

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.

1. ifstream (Input File Stream)

Used for reading files.

 Constructor Method: Open a file directly by passing the filename to the constructor.

cpp

ifstream file("filename.txt");

 open() Method: Open a file after creating an ifstream object.

cpp

ifstream file;
file.open("filename.txt", ios::in); // Open for reading

2. ofstream (Output File Stream)


Used for writing files.

 Constructor Method: Open a file directly by passing the filename to the constructor.
cpp

ofstream file("filename.txt");

 open() Method: Open a file after creating an ofstream object.

cpp

ofstream file;
file.open("filename.txt", ios::out); // Open for writing

3. fstream (File Stream)


Supports both reading and writing.

 Constructor Method: Open a file directly by passing the filename.

cpp

fstream file("filename.txt");

 open() Method: Open a file after creating an fstream object.

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.

You might also like