资讯详情

C++基础(二)

#include <iostream>  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: "<<this->name<<"\nage: "<<this->age<<endl;     } };  int main() { 
            Stu s("zhangsan", 20); // Stu s1 = s.setName("list"); // 当返回值为对象时,调用复制结构     s.showInfo();     Stu s1 = s.setName
      
       (
       "list"
       )
       ; s1
       .
       showInfo
       (
       )
       ; cout 
       << 
       "Hello World!" 
       << endl
       ; 
       return 
       0
       ; 
       } 
       /* * this指针 * 1、指向本对象 * this本身就是一个编译器提供的指针,指针保存的是本对象的地址 * 2、返回本对象 * 3、this不占内存空间 * 4、this保存本对象的地址。 * 5、类的成员函数中,静态函数没有this指针;产生对象以后, * 编译器才分配的一个指针;static修饰的函数,相当于是进程 * 加载静态区时,就已经存在的函数,则这个函数时存在于静态区的 * 而非动态区,是先于对象前产生的,所以它的函数参数中没有this指针。 * 也就是说这个静态函数,是没办法调用类中非静态成员。因为它没有this指针。 * * this的作用域是在类的内部,当在类的非静态成员函数中,访问类的非静态成员时 * 编译器会自动将对象本身的地址作为隐藏参数传递给函数。即使没写this指针,编译器也会生成 * this指针作为非静态成员函数的隐含参数,对各成员的访问都是this指针进行的。 * * 在成员函数内部,我们可以直接调用该函数的对象成员,而无需通过成员访问运算符来做到这一点 * 因为this所指向的正是这个对象。任何对类成员的访问都被看成this的隐式使用。 * * static修饰的对象时先于对象的,所以形参中时没有this的,所以没有办法调用类中的属性 */ 
      

#include <iostream>

using namespace std;

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

    int getAmount123()
    { 
       
        return amount;
    }

};
// 类内成员只是声明;类外定义
int Stu::amount = 100;
int main()
{ 
       
    cout<<Stu::getAmount()<<endl; //被静态函数修饰的函数,是属于本文件的,且不可被external引申,本文件的全局函数
    Stu stu;
    cout<<stu.getAmount123()<<endl;
    cout<<stu.getAmount()<<endl;
    return 0;
}

/* * 静态函数与普通函数区别: * 用static修饰的函数与其修饰的变量不同,static修饰的函数不是声明存储形式 * 是修饰的作用域,这个作用域就是本文件使用,相当于本文件使用的全局函数,全 * 局函数不依赖于某个对象,在没有对象情况下也是可以调用的,这个全局函数没有 * this指针,所以没办法在静态函数内部访问类中的属性,静态函数时本文件的全局函数 * 且只有类内可见,这函数可以在没有类对象时访问。 * * 静态函数有点: * 1、其他文件中定义同名函数,不会产生冲突 * 2、静态函数不能被其他文件所用。 * 3、属于类的,不创建对象也可以使用 * 4、全局,不依赖对象,没有this指针,无法调用类中属性 * 普通成员函数 * 1、在函数参数列表中隐藏了一个this指针,是可以访问类中属性,但是其依赖类对象 */

#include <iostream>

using namespace std;

class Desk
{ 
       
public:
    Desk()
    { 
       
        cout<<"Desk构造"<<endl;
    }
    ~Desk()
    { 
       
        cout<<"Desk析构"<<endl;
    }
};

class Chair
{ 
       
public:
    Chair()
    { 
       
        cout<<"Chair的构造"<<endl;
    }
    ~Chair()
    { 
       
        cout<<"Chair的析构"<<endl;
    }
};

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

int main()
{ 
       
    Room room;
    cout << "Hello World!" << endl;
    return 0;
}

/* * 当类中有对象时,类内对象先构造,然后构造本对象,如果析构 * 则先析构本对象,再析构类内对象 * 当本类中有多个类成员对象时,构造的顺序时先构造类中的对象 * 构造类中的顺序与声明顺序相同,析构时相反。 */

#include <iostream>

using namespace std;

class Key
{ 
       
public:
    Key()
    { 
       
        cout<<"Key的构造"<<endl;
    }
    ~Key()
    { 
       
        cout<<"Key的析构"<<endl;
    }
};

class Mouse
{ 
       
public:
    Mouse()
    { 
       
        cout<<"Mouse的构造"<<endl;
    }
    ~Mouse()
    { 
       
        cout<<"Mouse的析构"<<endl;
    }
};

class Master
{ 
       
public:
    int weight;
};

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

int main()
{ 
       
    Computer computer;
    cout<<computer.getName()<<endl;
// computer.setName("Lenovo"); //返回常对象
    cout << "-----------------------------------" << endl;
    cout<<computer.setName("Lenovo").getName()<<endl;
    //常对象不能调用普通函数,常对象调用常函数,普通函数也能调用常对象
    return 0;
}



/* * 常对象与常函数 * 1、const修饰的类内对象 * 2、常对象用途:保护数据不可以随意改变 * 3、常函数在函数体内是不可以修改类中属性的,返回对象不再修改类中属性 * 常对象不能调用普通函数 * 常对象调用常函数 * 普通函数也能调用常对象 * 常函数不能修改类属性操作,只能访问,不能写入 */

#include <iostream>

using namespace std;

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

int Stu::amount = 20;
int main()
{ 
       
    Stu::getAmount();
    cout<<"-------------------------------"<<endl;
    Stu stu1("zhangsan", 10);
    Stu stu2("lisi", 20);
    cout<<stu1.getAmount()<<endl;
    cout<<stu2.getAmount()<<endl;
    cout << "Hello World!" << endl;
    return 0;
}


/* * static修饰的变量,在静态区首先被加载,只开辟一块空间,所有动态区定义的对象共享这片空间 * static修饰的变量属于整个类,函数是本文件的全局函数,属于整个进程,类内可见 * * static修饰的变量或函数是属于整个进程,这个变量在类内,类外是不可见的 * static修饰的变量和函数是属于类的,是类层级的,可以在没有类对象时进行调用 * static修饰的函数是不可以访问类中非静态属性的,因为静态函数中是没有this指针的 * static修饰的变量是所有对象共享一块内存 * * 静态函数只能访问静态变量 */

#include <iostream>

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<<"工作赚了:"<<this->money<<endl;
    }
    friend void beautifulGril(Boy& _boy);
};


void beautifulGril(Boy& _boy)
{ 
       
    cout<<_boy.money<<endl;
}

int main()
{ 
       
    Boy zhangsan;
    zhangsan.work();
    beautifulGril(zhangsan);
    cout << "Hello World!" << endl;
    return 0;
}


/*C++中对友元有两种形式:友元函数,友元类 * 友元函数就是体现了这个全局函数与本类的亲密关系 * 友元函数可以不受访问权限约束。可以访问类内所有属性或方法 * */

#include <iostream>

using namespace std;

class Boy
{ 
       
private:
    string name;
public:
    Boy() = default;
    Boy(string name)
    { 
       
        this->name = name;
    }
    void hardWork()
    { 
       
        cout<<"写代码"<<endl;
    }
    friend class Gril; //友元类
};

class Gril
{ 
       
private:
    string name;
public:
    Gril() = default;
    Gril(string name)
    { 
       
        this->name = name;
    }
    void shopping()
    { 
       
        cout<<"买东西"<<endl;
    }
    void goShopping(Boy& boy, string shopping)
    { 
       
        cout<<boy.name<<"正陪着"<<this->name<<"逛"<<shopping<<endl;
    }
};

int main()
{ 
       
    Boy zhangsan("张三"); //友元是不能传递的,但能相互,份文件编程
    Gril xiaoli("小丽");
    xiaoli.goShopping(zhangsan, "家乐福");
    cout << "Hello World!" << endl;
    return 0;
}

#include <iostream>

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的=运算符重载函数"<<endl;
        return *this;  //返回本对象
    }
};

int main()
{ 
       
    A a1;
    a1.setNumber(100);
    A a2;
    a2.setNumber(200);
    a1 = a2;  //将a2中的属性值赋值给a1
    cout<<a1.getNumber()<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

#include <iostream>

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()<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

#include <iostream>
#include <cstring>

using namespace std;

class myString
{ 
       
private:
    char* p_data;
public:
    myString()
    { 
       
        p_data = new char[0];
        p_data[0] = '\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] = '\0';
    }
    ~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<<str.p_data;
    return cout;
}

int main()
{ 
       
    myString str("zhangsan");
    cout<< str <<endl;
    cout<<str[8]<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

#include <iostream>
#include <unistd.h>
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
        标签: 6j20高温电阻合金丝材

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台