1.C和CPP的差异
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> // C++标准支持 C++的与众不同
using namespace std;
int main() {
printf("降龙十八掌(C版)\n");
cout << "C++语言的学习" << endl;
cout << "擒龙功" << endl;
cout << "铁头功\n" << "金刚腿\n" << "铁布衫\n";
return 0; }
|
2.C与C++常量
C语言的常量,其实是个假常量。
1 2 3 4 5 6 7 8
| int main() { const int number = 100; int * numP = &number; *numP = 10000; printf("%d\n", number); return 0; }
|
1 2 3 4 5 6 7 8 9
| int main() { const int number = 100; printf("%d\n", number); return 0; }
|
3. &引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <iostream>
using namespace std;
void numberChange(int * number1, int * number2) { int temp = 0; temp = *number1; *number1 = *number2; *number2 = temp; }
void numberChange2(int & number1, int & number2) {
cout << "numberChange2 " << "n1地址:" << &number1 << " , n2地址:" << &number2 <<endl;
int temp = 0; temp = number1; number1 = number2; number2 = temp; }
int main() {
int number1 = 10; int number2 = 20;
cout << "main " << "n1地址:" << &number1 << " , n2地址:" << &number2 <<endl;
numberChange2(number1, number2);
cout << "n1:" << number1 << " , n2:" << number2 << endl;
cout << endl;
int n1 = 999; int & n2 = n1; int & n9 = n1; n2 = 777; n9 = 9527; cout << "地址:" << &n1 << "---" << &n2 << endl; cout << "值:" << n1 << "---" << n2 << endl;
return 0; }
|
1 2 3 4 5 6 7
| 运行结果 main n1地址:0x61fe0c , n2地址:0x61fe08 numberChange2 n1地址:0x61fe0c , n2地址:0x61fe08 n1:20 , n2:10
地址:0x61fe04---0x61fe04 值:9527---9527
|
4.常量引用
C++的常量是真的常量,C是伪常量,C的常量可以通过指针修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include <iostream> #include <string.h>
using namespace std;
typedef struct { char name[20]; int age; }Student;
void insertStudent(const Student & student) {
Student student2 = {"刘奋", 43};
cout << student.name << "," << student.age << endl; }
int main() { Student student = {"张无忌", 30}; insertStudent(student); return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| void insertStudent(Student student){ }
void insertStudent(const Student &student) { student2 = this; }
|
5.C++中栈空间和堆空间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include "Student.h"
int main() { std::cout << 1 << std::endl;
Student student1; student1.setAge(99); student1.setName("李连杰"); cout << "name:" << student1.getName() << " ,age:" << student1.getAge() << endl; Student * student2 = new Student(); student2->setAge(88); student2->setName("李元霸"); cout << "name:" << student2->getName() << " ,age:" << student2->getAge() << endl;
if (student2) delete student2; student2 = NULL;
return 0; }
|
6.命名空间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| namespace jack1 { int age = 33; char * name = "jack猛男1";
void show() { cout << "name:" << name << ", age:" << age << endl; }
void action() { cout << "jack1 action" << endl; } }
namespace jack2 { void action() { cout << "jack2 action" << endl; } }
namespace jack3 { namespace jack3Inner { namespace jack3Inner1 { namespace jack3Inner2 { namespace jack3Inner3 { void out() { cout << "爱恨情仇人消瘦,悲欢起落人寂寞" << endl; } } } } } }
int main() { cout << "命名空间" << endl;
using namespace jack1;
int ageValue = jack1::age; jack1::show();
ageValue = age; show();
using namespace jack2; jack1::action(); jack2::action();
using namespace jack3::jack3Inner::jack3Inner1::jack3Inner2::jack3Inner3; out();
jack3::jack3Inner::jack3Inner1::jack3Inner2::jack3Inner3::out();
return 0; }
|
7.构造与析构函数
// new/delete 是一套 会调用构造函数 与 析构函数 【C++标准规范】
// malloc/free是一套 不调用构造函数 与 析构函数 【C的范畴,虽然不推荐,但是也是可以的】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <iostream> #include <string.h> using namespace std;
class Student {
public: Student() { cout << "空参数构造函数" << endl; }
Student(char *name) :Student(name, 87) { cout << "一个参数的构造函数" << endl; this->name = name; }
Student(char *name, int age) {
this->name = (char *) (malloc(sizeof(char *) * 10)); strcpy(this->name, name);
this->age = age; cout << "两个参数的构造函数" << endl; }
~Student() { cout << "析构函数" << endl;
if (this->name) { free(this->name); this->name = NULL; } }
private: char *name; int age;
public: int getAge() { return this->age; }
char *getName() { return this->name; }
void setAge(int age) { this->age = age; }
void setName(char *name) { this->name = name; } };
int main() {
Student *stu = new Student("杜子腾", 26); cout << "name:" << stu->getName() << ", age:" << stu->getAge() << endl; delete stu;
return 0; }
|
8.拷贝构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #include <iostream> #include <string.h>
using namespace std;
class Student {
public: Student() { cout << "空参数构造函数" << endl; }
Student(char *name, int age) : name(name), age(age) { cout << "两个参数构造函数" << endl; }
~Student() { cout << "析构函数" << endl; }
Student(const Student & student) { cout << "拷贝构造函数" << endl;
this->name = student.name; this->age = student.age - 10;
cout << "自定义拷贝构造函数 内存地址 " << &student << endl; }
private: char *name; int age;
public: int getAge() { return this->age; }
char *getName() { return this->name; }
void setAge(int age) { this->age = age; }
void setName(char *name) { this->name = name; } };
struct Person { int age; char *name; };
int main() { Student *student1 = new Student("杜子腾", 39);
Student *student2 = student1;
student2->setAge(99);
cout << student1->getName() << student1->getAge() << endl;
return 0; }
|
9.指针常量、常量指针和常量指针常量
命名方面规律:命名顺着读,const靠近谁,谁就不能修改。
- const靠近*叫指针常量,int * const p;允许修改值,不允许修改地址;
- const靠近int叫常量指针,const int * p; 允许修改地址,不允许修改值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> #include <string.h> #include <string.h>
using namespace std;
int main() {
int number = 9; int number2 = 8;
const int * numberP1 = &number;
int* const numberP2 = &number; *numberP2 = 100;
const int * const numberP3 = &number;
return 0; }
|
版权声明: 此文章版权归Jack Ou所有,如有转载,请註明来自原作者