#include
int main(){
int a = 100;
int &b = a;
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("add: a = %p\n", &a);
printf("add: b = %p\n", &b);
/*
a = 100
b = 100
add: a = 0x7ffd3baf558c
add: b = 0x7ffd3baf558c
*/
}
#include
/*
void swp(int a, int b) {
a ^= b;
b ^= a;
a ^= b;
}
/*
a = 100;
b = 10
*/
/*
void swp(int *p, int *q) {
*p ^= *q;
*q ^= *p;
*p ^= *q;
}
*/
void swp(int &a, int &b) {
a ^= b;
b ^= a;
a ^= b;
}
int main() {
int a = 100;
int b = 10;
swp(a, b);
printf("a = %d\n", a);
printf("b = %d\n", b);
}
/*
a = 10
b = 100
*/
#include
void debug(const char *ptr = "------------") {
printf("%s\n", ptr);
}
int main() {
debug();
debug();
debug();
debug("hello");
debug("boy");
}
/*
------------
------------
------------
hello
boy
*/
#include
#include
int cmp(int a, int b) {
return a-b;
}
int cmp(const char *str1, const char *str2) {
return strcmp(str1, str2);
}
int main() {
printf("%d\n", cmp(1, 2));
printf("%d\n", cmp("bbb", "aaa"));
}
/*
-1
1
*/
#include
#include
int main() {
int *intp = new int;
*intp = 100;
printf("*intp = %d\n", *intp);
delete intp;
char *p = new char[10];
strcpy(p, "hello");
printf("p: %s\n", p);
delete [] p;
}
/*
*intp = 100
p: hello
*/
二、引入类与对象
#include
int main() {
int arr[100] = {0};
int tail = 0;
int n = 10;
while (n--) {
arr[tail++] = n;
}
int i = 0;
for (i = 0; i < tail; i++) {
printf("%d ", arr[i]);
}
printf("\n");
n = 5;
while (n--) {
arr[tail++] = n;
}
for (i = 0; i < tail; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
/*
9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0 4 3 2 1 0
*/
// array.h
#ifndef _ARR_
#define _ARR_
#define N 100
typedef struct arr{
int data[N];
int tail;
}ARR;
void init(ARR *arr);
void addtail(ARR *arr, int value);
void show(ARR *arr);
#endif
//array.c
#include "array.h"
#include
void init(ARR *arr) {
arr->tail = 0;
}
void addtail(ARR *arr, int value) {
arr->data[arr->tail++] = value;
}
void show(ARR *arr) {
int i = 0;
for (i = 0; i < arr->tail; i++) {
printf("%d ", arr->data[i]);
}
}
//main.c
#include
#include "array.h"
int main() {
ARR arr;
init(&arr);
int n = 10;
while (n--) {
addtail(&arr, n);
}
show(&arr);
printf("\n");
return 0;
}
/*
9 8 7 6 5 4 3 2 1 0
*/
#ifndef _ARR_
#define _ARR_
#define N 100
typedef struct arr{
int data[N];
int tail;
void (*show)(struct arr *arr);
void (*addtail)(struct arr *arr, int value);
}ARR;
void init(struct arr *arr);
#endif
#include "array.h"
#include
static void addtail(ARR *arr, int value) {
arr->data[arr->tail++] = value;
}
static void show(ARR *arr) {
int i = 0;
for (i = 0; i < arr->tail; i++) {
printf("%d ", arr->data[i]);
}
}
void init(ARR *arr) {
arr->tail = 0;
arr->addtail = addtail;
arr->show = show;
}
#include
#include "array.h"
int main() {
ARR arr;
init(&arr);
int n = 10;
while (n--) {
arr.addtail(&arr, n);
}
arr.show(&arr);
printf("\n");
return 0;
}
/*
9 8 7 6 5 4 3 2 1 0
*/
#include
class A{
public:
//无参构造函数
A() {
printf("construct....\n");
a = 0;
}
//有参构造参数
A(int data) {
a = data;
}
//析构函数
~A() {
printf("done!!!!\n");
}
void show() {
printf("xxxxxxx\n");
}
void setdata(int data) {
a = data;
}
int getdata(void);
private:
int a;
};
int A::getdata(void) {
return a;
}
int main() {
A x;
A y(1000);
printf("x.a: %d\n", x.getdata());
printf("y.a: %d\n", y.getdata());
x.show();
return 0;
}
/*
construct....
x.a: 0
y.a: 1000
xxxxxxx
done!!!!
done!!!!
*/
// array.h
#ifndef _ARR_
#define _ARR_
class ARR{
public:
ARR():tail(0) {
}
void addtail(int value);
void show(void);
private:
int data[100];
int tail;
};
#endif
// array.cpp
#include "array.h"
#include
void ARR::addtail(int data) {
this->data[tail++] = data;
}
void ARR::show() {
int i = 0;
for (i = 0; i < tail; i++) {
printf("%d ",data[i]);
}
printf("\n");
}
// main.cpp
#include
#include "array.h"
int main() {
ARR arr;
arr.addtail(12);
arr.addtail(14);
arr.addtail(17);
arr.show();
return 0;
}
#include
class A{
public:
A(){
printf("A()\n");
p = new char[10];
}
~A() {
printf("~A()\n");
delete [] p;
}
private:
char *p;
};
int main() {
A x;
A y = x;
return 0;
}
/*
A()
~A()
~A()
free(): double free detected in tcache 2
已放弃 (核心已转储)
*/
#include
#include
class A{
public:
A(){
printf("A()\n");
p = new char[10];
strcpy(p, "hello");
printf("P: %s\n", this->p);
}
A(const A &x) {
printf("A(const A &x)\n");
p = new char[10];
strcpy(p, x.p);
printf("P: %s\n", this->p);
}
~A() {
printf("~A()\n");
delete [] p;
}
private:
char *p;
};
int main() {
A x;
A y = x;
return 0;
}
/*
A()
P: hello
A(const A &x)
P: hello
~A()
~A()
*/
//
#include
class A{
public:
A(int a = 50, int data = 1000):b(data) { //b只能在这声明,不能在对象中声明。
this->a = a; // this指针的使用,区分类的数据与参数。
printf("AAAAAAAA\n");
}
~A() {
printf("----------\n");
}
void show() {
printf("b = %d\n", b);
printf("a = %d\n", a);
}
private:
int a;
const int b;
};
int main() {
A x(10); // default parameter of a
x.show();
A y(100);
y.show();
A z; // a:50 b:1000
z.show(); // 默认参数的用法
return 0;
}
/*
AAAAAAAA
b = 1000
a = 10
AAAAAAAA
b = 1000
a = 100
AAAAAAAA
b = 1000
a = 50
----------
----------
----------
*/
#include
class A{
public:
static void func(void) {
printf("xxxxxxx\n");
}
static int data;
};
int A::data = 10; //要单独在类外面声明以下这个数据,否则无法使用,与static函数的区别。
int main() {
A a;
a.func();
A::func();
A x;
x.data = 100;
printf("x.data = %d\n", x.data);
A::data = 1000;
printf("x.data = %d\n", x.data);
return 0;
}
/*
xxxxxxx
xxxxxxx
x.data = 100
x.data = 1000
*/
// array.h
#ifndef _ARR_
#define _ARR_
class ARR{
public:
ARR():tail(0) {
}
void addtail(int value);
void show(void);
friend void rev(ARR &arr);
private:
int data[100];
int tail;
};
#endif
// array.cpp
#include "array.h"
#include
void ARR::addtail(int data) {
this->data[tail++] = data;
}
void ARR::show() {
int i = 0;
for (i = 0; i < tail; i++) {
printf("%d ",data[i]);
}
printf("\n");
}
// main.cpp
#include
#include "array.h"
void rev(ARR &arr) {
int i = 0;
for (;i < arr.tail/2; i++) {
int tem = arr.data[i];
arr.data[i] = arr.data[arr.tail - i - 1];
arr.data[arr.tail - i -1] = tem;
}
}
int main() {
ARR arr;
arr.addtail(12);
arr.addtail(14);
arr.addtail(17);
arr.show();
rev(arr);
arr.show();
return 0;
}
/*
12 14 17
17 14 12
*/
#include
class A; // x需要在使用前声明一下。
class B{
public:
void printfA(A &x);
};
class A{
public:
A() {
x = 100;
}
friend class B;
private:
int x;
};
void B::printfA(A &x) {
printf("%d\n", x.x);
}
int main() {
A a;
B b;
b.printfA(a);
return 0;
}
// 100
#include
class A;
class B{
public:
void printfA(A &x);
};
class A{
public:
A() {
x = 100;
}
friend void B::printfA(A &x); //
private:
int x;
};
void B::printfA(A &x) {
printf("%d\n", x.x);
}
int main() {
A a;
B b;
b.printfA(a);
return 0;
}
// 100