0% found this document useful (0 votes)
26 views9 pages

Oop Question Bank Unit 2

Uploaded by

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

Oop Question Bank Unit 2

Uploaded by

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

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.

Static Member Functions:


1. Belong to the class rather than any instance.
2. Can only access static data members and other static member functions.
3. Cannot access instance-specific data or functions.
4. Called using the class name or through an instance.

Q3. State any four characteristics of static data members?


1. *Single Copy:* Only one copy of a static data member is shared by all instances of the
class.
2. *Class Scope:* Static data members are part of the class itself, not any specific object.
3. *Global Lifetime:* They have a lifetime that spans the entire duration of the program,
similar to global variables.
4. *Separate Definition:* They must be defined and optionally initialized outside the class
definition.

Q4. Describe use of static data member in C++ with example?


*Static Data Members Usage:*
Static data members are useful when a value needs to be shared across all instances of a
class, such as a counter that keeps track of the number of objects created from a class.

Example:
#include <iostream>
using namespace std;
class MyClass {
public:
static int objectCount;
MyClass() {
objectCount++;
}
};

// Definition and initialization of the static data member


int MyClass::objectCount = 0;
int main() {
MyClass obj1;
MyClass obj2;
MyClass obj3;

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) {}

// Declare the friend function


friend void printBoxVolume(const Box& b);
};

// Define the friend function outside the class


void printBoxVolume(const Box& b) {
double volume = b.length * b.width * b.height;
cout << "Volume of Box: " << volume << endl;
}

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?

a) A constructor is a special function


b) It has same name as the class name
c) The Access Specifier is always Public
d) It has no return type
e) The constructor may/may not have parameters
f) The constructor is called(Invoked) as soon as the object is created
g) If u do not write a constructor,The compiler will create a default constructor

Types:
1.)Default
2.)Parameterized
3.Copy
4.)Move

Why We Need It:


 Initialization: It helps in setting initial values for the object's data.
 Setup: It can perform tasks that are necessary when an object is created, like
allocating memory or opening files

Q10. How Do We Invoke a Constructor?


You don’t call a constructor directly. It gets called automatically when you create an object.
For example:
ClassName obj; // Calls the default constructor
ClassName obj(10); // Calls the constructor with one argument
Q11. Describe Constructors with Syntax and Example
Syntax:
class ClassName {
public:
// Default constructor (no arguments)
ClassName() {
// Set initial values
}
// Parameterized constructor (with arguments)
ClassName(int value) {
// Set initial values using arguments
}
// Copy constructor (copy from another object)
ClassName(const ClassName &other) {
// Copy values from another object
}
};
Example:
// C++ program to calculate the area of a wall

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

cout << "Area of Wall 1: " << wall1.calculateArea() << endl;


cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}

Four Types of Constructors


1. Default Constructor:
o This constructor has no arguments and sets default values.
Rectangle() : width(0), height(0) {}
2.) Parameterized Constructor:
 This constructor takes arguments to set specific values when creating the object
Rectangle(double w, double h) : width(w), height(h) {}
3.) Copy Constructor:
 This constructor creates a new object as a copy of an existing one
Rectangle(const Rectangle &rect) : width(rect.width), height(rect.height) {}
4.) Move Constructor (advanced, C++11 and later):
 This constructor transfers resources from one object to another, usually for efficiency
Rectangle(Rectangle &&rect) noexcept : width(rect.width), height(rect.height) {
rect.width = 0;
rect.height = 0;
}

Q13. . Explain object as function argument


In C++, you can pass objects to functions just like you pass other types of variables. This can
be done in three main ways:
1. Pass by Value: A copy of the object is made and passed to the function. Changes to
the object inside the function do not affect the original object.
2. Pass by Reference: A reference to the original object is passed to the function. This
avoids copying and allows the function to modify the original object.
3. Pass by Pointer: A pointer to the object is passed, which also avoids copying and
allows modification of the original object.
Examples
#include <iostream>
using namespace std;

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

// Method to change the name


void setName(string n) {
name = n;
}
};

// Function to demonstrate pass by value


void modifyByValue(Person p) {
p.setName("Alice"); // This changes the copy, not the original
cout << "Inside modifyByValue: ";
p.display();
}

// Function to demonstrate pass by reference


void modifyByReference(Person &p) {
p.setName("Bob"); // This changes the original object
cout << "Inside modifyByReference: ";
p.display();
}

// Function to demonstrate pass by pointer


void modifyByPointer(Person *p) {
p->setName("Charlie"); // This changes the original object
cout << "Inside modifyByPointer: ";
p->display();
}

int main() {
Person person("John");

cout << "Original Person: ";


person.display();

// 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;
}

Q14. Array of Objects


An array of objects is a collection of objects of the same class type stored in contiguous
memory locations. This is useful when you need to manage multiple objects of the same
type.
Example
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
// Constructor to initialize a Student object
Student(string n, int a) : name(n), age(a) {}

// Method to display student details


void display() const {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
// Create an array of 3 Student objects
Student students[3] = {
Student("Alice", 20),
Student("Bob", 21),
Student("Charlie", 22)
};
// Display details of each student
for (int i = 0; i < 3; ++i) {
students[i].display();
}
return 0;}

You might also like