Oop Question Bank Unit 2
Oop Question Bank Unit 2
Q1. What do you mean by inline function? Write its syntax and
Example?
The inline keyword suggests that the compiler substitute the code within the function
definition in place of each call to that function. In theory, using inline functions can make
your program faster because they eliminate the overhead associated with function calls.
Syntax:
inline return_type function_name(parameters) {
// Function body
}
Example:
inline int square(int x) {
return x * x;
}
int main() {
int num = 5;
int result = square(num); // This will directly expand into result = num * num;
return 0;
}
Q2. List characteristics of static data member and static member Function?
Static Data Members:
1. Shared by all instances of the class.
2. Initialized once, at the start of the program.
3. Accessed using the class name or through any instance.
4. Can only be initialized outside the class definition.
Example:
#include <iostream>
using namespace std;
class MyClass {
public:
static int objectCount;
MyClass() {
objectCount++;
}
};
cout << "Total objects created: " << MyClass::objectCount << endl;
return 0;
}
Q5. What is static member function ? How is it declare ?
A static member function is a function that belongs to the class itself, not to any specific
object of the class. It can be called using the class name and can only access static data
members and other static member functions.
Declaration:
You declare a static member function inside the class definition using the static keyword.
For example:
class MyClass {
public:
static void myStaticFunction(); // Declaration
};
In this declaration, myStaticFunction is a static member function of MyClass.
Q6. Explain friend function with example?
Rules for defining friend function?
1. *Friend functions are not members:* A friend function is not a member of the class to
which it is a friend.
2. **Declared with the friend keyword:** It must be declared within the class with the
friend keyword.
3. *Access to private and protected members:* Friend functions have access to the private
and protected members of the class.
4. *Not invoked using objects:* A friend function is not invoked using objects of the class.
5. **No this pointer:** Friend functions do not have a this pointer as they are not members
of the class.
6. *Defined outside the class:* The definition of a friend function is done outside the class.
Q8. Explain friend function with its syntax and how to declare it?
In C++, a friend function is a special type of function that is granted access to the private and
protected members of a class. This allows the function to interact with the internal state of
the class even though it is not a member of the class itself.
Syntax:
class ClassName {
// Declare the friend function
friend ReturnType FunctionName(ParameterList);
private:
protected:ss
public:
};
Example:
#include <iostream>
using namespace std;
class Box {
private:
double length;
double width;
double height;
public:
// Constructor to initialize the Box
Box(double l, double w, double h) : length(l), width(w), height(h) {}
int main() {
Box myBox(3.0, 4.0, 5.0);
printBoxVolume(myBox); // Accesses private members of Box via friend function
return 0;
}
Q9. What is a Constructor and Why Do We Need It?
Types:
1.)Default
2.)Parameterized
3.Copy
4.)Move
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// parameterized constructor to initialize variables
Wall(double len, double hgt)
: length{len}, height{hgt} {
}
double calculateArea() {
return length * height;
}
};
int main() {
// create object and initialize data members
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
class Person {
private:
string name;
public:
// Constructor to initialize Person
Person(string n) : name(n) {}
// Method to display the name
void display() const {
cout << "Name: " << name << endl;
}
int main() {
Person person("John");
// Pass by value
modifyByValue(person);
cout << "After modifyByValue: ";
person.display(); // Original person is unchanged
// Pass by reference
modifyByReference(person);
cout << "After modifyByReference: ";
person.display(); // Original person is modified
// Pass by pointer
modifyByPointer(&person);
cout << "After modifyByPointer: ";
person.display(); // Original person is modified
return 0;
}