[go: up one dir, main page]

0% found this document useful (0 votes)
17 views52 pages

Unit 2 OOP

Uploaded by

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

Unit 2 OOP

Uploaded by

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

Unit 2:

Functions and Constructors

(16 Marks)

OBJECT ORIENTED PROGRAMMING USING C++ - ALU FOUNDATION - DIPTESH D. DANGE 1


Learn MSBTE K-Scheme Syllabus

Choose Any 2 Subjects at Just ₹1500/-

Limited Seats • Limited Time Offer

Subjects Available:
1. Data Structure Using C (313301)
2. Database Management System (313302)
3. Object-Oriented Programming (313304)

-- What You Get


• Well-Structured Notes (Aligned to MSBTE K Scheme)
• Practical Coding Sessions
• Recorded Lectures – Watch Anytime
• Assignment Help & Viva Prep
• Doubt Solving Sessions
• Important questions and PYQ solving

3
2.1 Inline function, Static data members, Static member function,
Friend function: Using two different classes , Using non-member
function

Inline Function:

• In C++, inline functions provide a way to optimize the performance of the program by reducing the
overhead related to a function call.

• When a function is specified as inline the whole code of the inline function is inserted or substituted at the
point of its call during the compilation instead of using the normal function call mechanism.
• One of the objectives of using functions in a program is to save some memory space, which becomes
appreciable when a function is likely to be called many times. However, every time a function is called, it takes
a lot of extra time in executing a series of instructions for tasks such as jumping to the function, saving registers,
pushing arguments into the stack, and returning to the calling function. When a function is small, a substantial
percentage of execution time may be spent in such overheads.

• One solution to this problem is to use macro definitions, popularly known as macros. Preprocessor macros are
popular in C. The major drawback with macros is that they are not really functions and therefore, the usual error
checking does not occur during compilation.

• C++ has a different solution to this problem. To eliminate the cost of calls to small functions, C++ proposes a
new feature called inline function. An inline function is a function that is expanded in line when it is invoked.
That is, the compiler replaces the function call with the corresponding function code.
Syntax:

The inline keyword is used to write inline functions.


Example Program
Static Data Member:

• Static data members are class members that are declared using static keywords. Class
• A static member has certain special characteristics which are as follows: Static Int a = 5;

1. Only one copy of that member is created for the entire class and is shared by
all the objects of that class, no matter how many objects are created.
Obj1 Obj2 Obj3
2. It is initialized before any object of this class is created, even before the Int a = 5 Int a = 5 Int a = 5
main starts outside the class itself.

3. It is visible can be controlled with the class access specifiers.

4. Its lifetime is the entire program.


Syntax

className {
static data_type data_member_name;
.....
}
Static Member Function:
Static Member Function in C++

Static Member Function in a class is the function that is declared as static because of which function attains certain properties
as defined below:
• A static member function is independent of any object of the class.

• A static member function can be called even if no objects of the class exist.

• A static member function can also be accessed using the class name through the scope resolution operator.

• A static member function can access static data members and static member functions inside or outside of the class.

• Static member functions have a scope inside the class and cannot access the current object pointer.

• You can also use a static member function to determine how many objects of the class have been created.
The reason we need Static member function:

• Static members are frequently used to store information that is shared by all objects in a class.

• For instance, you may keep track of the quantity of newly generated objects of a specific class type using a static
data member as a counter. This static data member can be increased each time an object is generated to keep track of
the overall number of objects.
Friend function: Using two different classes , Using non-member function

- What is a Friend Function?

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

• To declare a friend function, we use the keyword friend inside the class.
Case 1: Friend Function using Two Different Classes

Let’s say we want to add two private data


members from two different classes:

Key Points:

• add is not a member of either class.


• It is a friend of both classes.
• Can access private data from both objects.
Case 2: Friend Function using Non-Member Function

A friend function can also be a normal function (not


inside any class) that is declared as friend inside a class.

Key Points:
• displaySecret() is outside the class.
• It is a friend function and can access private data.
2.2 Array of Objects, Object as function arguments
Array of Objects :

• An array of objects is a collection of objects of the same class, stored together like any other array.
Object as function arguments :

Like any other data type, an object may be used as a function argument. This can be done in two ways:

1. A copy of the entire object is passed to the function.


2. Only the address of the object is transferred to the function.

• The first method is called pass-by-value. Since a copy of the object is passed to the function, any changes made
to the object inside the function do not affect the object used to call the function.

• The second method is called pass-by-reference. When an address of the object is passed, the called function
works directly on the actual object used in the call.

- This means that any changes made to the object inside the function will reflect in the actual object.

- The pass-by reference method is more efficient since it requires to pass only the address of the object and not the
entire object.
Two Ways to Pass Objects to Functions:

1. Pass-by-Value
• A copy of the object is passed to the function.
• Changes made inside the function do not affect the original object.
• Memory-wise, this may be less efficient, especially for large objects.
2. Pass-by-Reference

• Only the address of the object is passed.


• The function works directly on the original object.
• Any changes made will affect the actual object.
• This is more efficient as only the address is passed.
2.3 Concepts of Constructors, Types of constructors
What is a Constructor?
• A constructor is a special member function of a class. Syntax:
• It is automatically called when an object is created.
class ClassName {
• Its name is same as the class name. public:
• It has no return type, not even void. ClassName() {
// Constructor code
}
Why Use Constructors? };
• To initialize objects when they are created.
• Saves time and ensures automatic initialization.
Simple Example:
Types of Constructors:

Type Description
1. Default Constructor No parameters. Initializes values by default.
2. Parameterized Constructor Takes arguments. Allows custom initialization.
3. Copy Constructor Copies data from one object to another.
4. Constructor Overloading Multiple constructors with different parameters.
1. Default Constructor
• Takes no arguments.
• Called when object is created without values.
• A default Constructor is automatically generated by the compiler if the programmer does not define one.

• This constructor doesn't take any argument as it is parameter less and initializes object members using
default values.

• It is also called a zero-argument constructor.

• However, the default constructor is not generated by the compiler if the programmer has explicitly defined
a constructor.
2. Parameterized Constructor

• Takes arguments.
• Used to initialize with custom values.
• Parametrized Constructor Allow us to pass arguments to constructors.

• Typically, these arguments help initialize an object's members.

• To create a parameterized constructor, simply add parameters to it the way you would to any other function.

• When you define the constructor’s body, use the parameters to initialize the object's members.

Syntax:

// Parameterized constructor
ClassName(parameters);
3. Copy Constructor

• Creates a new object by copying another object.

• Syntax: ClassName (ClassName &obj)


2.4 Constructor overloading and Constructors with default
arguments
Constructor Overloading

Definition:

• Constructor overloading means defining multiple constructors in the same class with different sets of
parameters.

• It allows objects to be initialized in different ways.


In C++, We can have more than one constructor in a class
with same name, as long as each has a different list of
arguments.

• This concept is known as Constructor Overloading and


is quite similar to function overloading.

• Overloaded constructors essentially have the same name


(exact name of the class) and different by number and type
of arguments.

• A constructor is called depending upon the number and


type of arguments passed.

• While creating the object, arguments must be passed to let


compiler know, which constructor needs to be called.
Constructors with Default Arguments

Definition:

Constructors can also have default values for parameters, allowing flexibility with fewer constructor definitions.
2.5 Destructors

What is a Destructor?
• A destructor is a special member function in C++.
• It is automatically called when an object goes out of scope or is deleted.
• Its purpose is to free resources (like memory or files) used by the object.

When is a Destructor Called?

• When an object goes out of scope

• When an object is deleted using delete (for dynamic objects)


Key Characteristics:

Property Description
Name Same as class, but with a ~ (tilde) prefix
Parameters None (cannot be overloaded)
Return Type No return type, not even void
Called Automatically When object is destroyed

Syntax: Why Use Destructors?

class ClassName { • To release memory allocated using new


public:
~ClassName() { • To close files or network connections
// Destructor code
} • To clean up resources used by the object
};
Destructor Example:
Thanks!
Do you have any questions?
foundationalu@gmail.com
+91 9370970021

Diptesh Dange
ALU FOUNDATION

You might also like