Posts

C++ 49: Containership

It is also called as container classes or composition or nested classes or member classes for aggregation or collection. Container ship represents has a relationship. Contains object of data member such a mechanism is called as containership. Example: Use the concept of container ship to design a class student with data paper roll number name date of birth department. define Member function get info and show in.   #include   <iostream> #include   <conio.h> using   namespace  std; class   Date { private:      int   D ,  M ,  Y ; public:      void   readData ()     {          cout   <<   "Enter Date (DD/MM/YYYY): " ;          cin   >>   D ;          cin . ignore ();       ...

C++ 48: This pointer

  Self referencing pointer /this pointer This is the name given to a pointer variable the this pointer contains address for which function or constructor or destructor is currently called this points to the current object inside the class only by using this pointer class determines data of which object to be used for processing the expression in a class are replaced by using this pointer in the compiled code as follows it is generally used to access the members of the object inside the class it is used to return the object itself note remember that the copy constructor overloading assignment operator function and this pointer automatically added in the compiled code of class in C plus plus program is compiled //The this pointer contains address of the object for which the //currently the  function is called #include   <iostream> using   namespace  std; class   Alpha { public:...

C++ 47:Polymorphism part 3

Image
  Pure Virtual Functions / Abstract Functions A virtual function without function definition (i.e. function body) is called as pure virtual function. The pure virtual functions are also called as abstract functions. A pure virtual function can be defined as follws: Syntax:               virtual return_type function_name(data_type_of_arg1, data_type_of_arg2..) = 0; Abstract Classes If a class contains at least one pure virtual function then such class is called as abstract class. An abstract class an abstract class cannot be initiated it means it is not possible to create objects of an abstract class but it is possible to create pointers abstract class  It is possible to derive new classes from an abstract class but if a class derived from abstract class then the derived class we need to override all the pure virtual functions inherited from the base class otherwise the newly derived class will become an abstract class. Example: //Te...

C++ 46: Polymorphism part 2

 Compile Time Polymorphism //Testing that c++ perfor compile time polymorphism or not #include   <iostream> using   namespace  std; class   Unit { public:      void   showData ()     {          cout   <<   "Unit of class showData function: "   <<   endl ;     }      void   Convert ()     {          cout   <<   "Unit Class Convert function"   <<   endl ;     } }; class   Distance  :  public   Unit { private:      int   F ,  I ; public:      Distance ( int  f,  int  i)     {      ...

C++ 45: Polymorphism

Polymorphism indicates the similar behavior of objects of different classes. The polymorphism can be achieved only for those classes whose behavior is same and whose base class is same. By using the technique we can develop a block of code which will work for different objects of the classes whose behavior is same. Such code is called as polymorphic code and process to develop such code is called as polymorphism. Polymorphism is actually a Greek word. In Greek poly   means many and Morph means forms. Polymorphism is categorized into following two types: 1) Compile time Polymorphism 2) Runtime Polymorphism Compile Time Polymorphism In Compile time polymorphism compiler decides the function call at compile time (i.e. before program begins its execution) as per type of pointer The compile time polymorphism is also called as early binding , Static Linking or static binding. C++ by default performs Compile time polymorphism. The operator overloa...

C++ 44: Dynamic Constructors and Destructors

 Dynamic Constructor The dynamic constructor which runtime reserves the memory for the data memebers of the class is called as dynamic constructor. Destructors / Deconstructors  The destructors are a special type of function in a class which is automatically involved just before the object is going to destroy. The destructors is used to truncate the object. That means to relese all the resources hold by object just before the object is going to destroy. Characteristics of destructors Destructors have same name as the class name with tilde(~)operator preceded. Destructor is automatically called just before object is going to destroyed Destructors cannot  have any return_type. Hence cannot return value. Destructor cannot accept any arguments hence not possible to overload. Destructors cannot inherit in derived class In case of derived classes the destructors invoke excatly reverse order in which class is created. Destructors is used to release the resources (memor...