Previous
Previous Product Image

Easy Notes Of Object oriented programming unit-1 @Computer Diploma

Original price was: ₹99.99.Current price is: ₹19.99.
Next

Easy Notes Of Object oriented programming unit-3 @Computer Diploma

Original price was: ₹99.99.Current price is: ₹19.99.
Next Product Image

Easy Notes Of Object oriented programming unit-2 @Computer Diploma

Original price was: ₹99.99.Current price is: ₹19.99.

Unit – II Functions and Constructors

2.1 Inline function, Static data members, Static member function, Friend function: Using two
different classes , Using non-member function
2.2 Array of Objects, Object as function arguments
2.3 Concepts of Constructors, Types of constructors
2.4 Constructor overloading and Constructors with default arguments
2.5 Destructors

Hurry Up!
Add to Wishlist
Add to Wishlist

Description

2.1 Inline Function, Static and Friend Members

Inline Function An inline function is a request to the compiler to replace the function call with the function’s body code at compile time. This is useful for small, frequently called functions to reduce the overhead of a function call. It can lead to faster execution but may increase the program’s size if the function is large.

Static Data Members A static data member is a class member that’s shared by all objects of the class. There’s only one copy of a static member, regardless of how many objects of the class are created. It must be defined outside the class declaration. It’s often used for counters or shared resources.

Static Member Function A static member function can only access static data members and other static member functions of the class. It can be called without creating an object of the class, using the class name and the scope resolution operator (::).

Friend Function A friend function is a non-member function that is granted special access to the private and protected members of a class. It’s not a member of the class it’s befriending.

  • Using a non-member function: The friend keyword is placed in the class definition followed by the function prototype.

    C++

    class MyClass {
    private:
        int data;
    public:
        // Declare a non-member function as a friend
        friend void displayData(MyClass obj);
    };
    
    void displayData(MyClass obj) {
        // Can access private member 'data'
        std::cout << obj.data;
    }
    
  • Using a function from another class: A member function of one class can be declared as a friend of another class. The syntax involves including the class name and the scope resolution operator within the friend declaration.

    C++

    class ClassB; // Forward declaration
    
    class ClassA {
    private:
        int valueA;
    public:
        friend void ClassB::showA(ClassA objA);
    };
    
    class ClassB {
    public:
        void showA(ClassA objA) {
            // Can access ClassA's private member
            std::cout << objA.valueA;
        }
    };
    

 

2.2 Array of Objects & Objects as Function Arguments

Array of Objects An array of objects is an array where each element is an object of a class. It’s declared and accessed similarly to a regular array, but each element is an object with its own data members and member functions. This is useful for managing multiple instances of a class. Example: Student students[5]; declares an array of 5 Student objects.

Object as Function Arguments Objects can be passed to functions in three ways:

  • By Value: A copy of the object is passed. Changes made to the object inside the function don’t affect the original object.
  • By Reference: A reference to the original object is passed. Changes inside the function directly modify the original object. This is often more efficient for large objects as it avoids the overhead of copying.
  • By Pointer: A pointer to the object is passed. It also allows direct modification of the original object.

 

2.3 Concepts of Constructors and Types

Constructors A constructor is a special member function of a class that’s automatically called when an object of that class is created. Its primary purpose is to initialize the object’s data members.

  • It has the same name as the class.
  • It has no return type, not even void.
  • It can be overloaded.

Types of Constructors

  • Default Constructor: A constructor that takes no arguments. If you don’t define any constructor, the compiler provides a default constructor that does nothing.
  • Parameterized Constructor: A constructor that takes one or more arguments to initialize an object with specific values.
  • Copy Constructor: A special constructor that initializes a new object using an existing object of the same class. It’s used when an object is passed by value, returned from a function, or explicitly initialized with another object.

2.4 Constructor Overloading & Default Arguments

Constructor Overloading Just like regular functions, constructors can be overloaded. This means a class can have multiple constructors with the same name (the class name) but different parameter lists (different number or types of arguments). This allows for object initialization in various ways.

Constructors with Default Arguments A constructor can have default arguments, which means if a value isn’t provided for a parameter during object creation, a default value is used. This can simplify initialization and reduce the need for multiple overloaded constructors.

 

2.5 Destructors

A destructor is a special member function of a class that is automatically called when an object is destroyed or goes out of scope. Its main purpose is to perform cleanup, such as deallocating memory or closing file handles that were allocated by the constructor or other member functions.

  • It has the same name as the class, but with a tilde ~ prefix.
  • It takes no arguments and has no return type.
  • A class can only have one destructor.

Keywords of this Topic: inline, static, friend, constructor, destructor, default, explicit, const.

Reviews

There are no reviews yet.

Be the first to review “Easy Notes Of Object oriented programming unit-2 @Computer Diploma”

Your email address will not be published. Required fields are marked *

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping