栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++基础(二)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++基础(二)

this指针

#include 

using namespace std;

class Stu
{
private:
    string name;
    int age;
public:
    Stu(){}
    ~Stu(){}
    Stu(string name, int age)
    {
        this->name = name; //区分参数中的变量和类内变量,形参变量名与类中变量名相同,用于区别类中的属性变量和形参。
        this->age = age;
    }

    Stu& setName(string name) //使用this返回本对象
    {
        this->name = name;
        return *this;
    }

    void showInfo()
    {
        cout<<"学生信息: n"<<"name: "<name<<"nage: "<age<
    Stu s("zhangsan", 20);
//    Stu s1 = s.setName("list"); // 返回值为对象时,调用拷贝构造
    s.showInfo();
    Stu s1 = s.setName("list");
    s1.showInfo();
    cout << "Hello World!" << endl;
    return 0;
}





静态函数

#include 

using namespace std;

class Stu
{
private:
    string name;
    int age;
    static int amount;
public:
    Stu()
    {
        cout<<"无参构造"<
        cout<<"析构"<
        this->name = name;
        this->age = age;
    }
    static int getAmount()  //没有生成对象时也可使用
    //不需要依赖某个实例,satic修饰的属于整个类,静态函数只能访问静态成员函数
    {
        return amount;
    }

    int getAmount123()
    {
        return amount;
    }

};
// 类内成员只是声明;类外定义
int Stu::amount = 100;
int main()
{
    cout< 

对象数组

#include 

using namespace std;

class Desk
{
public:
    Desk()
    {
        cout<<"Desk构造"<
        cout<<"Desk析构"<
public:
    Chair()
    {
        cout<<"Chair的构造"<
        cout<<"Chair的析构"<
private:
    Desk _desk[3]; //对象数组,每个数组元素都是对象
    Chair _chair[3];
public:
    Room()
    {
        cout<<"Room的构造"<
        cout<<"Room的析构"<
    Room room;
    cout << "Hello World!" << endl;
    return 0;
}



常对象

#include 

using namespace std;

class Key
{
public:
    Key()
    {
        cout<<"Key的构造"<
        cout<<"Key的析构"<
public:
    Mouse()
    {
        cout<<"Mouse的构造"<
        cout<<"Mouse的析构"<
public:
    int weight;
};

class Computer
{
private:
    Key _key;
    Mouse _mouse;
    string name;
public:
    Computer():name("Dell")
    {
        cout<<"Computer的构造"<
        cout<<"Computer的析构"<
        return this->name;
    }
    const Computer& setName(string name) //返回类对象
    {
        this->name = name;
        return *this;
    }
};

int main()
{
    Computer computer;
    cout< 

static静态函数

#include 

using namespace std;

class Stu
{
private:
    string name;
    int age;
    static int amount;// static修饰的属性,叫类中的声明,因为static是全局变量,在类外定义
public:
    Stu()
    {
        cout<<"Stu的无参空构造"<
        this->name = name;
        this->age = age;
    }
    void showInfo()
    {
        cout<<"【学生信息】:n"<<"姓名:"<name<<"n年龄:"<age<
        return amount;
    }
};

int Stu::amount = 20;
int main()
{
    Stu::getAmount();
    cout<<"-------------------------------"< 

友元函数

#include 

using namespace std;

class Boy
{
private:
    string name;
    int id;
    int money;
public:
    Boy()
    {

    }
    Boy(string name, int id, int money)
    {
        this->name = name;
        this->id = id;
        this->money = money;
    }
    void work()
    {
        this->money += 10000;
        cout<<"工作赚了:"<money<
    cout<<_boy.money<
    Boy zhangsan;
    zhangsan.work();
    beautifulGril(zhangsan);
    cout << "Hello World!" << endl;
    return 0;
}




友元类

#include 

using namespace std;

class Boy
{
private:
    string name;
public:
    Boy() = default;
    Boy(string name)
    {
        this->name = name;
    }
    void hardWork()
    {
        cout<<"写代码"<
private:
    string name;
public:
    Gril() = default;
    Gril(string name)
    {
        this->name = name;
    }
    void shopping()
    {
        cout<<"买东西"<
        cout<name<<"逛"<
    Boy zhangsan("张三"); //友元是不能传递的,但能相互,份文件编程
    Gril xiaoli("小丽");
    xiaoli.goShopping(zhangsan, "家乐福");
    cout << "Hello World!" << endl;
    return 0;
}

运算符重载

#include 

using namespace std;

class A
{
private:
    int number;
public:

    void setNumber(int num)
    {
        this->number = num;
    }
    int getNumber()
    {
        return this->number;
    }

    A& operator=(A& a){
        this->number = a.number;
        cout<<"这是类A的=运算符重载函数"<
    A a1;
    a1.setNumber(100);
    A a2;
    a2.setNumber(200);
    a1 = a2;  //将a2中的属性值赋值给a1
    cout< 

运算符重载

#include 

using namespace std;

class Apple
{
private:
    int price;
public:
    Apple()=default;
    Apple(int price)
    {
        this->price = price;
    }
    Apple& operator +(Apple& apple) //类对象作为形参,返回类对象,将一个类作为类的构造函数
    {
        this->price += apple.price;
        return *this;  //反复套娃 +完后还是对象
    }
    int getPrice()
    {
        return this->price;
    }
};

int main()
{
    Apple a1(5000);
    Apple a2(3000);
    Apple a3(8000);
    cout<<(a1+a2+a3).getPrice()< 

运算符重载

#include 
#include 

using namespace std;

class myString
{
private:
    char* p_data;
public:
    myString()
    {
        p_data = new char[0];
        p_data[0] = '';
    }
    myString(const char* str)
    {
        int len = strlen(str);
        p_data = new char[len+1];
        memcpy(this->p_data, str, strlen(str));
        p_data[len] = '';
    }
    ~myString()
    {
        if(p_data!=NULL)
        {
            delete[] p_data;
        }
    }
    friend ostream& operator<<(ostream& cout, myString& str);
    char& operator[](int index)
    {
        if(index<0 || index > strlen(this->p_data))
        {
            cout<<"访问越界";
        }

        return this->p_data[index];

    }
};

ostream& operator <<(ostream& cout, myString& str)
{
    cout<
    myString str("zhangsan");
    cout<< str < 

运算符重载

#include 
#include 
using namespace std;

class MyClock
{
private:
    int min;
    int sec;
public:
    MyClock() = default;
    MyClock(int min, int sec)
    {
        this->min = min;
        this->sec = sec;
    }
    MyClock& operator++() //C++规定,主要区分前后缀,加int是后缀++
    {
        sec++;
        if(sec == 60)
        {
            sec = 0;
            min++;
            min %= 60;
        }
        return *this;
    }
    void showTimeInfo()
    {
        cout<min<<":"<sec<
    MyClock clock(0, 0);
    while(true){
        ++clock;
        sleep(1);
        clock.showTimeInfo();
    }
    cout << "Hello World!" << endl;
    return 0;
}



继承

#include 
using namespace std;

class A
{
private:
     int number=10;
public:
    A()
    {
        cout<<"A的构造"<
        cout<<"A的析构"<
        cout<<"AAAAAAAAA"<
        return this->number;
    }
};

class B: public A
{
private:
    int number=1000;
public:
    B()
    {

        cout<<"B的构造"<
        cout<<"B的析构"<
        cout<<"BBBBBBBBB"<
    B *b = new B;  // 派生类对象

    b->showInfo();
    b->showInfo();
    cout<getNumber()< 

类继承

#include 
using namespace std;

class Plane
{
public:
    virtual void fly()
    {
        cout<<"飞机正在飞"<
public:
    void fly()
    {
        cout<<"载客飞行中"<
public:
    void drive(Plane& p)   //传入子类
    {
        p.fly();
    }
};

int main()
{
    flyer f;
    Plane p;
    BY_737 by;
    f.drive(p);
    f.drive(by);  //子类对象访问父类,禁止小域传向大于,只能大于传向小于,因为大于包含小域
    return 0;
}

接口类

#include 

using namespace std;

class Phone
{
public:
    int power;
public:
    Phone()
    {
        cout<<"Phone"<
        cout<<"~Phone"<
public:
    int power;
public:
    Computer()
    {
        cout<<"computer"<
        cout<<"~Computer"<
public:
    Pad()
    {
        cout<<"Pad"<
        cout<<"~Pad"<
        return this->power;
    }
    void show() override
    {
        cout<<"show"<
    Pad pad;
    cout << "Hello World!" << endl;
    return 0;
}
// 接口类

菱形继承

#include 

using namespace std;

class Base
{
    int a;
public:
    Base():a(10)
    {
        cout<<"Base"<
        cout<<"~Base"<
public:
    A()
    {
        cout<<"A"<
        cout<<"~A"<
public:
    B()
    {
        cout<<"B"<
        cout<<"~B"<
public:
    C()
    {
        cout<<"C"<
        cout<<"~C"<
    C c;
    cout< 

构造函数初始化列表

#include 

using namespace std;

class A
{
public:
//    A()
//    {
//        cout<<"A的构造"<
        cout<<"A的有参构造"<
        cout<<"~A析构"<
public:
    B():A::A(1)  //调用有参构造
    {
        cout<<"B的构造"<
        cout<<"~B的构造"<

};

int main()
{
    B b;
    cout << "Hello World!" << endl;
    C c;
    return 0;
}

类型转换

#include 

using namespace std;


class A
{
public:
    void showInfo()
    {
        cout<<"AAAAAAA"<
private:
    int a = 88;
public:
    void showBInfo()
    {
        cout<<"a = "<a<
    int a = 97;
    cout<(a)< 

多态

#include 

using namespace std;

class Plane
{
public:
    virtual void fly()
    {
        cout<<"飞机正在飞行"<
public:
    void fly() override
    {
        cout<<"空中客车在飞行"<
public:
    void fly() override
    {
        cout<<"J20正在隐形战斗巡航"<
public:
    void fly() override
    {
        cout<<"直升机正在空中盘旋"<
public:
    void drive(Plane& p)
    {
        p.fly();
    }

};

int main()
{
//    Plane* plane = new AirBus; //父类指针指向子类对象
//    Plane plane;
    plane->Plane::fly();  //调用父类
    plane->fly(); //调用子类
//    std::cout<< sizeof(plane) < 

纯虚函数

#include 

using namespace std;
class A  // 抽象类
{
public:
    virtual void show() = 0; // 接口
};
class B : public A
{
public:
    void show() override
    {
        cout<<"今天练习"<
    A* a = new B;
    a->show();
    cout << "Hello World!" << endl;
    return 0;
}




多态

#include 

using namespace std;
class A
{
public:
    A()
    {
        cout<<"A的构造"<
        cout<<"A的析构"<
        cout<<"AAAAAAAAA"<
public:
    B()
    {
        cout<<"B的构造"<
        cout<<"B的析构"<
        cout<<"BBBBBBBBB"<
    A* a = new B;
    a->showInfo();
    delete a;
    return 0;
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012890.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号