Professional Documents
Culture Documents
- special member method - class name - no return type - parameters - invoked object
creation
- object initialization - overloaded
types of constructors
1. default 2.parameterized 3.copy constructors
//default
- no args constructors - compiler generates if no const specified - garbage values
assigned to objects
- invoked for object created with no args
player ranjith;
player *ptr = new player ;
// Overloading constructors
-------------------------------------
* classes can have as many constructors as necessary
* each must have an unique signature
* default constructor is no longer compiler generator once another constructor is
declared.
// Best practices
--------------
* provide a copy constructor when your class has raw pointer members
* provide the copy constructor with const reference parameter.
* Use STL classes as they already provide copy constructor
* avoid using raw pointer data members if possible.
/*-------------COPY CONSTRUCTOR----------------------------------------------*/
class1 obj3= obj2 ; //Copy constructor
/*--------------ASSIGNMENT OPERATOR------------------------*/
class1 obj4; //object initialized first
obj4 = obj3; // copied from existing object
A()
{
x = 0;
y = 0;
z = 0;
}
// Constructor delegation
A(int z) : A()
{
this->z = z; // Only update z
}
A obj(3);
obj.show(); // 0 0 3
//DESTRUCTORS
- special member method - ~class_name - no return type and parameters - invoked
object destroyed
- release memory and other resourses - one per class - cannot be overloaded
* A virtual function is a member function which is declared within a base class and
is re-defined(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.
- Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for function call.
- They are mainly used to achieve Runtime polymorphism
- Functions are declared with a virtual keyword in base class.
- The resolving of function call is done at Run-time.
base* bptr;
derived d;
bptr = &d;
// virtual function, binded at runtime
bptr->print();
As discussed here, If a class contains a virtual function then compiler itself does
two things:
* A pure virtual function (or abstract function) in C++ is a virtual function for
which we don’t have implementation, we only declare it.
* A pure virtual function is declared by assigning 0 in declaration.
// An abstract class
class Test {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
In C++, virtual functions can be private and can be overridden by the derived
class.
For example, the following program compiles and runs fine.
In C++, a static member function of a class cannot be virtual. For example, below
program gives compilation error.
// Polymorphism in C++
1. Function Overloading:
* When there are multiple functions with same name but different parameters then
these functions are said to be overloaded.
* Functions can be overloaded by change in number of arguments or/and change in
type of arguments.
2. Operator Overloading:
* C++ also provide option to overload operators. For example, we can make the
operator (‘+’) for string class to concatenate two strings.
* We know that this is the addition operator whose task is to add two operands.
* So a single operator ‘+’ when placed between integer operands , adds them and
when placed between string operands, concatenates them.
// Runtime polymorphism:
* This type of polymorphism is achieved by Function Overriding.
1. Function overriding
* on the other hand occurs when a derived class has a definition for one of the
member functions of the base class.
* That base function is said to be overridden.
// Virtual Destructor
* Deleting a derived class object using a pointer to a base class that has a non-
virtual destructor results in undefined behavior.
* To correct this situation, the base class should be defined with a virtual
destructor.
For example, following program results in undefined behavior.