C++-class struct(类)
class和struct的区别就是class需要指明private 和 public 而 struct不需要
/* 类定义 */ #include <iostream> using namespace std; struct Student { public: void eat(const string& food) { cout << "我在吃" << food << endl; } void sleep(const int hour) { cout << "我睡了" << hour << "小时" << endl; } void study(const string& course) { cout << "我学习" << course << endl; } void who(void) { cout << "我是" << m_name << ",今年" << m_age << "岁,编号" << m_no << endl; } void set_name(const string& name) { m_name = name; } void set_age(const int age) { m_age = age; } void set_no(const int no) { m_no = no; } private: string m_name; int m_age; int m_no; }; int main(void) { Student s; s.set_name("小明"); s.set_age(12); s.set_no(10001); s.eat("饺子"); s.sleep(12); s.study("语文"); s.who(); }
建立初始化的构造函数,用于在初始化的时候,对类进行构造
#include <iostream> using namespace std; class Student{ public: Student(const string& name, int age, int no) { cout << "构造函数" << endl; //进行直接的构造 m_name = name; m_age = age; m_no = no; } void who(void) { cout << "我叫" << m_name << ",今年" << m_age << "岁, 学号是" << m_no << endl; } private: string m_name; int m_age; int m_no; }; int main(void) { //创建对象/实例化对象 //(...)指定构造函数需要的实参 Student s("张飞", 28, 10011); s.who(); //构造函数不能显示调用 s.Student("张三, 29, 10012"); return 0; }
使用time包构造函数,使用run进行代码的运行
#include <iostream> #include <ctime> #include <unistd.h> #include <cstdio> using namespace std; /* struct tm { int time_hour; int tm_min; int tm_sec; }; */ class Clock{ public: Clock(time_t t){ tm* local = localtime(&t); hour = local->tm_hour; minute = local->tm_min; sec = local->tm_sec; } void run(void) { while (1) { printf("1"); sec = sec + 1; if (sec == 60) { sec = 0; minute += 1; if (minute == 60) { minute = 0; hour += 1; if (hour == 24) { hour = 0; } } } printf("\r\n%02d:%02d:%02d", hour, minute, sec); fflush(stdout); sleep(1); } } private: int sec, minute, hour; }; int main() { Clock c(time(NULL)); c.run(); return 0; }
在数组中构造多个类,使用new构造动态的类数组
/* 多个对象数组 */ #include <iostream> using namespace std; class Student { public: Student(const string& name, int age, int no):m_name(name), m_age(age), m_no(no){} void who(void) { cout << "我是" << m_name << ",年龄" << m_age << ",编号" << m_no << endl; } private: string m_name; int m_age; int m_no; }; int main(void) { Student s = Student("小清", 10, 10002); //构造出对象数组 Student arr[3] = { Student("张飞", 12, 10001), Student("小明", 30, 200001), Student("小红", 20, 100001), }; arr[0].who(); arr[1].who(); arr[2].who(); Student *p_s = new Student("小白", 13, 10001); p_s->who(); delete p_s; p_s = NULL; Student* p_arry = new Student[2] { Student("小明", 10, 10001), Student("小花", 20, 10001) }; p_arry[0].who(); p_arry[1].who(); delete[] p_arry; p_arry = NULL; }
在类里面定义新的类类型
#include <iostream> using namespace std; class A{ public: /* A(void){ cout << "A的无参构造函数" << endl; m_i = 0; } */ A(int i){ cout << "A的有参构造函数" << endl; m_i = i; } int m_i; }; class B{ public: int m_j; //基本类型成员变量 A m_a = A(2); //成员子对象 进行无类型声明 }; int main(void) { B b; cout << "基本类型" << b.m_j << endl; cout << "类类型" << b.m_a.m_i << endl; return 0; }
两种条件定义下的构造函数, 即使用有参和无参的方式
#include <iostream> using namespace std; class Integer{ public: Integer(void) { cout << "Interger(void)" << endl; m_i = 0; } //进行显示转换 /*explicit*/ Integer(int i) { cout << "Integer(int)" << endl; m_i = i; } void print(void) { cout << m_i << endl; } private: int m_i; }; int main(void) { Integer i; i.print(); //1)先将100转换为Interger对象 //2)再使用转换结果(临时结果)对i进行赋值操作 //i = 100; //类型转换构造函数 // i.print(); //上面隐士转换代码可读性叉,推介显示转换 // i = (Integer)200; i = Integer(200); //C++风格 对象的实例化 i.print(); return 0; }
重新构造拷贝函数
#include <iostream> using namespace std; class A{ public: A(int data = 0) { cout << "A(int = 0)" << endl; m_data = data; } /* A(const A& that){ cout << "A(const A&)" << endl; m_data = that.m_data; } */ void print(void) { cout << m_data << endl; } int m_data; }; int main(void){ A a1(100); // A a2(a1); //拷贝构造 // A a2 = A(a1); A a2 = a1; //和上面写法等价 a1.print(); a2.print(); return 0; }
类类函数的函数构造, 通过定义获取
#include <iostream> using namespace std; class A{ public: A(int data = 0) { cout << "A(int = 0)" << endl; m_data = data; } A(const A& that){ cout << "A(const A&)" << endl; m_data = that.m_data; } int m_data; }; class B{ public: A m_a; //成员子对象 }; int main(void){ B b1; //无参 B b2 = b1; //拷贝构造 cout << b1.m_a.m_data << endl; //0 cout << b2.m_a.m_data << endl; //0 return 0; }
根据类进行拷贝赋值操作
#include <iostream> using namespace std; class A{ public: A(void) { cout << "A的无参构造" << endl; } A(const A& that) { cout << "A的拷贝构造" << endl; } }; void foo(A a){} A bar(void) { A a; //无参 cout << "&a:" << &a << endl; return a; //A tmp = a //拷贝 } int main(void) { A a1; //无参 A a2 = a1; //拷贝 foo(a1); //拷贝 A a3 = bar(); //拷贝 cout << "&a3:" << &a3 << endl; return 0; }
对有参类进行直接的初始化操作
#include <iostream> using namespace std; class A{ public: A(void) { cout << "A的无参构造" << endl; } A(const A& that) { cout << "A的拷贝构造" << endl; } }; void foo(A a){} A bar(void) { A a; //无参 cout << "&a:" << &a << endl; return a; //A tmp = a //拷贝 } int main(void) { A a1; //无参 A a2 = a1; //拷贝 foo(a1); //拷贝 A a3 = bar(); //拷贝 cout << "&a3:" << &a3 << endl; return 0; }
构造类类函数,进行类函数的定义
#include <iostream> using namespace std; class A{ public: A(int i){ cout << "A(int)" << endl; m_i = i; } int m_i; }; class B{ public: B(void):m_a(123){ //可以先开始进行构造 cout << "B(void)" << endl; } A m_a; //成员子对象 } int main(void) { B b; cout << b.m_a.m_i << endl; return 0; }
使用全局变量来初始化类
/* 使用全局变量构造函数 */ #include <iostream> using namespace std; int num = 100; class A { public: A(void):m_c(200), m_r(num){ } int& m_r; const int m_c; }; int main() { A a; cout << a.m_r << endl; }
对输入的参数,如果是NULL, 使用str?str:"" 构造三目运算
/* 构造类函数用于定义 */ #include <iostream> #include <cstring> using namespace std; class Dummy { public: //将NULL进行形式转换,转换成"" Dummy(const char* str):m_str(str?str:""), m_len(strlen(str?str:"")) {} int m_len; string m_str; }; int main() { Dummy d(NULL); cout << d.m_str << d.m_len << endl; return 0; }
每天更新pyQt5内容