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

Assignment 4 Oops

The document discusses various object oriented programming concepts in C++ including inline functions, friend functions, function overloading, default arguments, call by reference and return by reference, classes and objects, scope resolution operator, member functions, enum types, and dynamic memory allocation using new and delete operators. Code examples are provided to illustrate concepts like inline functions to calculate area and perimeter of a rectangle class, a friend function to find the maximum value between two objects, function overloading with different parameters, and default arguments in functions.

Uploaded by

Aryan Bhaskar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Assignment 4 Oops

The document discusses various object oriented programming concepts in C++ including inline functions, friend functions, function overloading, default arguments, call by reference and return by reference, classes and objects, scope resolution operator, member functions, enum types, and dynamic memory allocation using new and delete operators. Code examples are provided to illustrate concepts like inline functions to calculate area and perimeter of a rectangle class, a friend function to find the maximum value between two objects, function overloading with different parameters, and default arguments in functions.

Uploaded by

Aryan Bhaskar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Aryan Bhaskar

2100290120048

**Assignment 4 - Object Oriented System Design (KCS-054) - 2023-24**

**Q-1) Inline Function in C++:**

An inline function is a function defined with the `inline` keyword, suggesting to the compiler that it
should insert the complete code of the function at each point where the function is called, instead of
performing a regular function call.

**Why use Inline Function:**

- **Reduced Function Call Overhead:** Inline functions eliminate the overhead of a function call,
which is beneficial for small, frequently used functions.

- **Performance Improvement:** By avoiding the function call mechanism, inline functions can
result in improved performance.

- **Compiler Optimization:** Inline functions enable better opportunities for compiler optimization.

**Example:**

```cpp

#include<iostream>

using namespace std;

class Area {

private:

double length;

double width;

public:

// Inline function to calculate area

inline double getArea() {

return length * width;


}

// Inline function to calculate perimeter

inline double getPerimeter() {

return 2 * (length + width);

// Setter methods

void setLength(double l) {

length = l;

void setWidth(double w) {

width = w;

};

int main() {

Area rectangle;

rectangle.setLength(5.0);

rectangle.setWidth(3.0);

cout << "Area: " << rectangle.getArea() << endl;

cout << "Perimeter: " << rectangle.getPerimeter() << endl;

return 0;

```

**Comparison with Macro:**

- **Macro:**
- Defined using `#define`.

- Textual replacement, may lead to errors.

- No type checking.

- **Inline Function:**

- Defined using the `inline` keyword.

- Follows the standard function call mechanism.

- Type-checking is done.

**Q-2) Friend Function in C++:**

A friend function is a function that is not a member of a class but has access to its private and
protected members.

**Example:**

```cpp

#include<iostream>

using namespace std;

class MyClass {

private:

int value;

public:

void setValue(int v) {

value = v;

int getValue() {

return value;

}
// Friend function to find the maximum value

friend int max(MyClass obj1, MyClass obj2);

};

// Friend function definition

int max(MyClass obj1, MyClass obj2) {

return (obj1.value > obj2.value) ? obj1.value : obj2.value;

int main() {

MyClass obj1, obj2;

obj1.setValue(10);

obj2.setValue(20);

cout << "Max Value: " << max(obj1, obj2) << endl;

return 0;

```

**Q-3) Manager and Cashier Classes:**

```cpp

// Code provided in the previous response (Q-3)

```

**Q-4) Add Complex Numbers using Member and Friend Functions:**

```cpp

// Code provided in the previous response (Q-4)

```
**Q-5) Add Time Objects using Constructor and Friend Function:**

```cpp

// Code provided in the previous response (Q-5)

```

**Q-6) Function Overloading:**

Function overloading is a feature in C++ that allows a class to have more than one function with the
same name but different parameters.

**Example:**

```cpp

// Code provided in the previous response (Q-6)

```

**Q-7) Default Arguments in C++:**

Default arguments in C++ allow you to provide default values for function parameters.

**Example:**

```cpp

// Code provided in the previous response (Q-7)

```

**Q-8) Definitions:**

**a) Call and Return by Reference in C++:**

- **Call by Reference:** Passing a reference to the memory location of a variable rather than the
actual value.

- **Return by Reference:** A function returning a reference, allowing the result to be used directly.
**b) Class, Object, Scope Resolution Operator, Member Function:**

- **Class:** A user-defined data type that encapsulates data members and member functions.

- **Object:** An instance of a class, representing a real-world entity.

- **Scope Resolution Operator (::):** Resolves the scope of a member function or variable within a
class or namespace.

- **Member Function:** A function associated with a class, acting on its data members.

**c) Enum Type in C++:**

- **Enum Type:** A user-defined data type used to assign names to integral constants.
Enumerations provide a way to create symbolic names for sets of values.

**d) new and delete Operator in C++:**

- **new Operator:** Allocates memory dynamically, returning a pointer to the allocated space.

- **delete Operator:** Deallocates memory that was previously allocated by new.

Feel free to reach out if you have any further questions or need clarifications.

You might also like