It used to define the member functions of a class outside

 15 Demonstrate nesting of member function.

Class


Student { int marks;
void showMarks() { cout
<< "Marks = " << marks << endl;       } public: Void getMarks() { marks = 85; ShowMarks(); // Calling another member function } }; Int main() { Student s; S.GetMarks(); Return 0; }

4 Write a short note on defining member functions.1. Defining the function inside the class 

class Student
{   int age;

public:
    void display()
    {   cout << “Age = ” << age;
    }
};

2. Defining the function outside the class

class Student
{   int age;

public:
    void display();
};

void Student::display()
{  cout << “Age = ” << age;
}


Q49 Explain Virtual function; 

class Animal
{
public:
    virtual void sound()
    {   cout << “Animal makes a sound” << endl;
    }
};

class Dog : public Animal
{
public:
    void sound()
    {cout << “Dog barks” << endl;
    }
};

int main()
{Animal *a;
    Dog d;

    a = &d;

A->sound();

    return 0;
}

1. It must be declared in the base class

class A
{
Public:
    Virtual void show();
};

2. The function should be overridden in the derived class

class B : public A
{
Public:
    Void show()
    {
        Cout << "B";
    }
};


3. Virtual functions are mainly used with a base-class pointer or reference

A *ptr;
B obj;
Ptr = &obj;
Ptr->show();

4. The function is selected at runtime

This is what provides runtime polymorphism / dynamic binding.

5. The function signature should match when overriding

For example:

virtual void show();

should be overridden as:

void show();

6. A virtual function cannot be a static member function

static and virtual have different purposes.

7. A virtual function can be inherited

A derived class inherits the virtual nature of the function unless it provides its own overriding version.

8. A virtual function may also be pure virtual

virtual void show() = 0;

This makes the function pure virtual and is used for an abstract class.


Virtual FunctionPure Virtual Function
Can have a definition in the base classHas no definition in the base class
Declared using virtualDeclared using virtual ... = 0
Base class object can generally be createdBase class becomes abstract
Overriding in derived class is not necessarily requiredDerived class must implement it to become concrete

Explain Pure Virtual function

#include <iostream>
using namespace std;

class Animal
{
public:
    virtual void sound() = 0;   // Pure virtual function
};

class Dog : public Animal                                         
{
public:
    void sound()
    {  cout << “Dog barks”;
    }
};

int main()
{Dog d;
    d.Sound();

    return 0;
}                                              

Static BindingDynamic Binding
Also called early bindingAlso called late binding
Decision at compile timeDecision at runtime
FasterSlightly more runtime work
Common with normal functionsCommon with virtual functions
Compile-time polymorphismRun-time polymorphism


Late Binding / Dynamic Binding

class  A {
public:
    virtual void show()
    { cout << “A”;
    }
};

class B : public A
{
public:
    void show()
    {  cout << “B”;
    }

  

Polymorphism:
The ability of an object or function to take more than one form is called polymorphism.Run-time polymorphism:
The type of polymorphism in which the function call is resolved at runtime is called run-time polymorphism.Late/Dynamic binding:
The process of linking a function call with its actual function definition at runtime is called late binding or dynamic binding.

 
  


};

int main()
{A *ptr

   B obj;

    ptr = &obj;
    ptr->show();
}



. Explain Reference Variable with Example

Syntax  data_type &reference_name = variable;

Example

    int a = 10;
    int &b = a;

    cout << “a = ” << a << endl;
    cout << “b = ” << b << endl;

    b = 20;

    cout << “a = ” << a;

Q10. Explain Inline Function

inline int square(int x)
{
    return x * x;
}

int main()
{
    cout << square(5);

return 0;
}

Syntax

inline return_type function_name(parameters)
    // body

Q12. Explain Friend Function with Example

#include <iostream>
using namespace std;

class Student
{int marks;

public:
    Student()
    { marks = 90;
    }    /n friend void display(Student);
};

void display(Student s)
{cout << “Marks = ” << s.Marks;        }

int main()
{Student s;
    display(s);

    return 0;
}