没有合适的资源?快使用搜索试试~ 我知道了~
首页从python转c++
从python转c++
需积分: 44 77 下载量 99 浏览量
更新于2023-05-22
评论 3
收藏 128KB PDF 举报
从python转c++英文讲解,讲的很全面,不断补充。从python转c++英文讲解,讲的很全面,不断补充。
资源详情
资源评论
资源推荐
C++ for Python Programmers
Adapted from a document by Rich Enbody & Bill Punch of Michigan State University
Purpose of this document
This document is a brief introduction to C++ for Python programmers -- something to
help start a transition to C++. It is not meant to be a complete introduction to C++.
Brevity took precedence over details.
Why C++?
Why should Python programmers care about C++?
C++ and its ancestor language C allow one to operate closer to the operating system and
hardware. That closeness can result in better performance, but it is not guaranteed. Also,
C++ is a common and powerful language so it is worth learning. Look "under the hood"
of many applications and operating systems, and you find C++ or C.
One thing to keep in mind when learning C++ is to keep an eye out for the STL: the C++
Standard Template Library. The STL is frequently overlooked in textbooks, but it is a
powerful set of tools and will be your friend.
There are other languages worth learning, but this page is devoted solely to the transition
from Python to C++. Note that C++ preceded Java and C# so one finds many similarities
(as well as significant differences). Once one learns C++ it is relatively easy to transition
to the other two.
Finally, language choice need not be an "either/or" situation. You can code in Python and
then incorporate C++ where you need speed or low-level access to system components. It
is relatively easy to wrap C++ libraries for calling from Python (many useful ones are
already wrapped for you). Python plays well with other languages. Tools such as Bgen,
PySTL, and Sip help.
Here at Michigan State University our first course for Computer Science and Computer
Engineering majors is in Python, and the second course uses C++ in a Linux
environment.
Hello World!
The tradition in C programming is to write one's first program to simply print "Hello
World!" That is how the first C programming book The C Programming Language by
Kernighan and Ritchie began. Since C++ is derived from C, we'll start the traditional
way.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
The first thing a Python programmer notices is significant overhead to simply print a
string. The #include works like import in Python (not really, but the analogy works for
now) so in this case the input/output stream library is brought in. In this case, that is
where cout is defined. The using namespace specifies the namespace so we can
use cout instead of std::cout.
The int main() begins the declaration of the main function which in C++ indicates
the first function to executed, i.e. "begin here." The curly braces { and } delimit the
beginning and end of a block of code, in this case the beginning and end of the function
main.
The statement cout << "Hello World!"; directs the string to cout, the standard
output stream, which displays it. Indentation is optional in C++, but code will be
unreadable, if you do not indent. If you indent like you were forced to do in Python, your
code will be readable.
Gotcha #1: Statements in C++ end with a semicolon ";" (that isn't precisely correct, but
will do for now).
Running the program.
C++ is a compiled language so you must compile it before executing it. Compilation
takes the C++ code and translates it into machine language that a processor understands.
Compiling and executing code is system dependent. For simplicity, I will describe how to
do it in Linux.
The program must be written using some editor such as VI or EMACS on Linux. A C++
program file extension is .cpp so save the program as hello.cpp.
The GNU compiler, g++, is widely available so compiling is simply the line:
g++ hello.cpp
If compilation is successful, the executable code will by default be written to a file named
a.out which can be executed by simply typing a.out at the command prompt. If the
compiler finds syntactical errors in the program, error messages will be printed.
On a Windows computer the Visual C++ development environment is an excellent, but
sophisticated, way to create and run programs (named VisualStudio). You compile and
edit within the same application. Compilation is done by "building" the program.
Running is simply a command in the application.
C++ is strongly typed
C++ is a strongly typed language -- not precisely strongly typed, but close enough for
now. That is, all objects (things you name) must be declared before use. Python figures
out types based on the objects you assign names to, but C++ wants to know up front.
Python allows names to refer to different types of objects while the program is running;
in C++ the type remains fixed.
For example, the type of a variable must be stated before use.
int x; x = 2;
Gotcha #2: Forgetting to declare a variable before use.
(Don't worry the compiler will yell at you, if you forget.)
Note how each statement ends with a semicolon (Gotcha #1).
C++ types
Some C++ types are familiar:
• int
• float
• bool: same as Boolean
• string: may require #include<string>
Some are unfamiliar:
• char: single character, but a string of length one is not equivalent.
Some differences:
• int doesn't have extended precision. An int is limited by machine word size
(usually 32 bits, but 64-bit use is growing); roughly, a max of 2 billion.
• There are many variations of int: short, long, unsigned, unsigned long, etc., but
you can go far with only int.
Assignment
Assignment is familiar:
x = 4;
but, multi-assignment is not allowed:
剩余12页未读,继续阅读
weixin_43277708
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0