1 Crore+ students have signed up on EduRev. Have you? Download the App |
In a function, return type and function name are mandatory all else are just used as a choice.
Output of following program
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived n"; }
};
int main(void)
{
Base *bp, b;
Derived d;
bp = &d;
bp->show();
bp = &b;
bp->show();
return 0;
}
Initially base pointer points to a derived class object. Later it points to base class object,
How many max number of arguments can present in function in c99 compiler?
What is the output of the program?
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
int main(void)
{
Base b;
Base *bp;
return 0;
}
Since Base has a pure virtual function, it becomes an abstract class and an instance of it cannot be created. So there is an error in line "Base b". Note that there is no error in line "Base *bp;". We can have pointers or references of abstract classes.
What is the output of this program?
#include < iostream >
using namespace std;
void mani()
void mani()
{
cout << "hai";
}
int main()
{
main();
return 0;
}
We have to use the semicolon to declare the function in line 3. If we did means, the program will execute.
What is the output of the program?
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived n"; }
};
int main(void)
{
Derived d;
Base &br = d;
br.show();
return 0;
}
What is the scope of the variable declared in the user definied function?
The variable is valid only in the function block as in other.
Can a destructor be virtual? Will the following program compile?
#include <iostream>
using namespace std;
class Base {
public:
virtual ~Base() {}
};
int main() {
return 0;
}
A destructor can be virtual. We may want to call appropriate destructor when a base class pointer points to a derived class object and we delete the object. If destructor is not virtual, then only the base class destructor may be called. For example, consider the following program.
There are three ways of passing a parameter. They are pass by value,pass by reference and pass by pointer.
Can static functions be virtual? Will the following program compile?
#include<iostream>
using namespace std;
class Test
{
public:
virtual static void fun() { }
};
Static functions are class specific and may not be called on objects. Virtual functions are called according to the pointed or referred object.
What is the output of the program?
#include <iostream>
using namespace std;
class A
{
public:
virtual void fun() { cout << "A::fun() "; }
};
class B: public A
{
public:
void fun() { cout << "B::fun() "; }
};
class C: public B
{
public:
void fun() { cout << "C::fun() "; }
};
int main()
{
B *bp = new C;
bp->fun();
return 0;
}
The important thing to note here is B::fun() is virtual even if we have not uses virtual keyword with it. When a class has a virtual function, functions with same signature in all descendant classes automatically become virtual. We don't need to use virtual keyword in declaration of fun() in B and C. They are anyways virtual.
The three types of returning values are return by value, return by reference and return by address.
Predict the output of following C++ program
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->Base::show(); // Note the use of scope resolution here
return 0;
}
A base class function can be accessed with scope resolution operator even if the function is virtual.
73 videos|7 docs|23 tests
|
73 videos|7 docs|23 tests
|