Python program to Illustrate the inheritance of a
Class
Now you are going to execute a class program of
C++. The class program is of multilevel inheritance. This also gives output
using Python script
Example - C++
program to implement Multilevel Inheritance
// C++ program to implement Multilevel Inheritance
//Now select File→New in Notepad and type the C++
program
#include <iostream>
using namespace std;
base
class class Vehicle
{
public:
Vehicle()
{
cout<< "This is a
Vehicle" <<endl;
}
};
class threeWheeler: public
Vehicle { public:
threeWheeler()
{
cout<<"Objects with 3
wheels are vehicles"<<endl;
}
};
//sub class derived from two base
classes
class Auto: public threeWheeler{
public:
Auto()
{
cout<<"Auto has 3
Wheels"<<endl;
}
};
//main function
int main()
{
//creating object of sub class
will invoke the constructor of base classes
Auto obj;
return 0;
}
// Save this file as inheri_cpp.cpp
//Now select File → New in Notepad and type the
Python program
#Save the File as classpy.py
#Python classpy.py -i inheri_cpp command to execute
c++ program
import sys, os, getopt
def main (argv):
cpp_file = ''
exe_file = ''
opts, args = getopt.getopt (argv,
"i:",['ifile='])
for o, a in opts:
if o in ("-i",
"--ifile"):
cpp_file = a + '.cpp'
exe_file = a + '.exe'
run (cpp_file, exe_file)
def run(cpp_file, exe_file):
print ("Compiling " +
cpp_file)
os.system ('g++ ' + cpp_file + '
-o ' + exe_file)
print ("Running " +
exe_file)
print
("-------------------")
print
os.system (exe_file)
print
if __name__ =='__main__':
main (sys.argv[1:])
Output of the above program
Compiling c:\pyprg\class_file.cpp
Running c:\pyprg\class_file.exe
-------------------
This is a Vehicle
Objects with 3 wheels are
vehicles
Auto has 3 Wheels
From all these example One can understand the
Python script (program) used for integrating C++ is common. Only the name of
the Python script file and the C++ (cpp) file have changed. Remember it is not
mandatory to type the C++ coding if one’s computer contain already the C++
file. In this chapter to show all the types of C++ programs like normal,
function, inheritance program can be executed through python the coding of C++
is explicitly mentioned. Using this Python script you can even compile and
execute C program also. Since python automates the C++ program file to execute
without it’s IDE, Python can be called as a Scripting Language.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.