OOP
OOP
Ans:
Built-in data types is supported in Built-in & user-defined data types is supported in
C. C++.
Namespace features are not Namespace is used by C++, which avoid name
present inside the C. collisions.
Q3. A Write a program to calculate the sum of all odd numbers between 1 and 50
#include <iostream>
int main() {
int sum = 0; // initialize sum to 0
for (int i = 1; i <= 50; i += 2) { // iterate over odd numbers (increment by 2)
sum += i; // add each odd number to the sum
}
std::cout << "Sum of all odd numbers between 1 and 50: " << sum << std::endl;
return 0;
}
#include <iostream>
int main() {
// Accessing cout from std namespace using scope
// resolution operator
std::cout << "GeeksforGeeks";
return 0;
}
D. Use of Unions in C++
In C++, a union is a special data type that allows multiple variables of different data types to
share the same memory location. This is achieved by declaring a union with multiple
members, each of a different data type. Only one member can be active at any given time,
and accessing one member will overwrite the values of the others.
Key Characteristics
1. Shared Memory Location: All members of a union share the same memory location.
2. Only One Active Member: At any given time, only one member of the union can be
accessed or modified.
3. Overwriting: Accessing one member will overwrite the values of the others.
Use Cases
1. Memory Efficiency: Unions can be used to conserve memory when working with
large structures or arrays, as only one member needs to be stored at a time.
2. Tagged Unions: Unions can be used to implement tagged unions, where a single
memory location is used to store different types of data, and a tag or discriminator is
used to determine which type is stored.
3. Variant Types: Unions can be used to implement variant types, where a single
memory
E. What is the difference between Structure and Union? Give suitable examples to
justify your answer?
Memory Each member of a structure has its All members of a union share
Allocation own memory space. the same memory space.
Code Examples
Structure Example
#include <stdio.h>
struct Student {
int id;
float marks;
char name[20];
};
int main() {
struct Student s = {1, 92.5, "Alice"};
printf("ID: %d\n", s.id);
printf("Marks: %.2f\n", s.marks);
printf("Name: %s\n", s.name);
return 0;
}
Union Example
#include <stdio.h>
union Data {
int intVal;
float floatVal;
char charVal;
};
int main() {
union Data d;
The function works with a separate The function works directly with
Memory
copy, so changes do not affect the the original variable, so changes are
Usage
original variable. reflected.
Parameter Typically used for primitive types like Requires pointers (C/C++) or
Type int, float. references (C++, Python).
Used when you do not want to modify Used when you want to modify the
Use Case
the original variable. original variable or for efficiency.
class Base {
public:
virtual void function_name() = 0;
};
class Base {
public:
};
public:
};
int main() {
return 0;
}
int main() {
MyClass myObj; // Create an object of MyClass
4. Discuss the concept of file input and output functions using File handling in
C++.?
File handling is used to store data permanently in a computer. Using file handling we
can store our data in secondary memory (Hard disk).
How to achieve the File Handling
For achieving file handling we need to follow the following steps:-
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.
File Input and Output Functions: The file input and output functions in C++ are used to
read from and write to files. These functions are:
• ifstream: used to read from a file
• ofstream: used to write to a file
• fstream: used for both reading and writing to a file
Function Description
Mode Description
In C++, a static data member is a variable that is shared by all objects of a class. It is
declared within the class definition using the static keyword, and its definition is
typically provided outside the class definition. A static data member has the following
characteristics:
1. Single copy: Only one copy of the static data member exists, shared among all objects
of the class.
2. No object association: Static data members are not associated with individual objects
of the class.
3. Accessed using class name: Static data members can be accessed using the class name
and the scope resolution operator (::).
4. Initialized only once: Static data members are initialized only once, when the program
starts.
Example:
class Counter {
public:
static int count; // declaration
};
int Counter::count = 0; // definition
int main() {
cout << Counter::count << endl; // prints 0
Counter obj1;
Counter obj2;
cout << Counter::count << endl; // still prints 0
return 0;
}
characteristics:
1. No object association: Static member functions are not associated with individual
objects of the class.
2. Called using class name: Static member functions can be called using the class name
and the scope resolution operator (::).
3. Accesses only static members: Static member functions can access only static data
members and other static member functions.
4. Cannot access this pointer: Static member functions do not have access to
the this pointer, as they are not associated with an object.
Example:
class Math {
public:
static int add(int a, int b) { return a + b; }
};