Articles of level

Classes have complicated internal structures, including data and functions, object initialization and cleanup for classes is much more complicated than it is for simple data structures. The difference is-

  1. Constructors and destructors are special member functions of classes that are used to construct and destroy class objects. 
  2. Construction may involve memory allocation and initialization for objects. Destruction may involve cleanup and deallocation of memory for objects.
  3. Constructors and destructors do not have return types nor can they return values.
  4. References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.
  5. Constructors cannot be declared with the keyword virtual.
  6. Constructors and destructors cannot be declared const, or volatile.
  7. Unions cannot contain class objects that have constructors or destructors.
  8. Constructors and destructors obey the same access rules as member functions. For example, if you declare a constructor with protected access, only derived classes and friends can use it to create class objects.
  9. The compiler automatically calls constructors when defining class objects and calls destructors when class objects go out of scope. 
  10. A constructor does not allocate memory for the class object it’s this pointer refers to, but may allocate storage for more objects than its class object refers to. If memory allocation is required for objects, constructors can explicitly call the new operator. During cleanup, a destructor may release objects allocated by the corresponding constructor. To release objects, use the delete operator.

Method Overriding: Overriding means changing the functionality of a method without changing the signature. We can override a function in base class by creating a similar function in derived class. This is done by using virtual/overrides keywords.
Base class method has to be marked with virtual keyword and we can override it in derived class using override keyword. Derived class method will completely overrides base class method i.e. when we refer base class object created by casting derived class object a method in derived class will be called.

Use method overloading in situation where you want a class to be able to do something, but there is more than one possibility for what information is supplied to the method that carries out the task. 
You should consider overloading a method when you for some reason need a couple of methods that take different parameters, but conceptually do the same thing. 

Inheritance: The most important reason to use OOP is to make reusability of Code and eliminate the redundant code. That can be done by one of the OOP concept i.e inheritance. Inheritance supports reusability by defining a class and then use that class again and again.Inheritance supports a class hierarchy where Base class and Derived class exist, e.g. Animals is the [Base Class] and Dog is the [Derived Class].Derived Class inherits the base class. Also Derived Class can inherits all the member of Base Class.

Need Help? Contact Us.

Log in


Forgot password?


New User