当前位置: 首页 > wzjs >正文

搭建门户网站网站建设介绍书

搭建门户网站,网站建设介绍书,泸州住房城乡建设局官方网站,人力资源培训宏定义的数据是在预处理发生了替换 const类型的数据是在编译阶段发生的替换 命名空间 namespace 空间名{int a;void func_print(){printf("func_print");}struct Stu{int x;char *y;};//或者其他命名空间 } Space::x 20; cout << Space::x;using Space::x;…

宏定义的数据是在预处理发生了替换

const类型的数据是在编译阶段发生的替换

命名空间

namespace 空间名{int a;void func_print(){printf("func_print");}struct Stu{int x;char *y;};//或者其他命名空间
}
Space::x = 20;
cout << Space::x;using Space::x;using Space::y;
cout << x << y;using namespace Space;
x = 10;y = 20;
cout << x << y;{using namespace Space;x = 10;y = 20;
}
{using namespace Other;x = 30;y = 60;
}
//相同命名空间会自动合并

string

string s = "qwert";
char chr[50];
strcpy(chr,s.c_str());string s1 = "XXXX";
s.swap(s1);s1.find('i',0);
s1..find("in",0);
//找到返回起始地址,找不到返回-1

打开文件

C

fopen 函数

fopen 函数用于打开一个文件,并返回一个指向 FILE 结构的指针,该指针可用于后续的文件读写操作。fopen 的函数原型如下:

FILE *fopen(const char *filename, const char *mode);
  • filename:这是一个字符串,指定了要打开的文件名。可以是相对路径或绝对路径。

  • mode

  • :这是一个字符串,指定了文件的打开模式。常见的模式包括:

    • "r":只读模式,文件必须存在。

    • "w":写入模式,如果文件存在则清空文件内容,如果文件不存在则创建新文件。

    • "a":追加模式,如果文件存在则在文件末尾追加内容,如果文件不存在则创建新文件。

    • "r+":读写模式,文件必须存在。

    • "w+":读写模式,如果文件存在则清空文件内容,如果文件不存在则创建新文件。

    • "a+":读写模式,如果文件存在则在文件末尾追加内容,如果文件不存在则创建新文件。

  • rewind 函数

    rewind 函数用于将文件指针的位置重置到文件的开头。rewind 的函数原型如下:

  • void rewind(FILE *stream);

  • stream:这是一个指向 FILE 结构的指针,通常是由 fopen 函数返回的文件指针。

  • 使用 rewind 函数后,文件指针会回到文件的起始位置,这在需要重新读取文件内容而不关闭文件的情况下非常有用。

    e.g.

#include <stdio.h>int main() {FILE *fp;char str[] = "这是一个测试。\n再测试一次。\n";// 以写入模式打开文件fp = fopen("test.txt", "w");if (fp == NULL) {perror("无法打开文件");//用来输出错误信息return 1;}// 写入字符串到文件fputs(str, fp);// 关闭文件fclose(fp);// 以读取模式打开文件fp = fopen("test.txt", "r");if (fp == NULL) {perror("无法打开文件");return 1;}// 读取并打印文件内容char c;while ((c = fgetc(fp)) != EOF) {putchar(c);}// 重置文件指针到文件开头rewind(fp);// 再次读取并打印文件内容printf("\n再次读取文件内容:\n");while ((c = fgetc(fp)) != EOF) {putchar(c);}// 关闭文件fclose(fp);return 0;
}

封装特性一:对内数据开放,对外提供接口;数据和行为在一起存放;先有类,再创建对象,最后由对象调用函数

多文件编写更能体现封装

class.h

#include <iostream>
using namespace std;namespace Class
{class time{public:void init();void print();bool isleapyear();int getyear();private:int year;int month;int day;};}

class.cpp  

#include "class.h"
#include <iostream>
using namespace std;namespace Class{void time::init(){cin >> year;cin >> month;cin >> day;return;}void time::print(){cout << year << '.';cout << month << '.';cout << day << '.';return;}bool time::isleapyear(){if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))return true;elsereturn false;}int time::getyear(){return year;}}

main.cpp

#include <iostream>
#include "class.h"using namespace std;
using namespace Class;int main()
{time tim;tim.init();tim.print();if(tim.isleapyear())cout << "Yes," << tim.getyear() << " is a leap year.";elsecout << "No," << tim.getyear() << " is not a leap year.";return 0;
}

封装特性二:有权限控制

构造器(构造函数):

与类名相同;在构造类对象时自动调用,用来初始化;可以有参数,构造器的重载,默认参数;

注意:默认参数只能在声明上

不添加构造器,系统会有默认无参构造器;重载和默认参数不要同时使用,会有ambiguous错误 但一定要包含标配(无参)构造器以实现对象的无参创建

析构器(析构函数)

~与类名相同,无参无返回,用于对象销毁时的内存处理工作

销毁和创建要在同级内对应完成

class Str
{public:Str(){str = new char[100];}~Str(){delete []str;}private:char *str;
};
int main()
{Str *eg = new Str;strcpy(eg.str,"hello,world");delete eg;
}

析构注意事项:

先析构里面的再析构外面的

多个同级对象则按照出入栈的顺序,先创建的后析构,后创建的先析构

自实现string:

mystr.h

using namespace std;
class mystr{public://mystr();mystr(const char * new_str = nullptr);char * c_str();~mystr();private:char * str;
};

mystr.cpp

#include "my_string.h"
#include <string.h>
using namespace std;/*
mystr::mystr()
{str = new char[1];*str = '\0';
}
*/mystr::mystr(const char * new_str)
{if(new_str == nullptr){str = new char[1];*str = '\0';}else{int len = strlen(new_str) + 1;str = new char[len]; strcpy(str,new_str);}
}char * mystr::c_str()
{return str;
}mystr::~mystr()
{delete []str;
} 

main.cpp

#include <iostream> 
#include "my_string.h"
using namespace std;int main()
{mystr str1;mystr str2("abcdefg");cout << str1.c_str() << endl;cout << str2.c_str() << endl;string * p = new string("123456");cout << (*p).c_str() << endl;delete p;mystr * q = new mystr("654321");cout << (*q).c_str() << endl;delete q;return 0;
}

思考:string的本质实现还是char*,所以string *是一个指向char *的指针, *p和 *q是解引用,使其指向char *,即一个字符数组;

自实现list部分功能

mylist.h

using namespace std;struct node
{int num;node *next = nullptr;
};class mylist
{public:mylist();~mylist();void insert(const int &data);void traverseList();private:node * head;
};

mylist.cpp

#include "mylist.h"
#include <iostream>
using namespace std;mylist::mylist()
{head = new node;head -> next = nullptr;//(*head).next = nullptr;
}
mylist::~mylist()
{node * t = head;if(t != nullptr){head = head -> next;delete t;t = head;}}
void mylist::insert(const int &data)
{node *p = new node;p -> num = data; p -> next = head -> next;head -> next = p;
}void mylist::traverseList()
{node * q = head -> next;while(q != nullptr){cout << q -> num << endl;q = q -> next;}
}

main.cpp

#include "mylist.h"
using namespace std;int main()
{mylist mlis;mlis.insert(1);mlis.insert(2);mlis.insert(3);mlis.traverseList();return 0;
}

q:->和.有什么区别?

a:. 操作符(点操作符)

  • 用于直接访问结构体或类的成员。

  • 当您有一个结构体或类的实例(非指针)时,使用.来访问其成员。

-> 操作符(箭头操作符)

  • 用于通过结构体或类的指针来访问其成员。

  • 当您有一个指向结构体或类的指针时,首先需要使用->来解引用指针,然后访问其成员。

示例:

总结:

  • 使用.操作符时,您已经有一个结构体或类的实例。

  • 使用->操作符时,您有一个指向结构体或类的指针,并且需要通过这个指针来访问其成员。


文章转载自:

http://tLofZ1AB.pngdc.cn
http://gSUHM5Vc.pngdc.cn
http://MDEzV6BY.pngdc.cn
http://S7dfRC8y.pngdc.cn
http://tgskf3Cj.pngdc.cn
http://gJC7YtwK.pngdc.cn
http://sZyboGiY.pngdc.cn
http://ropemJXA.pngdc.cn
http://LppS9DkZ.pngdc.cn
http://J8bYKPwj.pngdc.cn
http://XxJT4xCo.pngdc.cn
http://W9G5bHQ5.pngdc.cn
http://I9HrS2lV.pngdc.cn
http://SYtmLjlP.pngdc.cn
http://bkzHvx9e.pngdc.cn
http://oH37tLGj.pngdc.cn
http://fucw9UXe.pngdc.cn
http://nXJY7U0Q.pngdc.cn
http://63IC4xET.pngdc.cn
http://Y8bDnZIK.pngdc.cn
http://NneqzaQ0.pngdc.cn
http://RRepb87g.pngdc.cn
http://hCRTLRl8.pngdc.cn
http://2ebU8LDO.pngdc.cn
http://IXBtF9mA.pngdc.cn
http://ikzA4WSV.pngdc.cn
http://PxpYhoc3.pngdc.cn
http://MuTstVDv.pngdc.cn
http://1ylaeWo4.pngdc.cn
http://CHCla7Z3.pngdc.cn
http://www.dtcms.com/wzjs/765183.html

相关文章:

  • 免费无代码开发平台手机网站如何优化
  • zero的大型网站seo教程荷塘网站建设
  • 做推广便宜的网站包装设计网上设计平台
  • 网站建设域名服务器广州公司网站设计制作
  • 顺义推广建站现在流行什么做网站
  • 网站免费正能量直播网站建设工作进度
  • 商丘市做1企业网站的公司高端网站建设公司推荐
  • 发表评论的wordpress网站模板广西桂林自驾游最佳线路推荐
  • 五合一网站做优化好用吗摄影比赛投稿网站
  • 地方网站怎么做推广上海网站建设信息网
  • 时光轴 网站赌城网站怎么做
  • iis做的网站模板wordpress都有哪些权限
  • 免费网站地址申请做网站的详细流程
  • 公司网站网页设计如何对现有的网站改版
  • 生成论坛网站英文网站建设方案
  • 网站点击量怎么看砀山县住房和城乡建设局网站
  • 有没有专门做建筑造价的私单网站手机企业网站怎么做
  • 沧州市做网站上海市建设工程安全生产协会网站
  • 扬中网站建设要多少钱wordpress下载
  • ps做网站要多大软件开发和网站建设哪个好
  • 外贸网站建设服务创意网站页面
  • jsp类型网站托管费用工程建设信息网站资质公告
  • 做网站业务的怎么寻找客户wordpress采集查卷
  • 烟台市网站建设网站切版教程
  • 网站内容收录wordpress 推酷
  • 如何把自己电脑做网站服务器网页美工设计课程
  • 做商城网站的流程介绍中关村在线网站的建设
  • 怎么做售房网站seo是一种利用搜索引擎
  • 在互联网上建设网站可选择的方案有社交公众号开发公司
  • 有趣的网站官网邯郸网络营销和网络推广