Professional Documents
Culture Documents
Object Oriented Programming With C++
Object Oriented Programming With C++
The access status of the base class members inside the derived class is determined by the access specifier used for inheriting the base class.
The base class access specifier must be public, private, or protected.
If the access specifier is not used, then it is private by default if the derived class is a class.
If the derived class is a struct, then public is the default in the absence of an explicit access specifier.
Protected member
When a member of a class is declared as protected, that member is not accessible to other, non-member elements of the program.
With one important exception, access to a protected member is the same as access to a private member; it can be accessed only by other
members of the class of which it is a part.
The sole exception to this rule is when a protected member is inherited.
A private member of a base class is not accessible by any other part of your program, including any derived class.
When a base class is inherited as public, protected members in the base class become protected members of the derived class, and are
accessible to the derived class.
Therefore, by using protected, you can create class members that are private to their class, but that can still be inherited and accessed by a
derived class.
.
When a base class is inherited as protected, all public and protected members of the base class become protected members of the derived
class.
Here is an example:
Copy
// Demonstrate inheriting a protected base class.
#include <iostream>
using namespace std;
class base {
int i;
protected:
int j;
public:
int k;
void seti(int a) { i = a; }
int geti() { return i; }
};
int main()
{
derived ob;
return 0;
}
Output: 10 12
class base1 {
protected:
int x;
public:
void showx() { cout << x << "\n"; }
};
class base2 {
protected:
int y;
public:
void showy() { cout << y << "\n"; }
};
int main()
{
derived ob;
return 0;
}
Constructor
C++ Passing Parameters to Base Class
Constructors
PreviousNext
When only the constructor of the derived class requires one or more arguments, you use the standard parameterized constructor syntax.
To pass arguments to a constructor in a base class, use an expanded form of the derived class' constructor declaration, which passes arguments
along to one or more base class constructors.
The general form of this expanded declaration is shown here:
Copy
derived-constructor(arg-list) : base1(arg-list),
base2(arg-list), ...
baseN(arg-list);
{
body of derived constructor
}
Here, base1 through baseN are the names of the base classes inherited by the derived class.
Notice that a colon separates the constructor declaration of the derived class from the base classes, and that the base classes are separated
from each other by commas, in the case of multiple base classes.
Consider this sample program:
Copy
#include <iostream>
using namespace std;
class base {
protected:
int i;
public:
base(int x) { i = x; cout << "Constructing base\n"; }
~base() { cout << "Destructing base\n"; }
};
int main()
{
derived ob(3, 4);
ob.show(); // displays 4 3
return 0;
}
Virtual base class in C++
An ambiguity can arise when several paths exist to a class from the same base class. This means
that a child class could have duplicate sets of members inherited from a single base class.
C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary
care is taken so that the duplication is avoided regardless of the number of paths that exist to the child
class.
When two or more objects are derived from a common base class, we can prevent multiple
copies of the base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved
by preceding the base class’ name with the word virtual.
Example for the virtual base class
class A
{
public:
int i;
};
int sum;
};
int main()
{
D ob;
ob.i = 10; //unambiguous since only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << “Value of i is : ”<< ob.i<<”\n”;
cout << “Value of j is : ”<< ob.j<<”\n”; cout << “Value of k is :”<< ob.k<<”\n”;
cout << “Sum is : ”<< ob.sum <<”\n”;
return 0;
}
C++ Virtual Functions
Virtual Function
A virtual function is a member function which is declared within a base class and is re-efined (Overriden)
by a derived class. When you refer to a derived class object using a pointer or a reference to the base
class, you can call a virtual function for that object and execute the derived class’s version of the
function.
-time.
3. The prototype of virtual functions should be same in base as well as derived class.
4. They are always defined in base class and overridden in derived class. It is not mandatory for
derived class to override (or re-define the virtual function), in that case base class version of
function is used.
5. A class may have virtual destructor but it cannot have a virtual constructor.
Functions can be pure virtual by specifying the = 0; at the end of the function declaration.
Pure virtual functions do not have definitions and are also called interfaces.
Pure virtual functions must be re-defined in the derived class.
Classes having at least one pure virtual function are called abstract classes and cannot be instantiated.
They can only be used as base classes.
Example:
Copy
#include <iostream>
class MyAbstractClass
{
public:
virtual void dowork() = 0;
};
int main()
{
MyAbstractClass* o = new MyDerivedClass;
o->dowork();
delete o;
}
When perform Early Binding, an object is assigned to a variable declared to be of a specific object type. Early binding objects are basically a strong
type objects or static type objects. While Early Binding, methods, functions and properties which are detected and checked during compile time and
perform other optimizations before an application executes. The biggest advantage of using early binding is for performance and ease of
development.
Late binding (Dynamic binding)
By contrast, in Late binding functions, methods, variables and properties are detected and checked only at the run-time. It implies that the compiler
does not know what kind of object or actual type of an object or which methods or properties an object contains until run time. The biggest
advantages of Late binding is that the Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects