本人使用的QT5.12.10下载地址http://download.qt.io/archive/qt/5.12/5.12.10/
1.什么是C++
C++是C语言的升级版,在C的基础上加了很多功能,是一种高级语言
2.如何定义一个类
class Student
{
public:
char name[64];
int age;
string address;
};
3.什么是对象,如何定义
对象是类的实例化
Student myStudent;//直接定义
Student *myStu=new Student;//在堆里定义
delete myStu;//删除堆里的对象
4.如何访问类的成员变量
普通变量通过" . “,指针变量通过” -> "
Student myStudent;
Student *myStu=new Student;
myStudent.age=18;
myStu->age=22;
cout<<myStudent.age<<endl;
cout<<myStu->age<<endl;
5.类的函数成员
一:在类里面声明
二:实现这个函数,我们可以直接在类的里面写,也可以写在类的外面。
class Student
{
public:
char name[64];
int age;
string address;
void test();//写在类的里面
};
void Student::test()//Student::这样子写表示test函数属于这个类,不然的话表示他是一个普通的函数成员
{
cout<<235<<endl;
}
6.类函数的重载特性
类函数的重载特性就是说我们可以在类里面定义同名的函数,但是参数不同。
class Student
{
public:
char name[64];
int age;
string address;
void test();
void test(int a);
private:
int telephone;
};
7.构造函数个析构函数
析构函数:假如我们定义了析构函数,当对象被删除或者生命周期结束的时候,就会触发析构函数。
构造函数:假如我们定义了对象,就会触发构造函数。
怎么定义:
1.构造函数和析构函数的函数名和类名一样;
2.析构函数在名字前面加~,析构函数不能被重载。
class Student
{
public:
Student();
Student(int a);//构造函数重载
~Student();
char name[64];
int age;
string address;
void test();
void test(int a);
private:
int telephone;
};
Student::Student(){
cout<<"hello"<<endl;
}
Student::~Student(){
cout<<"end"<<endl;
}
8.类的继承
类的继承允许我们在新的类里面继承父类的public和protected部分。
class BanJi:Student
{
public:
int peopleNum;
};
9.虚函数和纯虚函数
虚函数:有实际定义的,允许派生类对他进行覆盖式的替换,virtual修饰。
纯虚函数:没有实际定义的虚函数就是纯虚函数。
class BanJi:Student
{
public:
int peopleNum;
void test(){
cout<<698<<endl;
};
};
虚函数的优点是可以预留接口。
建立一个简单的QT界面
QT的移植性很强,一套代码不需要改太多,可以直接通用所有平台。
最主要的基类选择注意一下
点击Forms,双击ui文件,就能进入ui编辑界面。
看下图做的界面
下一篇查看QT重点:信号和槽