第一部分:主要包含基础数据类型、基础包的调用
#include <iostream>
#include <limits> // get the max or min value of a type
#include <bitset> // show the binary value of a variable
#include <ctime> // generate the seed for random
#include <cstdlib> // get the rand() func
#include <cmath> // math functions
#include <cstring> // get the class of string
#include <iomanip> // set the format for the data: float or double and so on
using namespace std;
#define PI 3.14
void show_data_type();
void toy_example();
void usage_of_typdef_wt_enum();
void usage_of_auto();
void usage_of_operator();
void usage_of_rand();
void usage_of_array_ad_string();
void usage_of_pointer();
void usage_of_reference();
void usage_of_time();
void usage_of_baseio();
void usage_of_struct();
int main()
{
// 展示C++数据类型˜
show_data_type();
// toy_example
toy_example();
// typdfe
usage_of_typdef_wt_enum();
usage_of_auto();
usage_of_operator();
usage_of_rand();
usage_of_array_ad_string();
usage_of_pointer();
usage_of_reference();
usage_of_time();
usage_of_baseio();
usage_of_struct();
return 0;
}
void show_data_type()
{
cout << "type: \t\t"
<< "************size**************" << endl;
cout << "bool: \t\t"
<< "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t"
<< "所占字节数:" << sizeof(char);
cout << "\t最大值:" << +(numeric_limits<char>::max)(); // 利用 + 实现整型提升,从而实现数值输出
cout << "\t\t最小值:" << +(numeric_limits<char>::min)() << endl;
cout << "signed char: \t"
<< "所占字节数:" << sizeof(signed char);
cout << "\t最大值:" << +(numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << +(numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t"
<< "所占字节数:" << sizeof(unsigned char);
cout << "\t最大值:" << +(numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << +(numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t"
<< "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t"
<< "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t"
<< "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t"
<< "所占字节数:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t"
<< "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t"
<< "所占字节数:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t"
<< "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t"
<< "所占字节数:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t"
<< "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "string: \t"
<< "所占字节数:" << sizeof(string) << endl;
cout << "type: \t\t"
<< "************size**************" << endl;
return;
}
void toy_example()
{
int i = 1;
// cout << ++i << i++ << i << i++ << ++i << endl;
}
void usage_of_typdef_wt_enum()
{
typedef int gesus;
gesus i;
cout << typeid(i).name() << endl;
enum cars
{
sports_car,
super_car,
common_car
} cr;
cr = super_car;
cout << cr << endl;
const int ABC = 10;
cout << ABC << endl;
}
static int n = 20;
void op()
{
static int x = 10;
x -= 1;
cout << x << "\t\t" << n << endl;
}
void usage_of_auto()
{
// auto a = "hello_world!";
while (n > 0)
{
n -= 1;
op();
}
}
void usage_of_operator()
{
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;
c = a & b; // 12 = 0000 1100
cout << "Line 1 - c 的值是 " << c << endl;
// cout << bitset<sizeof(c) * 8>(c) << endl; bitset<n> 其中n用于指定二进制的位数
cout << bitset<8>(c) << endl;
c = a | b; // 61 = 0011 1101
cout << "Line 2 - c 的值是 " << c << endl;
cout << bitset<8>(c) << endl;
c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - c 的值是 " << c << endl;
cout << bitset<8>(c) << endl;
c = ~a; // -61 = 1100 0011
cout << "Line 4 - c 的值是 " << c << endl;
cout << bitset<8>(c) << endl;
c = a << 2; // 240 = 1111 0000
cout << "Line 5 - c 的值是 " << c << endl;
cout << bitset<8>(c) << endl;
c = a >> 2; // 15 = 0000 1111
cout << "Line 6 - c 的值是 " << c << endl;
cout << bitset<8>(c) << endl;
}
void usage_of_rand()
{
srand((unsigned)time(NULL));
int left = 10;
int right = 200;
for (int i = 0; i < 10; i++)
{
cout << "One time: " << endl;
cout << rand() % (right - left) + left << endl; // [left, right)
cout << rand() % (right - left + 1) + left << endl; // [left, right]
cout << rand() % (right - left) + left + 1 << endl; // (left, right]
}
}
void usage_of_array_ad_string()
{
int a[] = {1, 2, 3, 4, 5};
int b[5];
// it should be notice that "int a[];" is not allowed! Because the size of array is not setted!
cout << a[0] << " " << &b << endl; // &b is the address of the array, *b represents get the value of the corresponding address
// define a string by using string class, in this condition, strcpy() is not allowed to use!
string str1 = "runoob";
string str2 = "google";
string str3;
int len;
// 复制 str1 到 str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// 连接 str1 和 str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// 连接后,str3 的总长度
len = str3.size();
cout << "str3.size() : " << len << endl;
// define a string by using char, in this condition, strcpy() is allowed!
char str11[] = "alibaba";
char str22[] = "youkusearch";
char str33[20];
cout << strcpy(str33, str11) << endl;
cout << strcmp(str33, str11) << endl;
}
void deal_arr(int *arr, int lens)
{
for (int i = 0; i < lens; i++)
{
arr[i] -= 1;
cout << arr[i] << endl;
}
return;
}
int *getRandom()
{
static int r[10]; // 此处设置静态变量,否则局部变量会随着函数体执行的结束而被释放
// int r[10];
// 设置种子
srand((unsigned)time(NULL));
for (int i = 0; i < 10; ++i)
{
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
void usage_of_pointer()
{
int *p; // a pointer is a variable, whose value is the address of another variable, array, and so on
int num = 10;
p = #
cout << p << " " << *p << endl;
int arr[10]; // arr is a const points to the array, it is unchangeable
int *ap;
ap = arr;
for (int i = 0; i < 10; i++)
{
cout << ap << " " << *ap << endl;
ap++;
}
const char *lcp[] = {"Tencent", "Alibaba", "Bytedance"};
int nc = sizeof(lcp) / sizeof(lcp[0]); // sizeof returns how many bytes the target takes
cout << nc << endl;
int i = 0;
while (i < nc)
{
cout << lcp[i] << endl;
i++;
}
int new_arr[] = {1, 2, 3, 4, 5};
int lens = sizeof(new_arr) / sizeof(int);
for (int i = 0; i < 2; i++)
{
deal_arr(new_arr, lens);
for (int j = 0; j < 5; j++)
{
cout << new_arr[j] << " ";
}
cout << endl;
}
int *pf;
pf = getRandom();
for (int i = 0; i < 10; i++)
{
cout << "*(pf + " << i << ") : ";
cout << *(pf + i) << endl;
}
}
void swap(int a, int b) // 传递时将输入的变量值复制给形参,因此输入变量的数值并不会改变
{
a = a + b;
b = a - b;
a = a - b;
// cout << a << " " << b << endl;
}
void swap2(int &a, int &b) // 输入为引用的形式,此时对a与b的修改会直接改变
{
a = a + b;
b = a - b;
a = a - b;
// cout << a << " " << b << endl;
}
int &setValue(int ind, int *arr)
{
int &per = arr[ind];
return per;
}
void usage_of_reference()
{
int a = 10;
int b = 20;
cout << "init: " << a << " " << b << endl;
swap(a, b);
cout << "swap: " << a << " " << b << endl; // a,b并未发生数值上的交换
swap2(a, b);
cout << "swap2: " << a << " " << b << endl;
int arr[] = {1, 2, 3, 4, 5, 6};
setValue(2, arr) = -1; // 返回对元素的引用
for (int i = 0; i < 6; i++)
{
cout << arr[i] << " ";
}
}
void usage_of_time()
{
time_t now = time(0);
char *dt = ctime(&now);
cout << "Current time is: " << dt << endl;
tm *ltm = localtime(&now);
cout << now << " " << 1900 + ltm->tm_year << endl;
cout << 1 + ltm->tm_mon << endl;
cout << ltm->tm_mday << endl;
cout << ltm->tm_hour << endl;
cout << ltm->tm_min << endl;
cout << ltm->tm_sec << endl;
}
void usage_of_baseio()
{
// string name;
// int age;
// cin >> name >> age;
// cout << name << " " << age << " " << name[1] << endl;
// cout << name[6] << endl;
double a = 123.456789;
cout << setiosflags(ios::left | ios::fixed);
cout << setprecision(3);
cout << a << endl;
}
struct cars
{
char name[20];
int max_speed;
float price;
};
void set_a_car(struct cars *car, const char *name, int sp, float pr) // c++中string是 const char 类型,“”默认是string,所以不加const这里报编译错误
{
strcpy(car->name, name);
car->max_speed = sp;
car->price = pr;
}
void show_cars_str(struct cars car_ins)
{
cout << "here is the name of the car: " << car_ins.name << endl;
cout << "here is the max speed of the car: " << car_ins.max_speed << endl;
cout << "here is the price of the car: " << car_ins.price << endl;
}
void usage_of_struct()
{
cars car_ins;
set_a_car(&car_ins, "koenigsegg", 123, 23.1);
show_cars_str(car_ins);
}
第二部分:主要是类、继承等
#include <iostream>
#include <limits> // get the max or min value of a type
#include <bitset> // show the binary value of a variable
#include <ctime> // generate the seed for random
#include <cstdlib> // get the rand() func
#include <cmath> // math functions
#include <cstring> // get the class of string
#include <iomanip> // set the format for the data: float or double and so on
using namespace std;
void usage_of_class_BaseUse_Inheritance(); // 类中变量与函数的定义、访问修饰符、类的继承
void usage_of_class_Constructor_Destructor(); // 类的构造函数、析构函数
void usage_of_friend_inline(); // 友元函数、内联函数
void usage_of_this_wt_pointer(); // this指针、指向类的指针
void usage_of_class_static(); // 静态类成员
int main()
{
usage_of_class_BaseUse_Inheritance();
usage_of_class_Constructor_Destructor();
usage_of_friend_inline();
usage_of_this_wt_pointer();
usage_of_class_static();
return 0;
}
class gta_cars // 类的基本使用:变量定义、函数定义、变量使用、函数使用、访问修饰符
{
public:
//变量定义
char name[20];
int ms; // max speed
int price;
int weight; // weight of the car
int mp; // max person the car can take
//函数定义
void set_cinfo(const char *cname, int cms, int cprice, int cweight, int cmp)
{
strcpy(name, cname);
ms = cms;
price = cprice;
weight = cweight;
mp = cmp;
}
void show_cinfo()
{
cout << "------Below comes the info of the car-------" << endl;
cout << "name of the car is: \t\t" << name << endl;
cout << "max speed of the car is: \t" << ms << endl;
cout << "price of the car is: \t\t" << price << endl;
cout << "weight of the car is: \t\t" << weight << endl;
cout << "max person of the car is: \t" << mp << endl;
cout << "-----Upper finish the info of the car-------" << endl;
show_f(); // private 成员在类内可以使用,但class外不可调用,同样不能在类外进行private函数的定义
}
// 函数声明(主要用于类外函数定义)
void alert_cinfo_ms(int cms); // 外部定义类成员函数时需要首先在这里进行声明
private: // 访问修饰符,该部分变量或者函数只能在类内进行使用
int frame_pics_num;
void show_f()
{
frame_pics_num = 200; // 汽车制造的框架图数量
cout << "num of pictures of the car frame is: " << frame_pics_num << endl;
};
};
void gta_cars::alert_cinfo_ms(int cms) //类外函数定义
{
ms = cms;
}
class attack_cars : public gta_cars // 类的继承
{
// class attack_cars : [protected|private] gta_cars
public:
int ap; // attack power
void set_ap()
{
ap = 100;
}
void check_cc_pub(const char *user_input)
{
set_cc();
if (check_cc(user_input))
{
cout << "Success!" << endl;
}
else
{
cout << "Failed!" << endl;
}
}
private:
char cheat_code[20]; // 作弊码
void set_cc()
{
strcpy(cheat_code, "acker"); // 设置作弊码
}
bool check_cc(const char *ucc)
{
if (strcmp(ucc, cheat_code) != 0)
{
return false;
}
return true;
}
};
void usage_of_class_BaseUse_Inheritance()
{
// gta_cars c1;
// gta_cars c2;
// c1.set_cinfo("koenigsegg", 240, 120000000, 1080, 2);
// c2.set_cinfo("bugatti", 242, 160000000, 1280, 2);
// c1.show_cinfo();
// c2.show_cinfo();
// c1.alert_cinfo_ms(199);
// c1.show_cinfo();
// c1.show_f(); 错误调用方式
attack_cars c3;
c3.set_cinfo("pangdiyake", 180, 180000000, 4200, 4);
c3.show_cinfo();
c3.check_cc_pub("acker");
}
class gta_guns
{
public:
char name[20];
int capacity; // 弹匣容量
float attack; // 单次击发伤害
float weight; // 质量
// 这里只是完成了变量类型定义,并未赋值或进行数据预处理等等,那么可以写一个函数,进行初始化
// 能不能不写这个赋值函数呢?因为这样每次定义一个类对象,都需要对类对象的成员进行初始化
gta_guns()
{
// 构造函数就是解决上述问题的函数,可以在定义对象时直接完成相关op
// 构造函数的函数名与类型必须完全相同!且同样可以传参
strcpy(name, "default");
capacity = 30;
attack = 24.5;
weight = 9.6;
cout << "A new gun is created and the default value is setted!" << endl;
}
// 如果连续定义多个初始对象,那么能不能用已有的对象进行复制?
gta_guns(const gta_guns &already_gun)
{
strcpy(name, already_gun.name);
capacity = already_gun.capacity;
attack = already_gun.attack;
weight = already_gun.weight;
cout << "A new gun is created with the initialized gun!" << endl;
}
void show_ginfo()
{
cout << "\n------Below comes the info of the gun-------" << endl;
cout << "name of the car is: \t\t" << name << endl;
cout << "max speed of the car is: \t" << capacity << endl;
cout << "price of the car is: \t\t" << attack << endl;
cout << "weight of the car is: \t\t" << weight << endl;
cout << "-----Upper finish the info of the gun-------\n"
<< endl;
}
~gta_guns()
{
// 析构函数是在所创建函数被删除时执行的函数
cout << "the gun has been delete!" << endl;
}
};
void usage_of_class_Constructor_Destructor()
{
gta_guns g1;
g1.show_ginfo();
cout << "test if ~gta_guns is executed" << endl; // 说明当前函数执行完毕后,相关对象会自动删除
gta_guns g2 = g1; // 可以看出,g2通过g1完成了初始化
}
class class_a
{
friend class f_class_a; // 不添加则在后续tmp.paa时会报错
friend void show_v(class_a paa);
public:
class_a()
{
paa = 10;
cout << "class_a is creating!" << endl;
}
private:
int paa;
};
void show_v(class_a paa)
{
cout << "get paa from a friend function and the val is: " << paa.paa << endl;
}
class f_class_a
{
public:
f_class_a(class_a tmp)
{
cout << "visit class_a.paa from friend class and its val is: " << tmp.paa << endl;
};
};
inline int time_testing(int i)
{
if (i >= 10 && i < 20)
return i + 10;
else if (i >= 20 && i < 30)
return i + 20;
else
return i + 30;
}
int time_testing2(int i)
{
if (i >= 10 && i < 20)
return i + 10;
else if (i >= 20 && i < 30)
return i + 20;
else
return i + 30;
}
void usage_of_friend_inline()
{
// class_a ia;
// f_class_a ib(ia);
// show_v(ia);
long top_line;
top_line = 10000000000;
clock_t start = clock();
for (long i = top_line; i > 0; i--)
{
time_testing2(i);
}
clock_t end = clock();
cout << (double)(end - start) / CLOCKS_PER_SEC << endl;
clock_t start2 = clock();
for (int i = top_line; i > 0; i--)
{
time_testing(i);
}
clock_t end2 = clock();
cout << (double)(end2 - start2) / CLOCKS_PER_SEC << endl;
}
class box
{
public:
box(double l = 2.0, double w = 2.0, double h = 2.0)
{
cout << "A box has beed created!" << endl;
length = l;
width = w;
high = h;
}
void get_v()
{
cout << "Volume of the box is: " << length * width * high << endl;
}
void show_v()
{
this->get_v();
}
private:
double length;
double width;
double high;
};
void usage_of_this_wt_pointer()
{
box a(3.0, 3.0, 6.0);
box b(2.0, 2.0, 10.0);
box *box_ptr; // 类的指针
box_ptr = &a;
box_ptr->show_v();
box_ptr = &b;
box_ptr->show_v();
}
class person
{
public:
static int person_num; // 定义静态变量用于统计人员数量
person(string na = "undefine", int a = 24, double w = 78.3)
{
name = na;
age = a;
weight = w;
person_num += 1;
rank = person_num;
}
void show_info()
{
cout << "Person " << this->rank << " 's info is shown as belows:" << endl;
cout << this->name << " " << this->age << " " << this->weight << endl;
}
static void get_person_num()
{
cout << person_num << endl;
}
private:
string name;
int age;
double weight;
int rank;
};
int person::person_num = 0; // 初始化静态变量
void usage_of_class_static()
{
person::get_person_num();
person p1("123师兄", 26, 70);
person p2("234师兄", 27, 67);
person p3("456师姐", 26, 51);
person p4("567师弟", 24, 70);
person p5;
p3.show_info();
person::get_person_num();
}