dede移动端网站源码欧米伽官方网站手表价格
- 作者: 多梦笔记
- 时间: 2026年02月16日 10:04
当前位置: 首页 > news >正文
dede移动端网站源码,欧米伽官方网站手表价格,怎样做微信公众号小程序,菏泽科技网站建设目录 1、C基础 IO流 namespace 引用、const inline、函数参数 重载 2、类和对象 类举例 3、 内存管理 new/delete 对象内存分布 内存泄漏 4、继承 继承权限 继承中的构造与析构 菱形继承 1、C基础 IO流 #include iostream #include iomanip //…目录 1、C基础 IO流 namespace 引用、const inline、函数参数 重载 2、类和对象 类举例 3、 内存管理 new/delete 对象内存分布 内存泄漏 4、继承 继承权限 继承中的构造与析构 菱形继承 1、C基础 IO流 #include iostream #include iomanip // 对于setprecision, setw等 #include string #include fstream // 对于文件操作using namespace std; int main() {int num;cin num; // 输入流if (cin.fail()){cerr input not right; // 标准err输出}cout num endl; // 标准输出// 二进制文件读写ofstream binfile(test.bin, ofstream::out | ofstream::binary);char in[] A;binfile.write(in, 1);binfile.put(B);binfile.close();ifstream ifile(test.bin, ofstream::in | ofstream::binary); // 直接构造也可以ifile.seekg(0, ifile.end); // 跳转到文件末尾int length ifile.tellg(); // 获取当前字符在文件当中的位置即文件的字符总数ifile.seekg(0, ifile.beg); // 重新回到文件开头char data[1024];ifile.read(data, length); // 将文件当中的数据全部读取到字符串data当中cout data endl;ifile.close(); // 关闭文件ofstream outFile(example.txt); // 只读创建 ofstream 对象if (!outFile){std::cerr Unable to open file for writing;return 1; // 返回错误代码}outFile hello endl;outFile test endl;outFile.close();ifstream input(example.txt); // 只读if (!input){cerr Unable to open file;return 1;}std::string line;while (std::getline(input, line)){std::cout line std::endl;}input.close(); // 关闭文件fstream file(example.txt, std::ios::in | std::ios::out | std::ios::app);file \nAppending this line to the file. std::endl;file.seekg(0, std::ios::beg);// 从文件读取数据并输出到控制台std::string line1;while (std::getline(file, line1)){std::cout line1 std::endl;}file.close(); // 关闭文件return 0; } namespace #include iostream using namespace std; namespace A {int a1; } // namespace A namespace B {int a2;namespace C{struct T{int a4;char c;};int a3;} // namespace C }; // namespace B int main() {coutA::a A::a; B::a B::a; C::a B::C::aendl;B::C::T t1;coutt1.aendl; using B::C::T;T t2;coutt2.aendl;bool Ftrue;coutboolalphaFendl; //noboolalpha 输出1int a1,b2,c3;auto max (a b) ? ((a c) ? a : c) : ((b c) ? b : c);//三目运算(ab? a : b) 30;//C中的三目运算符返回的是变量值不能作为左值使用.而C则可以在任意地方return 0; } 引用、const #include iostream using namespace std; struct A {int a;char c; }; void show(A *p) {cout show func 指针调用前 endl;cout p-a \t p-c endl;p-a 20;cout show fun 指针修改调用后 endl;cout p-a \t p-c endl; } void show1(A p) {cout show1 引用调用前 endl;cout p.a \t p.c endl;p.a 35;cout show1 引用修改调用后 endl;cout p.a \t p.c endl; } void show2(A p) { // 临时变量实参的一份拷贝修改对实参没有影响cout show2 传值调用前 endl;cout p.a \t p.c endl;p.a 45;cout show2 传值修改调用后 endl;cout p.a \t p.c endl; } // 指针所指向的内存空间,不能被修改 int operA(const A *p) {// p-a10;return 0; } // 指针变量本身不能被修改 int operatorA1(A *const pT) {pT-a 10;// pT NULL; //return 0; } // 指针变量和所指的值都不可以改变 int operatorA2(const A *const pT) {// pT-a 10;// pT NULL; //cout A! endl;return 0; } // 数据交换, 值交换无法完成 void myswap(int i, int j) {int c 0;c i;i j;j c; } // 数据交换, 指针直接对实参修改 void myswap1(int *i, int *j) {int c 0;c *i;*i *j;*j c; } // 数据交换, 引用交换,引用做函数参数时不用初始化 void myswap2(int i, int j) {int c 0;c i;i j;j c; } // 返回a的本身 a的副本 int getA1() {int a 10;return a; } /*想让引用返回可以用就需要将变量生命为static的 /当函数返回值为引用时若返回栈变量不能成为其它引用的初始值 不能作为左值使用若返回静态变量或全局变量可以成为其他引用的初始值 即可作为右值使用也可作为左值使用/ int getA2() {int a 20;// warning: reference to local variable ‘a’ returned [-Wreturn-local-addr]return a; } int *getA3() {int a 30;// warning: reference to local variable ‘a’ returnedreturn a; } int main() {// const operatorA*const int x 0;int const y 2;// x3; y2;//两个定义一样的效果修改报错assignment of read-only variable x// int const z; //未被初始化 error: uninitialized const z// 引用int var 10;int temp var; // 引用需要初始化像一个常量有自己的空间sizeof与指针一样temp 12; // 变量的别名cout var: var temp endl; // 修改引用同修改本身 12 12int temp1 10, temp2 110;cout berfor myswap temp1: temp1 temp2: temp2 endl;myswap(temp1, temp2); // 数据交换, 值交换无法完成cout after myswap temp1: temp1 temp2: temp2 endl;myswap1(temp1, temp2); // 指针传入交换cout after myswap temp1: temp1 temp2: temp2 endl;myswap2(temp1, temp2); // 引用的本质时间接修改实参编译器创造了指针然后修改// 间接赋值的三个条件1定义两个变量2变量之间建立关联3形参间接修改实参引用是后两个条件合二为一cout after myswap temp1: temp1 temp2: temp2 endl;cout —-复杂类型引用 学习—- endl;A t {101, A}; // 结构体 列表初始化cout main调用指针调用前 endl;cout t.a \t t.c endl; // 101 Ashow(t);cout main调用指针调用后 endl;cout t.a \t t.c endl;show1(t);cout main引用调用后 endl;cout t.a \t t.c endl;show2(t);cout main传值调用后 endl;cout t.a \t t.c endl;cout ——函数返回值是一个引用——- endl;int ga 1;int ga1 2;ga getA1();// ga1getA2(); //引用相当于指针临时变量返回易出错。因为值被释放了// int ga2getA2(); //栈变量返回不能作为初始值cout ga: ga endl;int *p NULL; // 指针可以为null// p getA3();// cout *p endl; // 证明了引用和指针一样返回临时变量的指针有可能被释放了再去使用出现异常return 0; } inline、函数参数 #include iostream using namespace std; #define MYFUNC(a,b)((a)(b)?(a):(b)) inline void printA() {int a 10;cout a: a endl; } inline int Myfun(int a,int b){return ab?a:b; } struct A {char c;int a; }; void show(const A m) {// m.c10; // error: assignment of member A::c in read-only objectcout m.a endl; } //函数占位参数 函数调用时,必须写够参数 void func1(int a,int b, int){couta: a b: bendl; } //默认参数与占位参数 void func2(int a,int b, int0){couta: a b: bendl; } // void func3(int a0,int b){ 在默认参数规则 如果默认参数出现那么右边的都必须有默认参数 // } int main() {int a 10;int b a; // 普通引用必须初始化// int c10; //字面值不可以引用要取地址int x 12;const int y x; // 让变量引用只读属性不能通过y修改x// y20; 不能修改, x可以改变x 13; //修改值y也变了// 常引用的初始化可以用变量或者常量值{const int a 10;// int m11;const int m 40;}A t {1, 1};show(t);cout —-inline——–endl;printA();//与下相同// {// int a 10;// coutaaendl;// }C编译器直接将函数体插入在函数调用的地方//内联函数省去了普通函数调用时压栈跳转和返回的开销int a11, b13;int cMYFUNC(a1,b1);int dMyfun(a1,b1);cout define: a1: a1 b1: b1 c: cendl;cout inlice: a1: a1 b1: b1 d: dendl;//int c1MYFUNC(a1,b1); //a3 b3 c3 //宏替换并展开 ((a) (b) ? (a) : (b)) //a3 b3 c3// cout define : a1: a1 b1: b1 c1: c1endl;int d1Myfun(a1,b1); //a2 b3 c2cout inlice : a1: a1 b1: b1 d: d1endl;func1(a,b,c); //占位符必须有参数func2(a,b); //okfunc2(a,b,c); //okreturn 0; } 重载 #include iostream using namespace std; int func(int x) {return x; } int func(int x, int y) {return x y; } // 函数重载 和 函数默认参数 在一起 // int func(int x, int y, int c 0) // { // return x y c; // } auto func(const string s) {return s.size(); } // void func(int x,int y){ //函数返回值不是函数重载的判断标准 // x xy; // coutxendl; // } void func(string *s1, string *s2) {cout *s1 \t *s2 endl; } // 定义一个函数类型 typedef int(myfun)(int a, int b); myfun *f nullptr; // 定义函数指针指向函数入口 // 声明一个函数指针类型 typedef int (*myfunc1)(int a, int b); //myfunc1 fp nullptr; // 定义一个函数指针 变量 void (*myVarPFunc)(int a, int b); int main() {int a 0;a func(1);cout func a: a endl;// afunc(1,2); //重载与默认参数在一起产生了二义性cout func1 a: a endl;cout func2 a: func(abcd) endl;string s abcde;string s1 hjic;func(s, s1);myfunc1 fp;fp func;coutfp(1,3)endl;return 0; } 2、类和对象 类举例 #include iostream #include stringclass Student { private:std::string name; // 学生姓名int age; // 学生年龄public:// 默认构造函数Student() : name(Unknown), age(0) {std::cout Default constructor called.\n;}// 带参数的构造函数Student(const std::string name, int age) : name(name), age(age) {std::cout Parameterized constructor called for name .\n;}// 拷贝构造函数Student(const Student other) {this-name other.name; // 使用 this 指针this-age other.age; // 使用 this 指针std::cout Copy constructor called for this-name .\n;}// 拷贝赋值运算符Student operator(const Student other) {if (this ! other) { // 防止自我赋值this-name other.name; // 使用 this 指针this-age other.age; // 使用 this 指针std::cout Copy assignment operator called for this-name .\n;}return this; // 返回当前对象的引用}// 析构函数~Student() {std::cout Destructor called for name .\n;}// 显示学生信息void display() const {std::cout Name: name , Age: age \n;} };int main() {// 使用默认构造函数Student student1;student1.display();// 使用带参数的构造函数Student student2(Alice, 20);student2.display();// 使用拷贝构造函数Student student3 student2; // 调用拷贝构造函数student3.display();// 使用拷贝赋值运算符Student student4;student4 student2; // 调用拷贝赋值运算符student4.display();return 0; } 3、 内存管理 new/delete #include iostream #include cstdlib // 包含 malloc 和 free 的头文件class MyClass { public:MyClass() {std::cout MyClass constructor called.\n;}~MyClass() {std::cout MyClass destructor called.\n;} }; int main() {// 使用 new 和 deletestd::cout Using new and delete:\n;MyClass obj1 new MyClass(); // 使用 new 分配内存delete obj1; // 使用 delete 释放内存// 使用 malloc 和 freestd::cout \nUsing malloc and free:\n;// 注意malloc 只分配内存不调用构造函数MyClass* obj2 (MyClass)malloc(sizeof(MyClass)); // 使用 malloc 分配内存if (obj2) {new (obj2) MyClass(); // 手动调用构造函数}// 使用 free 释放内存但需要手动调用析构函数if (obj2) {obj2-~MyClass(); // 手动调用析构函数free(obj2); // 使用 free 释放内存}return 0; } 对象内存分布 #include iostream #include stringclass MyClass { public:int value;MyClass(int v) : value(v) {std::cout constructor called for value: value \n; //10}~MyClass() {std::cout destructor called for value: value \n;} };// 全局变量存储在数据区 MyClass globalObj(10); int main() {// 栈区对象MyClass stackObj(20); std::cout Address of stackObj: stackObj \n; //10// 堆区对象MyClass heapObj new MyClass(30);std::cout Address of heapObj: heapObj \n;// 数据区对象std::cout Address of globalObj: globalObj \n;// 程序区代码区对象std::cout Address of main function: (void*)main \n;// 释放堆区对象delete heapObj;/constructor called for value: 10constructor called for value: 20Address of stackObj: 0x6963bffbd4constructor called for value: 30Address of heapObj: 0x22296241640Address of globalObj: 0x7ff6cc537030Address of main function: 0x7ff6cc531450destructor called for value: 30destructor called for value: 20destructor called for value: 10/return 0; }内存泄漏 #include iostreamclass MyClass { public:MyClass() {std::cout MyClass constructor called.\n;}~MyClass() {std::cout MyClass destructor called.\n;} };void createMemoryLeak() {MyClass* obj new MyClass(); // 动态分配内存// 忘记释放内存导致内存泄漏 } void createNoMemoryLeak() {std::unique_ptrMyClass obj std::make_uniqueMyClass(); // 使用智能指针// 不需要手动释放内存智能指针会自动管理 }int main() {for (int i 0; i 5; i) {createMemoryLeak(); // 每次调用都会造成内存泄漏}std::cout Memory leak has occurred.\n;for (int i 0; i 5; i) {createNoMemoryLeak(); // 不会造成内存泄漏}return 0; }4、继承 继承权限 #include iostream using namespace std;// 继承 // public 修饰的成员变量 方法 在类的内部 类的外部都能使用 // protected: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ;其他 类的外部不能被使用 // private: 修饰的成员变量方法 只能在类的内部使用 不能在类的外部 // 这几个修饰符用在继承里影响的是子类的访问全限// 类型兼容性原则 class Parent { public:void show(){cout show 父类 endl;a 0;b 1;c 2;cout a: a b: b c: c endl;}int a;Parent(){cout –父类 无参 构造 – endl;}Parent(const Parent p){cout 父类 拷贝 构造— endl;}protected:int b;private:int c; }; class Child1 : public Parent { private:int c1; // 子类有自己的属性 public:int a1;void show1(){ // 访问控制a1 1;b1 2;c1 3;cout show child1 子类1 endl;cout a b a1 b1 c1 endl;// c 在子类范围内父类里的属性不变c是私有已经是类外了}protected:int b1; }; class Child2 : protected Parent { public:int a2;void show2(){ // 访问控制 protected继承public变成protecteda2 2;b2 4;c2 4;cout child2 子类2 endl;cout a b a2 b2 c2 endl;// c 在子类范围内父类里的属性不变c是私有已经是类外了}protected:int b2;private:int c2; // 子类有自己的属性 public: }; class Child3 : private Parent { public:int a3;void show3(){ // 访问控制 私有继承全变私有a 0;b 1;a3 4;b3 4;c3 5;cout child3 子类3 endl;cout a b a3 b3 c3 endl;// c 在子类范围内父类里的属性不变c是私有已经是类外了}protected:int b3;private:int c3; }; void howShow(Parent *base){ //指针base-show();//父类成员函数 } void howShow1(Parent base){ //引用base.show();//父类成员函数 }int main() {Child1 cl;// 子类继承了父类的所有属性和方法cout 公有继承 endl;cl.a 1; // public 类外可以// 全是类外不可以访问// cl.b1;// cl.c1;// cl.a12;// cl.b12;// cl.c13;cl.show1();cout 保护共有继承 endl;Child2 c2;// 子类继承了父类的所有属性和方法// c2.a 1; //变protected 类外可以// 全是类外不可以访问// c2.b1;// c2.c1;// c2.a22;// c2.b22;// c2.c23;c2.show2();Child3 c3;// 私有更不用说了c3.show3();Parent p1;p1.show();//基类指针指向子类Parent *pnullptr;pcl;p-show();//指针做函数参数howShow(p);howShow(cl);//引用做函数参数howShow1(p1);howShow1(cl);//第二层含义//可以让子类对象 初始化 父类对象//子类就是一种特殊的父类Parent p3 cl;return 0; }继承中的构造与析构 #include iostream #include cstring using namespace std;/*1、子类对象在创建时会首先调用父类的构造函数2、父类构造函数执行结束后执行子类的构造函数3、当父类的构造函数有参数时需要在子类的初始化列表中显示调用4、析构函数调用的先后顺序与构造函数相反 */ class Object { public:Object(int a, int b){this-a a;this-b b;coutobject构造函数 执行 aa b bendl;}~Object(){coutobject析构函数 \n;} protected:int a;int b; }; class Parent : public Object { public:Parent(int p) : Object(1, 2){this-p p;cout父类构造函数…pendl;}~Parent(){cout析构函数…pendl;}void printP(int a, int b){cout我是爹…endl;} protected:int p;}; class child : public Parent { public:child(int p) : Parent(p) , obj1(3, 4), obj2(5, 6){this-p p;cout子类的构造函数pendl;}~child(){cout子类的析构pendl;}void printC(){cout我是儿子endl;} protected:int p;Object obj1;Object obj2; };菱形继承 #include iostream// 基类 class Base { public:void show() {std::cout Base class show function called.\n;} };// 派生类 A class DerivedA : public Base { public:void show() {std::cout DerivedA class show function called.\n;} };// 派生类 B class DerivedB : public Base { public:void show() {std::cout DerivedB class show function called.\n;} };// 最终派生类 C class DerivedC : public DerivedA, public DerivedB { public:void show() {std::cout DerivedC class show function called.\n;} };int main() {DerivedC obj;// 调用 DerivedC 的 show 方法obj.show(); // 输出 DerivedC 的 show 方法// 访问基类的 show 方法obj.DerivedA::show(); // 明确调用 DerivedA 的 show 方法obj.DerivedB::show(); // 明确调用 DerivedB 的 show 方法return 0; }在菱形继承中DerivedC 通过 DerivedA 和 DerivedB 都继承了 Base 类的成员。如果不明确指定编译器无法确定应该调用 DerivedA 还是 DerivedB 中的 Base 类成员这就导致了二义性。 如果加virtual就可以访问基类的 show 方法 obj.Base::show(); // 通过虚继承只有一个 Base 的实例 。
- 上一篇: dede小游戏php网站源码镇江网站建设一般多少钱
- 下一篇: dede增加手机网站专业SEO教程网站
相关文章
-
dede小游戏php网站源码镇江网站建设一般多少钱
dede小游戏php网站源码镇江网站建设一般多少钱
- 站长
- 2026年02月16日
-
dede网站栏目管理空白公司响应式网站建设报价
dede网站栏目管理空白公司响应式网站建设报价
- 站长
- 2026年02月16日
-
dede网站建设企业网站 建设公司
dede网站建设企业网站 建设公司
- 站长
- 2026年02月16日
-
dede增加手机网站专业SEO教程网站
dede增加手机网站专业SEO教程网站
- 站长
- 2026年02月16日
-
dede做的网站怎样去换模版佛山制作手机网站
dede做的网站怎样去换模版佛山制作手机网站
- 站长
- 2026年02月16日
-
dede做购物网站南宁网站制作公
dede做购物网站南宁网站制作公
- 站长
- 2026年02月16日
