C++语法学习记录

1 class

C++是一种面向对象的编程语言,需要创建一个个对象,也就是类。

如下程序所示,创建了一个体积类(volume),类包括长宽高。

而A是体积类的一个实例。

#include <iostream>
using namespace std;

class volume  //创建类的格式:class 类的名称
{
public:     //确定类的成员属性,如果没有定义,默认为私有的。
    int length;
    int width;
    int height;
};        //注意这里有一个分号

void main()
{
    volume A;
    A.length = 1;
    A.width = 2;
    A.height = 3;
    cout << "length = " << A.length << endl;
}

2 public与private

在定义一个类时,在类里需要定义变量与函数,在定义前需要说明属性,通过public、private、protected三个关键字申明。

如下程序所示,将public该为private,进行编译。

#include <iostream>
using namespace std;

class volume
{
private:
    int length;
    int width;
    int height;
};

void main()
{
    volume A;
    A.length = 1;
    A.width = 2;
    A.height = 3;
    cout << "length = " << A.length << endl;
}

在编译过程中,程序报错,如下图所示。

error C2248: 'length' : cannot access private member declared in class 'volume'

私有成员不能直接使用成员访问符(.)来访问。

3 using namespace std;

在学习C++时,教材上的每一个例程都包含如下语句:

using namespace std;

这里namespace为命名空间关键字,std为一个命名空间,添加这条语句后,使用cout、cin输入输出语句比较方便。

当把这条语句注释后,进行编译。

#include <iostream>
//using namespace std;

class volume
{
public:
    int length;
    int width;
    int height;
};

void main()
{
    volume A;
    A.length = 1;
    A.width = 2;
    A.height = 3;
    cout << "length = " << A.length << endl;
}

在编译过程中,程序报错,提示如下:

error C2065: 'cout' : undeclared identifier
error C2065: 'endl' : undeclared identifier

cout与endl是没有说明的关键字。解决的方法是在cout与endl前加上相应的命名空间,如下所示。

std::cout << "length = " << A.length << std::endl;

为了简便,直接在程序开头加上如下语句即可。

using namespace std;

4 cout与cin

cin是输入语句,与 >> 配合使用,如下所示。

volume A;
cin >> A.length;

cout是输出语句,与 << 配合使用,如下所示。

cout << "length = " << A.length << endl;

其中endl是换行的意思。

5 int main(int argc, char* argv[])

char* argbv[]形参解析:

首先类型为一个指针,指向字符的指针;

然后变量不是一个,而是一个数组,数组里存放的都是指向字符的指针。

如下是一个测试程序:

#include <iostream>
using namespace std;

void test(char* argv[])
{
    char A[]= "hello";
    char B[] = "world";
    argv[0] = A;    //数组名相当于一个指针
    argv[1] = B;
    cout << (argv[0]) << endl;
    cout << (argv[1]) << endl;
}

void main()
{
    char * c[2];
    test(c);
}

输出结果如下:

hello
world
Press any key to continue

6 string的用法

定义string的语句如下。

string sNew;

在使用如下语句时,运行程序显示报错,内存溢出。

 

    while(s[i] != '\0')
    {
        if(s[i] == ' ')
        {
            sNew[count++] = '%';
            sNew[count++] = '2';
            sNew[count++] = '0';
        }
        else
        {
            sNew[count++] = s[i];
        }
        i++;
    }

 

程序进行如下修改,可通过编译。可以看出在C++中,字符串可直接进行相加。

    while(s[i] != '\0')
    {
        if(s[i] == ' ')
        {
            sNew += "%20";
        }
        else
        {
            sNew += s[i];
        }
        i++;
    }

疑问:定义一个string类型后,内存分配的空间有多大?

为什么第一段程序会报错?

7 结构体创建

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
//创建一个结点
TreeNode* root_node;
root_node = (struct TreeNode*)malloc(sizeof(struct TreeNode));//需要开辟空间
root_node = (struct TreeNode*)malloc(sizeof(struct TreeNode*));//这样开辟会报错

疑问:什么时候要用struct关键字,什么时候不需要?

8 科学计数法

100可以用1e2表示

103可以用1e2+3表示

9 取模

题目的要求为:“答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。”

取模的意思就是如果n大于1e9+7,n = n - 1e9+7。代码可以这么写:

 

n = n % (1e9 + 7);

 

实际测试这么取余好像有问题,报错如下。

Line 26: Char 18: fatal error: invalid operands to binary expression ('int' and 'double')
            temp %= 1e9+7;

找到报错的原因:不要用科学计数法,直接用1000000007。

n = n % 1000000007;

另外直接 n = n - 1000000007;不能达到取模的效果,因为n = 3 * 1000000007的时候,还是没能完整取模。

 

参考

面向过程和面向对象区别

https://www.bilibili.com/video/BV11J41157TJ

C++ 菜鸟教程

https://www.runoob.com/cplusplus/cpp-classes-objects.html

重学C++[Modern C++]:namespace 详解

https://www.bilibili.com/video/BV1ZE41147mj?from=search&seid=15370591080568859981

原文链接: https://www.cnblogs.com/QQ2962269558/p/12923785.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++语法学习记录

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/349662

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午5:49
下一篇 2023年3月2日 上午5:49

相关推荐