Haverford Cascade
Mentoring Program
Computer Programming:
C++ to Python Conversion
Professor: Dave Wannacott
Student: Kris Brower
Dobbins Vocational Tech
Teacher: Andre O’Brien
Student: Denisha Davis
C++ and Python
Brief History: C++
In 1985 Bjarne Stroustrup, also of Bell Labs,
invented the C++ programming language. To the C
language he added features for data abstraction and
object
-
oriented programming. Instead of naming the
language D, the Bell Labs group named it C++ in a
humorous vein. As we see later, ++ signifies the
increment operation
in the C and C++ language. Given
a variable x, the expression x++ means to increment
(add one to) the current value of x. Therefore, the
name C++ suggests an enhanced ("incremented") version
of the C++ language.
Although C originally was intended as a system
programming language, both C and C++ are widely used
today in business, industry, and personal computing.
C++ is powerful and versatile, embodying a wide range
of programming concepts.
C++ and Python
Brief History: Python
Python is a portable, interpreted, object
-
oriented programming
language. Its development started in 1990 at
CWI
in Amsterdam,
and continues under the ownership of the
Python Software
Foundation
. The language has an elegant (but not over
-
simplified)
syntax; a small number of powerful high
-
level data types are built
in. Python can be extended in a systematic fashion by adding new
modules implemented in a compiled language such as C or C++.
Such extension modules can define new functions and variables as
well as new object types.
Differences Between C++ and
Python
Simple program
Comments (Declarations)
Syntax (input/output)
Variables (Identifier and Data Type)
If statement
While loops
For loops
Functions
Simple program
C++
#include <iostream.h>
int main()
{
cout<<“Hello
Haverford”<<endl;
return 0;
}
Python
print “Hello Haverford”
Simple program (with comments)
C++
//this program demonstrates C++
//in its simplest form
#include <iostream.h>
int main()
{
cout<<“Hello
Haverford”<<endl;
return 0;
}
Python
#this program demonstrates
#python in its simplest form
print “Hello Haverford”
Syntax (input/output)
C++
cout<<“Please enter a name ”<<endl;
cin>>name;
cout<<“the name you entered was”,
name<<endl;
Python
name = raw_input(“Please enter a name”)
print “the name you entered was”, name
Variables
C++
int number;
char name;
float calculate;
number = 27;
name = “Denisha”;
calculate = 99.8;
Python
Number = 27
Name = “Denisha”
Calculate = 99.8
If statement (If
-
Then
-
Else form)
C++
Num1 = 10;
Num2 = 32;
Num3 = 14;
If (num1 > num2)
{
cout<<“10 is greater than 32<<“endl;
}
Else if (num2 < num3)
{
cout<<“32 is less than 14”<<endl;
}
Else if (num3 > num2)
{
cout<<“14 is greater than 32”<<endl;
}
Else
cout<<“please review your first grade math
material”<<endl
Python
Num1 = 10
Num2 = 32
Num3 = 14
If num1 > num2:
print “10 is greater than 32”
Elif num2 < num3:
print “32 is less than 14”
Elif num3 >num2:
print “14 is greater than 32”
Else
print “please review your first grade math
material”
While loops
C++
Count = 0;
While (count < 10)
{
cout<<“Andre
O’Brien”<<endl;
count =count +1;
}
Python
Count = 0
While count < 10:
print “Andre O’Brien”
count = count + 1
For loops
C++
For (loopCount = 1; loopCount<=10;
loopCount++)
{
cout<<“Andre O’Brien”<<endl;
}
Python
For loopCount in range(10):
print “Andre O’Brien”
Functions
C++
Void someFunction()
{
count = 0;
while (count<10)
{
cout<<“Hey OB”<<endl;
}
someFunction() //function call
Python
Def someFunction:
count = 0
while count < 10:
print “Hey OB”
someFunction() #function call
Differences Between C++ and Python
Summary
On the average Python programs compile slower than C++
programs. However, it usually takes less time to write programs in
Python and they tend to be shorter in length than their C++ counter
parts. The approach to programming remains virtually the same.
Another noteworthy comparison deals with how C++ and
Python deals with debugging issues. C++ highlights a syntax or
logic error but you must know the language well to understand
what’s wrong. Python, on the other hand, doesn’t highlight your
errors. It does, however, tell you what line your error can be found
on and it tells you what datatype or function is undefined. I
personally prefer Python’s approach better.
Summer Experiment
–
Lab 1
Do two lines overlap?
Do two rectangles overlap?
Do two circles overlap?
Does a rectangle and a circle overlap?
Do two line segments intersect?
–
beyond
the scope of my current skill level.
Lab 1
Do two lines overlap?
Level
–
beginner
# Do 2 ranges overlap?
def range_overlap(min1,max1,min2,max2):
if max1 < min2 or min1 > max2:
return False
else:
return True
Do two rectangles overlap?
Level
–
beginner
# Do 2 Windows overlap?
Def window_overlap(minx1,maxx1,miny1
,maxy1,minx2,maxx2,miny2,maxy2):
if maxy1<miny2 or maxx1<minx2 or maxy2 < miny1
or
maxx2 < minx1:
return False
else:
return True
Do two circles overlap?
Level
–
intermediate
# Do 2 Cicles overlap?
def circle_overlap(x1,y1,r1,x2,y2,r2):
distanceX = x2
-
x1
distanceY = y2
-
y1
distanceGAP = pow(distanceX,2) + pow(distanceY,2)
roc = r1 + r2
roc = pow(roc,2)
if distanceGAP<=roc:
return True
else:
return False
Does a rectangle and a circle
overlap?
Level
–
hard
# Do a circle and a rectangle overlap?
def circle_rectangle_overlap(center_x,center_y,
radius,xmin,xmax,ymin,ymax):
testX = center_x
testY = center_y
if testX < xmin:
testX = xmin
if testX > xmax:
testX = xmax
if testY < ymin:
testY = ymin
if testY > ymax:
testY = ymax
distanceX2 = (center_x
-
testX) * (center_x
-
testX)
distanceY2 = (center_y
-
testY) * (center_y
-
testY)
distTotal2 = distanceX2 + distanceY2
fDist = sqrt(distTotal2)
if fDist < radius:
return True
else:
return False
Do two line segments intersect?
Level
–
down right unfair!
Conclusion
C++ and Python
Out with the Old in with the New!!!
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Comments 0
Log in to post a comment