C++ 第五讲

1.C++ 继承过程中特殊成员函数

#include <iostream>

using namespace std;

class Father
{
protected:
    string name;

public:
    //无参构造
    Father();
    //有参构造
    Father(string n);
    //拷贝构造
    Father(const Father &other);
    //拷贝赋值函数
    Father & operator= (const Father& other);
    //析构函数
    ~Father();
};

class Son : public Father
{
private:
    string toy;

public:
    //无参构造
    Son();
    //有参构造
    Son(string n,string t);
    //拷贝构造
    Son(const Son &other);
    //拷贝赋值
    Son &operator = (const Son &other);
    //析构函数
     ~Son();
    //输出
    void show();

};


int main()
{
    cout << "************ s1" << endl;
    Son s1; // 无参构造
    s1.show();

    cout << "************ s2" << endl;
    Son s2("zhangsan", "air"); //有参构造
    s2.show();

    cout << "************ s3" << endl;
    Son s3(s2);// 拷贝构造
    s3.show();

    cout << "************ s1" << endl;
    s1 = s3; //拷贝赋值
    s1.show();

    return 0;
}





Father::Father() {
    cout << "Fa no have" << endl;
}

Father::Father(string n)
    :name(n){
    cout << "Fa have" << endl;
}

Father::Father(const Father &other)
    :name(other.name){
    cout << "Fa copy gouzao" << endl;
}

Father &Father::operator=(const Father &other){
    if(this != &other){
        this->name = other.name;
    }
    cout << "Fa copy fuzhi" << endl;
    return *this;
}

Father::~Father(){
    cout << "Fa destroy" << endl;
}

Son::Son() {
    cout << "Son no have" << endl;
}

Son::Son(string n, string t)
    :Father(n),
      toy(t){
    cout << "Fa have" << endl;
}

Son::Son(const Son &other)
    :Father(other),
      toy(other.toy){
    cout << "Son copy gouzao" << endl;
}

Son &Son::operator =(const Son &other){
    if(this != &other){
        this->toy = other.toy;
        Father::operator=(other);
    }
    cout << "Son copy fuzhi" << endl;
    return *this;
}

Son::~Son() {
    cout << "Son destroy" << endl;
}

void Son::show(){
    cout << "name = " << name << "toy = " << toy << endl;
}

2.父类,子类练习

#include <iostream>

using namespace std;

class A
{
public:
    int num = 520;
    double score = 100;
    void show(){
        cout << "num = " <<num << "  score =  " << score << endl;
    }
};

class B:public A{
public:
    int key = 123456;
    //A a;
    int num = 1314;
    void show(){
        cout << "num = " <<num << "  score =  " << score <<"  key = " << key << "  A::num = " <<A::num << endl;
    }

};


int main()
{
    B t;
    cout << "t.num = " << t.num<< endl;
    cout << "t.A::num = " << t.A::num << endl;

    t.show();
    t.A::show();


    //cout << "&t.a = " << &t.a << endl;
    //cout << "&t.a.num = " << &t.a.num << endl;



    return 0;
}

3.父类,子类地址验证

#include <iostream>

using namespace std;

class A
{
public:
    int num;
    double score;
};

class B:public A{
public:
    int key;
    A a;
};


int main()
{
    B t;
    cout << "&t = " << &t<< endl;
    cout << "&t.key = " << &t.key << endl;
    cout << "&t.a = " << &t.a << endl;
    cout << "&t.a.num = " << &t.a.num << endl;



    return 0;
}

4.静态成员函数练习

#include <iostream>

using namespace std;

// 父类
class Hero
{
public:
    string name;
protected:
    int Hp;
private:
    int speed;

public:
    Hero();
    Hero(string n, int h, int s);
    ~Hero();
};

// 子类
class Magic :public Hero
{
private:
    int ap;

public:
    Magic();
    Magic(string n,int h, int s, int a);
    void show();
    ~Magic();
};






int main()
{
    Magic m1("妲己", 2000,100,50);
    m1.show();
    cout << "name = " << m1.name << endl;
    return 0;
}







//父类无参
Hero::Hero() {
    cout << " Hero not have" << endl;
}
//父类有参
Hero::Hero(string n, int h, int s)
    :name(n),
      Hp(h),
      speed(s){
    cout << " Hero have" << endl;
}
//父类析构
Hero::~Hero() {
    cout << " Hero destroy" << endl;
}


//子类无参
Magic::Magic() {
    cout << " Magic not have" << endl;
}
//子类有参
Magic::Magic(string n,int h, int s, int a)
    :Hero(n,h,s),
    ap(a){
    cout << " Magic  have" << endl;
}

//子类析构
Magic::~Magic() {
    cout << " Magic destroy" << endl;
}

//子类输出
void Magic::show(){
    cout << "name = " << name << endl;
    cout << "Hp = " << Hp << endl;
    //cout << "speed = " << speed << endl; 父类私有
    cout << "ap = " << ap << endl;
}

5.静态成员函数,静态成员变量练习

#include <iostream>

using namespace std;

class Stu
{
private:
    int age;
    double score;

public:
    static int num;
    Stu() {
        cout << "no" << endl;
    }
    Stu(double s, int a)
        :score(s),
        age(a)
    {
        cout << "have" << endl;
    }
    void show(){
        cout << "score = " << score << endl;
        cout << "age = " << age << endl;
        cout << "num = " << num << endl;
    }
    static void display(){
        //在静态成员函数中,不能使用非静态成员变量
        num = 999;
        cout << "num = " << num << endl;
    }


};

int Stu::num = 520;

int main()
{
    cout << "Stu::num = " << Stu::num << endl;
    Stu::num = 666;

    cout << "*****************" << endl;
    Stu s1(99.5,18);
    s1.show();
    cout << "sizeof s1 = " << sizeof(s1) << endl;

    cout << "*****************" << endl;
    Stu s2(66.6, 20);
    s2.show();

    cout << "*****************" << endl;
    s2.num = 1314;
    s1.show();

    cout << "__________________" << endl;
    Stu::display();


    s1.display();





    return 0;
}

6.静态局部变量练习

#include <iostream>

using namespace std;

class Stu
{
private:
    int age;
    double score;

public:
    static int num; //声明一个静态局部变量

    Stu() {
       cout << "无参构造" << endl;
    } //无参构造
    Stu(double s, int a):score(s),age(a){
        cout << "有参构造" << endl;
    }
    void show(){
        cout << "score = " << score << endl;
        cout << "age = " << age << endl;
        cout << "num = " << num << endl;
    }
};

int Stu::num = 520;


int main()
{
    cout << "Stu::num = " << Stu::num << endl;
    Stu::num = 666;

    cout << "*****************" << endl;
    Stu s1(99.5,18);
    s1.show();
    cout << "sizeof s1 = " << sizeof(s1) << endl;

    cout << "*****************" << endl;
    Stu s2(66.6, 20);
    s2.show();

    cout << "*****************" << endl;
    s2.num = 1314;
    s1.show();

    cout << "*****************" << endl;

    return 0;
}

思维导图

猜你喜欢

转载自blog.csdn.net/MaGuangming001/article/details/131839891
今日推荐