Configuring Xampp to host Python sites

  • Go to your xampp directory.
  • Then go to apache->conf folder.
  • There you can see httpd.conf file.Open it and look for,

“AddHandler cgi-script .cgi .pl .asp”

  • and add “.py” to it.
  • Now write a python program and place it htdocs folder.
  • Go to your browser and go the file in the localhost.

Eg python program is as follows,

#!C:\Python34\python.exe
import cgitb
cgitb.enable()
print (“Status: 200 OK\n”)
print (“<p><h1>It Works!</h1></p><h2><i>It Again Works!</i></h2>”)

Dive Deep into CPP: Abstract Class and Pure Virtual Function

Pure virtual function are virtual function with no definition.

Syntax:

virtual void func()=0;

Class with atleast one pure virtual is made as abstract class. Classes that inherit the abstract class must provide the definition to the pure virtual function.

What happens when we create a pure virtual function ?

When we create a pure virtual function in abstract class, we reserve slot for that function in the VTABLE(virtual table, maintained by the compiler), but doesnt put any address in that slot. So the VTABLE will be incomplete.Since the VTABLE for the abstract class is incomplete, compiler will not allow the creation of the object and will throw an error.

Pure Virtual Definitions:

These pure virtual functions can have definition in abstract class, which we want all the derived class to have. These definitions should be provided outside the class definition.

What does pure virtual function mean ?

When a function is declared pure virtual function, it cannot be called dynamically through a virtual dispatch mechanism.But it can be called statically without virtual dispatch. So, yes. Pure virtual functions can have definition. But it makes the class abstract without completing the VTABLE.

Dive Deep into CPP: Copy Constructor

Copy Constructor is usually invoked by passing reference to the old object of a class to a newly created object of the same class. Compiler by default provide a copy constructor to a class even if you didnt provide one. When the Copy Constructor is invoked either by calling copy constructor during declaration or using an assignment operator, the constructor will get the reference and then it simply assigns it to the new object using the assignment operator.So Copy Constructor is implemented by overloading the assignment operator.

Why program runs into an infinite loop , if copy constructor is invoked by value ?

If we didnt pass the old object by reference and if we pass by value, in the copy constructor it will be assigned to the new object by an assignment operator and the compiler will inturn call the copy constructor again and then assignment operator and so on in an infinite loop.

Dive Deep into CPP: Types of collaboration between classes

Auto
Message to self.Calling m1 from m2 where they are the methods of the same class. Preferred relationship is “has-a “.We will be using “this” keyword for invocation. Which means compiler already created an object of the same class within itself.

Base derived
We can call the method of base class from derived using “super” or “base” keywords which are the objects of base and it is created within the derived class. So we use “is-a” relation but it is implemented through “has-a” relation. But if there is no “is-a” relation between the base and derived class, then those objects won’t be created. So its an equalizer

Sibling
Accessing from sibling class. Preferred relation  is “has-a”.

Peer to peer
Preferred relationship is “has-a ”

So,
Generalization is having both the interface(type contracts etc) and the implementation of the base class.
Realization is having only the interface of the base class and creating their own implementation for the methods in the base class.
In “has-a” relationship, we can use only the  implementation of the class, but not its interface.
Finally, we can say  generalization =realization +has a
So generalization which is tightly coupled can be replaced with realization and association relationships which will make the classes loosely coupled.This will give the flexibility we need and will give us a good clean design .

Dive Deep into CPP : Constructors

You can use constructor with initialization list in addition to the default ways. You cant use initialization list with arrays,list, vector etc. Also static variables cant be initialized using this method. Initialization of reference variable and const variable can only be done through initialization list. It cant be done through constructor body. Body of the constructor believes that the variables have been initialized and so it assigns. Initialization is the first assignment. const and reference variables can be initialized only once. So it must be initialized through initialization list. Static members do not belong to one object of that class. So it cant be initialized through initialization list. But can be done through body, because it only assigns.

Eg:
A(): x(0),y(0)
{}

Constructor is overloaded using initialization list  like

A(int x_=0) : x(x_)
{}

Class with private constructor cannot be inherited. It can be instantiated by friends of the class or through a public static function.Classes with protected constructors cannot be instantiated outside. Can only be used in inheritance. It will be like abstract base class when virtual functions are not feasible. Base class can be instantiated from outside by using copy constructor. Because it is provided by default by the compiler. So it is partial abstract class. If you want to make it as pure abstract class, make all constructor as protected.
To set a field read only, use constructor to initialize the variable and remove the setter function.

Dive Deep into CPP : Inheritance and Access Specifier

When a class inherits a base class with private variables, private variable is also inherited but it is invisible to the derived class. We can access the private variables using the public setter and getter method implemented in the base class.
So the size of the derived class will be same of the base class if it doesn’t have any members. It is similar to member functions.

Deep Dive into CPP: Type Casting operators

(int) x – c style casting
int(x) – cpp style
static_cast<int>x – new cpp style looks like a template casting.

C style casting is always dangerous.

class Account
{
double bal;
public:
double getBalance()
{
return bal;
}
};
Account acc = new Account(1000);
acc.bal = 100000; //  error cant access private variable.

But,

double *p = (double*) &acc
*p = 1000000; // now bal will be changed.

Because the 1st statement will be reinterpreted as

Double *p = reinterpret_cast<double*> (&acc)

Which the compiler moves the line to runtime, where reinterpret cast will simply assign the address of the class to the destination. Which now points to the bal variable which is at first. Now you can change. Now, many can ask, we can use directly the reinterpret_cast to change the values in private variables. But that will happen with the programmers consent, but here it happens without his knowledge which is dangerous. Therefore, use reinterpret_cast with utmost care.
There are 4 types of static_cast,dynamic_cast,reinterpret_cast and const_cast.

  • static_cast can perform conversions between pointers to related classes, not only upcasts (from pointer-to-derived to pointer-to-base), but also downcasts (from pointer-to-base to pointer-to-derived). No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe.
  • dynamic_cast can only be used with pointers and references to classes (or with void*). Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type.
  • reinterpret_cast will compare bit by bit to find any similarities between source and destination during runtime. But it ends up just assigning the value to the destination. reinterpret_cast should be used by the user with caution.
  • const_cast manipulates the constness of the object pointed by a pointer, either to be set or to be removed.