C/C++进修手札(一)
副标题#e#
出于需要,最近研究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.由此可以看出布局体就是此刻面向工具的基本,尤其是数据工具的前身。
#p#副标题#e#
我们但愿打印操纵可以或许独立出来,成为一个函数,可以这么写:
// 声明函数
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");
    }
/**
 * 倒三角打印
 */
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;
}
#p#分页标题#e#
这里的location就是一个类Location的实例了。同样是赋值操纵,对x赋值挪用location.setX(x);要领,而内部实现是this->x = x;明明的指针特色->而不是。。这个时候有了私有变量的观念,上面C的代码中的location.x就不符合了。必需利用location.getX()要领输出x的值。仍然让我利用起来不舒服的是cin 与 cout ,为什么在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个措施的最终输出:
console代码
#p#分页标题#e#
输入X坐标:  9
输入Y坐标:  9
X坐标是:   9
Y坐标是:   9
1   * * * * * * * * *
2   * * * * * * * *
3   * * * * * * *
4   * * * * * *
5   * * * * *
6   * * * *
7   * * *
8   * *
9   *
换成Java实现:
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实现,感受就是好!