Comp Sci
Comp Sci
INSTANCE MEMBERS
In C++, instance data members, also called instance variables or object variables, are data members of a class that
are associated with each specific instance (object) of that class. Each object of the class gets its own independent
copy of the instance data members.
class Dog {
public:
std::string name; // Instance data member
int age; // Instance data member
};
void disp()
{ cout<< area<< endl; }
};
int main()
{
// Constructor Overloading ith two different constructors of class name
car car1;
car car2( 10, 20);
car1.disp();
car2.disp();
return 1;
}
Output:
0
200
EXPLAIN THE USE OF TRY, CATCH, AND THROW KEYWORDS WITH AN EXAMPLE.
An exception is an unexpected event that occurs during program execution. For
example,divide_by_zero = 7 / 0;
The above code causes an exception as it is not possible to divide a number by 0.The
process of handling these types of errors in C++ is known as exception handling.In C++,
we handle exceptions with the help of the try and catch blocks, along with the throw
keyword.
try - code that may raise an exception
throw - throws an exception when an error is detected
catch - code that handles the exception thrown by the throw keyword
Note: The throw statement is not compulsory
Syntax for Exception Handling in C++
try {
// code that may raise an exception
throw argument;
}
catch (exception) {
// code to handle exception
}
When an exception occurs, the throw statement throws an exception, which is caught by
the catch block.
The catch block cannot be used without the try block.
Example: C++ Exception Handling
// program to divide two numbers
// throws an exception when the divisor is 0
#include <iostream>
using namespace std;
int main() {
double n, d, divide;
cout<< "Enter numerator: ";
cin>> n;
cout<< "Enter denominator: ";
cin>> d;
try {
// throw an exception if denominator is 0
if (d == 0)
throw 0;
// not executed if denominator is 0
divide = n / d;
cout<< n << " / " << d << " = " << divide <<endl;
}
catch (intnum_exception) {
cout<< "Error: Cannot divide by " <<num_exception<<endl;
}
return 0;
}
Run Code
Output
Enter numerator: 72
Enter denominator: 0
Error: Cannot divide by 0
DIFFERENCE BETWEEN PRIVATE, PROTECTED, AND PUBLIC ACCESS SPECIFIERS IN C++.
Access Specifiers in C++
Access specifiers define how members (variables and functions) of a class can be accessed from outside the
class or in derived classes.
There are three main access specifiers:
1. private
Default access level for class members.
Accessible only within the class.
Not accessible from outside the class or by derived classes.
class MyClass {
private:
int secret;
public:
void setSecret(int s) {
secret = s; // OK
}
};
2. protected
Similar to private, but with one exception:
Accessible within the class and by derived classes.
Still not accessible from outside the class.
class Base {
protected:
int value;
};
Comparison Table:
Access Specifier Within Class Derived Class Outside Class
private Yes No No
protected Yes Yes No
public Yes Yes Yes
class Example {
private:
int a; // Only accessible inside class
protected:
int b; // Accessible in class and derived class
public:
int c; // Accessible everywhere
WHAT IS ARRAY? EXPLAIN THE PROCESS OF PASSING ARRAYS TO FUNCTIONS
An array is a collection of elements of the same data type stored in contiguous memory locations. It allows
multiple values to be stored under one name, using an index.
Syntax:
type arrayName[size];
Example:
int numbers[5] = {10, 20, 30, 40, 50};
numbers[0] = 10, numbers[1] = 20, etc.
Index starts from 0.
Passing Arrays to Functions in C++
In C++, you can’t pass the whole array directly by value. Instead, you pass:
A pointer to the first element of the array.
Optionally, the size of the array as a separate parameter.
Passing 1D Array to Function
Function Definition:
void display(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
}
Function Call:
int main() {
int numbers[] = {1, 2, 3, 4, 5};
display(numbers, 5);
return 0;
}