📄 新建 文本文档.txt
字号:
//cout << *&furong << endl;//Person*
cout << *&furong.name << endl;//string*
cout << "sizeof(furong)=" << sizeof(furong) << endl;
cout << "sizeof(furong.name)=" << sizeof(furong.name) << endl;
cout << "sizeof(furong.gender)=" << sizeof(bool) << endl;
cout << "sizeof(furong.age)=" << sizeof(furong.age) << endl;
cout << "sizeof(furong.income)=" << sizeof(double) << endl;
cout << "&furong.name=" << &furong.name << endl;
cout << "&furong.gender=" << &furong.gender << endl;
cout << "&furong.age=" << &furong.age << endl;
cout << "&furong.income=" << &furong.income << endl;
}
bash-2.05$ cat charptr.cc
#include <iostream>
using namespace std;
int main()
{
char a[10]={"good"};
char* p = a;
const char* q = "afternoon";
p[2] = 'O';
//q[2] = 'X';//ERROR!!
char* r = "everyone";//danger!!!!
//r[2] = '*';//runtime error!!!segementation fault!!!
cout << a << endl;
cout << q << endl;
cout << r << endl;
//char* x;//野指针!!!
//cin >> x;//试图用指针本身来保存字符串,不允许!!!!
}
bash-2.05$ cat ptr2ptr.cc
#include <iostream>
using namespace std;
int main()
{
double d=123.45;
double* p = &d;
double** q = &p;
cout << *p << endl;
cout << **q << endl;
}
bash-2.05$ cat struct.cc
#include <iostream>
using namespace std;
#include <string>
struct Person{
string name;
bool gender;
int age;
double income;
};
void show(Person obj);
Person input();
const char g[2][10]={"美女","帅哥"};
int main()
{
Person cyh;
cyh.name = "程一航";
cyh.gender = true;
cyh.age = 21;
cyh.income = 800;
Person ljq = {"刘家奇", true, 22, 600};
Person gy;
gy = cyh;
gy.name = "郭英";
gy.gender = false;
gy.age = 18;
cout << cyh.name << ':' << g[cyh.gender] << ",今年" << cyh.age << ",未
婚,月收入" << cyh.income << "元。" << endl;
//cout << cyh << endl;
show(ljq);
show(gy);
Person quange;
quange = input();
show(quange);
}
void show(Person obj)
{
cout << obj.name << ':' << g[obj.gender] << ",今年" << obj.age << ",未
婚,月收入" << obj.income << "元。" << endl;
}
Person input()
{
cout << "请输入姓名、性别、年龄和月收入: " << endl;
Person p;
cin >> p.name >> p.gender >> p.age >> p.income;
return p;
}
bash-2.05$ cat array.cc
#include <iostream>
using namespace std;
int main()
{
int a[5]={10,20,30,40,50};
char b[8]={"abcdefg"};
int* pa = a;
char* pb = b;
for(int i=0; i<5; i++)
cout << pa[i] << ' ';
cout << endl;
for(int i=0; i<8; i++)
cout << pb[i] << ' ';
cout << endl;
cout << "pb=" << pb << endl;
for(int i=0; i<5; i++)
cout << *(pa+i) << ',';
cout << endl;
for(int i=0; i<5; i++)
cout << *(pa++) << ' ';
cout << endl;
pa -= 5;
cout << "pa=" << pa << endl;
pa = a;
cout << "pa=" << pa << endl;
for(int i=0; i<5; i++)
pa[i] *= 8;
for(int i=0; i<5; i++)
cout << a[i] << ' ';
cout << endl;
}
bash-2.05$ cat enum.cc
#include <iostream>
using namespace std;
enum Color{BLACK, YELLOW, GREEN, BLUE, RED, WHITE, PINK};
enum Cloth{COAT=5, SHIRT, TROUSERS=11, SKIRT, CAP, SOCKES, SHOES};
enum Flags{NONE=0, EXECUTE=1, WRITE=2, READ=4, RDWR = WRITE|READ, RDEX=READ|EXEC
UTE, WREX=WRITE|EXECUTE};
int main()
{
Flags perm=RDWR;
//Flags p = (Flags)6;
Cloth cth = CAP;
//Cloth ct = (Cloth)13;
Color clr = GREEN;
//Color cl = (Color)2;
//cl = SKIRT;//ERROR!!
}
bash-2.05$ cat ptrarr.cc
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
int* x[3]={&a, &b, &c};
for(int i=0; i<3; i++)
*x[i] = 10+i;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
}
bash-2.05$ cat structarr.cc
#include <iostream>
using namespace std;
#include <string>
struct Person{
string name;
bool gender;
int age;
double income;
};
void show(Person obj);
int main()
{
Person nmgd[3]={
{"程一航", true, 21, 800},
{},
{"郭英", false, 18, 800}
};
nmgd[1].name = "刘家奇";
nmgd[1].gender = true;
nmgd[1].age = 22;
nmgd[1].income = 700;
for(int i=0; i<3; i++)
show(nmgd[i]);
}
void show(Person obj)
{
const char g[2][10]={"美女","帅哥"};
cout << obj.name << ':' << g[obj.gender] << ",今年" << obj.age << ",未
婚,月收入" << obj.income << "元。" << endl;
}
bash-2.05$ cat arraypara.cc
#include <iostream>
using namespace std;
//void func(double a[], int n)
void func(double* a, int n)
{
cout << "sizeof(a)=" << sizeof(a) << endl;
//a = NULL;
for(int i=0; i<n; i++)
cout << a[i] << ' ';
cout << endl;
}
int main()
{
double b[3] = {1.1,2.2,3.3};
cout << "sizeof(b)=" << sizeof(b) << endl;
func(b, 3);
//b = NULL;//ERROR!!
}
bash-2.05$ cat input.cc
#include <iostream>
using namespace std;
int input()
{
cout << "input an integer: ";
int n;
cin >> n;
return n;
}
void input(int* p)
{
cout << "input an integer: ";
cin >> *p;
}
void input(int& n)
{
cout << "input an integer: ";
cin >> n;
}
int main()
{
int m;
m = input();
cout << "m=" << m << endl;
input(&m);
cout << "m=" << m << endl;
input(m);
cout << "m=" << m << endl;
}
bash-2.05$ cat ptrpara.cc
#include <iostream>
using namespace std;
void func(double* x, double* y);
void func(double m, double n)
{
double t=m;
m = n;
n = t;
}
int main()
{
double a=1.1, b=2.2;
func(a, b);
cout << "a=" << a << ", b=" << b << endl;
double* p=NULL;
double* q=NULL;
p = &a;
q = &b;
double t=*p;
*p = *q;
*q = t;
cout << "a=" << a << ", b=" << b << endl;
}
bash-2.05$ cat structptr.cc
#include <iostream>
using namespace std;
struct Date{
int year;
int month;
int day;
};
int main()
{
Date d1={2008,8,14}, d2={2005,10,1};
Date* p=NULL;
p = &d1;// p point to d1
cout << (*p).year << '-' << (*p).month << '-' << (*p).day << endl;
cout << p->year << '-' << p->month << '-' << p->day << endl;
p = &d2;
cout << p->year << '-' << p->month << '-' << p->day << endl;
}
cpp07
bash-2.05$ cat constructor.cc
#include <iostream>
using namespace std;
class Date{
//private:
int year;
int month;
int day;
public:
Date(){
cout << "无参构造函数" << endl;
year = month = day = 1;
}
Date(int y, int m, int d){
cout << "三参构造函数" << endl;
year = y;
month = m;
day = d;
}
void set(){
year = 2008;
month = 8;
day = 15;
}
void print(){
cout << year << "年" << month << "月" << day << "日" << endl;
}
bool leap(){
if(year%4==0&&year%100!=0||year%400==0)
return true;
return false;
}
void set(int y, int m, int d){
year = y;
month = m;
day = d;
}
};
int main()
{
Date d;//={2008,8,15};
Date o(2020, 9, 9);
Date func();//function declaration
}
bash-2.05$ cat funcptr3.cc
#include <iostream>
using namespace std;
struct Person{
char name[20];
bool gender;
int age;
double income;
};
bool isFemale(Person p)
{
if(p.gender==false)
return true;
else
return false;
}
bool age20(Person p)
{
if(p.age>=20)
return true;
else
return false;
}
bool lowIncome(Person p)
{
if(p.income<1000)
return true;
else
return false;
}
void select(Person s[], int n, bool(*f)(Person))
{
for(int i=0; i<n; i++)
if(f(s[i]))
cout << s[i].name << endl;
}
int main()
{
Person s[3]={
{"程一航", true, 21, 800},
{"李燕成", true, 20, 1000},
{"郭英", false, 18, 900}
};
cout << "选美女!" << endl;
select(s, 3, isFemale);
cout << "选年龄够20的人!" << endl;
select(s, 3, age20);
cout << "选收入低于1000的人!" << endl;
select(s, 3, lowIncome);
}
bash-2.05$ cat reference.cc
#include <iostream>
using namespace std;
void show(const int& n)
{
cout << "&n=" << &n << ", n=" << n << endl;
}
int main()
{
int m=10;
cout << "&m=" << &m << ", m=" << m << endl;
show(m);
show(123);
show(m*5);
}
bash-2.05$ cat voidptr.cc
#include <iostream>
using namespace std;
void show(void* addr, int bytes)
{
unsigned char* p = (unsigned char*)addr;
for(int i=0; i<bytes; i++)
cout << hex << (int)*(p+i) << ' ';
cout << endl;
}
struct Person{
char name[8];
int age;
};
int main()
{
int a=1234567890;
bool b=true;
char c='2';
double d=1.2;
Person fr={"furong", 38};
show(&a, sizeof(a));
show(&b, sizeof(b));
show(&c, sizeof(c));
show(&d, sizeof(d));
show(&fr, sizeof(fr));
}
bash-2.05$ cat array.cc
#include <iostream>
using namespace std;
int main()
{
unsigned int n;
cout << "input the number of element: ";
cin >> hex >> n;
double* p = new double[n];
cout << "p=" << p << endl;
for(unsigned int i=0; i<n; i++)
cout << p[i] << ' ';
cout << endl;
cout << "p=" << p << endl;
for(unsigned int i=0; i<n; i++)
cout << *p++ << ' ';
cout << endl;
cout << "p=" << p << endl;
p = p - n;
cout << "p=" << p << endl;
delete[] p; p=NULL;
}
bash-2.05$ cat funcptr.cc
#include <iostream>
using namespace std;
struct Person{
char name[20];
bool gender;
int age;
double income;
};
bool isFemale(Person p)
{
if(p.gender==false)
return true;
else
return false;
}
bool age20(Person p)
{
if(p.age>=20)
return true;
else
return false;
}
bool lowIncome(Person p)
{
if(p.income<1000)
return true;
else
return false;
}
void select(Person s[], int n, bool(*f)(Person))
{
for(int i=0; i<n; i++)
if((*f)(s[i]))
cout << s[i].name << endl;
}
int main()
{
Person s[3]={
{"程一航", true, 21, 800},
{"李燕成", true, 20, 1000},
{"郭英", false, 18, 900}
};
cout << "选美女!" << endl;
select(s, 3, &isFemale);
for(int i=0; i<3; i++)
if(isFemale(s[i]))
cout << s[i].name << endl;
cout << "选年龄够20的人!" << endl;
select(s, 3, &age20);
for(int i=0; i<3; i++)
if(age20(s[i]))
cout << s[i].name << endl;
cout << "选收入低于1000的人!" << endl;
select(s, 3, &lowIncome);
for(int i=0; i<3; i++)
if(lowIncome(s[i]))
cout << s[i].name << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -