Here we compare python class and c++ class. The class will be called simply A. First we show the two equivalent codes in python and c++ programming languages and then point out some differences.

Before reading this, we inform that in this example we did not use constructors(destructors). We did not implement the private, protected, public access specifies in python. Also we did not show inheritance in this example. This is because this example is just a basic example to show the basic differences in writing python class and c++ class.

Python class

 In python programming language a class declaration and definition is done inside a module(just a file with .py extension) and that module is called by the main program module.

Let the class module be called and let A be the class. The a class A is declared/defined as follows in python.

class A:
    name = None
    d = None
    def add(self, a, b):
        return a+b
    def display(self, name):
        print("The name is %s" %name)

Then to use this class A we import it in the main program file. Let this program be called It's content is as follows-

from mymodule import A

obj = A()

x = obj.add(3,4)

print("The sum is: %f" %x)


The output from this python program is as follows:

Now we will look how the same class A is implemented/used in C++ programming language.

C++ class

 The same class A is declared in a header file called myheader.h as follows-

#ifndef myheader_h
#define myheader_h

#include <iostream>
#include <string>

using namespace std;

class A{
    string name;
    int d;

    float add(float, float);         //function prototype
    void display(string);            //function prototype


 Once we have the class we can then implement the methods add and display in the program file. Let call the program file cpptut.cpp the content of which is below-

#include "myheader.h"

float A::add(float a, float b){
    return a + b;

void A::display(string y){
    cout << "The name is " << y << endl;

int main(){

    A obj;
    float x;

    x = obj.add(3,4);

    cout << "The sum is: " << x << endl;

    return 0;


The output of this c++ program is shown below:

Some differences and similarities

  •  A python class and c++ class can be implemented within the main program file or in separate files. If they are implemented in separate file, for code molecularity, python class is declared/implemented in separate module file and c++ class is declared in a separate header file.  Thus python class module and c++ class header are similar.
  • In python there is no need to declare variables in advance. The variables can be created at the moment it is to be used. In C++, the variables have to be declared in advance. The variable declaration name = None and d = None in python class is not useful. However they were written to conform with the comparison. Thus only class method(or functions) are really required in python.
  • Public, Protected, Private: By convention, python does not have access specifiers(public, protected, private). By default all members are public. But there is alternative to implement the access specifier. This is done using __ ; the double underscore to the members. C++ uses the access specifier. By default, the member are private.
  • In the above example, the constructor(destructors) were not implemented in both in python and c++. Python uses __init__ method to implement constructor. c++ uses the definition of method with the same name of the class to implement constructor.
  • To use the python class we use from mymodule import A whereas in C++ we use #include "myheader.h".
  • In python we create an instance/object using obj = A( ). In C++ we create an instance/object using A obj
  • In python we use the class members by writing obj.add(3,4) which is the same in c++, obj.add(3,4).


Post a Comment