---
title: "C/C++ 学习手札（一）"
author: "snowolf"
date: "2009-06-05T13:39:33Z"
tags: ["C", "C++"]
categories: ["编程语言"]
slug: "c-c"
draft: false
lastmod: "2010-01-16T13:12:23Z"
id: 402483
url: "https://snowolf.iteye.com/blog/402483"
---

出于需要，最近研究C/C++。简单熟悉一下这个让我遗忘多年的语言。作为学习，在这里记录。同时对比C与C++的差别。 C的代码： \[code="c"] #include #include /\** * 定义一个结构体 \*/ struct Location { int x; // 横坐标 int y; // 纵坐标 } location; int main(void) { printf("输入X坐标：\\t\\n"); int x; scanf("%d", &x); location.x = x; printf("输入Y坐标：\\t\\n"); int y; scanf("%d", &y); location.y = y; printf("X坐标是：\\t%d\\n", location.x); printf("Y坐标是：\\t%d\\n", location.y); // 做倒三角打印 int i; for (i = 0; i &lt; y; i++) { printf("%d\\t", i + 1); int j; for (j = i; j &lt; x; j++) { printf("* "); } printf("\\n"); } return EXIT\_SUCCESS; } \[/code] 这里使用了结构体\[b]Location\[/b]，并生成了一个实例\[b]location\[/b]。通过\[b]scanf\[/b]向x、y输入数字。以\[b]location.x = x;\[/b]方式将数值赋值给结构体\[b]location\[/b]的变量\[b]x\[/b]。由此可以看出结构体就是现在面向对象的基础，尤其是数据对象的前身。 :D 我们希望打印操作能够独立出来，成为一个函数，可以这么写： \[code="c"] // 声明函数 void print(int x, int y); \[/code] c是面向过程的计算机语言，要在主函数内调用其他函数，必须要在主函数前声明函数，要么就直接把函数按照调用次序逆次由上到下排序。即便是面向对象的C++，也是如此。 \[code="c"] /\** * 倒三角打印 \*/ void print(int x, int y) { int i; for (i = 0; i &lt; y; i++) { printf("%d\\t", i + 1); int j; for (j = i; j &lt; x; j++) { printf("* "); } printf("\\n"); } \[/code] 整体代码如下： \[code="c"] #include #include /\** * 定义一个结构体 \*/ struct Location { int x; // 横坐标 int y; // 纵坐标 } location; // 声明函数 void print(int x, int y); int main(void) { printf("输入X坐标：\\t\\n"); int x; scanf("%d", &x); location.x = x; printf("输入Y坐标：\\t\\n"); int y; scanf("%d", &y); location.y = y; printf("X坐标是：\\t%d\\n", location.x); printf("Y坐标是：\\t%d\\n", location.y); // 做倒三角打印 print(x, y); return EXIT\_SUCCESS; } /\** * 倒三角打印 \*/ void print(int x, int y) { int i; for (i = 0; i &lt; y; i++) { printf("%d\\t", i + 1); int j; for (j = i; j &lt; x; j++) { printf("* "); } printf("\\n"); } } \[/code] 对比C++的代码： \[code="c++"] #include using namespace std; // 定一个类 class Location { private: int x; // 横坐标 int y; // 纵坐标 public: Location() { } Location(int x, int y) { this-&gt;x = x; this-&gt;y = y; } int getX() { return x; } void setX(int x) { this-&gt;x = x; } int getY() { return y; } void setY(int y) { this-&gt;y = y; } }; int main() { // 声明 Location location; cout &lt;&lt; "输入X坐标：\\t"; int x; cin &gt;&gt; x; location.setX(x); cout &lt;&lt; "输入Y坐标：\\t"; int y; cin &gt;&gt; y; location.setY(y); cout &lt;&lt; "X坐标是：\\t" &lt;&lt; location.getX() &lt;&lt; endl; cout &lt;&lt; "Y坐标是：\\t" &lt;&lt; location.getY() &lt;&lt; endl; // 做倒三角打印 int i; for (i = 0; i &lt; y; i++) { cout &lt;&lt; i + 1 &lt;&lt; "\\t"; int j; for (j = i; j &lt; x; j++) { cout &lt;&lt; "* "; } cout &lt;&lt; endl; } return 0; } \[/code] 这里的\[b]location\[/b]就是一个类\[b]Location\[/b]的实例了。同样是赋值操作，对\[b]x\[/b]赋值调用\[b]location.setX(x);\[/b]方法，而内部实现是\[b]this-&gt;x = x;\[/b]明显的指针特色\[color=red]\[b]-&gt;\[/b]\[/color]而不是\[color=red]\[b].\[/b]\[/color]。这个时候有了私有变量的概念，上面C的代码中的\[b]location.x\[/b]就不合适了。必须使用\[b]location.getX()\[/b]方法输出\[b]x\[/b]的值。仍然让我使用起来不舒服的是\[b]cin\[/b] 与 \[b]cout\[/b] ，为什么在C++面向对象的语言里，却不能以方法的方式实现，还是要使用这样的特定的字符串来（\[b]&gt;&gt;\[/b] 与 \[b]&lt;&lt;\[/b]）控制呢？真的很别扭。特别是在熟悉Java的实现后，你会觉得C++有的时候很别扭。如果我要重新定义一个公有的方法，除了上述的方式，可以这样做: \[code="c++"] #include using namespace std; class Location { private: int x, y; public: Location() { } Location(int x, int y); int getX() { return x; } void setX(int x) { this-&gt;x = x; } int getY() { return y; } void setY(int y) { this-&gt;y = y; } }; Location::Location(int x, int y) { this-&gt;x = x; this-&gt;y = y; } // 省略 \[/code] 现在类中定义方法\[b]Location(int x, int y);\[/b]然后在类外实现该方法： \[code="c++"] Location::Location(int x, int y) { this-&gt;x = x; this-&gt;y = y; } \[/code] 上述是一个构造方法，如果要返回值的值类型要定义在方法最前面，如： \[code="c++"] int Location::getX() { return y; } \[/code] 我们把打印操作改成函数实现，\[color=red]注意：在C++里如果一个函数被高频度执行，声明为内联函数（\[b]inline\[/b]），可以提高执行效率！\[/color] \[code="c++"] // 声明函数 inline void print(int x, int y); \[/code] C++一样没有跳出C的特色，要在主函数调用前声明函数。 :( \[code="c++"] /\** * 倒三角打印 \*/ inline void print(int x, int y) { int i; for (i = 0; i &lt; y; i++) { cout &lt;&lt; i + 1 &lt;&lt; "\\t"; int j; for (j = i; j &lt; x; j++) { cout &lt;&lt; "* "; } cout &lt;&lt; endl; } } \[/code] 给出全部代码： \[code="c++"] #include using namespace std; /\** * 定义一个类 \*/ class Location { private: int x; // 横坐标 int y; // 纵坐标 public: Location() { } Location(int x, int y) { this-&gt;x = x; this-&gt;y = y; } int getX() { return x; } void setX(int x) { this-&gt;x = x; } int getY() { return y; } void setY(int y) { this-&gt;y = y; } }; // 声明函数 inline void print(int x, int y); int main() { // 声明 Location location; cout &lt;&lt; "输入X坐标：\\t"; int x; cin &gt;&gt; x; location.setX(x); cout &lt;&lt; "输入Y坐标：\\t"; int y; cin &gt;&gt; y; location.setY(y); cout &lt;&lt; "X坐标是：\\t" &lt;&lt; location.getX() &lt;&lt; endl; cout &lt;&lt; "Y坐标是：\\t" &lt;&lt; location.getY() &lt;&lt; endl; // 做倒三角打印 print(x, y); return 0; } /\** * 倒三角打印 \*/ inline void print(int x, int y) { int i; for (i = 0; i &lt; y; i++) { cout &lt;&lt; i + 1 &lt;&lt; "\\t"; int j; for (j = i; j &lt; x; j++) { cout &lt;&lt; "* "; } cout &lt;&lt; endl; } }\[/code] 学过Java的人觉得很别扭。呵呵，我也一样。 :D 最后，让我们看看这2个程序的最终输出： \[code="console"] 输入X坐标： 9 输入Y坐标： 9 X坐标是： 9 Y坐标是： 9 1 * * * * * * * * * 2 * * * * * * * * 3 * * * * * * * 4 * * * * * * 5 * * * * * 6 * * * * 7 * * * 8 * * 9 * \[/code] 换成Java实现： \[code="java"] import javax.swing.JOptionPane; public class Location { private int x; private int y; /\** * @return the x \*/ public int getX() { return x; } /\** * @param x * the x to set \*/ public void setX(int x) { this.x = x; } /\** * @return the y \*/ public int getY() { return y; } /\** * @param y * the y to set \*/ public void setY(int y) { this.y = y; } /\** * @param args \*/ public static void main(String\[] args) throws Exception { Location location = new Location(); int x = Integer.parseInt(JOptionPane.showInputDialog("输入X坐标：")); int y = Integer.parseInt(JOptionPane.showInputDialog("输入Y坐标：")); location.setX(x); location.setY(y); location.print(x, y); } /\** * 倒三角打印 * * @param x * @param y \*/ public void print(int x, int y) { for (int i = 0; i &lt; y; i++) { System.out.print(i + 1 + "\\t"); for (int j = i; j &lt; x; j++) { System.out.print("* "); } System.out.println(); } } } \[/code] 呵呵，用Java实现，感觉就是好！ :D 相关链接： \[url=http://snowolf.iteye.com/blog/402483]C/C++ 学习手札（一）\[/url] \[url=http://snowolf.iteye.com/blog/410102]C/C++ 学习手札（二）\[/url] \[url=http://snowolf.iteye.com/blog/410984]C/C++ 学习手札（三）\[/url]