快速入门 QT5 C++基础

1 篇文章 0 订阅
1 篇文章 0 订阅

1.QT5中文显示乱码

方法一:

system("chcp 65001");  // 放在主函数中

方法二:

首先引入库  

#include "windows.h"

再在主函数中写 

  SetConsoleOutputCP(CP_UTF8);

2.什么是类,如何创建一个类

#include <iostream>
#include "windows.h"

using namespace std;
class Dog{
// 类的成员不仅可以是变量,还可以是函数   --》对象
public:
    string name;
    int age;
    void out_tlc(void)
    {
        cout << age << endl;
    };
protected:
    float weight;

private:


};

int main()
{
    SetConsoleOutputCP(CP_UTF8);
//    system("chcp 65001");
    //从栈中实例化对象
    Dog Dog1;
    Dog1.name = "张三";
    Dog1.age = 3;
    cout << Dog1.name <<" "<<Dog1.age << endl;
    Dog1.out_tlc();

    //从堆中实例化对象
    Dog *Dog2 = new Dog;
    Dog2 ->name = "李四";
    Dog2 ->age = 2;
    cout<< Dog2->name << " "<< Dog2->age << endl;
    Dog2->out_tlc();
    delete Dog2;  // 用new之后一定要用delete

    return 0;
}

3.构造函数与析构函数

#include <iostream>
#include "windows.h"

using namespace std;
class Dog{
public:
    Dog()  // 构造函数 开始时
    {
      cout << "牛逼!!" << endl;
    };

    ~Dog(); // 析构函数 被销毁时

};

Dog::~Dog()  // 在Dog的作用域下
{
   cout<< "析构函数被调用了!!" << endl;
}

int main()
{   SetConsoleOutputCP(CP_UTF8); // 中文
    // 栈
//    Dog Dog1;
//    cout << "Hello World!" << endl;


    // 堆
    Dog *Dog2 = new Dog;
    delete Dog2;
    return 0;
}

4.类的继承

#include <iostream>
#include "windows.h"

using namespace std;

//继承
// 基类 也叫父类
class Anmial{

public:
    string name;
    int age;
    void my_naem(void)
    {
      cout << name << age << endl;
    };
};

//派生类 也叫子类
class Dog: public Anmial  // 只能访问基类中的public 和 protected 成员
{


};

/*
protected Anmial和 private Anmial都只能访问基类中的public 和 protected 成员
派生类的对象不能访问基类的任何成员
*/

int main()
{   SetConsoleOutputCP(CP_UTF8);

    Dog Dog1;  // 派生类的对象只能访问基类的public成员
    Dog1.age = 1;
    Dog1.name = "小三";
    Dog1.my_naem();

    return 0;
}

5.函数重载

#include <iostream>
#include "windows.h"

using namespace std;

// 函数重载
class Dog{
public:

    Dog(int age)
    {
        cout << "int类型" << age <<endl;

    };
    Dog(double age)
    {
        cout << "double类型" << age << endl;
    };
    void get_weight(int weight)
    {
        cout << "int类型" << weight <<endl;
    };
    void get_weight(double weight)
    {
        cout << "double类型" << weight <<endl;
    };


};
int main()
{
    SetConsoleOutputCP(CP_UTF8);
    Dog Dog1(10);
    Dog Dog2(10.1);
    Dog Dog3(10);
    Dog3.get_weight(10);
    Dog3.get_weight(10.3);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值