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

2024-01-15 19:08
文章标签 c++ 学习 手札

本文主要是介绍C/C++ 学习手札(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

出于需要,最近研究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;
}

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

我们希望打印操作能够独立出来,成为一个函数,可以这么写:

// 声明函数
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;
}

这里的[b]location[/b]就是一个类[b]Location[/b]的实例了。同样是赋值操作,对[b]x[/b]赋值调用[b]location.setX(x);[/b]方法,而内部实现是[b]this->x = x;[/b]明显的指针特色[color=red][b]->[/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]>>[/b] 与 [b]<<[/b])控制呢?真的很别扭。特别是在熟悉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;
}
// 省略

现在类中定义方法[b]Location(int x, int y);[/b]然后在类外实现该方法:

Location::Location(int x, int y) {
this->x = x;
this->y = y;
}

上述是一个构造方法,如果要返回值的值类型要定义在方法最前面,如:

int Location::getX() {
return y;
}


我们把打印操作改成函数实现,[color=red]注意:在C++里如果一个函数被高频度执行,声明为内联函数([b]inline[/b]),可以提高执行效率![/color]

// 声明函数
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的人觉得很别扭。呵呵,我也一样。 :D


最后,让我们看看这2个程序的最终输出:

输入X坐标: 9
输入Y坐标: 9
X坐标是: 9
Y坐标是: 9
1 * * * * * * * * *
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实现,感觉就是好! :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]

这篇关于C/C++ 学习手札(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/609907

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序