Entrance Exams
Govt. Exams
In C++, a derived class can override a virtual function with a different access specifier than the base class. The access specifier controls who can call the function, but it doesn't affect the polymorphic behavior. However, it's a best practice to maintain the same access level.
An abstract class with at least one pure virtual function cannot be instantiated directly. However, it serves as a blueprint for derived classes and can have constructors, destructors, and other functions.
class Test { public: static int count; };
int Test::count = 0;
int main() { Test t1, t2; Test::count = 5; cout
Static members are shared by all instances of a class. When Test::count is set to 5, this value is shared across all objects t1 and t2, so both print 5.
Method overloading allows multiple functions with the same name but different parameter lists (number, type, or order). Return type alone is insufficient for overloading.
A const member function cannot modify any non-static data members of the object. It guarantees that calling the function will not change the object's state.
In C++, a pure virtual function is declared by adding '= 0' after the function signature. C++ does not have 'abstract' or 'pure' keywords like Java.
The Diamond Problem occurs when a derived class inherits from two classes that both inherit from a common base class, creating multiple inheritance paths and ambiguity about which base class members to use. Virtual inheritance solves this.
Constructors have no return type, a class can have multiple constructors (constructor overloading), and constructors are not automatically inherited in C++ (until C++11 explicit inheritance). They are automatically invoked when objects are created.
class A { public: virtual void func() { cout
With virtual functions, the actual object type (C) determines which function is called, not the pointer type. C::func() is invoked through multilevel inheritance.
The 'protected' access specifier allows access within the class and derived classes, but not to external code. This is ideal for members that derived classes need to use but external code should not access.