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++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ