C/C++ 学习手札(一)

来源:互联网 发布:近几年石油开采量数据 编辑:程序博客网 时间:2024/05/19 15:40
    出于需要,最近研究C/C++。简单熟悉一下这个让我遗忘多年的语言。作为学习,在这里记录。同时对比C与C++的差别。
    C的代码:
#include <stdio.h>#include <stdlib.h>/** *  定义一个结构体 */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 < y; i++) {printf("%d/t", i + 1);int j;for (j = i; j < x; j++) {printf("* ");}printf("/n");}return EXIT_SUCCESS;}

这里使用了结构体Location,并生成了一个实例location。通过scanf向x、y输入数字。以location.x = x;方式将数值赋值给结构体location的变量x。由此可以看出结构体就是现在面向对象的基础,尤其是数据对象的前身。

我们希望打印操作能够独立出来,成为一个函数,可以这么写:
// 声明函数void print(int x, int y);

c是面向过程的计算机语言,要在主函数内调用其他函数,必须要在主函数前声明函数,要么就直接把函数按照调用次序逆次由上到下排序。即便是面向对象的C++,也是如此。
/** * 倒三角打印 */void print(int x, int y) {int i;for (i = 0; i < y; i++) {printf("%d/t", i + 1);int j;for (j = i; j < x; j++) {printf("* ");}printf("/n");}


整体代码如下:
#include <stdio.h>#include <stdlib.h>/** *  定义一个结构体 */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 < y; i++) {printf("%d/t", i + 1);int j;for (j = i; j < x; j++) {printf("* ");}printf("/n");}}


    对比C++的代码:
#include <iostream>using namespace std;// 定一个类class Location {private:int x; // 横坐标int y; // 纵坐标public:Location() {}Location(int x, int y) {this->x = x;this->y = y;}int getX() {return x;}void setX(int x) {this->x = x;}int getY() {return y;}void setY(int y) {this->y = y;}};int main() {//声明Location location;cout << "输入X坐标:/t";int x;cin >> x;location.setX(x);cout << "输入Y坐标:/t";int y;cin >> y;location.setY(y);cout << "X坐标是:/t" << location.getX() << endl;cout << "Y坐标是:/t" << location.getY() << endl;//做倒三角打印int i;for (i = 0; i < y; i++) {cout << i + 1 << "/t";int j;for (j = i; j < x; j++) {cout << "* ";}cout << endl;}return 0;}

这里的location就是一个类Location的实例了。同样是赋值操作,对x赋值调用location.setX(x);方法,而内部实现是this->x = x;明显的指针特色->而不是.。这个时候有了私有变量的概念,上面C的代码中的location.x就不合适了。必须使用location.getX()方法输出x的值。仍然让我使用起来不舒服的是cincout ,为什么在C++面向对象的语言里,却不能以方法的方式实现,还是要使用这样的特定的字符串来(>><<)控制呢?真的很别扭。特别是在熟悉Java的实现后,你会觉得C++有的时候很别扭。如果我要重新定义一个公有的方法,除了上述的方式,可以这样做:
#include <iostream>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->x = x;}int getY() {return y;}void setY(int y) {this->y = y;}};Location::Location(int x, int y) {this->x = x;this->y = y;} // 省略

现在类中定义方法Location(int x, int y);然后在类外实现该方法:
Location::Location(int x, int y) {this->x = x;this->y = y;}

上述是一个构造方法,如果要返回值的值类型要定义在方法最前面,如:
int Location::getX() {return y;}


我们把打印操作改成函数实现,注意:在C++里如果一个函数被高频度执行,声明为内联函数(inline),可以提高执行效率!
// 声明函数inline void print(int x, int y);

C++一样没有跳出C的特色,要在主函数调用前声明函数。
/** * 倒三角打印 */inline void print(int x, int y) {int i;for (i = 0; i < y; i++) {cout << i + 1 << "/t";int j;for (j = i; j < x; j++) {cout << "* ";}cout << endl;}}


给出全部代码:
#include <iostream>using namespace std;/** *  定义一个类 */class Location {private:int x; // 横坐标int y; // 纵坐标public:Location() {}Location(int x, int y) {this->x = x;this->y = y;}int getX() {return x;}void setX(int x) {this->x = x;}int getY() {return y;}void setY(int y) {this->y = y;}};// 声明函数inline void print(int x, int y);int main() {//声明Location location;cout << "输入X坐标:/t";int x;cin >> x;location.setX(x);cout << "输入Y坐标:/t";int y;cin >> y;location.setY(y);cout << "X坐标是:/t" << location.getX() << endl;cout << "Y坐标是:/t" << location.getY() << endl;//做倒三角打印print(x, y);return 0;}/** * 倒三角打印 */inline void print(int x, int y) {int i;for (i = 0; i < y; i++) {cout << i + 1 << "/t";int j;for (j = i; j < x; j++) {cout << "* ";}cout << endl;}}

学过Java的人觉得很别扭。呵呵,我也一样。


最后,让我们看看这2个程序的最终输出:
输入X坐标:9输入Y坐标:9X坐标是:9Y坐标是:91* * * * * * * * * 2* * * * * * * * 3* * * * * * * 4* * * * * * 5* * * * * 6* * * * 7* * * 8* * 9* 


换成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 < y; i++) {System.out.print(i + 1 + "/t");for (int j = i; j < x; j++) {System.out.print("* ");}System.out.println();}}}

呵呵,用Java实现,感觉就是好!

相关链接:
C/C++ 学习手札(一)
C/C++ 学习手札(二)
C/C++ 学习手札(三)
原创粉丝点击