//定义和初始化分开
char c1;
c1 = 'k';
cout << c1<
//字符数组定义和初始化必须在一起(一行),否则会报错;
char c3[10]="hello";
cout << c3 << endl;
char c4[10];
// c4[10] = "hello"; //错误,不能将 "const char *" 类型的值分配到 "char *" 类型的实体
//char c4[5] = "hello"; //错误,字符串的长度超过字符变量的长度
char c5[] = "good morning sir!"; //不指定字符变量长度
cout << c5<
char* c5;
//c5 = "hello, keson!"; //错误,不能将 "const char *" 类型的值分配到 "char *" 类型的实体
//char* c6 = "hello, keson!";//错误,不能将 "const char *" 类型的值分配到 "char *" 类型的实体
const char* c7;
c7 = "hello, keson!";
cout << "c7: " << c7 << endl;
const char* c8 = "hello, keson!";
cout << "c8: " << c8 << endl;
cout << *(c8 + 1); //访问第二个字符
string s1 = "hello,keson";
char ch[20];
//char ch[]; //错误,要声明数组长度
strcpy_s(ch, s1.c_str());
cout << ch << endl;
string s2 = "hello,kai";
const char * c1 = s2.c_str();
const char* c2 = s2.data();
/* 必须是const char *,否则会报错
char* c1 = s2.c_str();
char* c2 = s2.data();
*/
cout << c1 << endl;
cout << c2<
string s1;
const char* c = "c: hello,keson";
s1 = c;
cout << s1 << endl;
const char c2[20] = "c2: hello,kai";
const char c3[] = "c3: hello,kai";
cout << c2 << endl;
cout << c3 << endl;
char c4[20] = "c4: hello,kai";
char c5[] = "c5: hello,kai";
cout << c4 << endl;
cout << c5 << endl;
//下面的代码是声明结构体,不是定义
struct student {
string name;
int age;
int score;
};
student stu[2] = {
{"keson",28},
{"keson2",17}
};
cout << "初始化结构体数组: " << endl;
cout << stu[0].name << endl;
cout << stu[0].age << endl;
cout << stu[1].name << endl;
cout << stu[1].age << endl<
student stu[2] ;
stu[2] = {
{"keson",28},
{"keson2",17}
};
student stu2{ "keson",28 };
student* p = &stu2;
cout <<"结构体变量: " << p->age << endl;
Student stu[2];
student* p_arry = stu;
cout<< "结构体数组1: " << (p_arry)->age<age << endl;
union person
{
const char * name;
/*const char * name;*/
int age;
};
union person p 错误,这里指针变量不能是p,因为上面已经用定义过p了
union person person1;
person1.name = "张三丰";
cout << "person1.name: " << person1.name<
int a[5] = { 0,1,2,3,4 };
int end = sizeof(a) / sizeof(a[0])-1;
int arr_len = end;
for (int i = 0; i < arr_len; i++) {
cout << a[i] << endl;
}
int temp,start=0;
while (int(end/2)) {
temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
for (int i = 0; i < arr_len; i++) {
cout << a[i]<
enum week //声明枚举变量
{
mon, tue, wen, thur, fri, sta, sun
};
for (int i = 0; i < 7; i++) {
cout << week(i);
} //打印结果:0123456
week week1; //定义枚举变量
week1 = week(0); //初始化枚举变量
switch (week(0))
{
case mon:
cout << "星期一";
break;
case tue:
cout << "星期二";
break;
case wen:
break;
case thur:
break;
case fri:
break;
case sta:
break;
case sun:
break;
default:
break;
}
"head.h"文件代码
using namespace std;
#include "iostream"
#include "string"
int int_max(int a, int b);
int max(int a, int b);
void print_fun(int (*pf)(int, int));
void revese(int a[],int size);
void display(const string s[], int n);
结构体
struct student {
string name;
int age;
int score;
};
void fun_student(student stu1, student stu2); //结构声明应该在函数声明之前,不然会报错
//main.cpp 程序
#include “head.h”
int main() {
本质是一个指针变量,指向函数
int (*pf)(int, int);
pf = max;
print_fun(pf); //函数指针作为形参
结构体指针
student stu1 = { "zhangsan",10,80 };
student stu2 = { "Lisi",10,90 };
fun_student(stu1, stu2);
3 int arry[] = { 1,2,3,4,5,6 };
revese(arry, (int)sizeof(arry) / sizeof(arry[0]));
const int num = 3;
string s[num] = { "hello","keson" ,"goodJob" };
display(s, num);
}
传递函数指针
void print_fun(int (*pf)(int, int)) {
cout << "函数指针作为形参: ";
cout << "两数的大值是: " << pf(4, 9) << endl;
}
int max(int a, int b) {
return a > b ? a : b;
}
//传递数组/函数重载/数组反转
void revese(int a[], int lenth) {
cout << "数组作为形参,并实现数组元素反转: ";
int start = 0, end = lenth - 1, temp;
while (start < end) {
temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
for (int i = 0; i < lenth; i++) {
cout << a[i] << " ";
}
cout << endl;
}
//结构作为函数参数
void fun_student(student stu1, student stu2) {
cout << "结构体作为形参: ";
student stu;
stu.name = "wangwu";
stu.age = stu1.age + stu2.age;
stu.score = stu1.score + stu2.score;
cout << stu.name << " " << stu.age << " " << stu.score << endl;
}
//字符串数组
void display(const string s[], int n) {
cout << "字符串数组作为形参: " ;
for (int i = 0; i < n; i++) {
cout << s[i] << " ";
}
}
本质就是函数,返回的结果是指针
int* pf_sum(int a, int b) {
int* p;
static int sum = a + b; //必须在前面添加一个static,因为局部变量是在栈内创建,调用结束后,就释放;
p = ∑
return p;
}
const int a = 1, b = 2,c=3; //const 修饰的是int
// a =9 //错误 , a 的值被const修饰,不允许被改变了
const int arr[] = {1,2,3};
// arr[0] = 9; //错误 , arr的值被const修饰,不允许被改变了
//做定值,右定向(const在*左边,指向的值不能改变;const在*右边,指向的变量不能改变)
const int* p;
p = &a;
cout << *p << endl;
p = &b;
cout << *p< const int* p
cout << *p1 << endl;
//*p1 = 20; /错误,const在*的左边,不能改变值;
//int * const p2 = c; //错误,"int" 类型的值不能用于初始化 "int *const" 类型的实体,
//报错的原因是c已经声明为const int,而const int 值是不能改变的.但是int * const只是
//不允许指向改变,但是允许指向的值改变,两者存在冲突;
int d = 6,f=7;
int* const p3 = &d;
cout << "初始值 d= " << *p3<
using namespace std;
#include "iostream"
#include "string"
template
void Swap(T x, T y) {
T temp;
temp = x;
x = y;
y = temp;
}
int main() {
//无论x,y的值是整型还是浮点型,还是字符型通用
//int x = 1, y = 99;
//float x = 1, y = 99;
char x='a',y='b';
cout << "原始值: " << x << " " << y << endl;
Swap(&x, &y);
cout << "交换后: " << x << " " << y << endl;
}
//#include “head.h”
using namespace std;
#include "iostream"
#include "string"
class annimal {
public:
void sit();
void sound();
annimal(); //无参构造
annimal(string name, int age); //有参构造
~annimal();
private:
string name;
int age;
};
annimal::annimal() {
name = "狗";
age = 1;
cout << "无参构造----->name: " << name << " age: " << age << endl;
}
annimal::annimal(string name, int age) {
this->name = name;
this->age = age;
cout << "有参构造----->name: " << name << " age: " << age << endl;
}
annimal::~annimal() {
cout << "调用析构函数" << endl;
}
void annimal::sit() {
cout << "小狗,坐下" << endl;
}
void annimal::sound() {
cout << "小狗,旺旺叫" << endl;
}
class dog : public annimal {
public:
dog();
dog(string color);
private:
string color;
};
dog::dog() {
this->color ="black";
cout << "颜色:" << this->color<color = color;
cout << "颜色:" << this->color << endl;
}
int main() {
annimal a("大黄", 5);
dog d("红");
}
#pragma once
#include "iostream"
#include "string"
using namespace std;
#define max_size 20
//声明联系人结构体
struct Person
{
string name;
int age;
string phone_num;
};
//声明通讯录结构体
struct address_book
{
//通讯录联系人数组名称
struct Person person[max_size];
//通讯录当前记录的人员个数
int person_num;
};
//函数声明
void show();
void addperson(address_book* book);
void showperson(address_book* book);
int is_exist(address_book* book, string name);
void delete_person(address_book* book);
void query_person(address_book* book);
void change_person(address_book* book);
void clear_people(address_book* book);
void clear_people(address_book* book);
#include"head.h"
//菜单界面
void show() {
cout << "************************" << endl;
cout << "***** 1.添加联系人 *****" << endl;
cout << "***** 2.显示联系人 *****" << endl;
cout << "***** 3.删除联系人 *****" << endl;
cout << "***** 4.查找联系人 *****" << endl;
cout << "***** 5.修改联系人 *****" << endl;
cout << "***** 6.清空联系人 *****" << endl;
cout << "***** 0.退出联系人 *****" << endl;
cout << "************************" << endl;
}
//添加联系人
void addperson(address_book* book) {
system("cls");
book->person_num++;
if (book->person_num > max_size) {
cout << "通讯录已经满了,无法添加";
}
else {
string name;
cout << "请输入姓名:" << endl;
cin >> name;
book->person[book->person_num].name = name;
int age;
cout << "请输入年龄:" << endl;
cin >> age;
book->person[book->person_num].age = age;
string phone_num;
cout << "请输入电话:" << endl;
cin >> phone_num;
book->person[book->person_num].phone_num = phone_num;
cout << "已经添加联系人" << endl;
}
system("pause");
}
//显示联系人
void showperson(address_book* book) {
system("cls");
cout << "查询到的通讯录列表为: " << endl;
for (int i = 0; i <= book->person_num; i++) {
cout << "姓名:" << book->person[i].name << "\t" << "年龄:" << book->person[i].age
<< "\t" << "电话号码:" << book->person[i].phone_num << endl;
}
cout << endl;
system("pause");
}
//检测联系人是否存在,存在返回索引,否则返回-1
int is_exist(address_book* book, string name) {
for (int i = 0; i <= book->person_num; i++)
{
if (book->person[i].name == name) {
return i;
}
}
return -1;
}
//删除联系人
void delete_person(address_book* book) {
system("cls");
string delete_name;
cout << "请输入要删除的名字: " << endl;
cin >> delete_name;
int int_exist = is_exist(book, delete_name);
if (int_exist != -1) {
for (int i = int_exist; i < book->person_num; i++)
{
book->person[i] = book->person[i + 1];
}
book->person_num--;
cout << "删除成功" << endl;
system("pause");
}
else
{
cout << "查无此人" << endl;
system("pause");
}
}
//查找联系人
void query_person(address_book* book) {
system("cls");
string query_name;
cout << "请输入你要查找的联系人: " << endl;
cin >> query_name;
int int_exist = is_exist(book, query_name);
if (int_exist != -1)
{
cout << "查询到联系人:" << endl;
cout << book->person[int_exist].name << "\t";
cout << book->person[int_exist].age << "\t";
cout << book->person[int_exist].phone_num << "\t" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
}
void change_person(address_book* book) {
system("cls");
string change_name;
cout << "请输入你要修改的联系人: ";
cin >> change_name;
int int_exist = is_exist(book, change_name);
if (int_exist != -1)
{
cout << "查找到该人信息:";
cout << book->person[int_exist].name << "\t";
cout << book->person[int_exist].age << "\t";
cout << book->person[int_exist].phone_num << "\t" << endl;
cout << "您将要修改" << book->person[int_exist].name << "的信息!!!" << endl;
cout << "请输入修改后姓名:" << endl;
string name;
cin >> name;
book->person[int_exist].name = name;
cout << "请输入修改后年龄:" << endl;
int age;
cin >> age;
book->person[int_exist].age = age;
cout << "请输入修改后电话:" << endl;
string phone_num;
cin >> phone_num;
book->person[int_exist].phone_num = phone_num;
cout << "修改后的信息: " << endl;
cout << book->person[int_exist].name << "\t";
cout << book->person[int_exist].age << "\t";
cout << book->person[int_exist].phone_num << "\t" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
}
//清除联系人
void clear_people(address_book* book) {
system("cls");
cout << "你确认要清除所有联系人吗?"<> input_num;
if (input_num == 1) {
memset(book, 0, sizeof(book));
}
else if (input_num == 2)
{
}
else {
}
}
#include "head.h"
//显示通讯录
int main() {
address_book book;
book.person_num = 2;
int select_id = 0;
book.person[0].name = "张飞";
book.person[0].age = 25;
book.person[0].phone_num = "666";
book.person[1] = { "赵云",56,"119" };
book.person[2] = { "马超",40,"250" };
while (true)
{
show();
cout << "请选择你要选择的功能: ";
cin >> select_id;
switch (select_id)
{
case 1:
addperson(&book);
break;
case 2:
showperson(&book);
break;
case 3:
{
delete_person(&book);
break;
}
case 4:
query_person(&book);
break;
case 5:
change_person(&book);
break;
case 6:
clear_people(&book);
break;
case 0:
cout << "将要退出系统,欢迎下次使用" << endl;
system("pause");
break;
default:
cout << "输入数字不正确 ! 请输入数字0-6" << endl;
system("pause");
break;
}
if (select_id == 0) {
break;
}
}
}
int* p = new int(23);//开辟一块内存,返回指针;
cout << p << endl;
cout << *p<
int a = 10;
int& b = a; //引用必须初始化;引用一旦初始化后,就不可以更改了
b = 20;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
template
void Swap1(T x, T y) {
T temp;
temp = x;
x = y;
y = temp;
}
int main() {
int a = 1, b = 2;
Swap1(a, b); //值传递
cout << "a = " << a << " b = " << b << endl;
}
void Swap2(T *x, T *y) {
T temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
float x = 1.1, y = 2.2;
Swap2(&x, &y); //值传递
cout << "x = " << x << " y = " << y << endl;
}
void Swap1(T &x, T &y) {
T temp;
temp = x;
x = y;
y = temp;
}
int main() {
int a = 1, b = 2;
Swap1(a, b); //值传递
cout << "a = " << a << " b = " << b << endl;
}
//1.不要返回局部变量
//2.函数调用可以为左值
T &Swap1(T &x, T &y) {
return x > y ? x : y;
}
int main() {
int a = 1, b = 9;
int &ref =Swap1(a, b); //值传递
cout << "ref: " <
int a = 99;
int& b = a;
int* const p1 = &a;
int* const p2= &b;
cout << "a = "<
void show_value(const int &v) {
//v = 30; //错误.不能修改该值;
cout << "v = " << v << endl;
int main() {
//int& a = 10; //错误
const int& a = 10; //创建一个临时空间来提供引用
cout << "a = " <
class Student {
public:
//1.所有对象共享一份数据
//2.编译阶段就分配内存
//3.类内声明,类外初始化操作
static int age;
private:
int score;
};
int Student::age = 100;
int Student::age = 99;
int main() {
Student stu,stu1;
stu.age = 200;
cout << stu.age << endl;
cout << stu1.age << endl; //值变成200,并不是100;
cout << Student::age << endl; //可以通过类名直接访问;
// stu.score = 33; //私有变量不能访问
}
class Student {
public:
//1.所有对象共享一份数据
//2.编译阶段就分配内存
//3.静态成员函数只能调用静态成员变量
//4.静态成员函数也有访问权限
static void fun() {
cout << "调用静态成员函数"<
class Student {
};
class Student1 {
public:
int a;
void fun();
};
class Student2 {
static int a;
};
void Student1::fun() {
}
int main() {
//1.空对象
Student stu;
cout << sizeof(stu) << endl;; //空对象占用1个字节;
//2.非静态成员变量
Student1 stu1;
cout << sizeof(stu1) << endl; //对象占用4个字节,函数也不包括;
//3.静态成员变量
Student2 stu2;
cout << sizeof(stu2) << endl; //空对象占用1个字节;
}
#include "head.h"
//使用条件:
//子类继承父类;
//子类重写父类虚函数;
//父类指针或者引用指向子类对象.
class Student {
public:
int a = 100, b = 99;
};
class animal {
public:
virtual void speak() {
cout << "动物在睡觉" << endl;
}
};
class cat :public animal {
public:
void speak() {
cout << "小猫在睡觉" << endl;
}
};
class dog :public animal {
public:
void speak() {
cout << "小狗在睡觉" << endl;
}
};
void dospeak(animal &animal) { //animal & animal = cat; //父类指针或者引用指向子类对象
animal.speak();
}
int main() {
Student stu;
cat cat1;
dospeak(cat1); //小猫在睡觉
dog dog1;
dospeak(dog1);// 小狗在睡觉
cout << "sizeof anmial: " << sizeof(animal) << endl;
animal animal1;
dospeak(animal1);// 动物在睡觉
}
#include "head.h"
//计算器抽象父类
class father {
public:
int num1, num2;
virtual int getresult() {
return 0;
}
};
//加法计算器类
class Addcalculate :public father {
public:
int getresult() {
return num1 + num2;
}
};
//减法计算器类
class Subcalculate :public father {
public:
int getresult() {
return num1 - num2;
}
};
//乘法计算器类
class Multicalculate :public father {
public:
int getresult() {
return num1 * num2;
}
};
int main() {
father* p = new Addcalculate; //父类指针指向子类
p->num1 = 100;
p->num2 = 11;
cout << p->num1 << "+" << p->num2 << "=" << p->getresult() << endl;
delete p;
// father* p = new Subcalculate; //错误,指向父类的指针还在,delete销毁的是指针指向的对象
p = new Subcalculate;
p->num1 = 100;
p->num2 = 11;
cout << p->num1 << "-" << p->num2 << "=" << p->getresult() << endl;
delete p;
p = new Multicalculate;
p->num1 = 100;
p->num2 = 11;
cout << p->num1 << "*" << p->num2 << "=" << p->getresult() << endl;
delete p;
}
//抽象类: 只有一个纯虚函数
//1.抽象类不允许实例化对象(包括堆上和栈上)
//2.子类必须重写纯虚函数
class father {
public:
virtual void fun() = 0;
};
class son:public father
{
public:
void fun() {
cout << "调用子类虚函数" << endl;
}
};
int main() {
//father p; //错误,抽象类不允许实例化对象(包括堆上和栈上)
//son son1; //错误,子类抽象类不允许实例化对象(包括堆上和栈上)
father* p = new son; //父类指针或者引用指向子类;
p->fun();
}