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 Function | Pure Virtual Function |
|---|---|
| Can have a definition in the base class | Has no definition in the base class |
Declared using virtual | Declared using virtual ... = 0 |
| Base class object can generally be created | Base class becomes abstract |
| Overriding in derived class is not necessarily required | Derived 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 Binding | Dynamic Binding |
|---|---|
| Also called early binding | Also called late binding |
| Decision at compile time | Decision at runtime |
| Faster | Slightly more runtime work |
| Common with normal functions | Common with virtual functions |
| Compile-time polymorphism | Run-time polymorphism |
Late Binding / Dynamic Binding
class A {
public:
virtual void show()
{ cout << “A”;
}
};
class B : public A
{
public:
void show()
{ cout << “B”;
}
Polymorphism: | |
};
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;
}
