C++入门05 --命名空间,继承,访问权限,初始化列表

2021-08-12  本文已影响0人  YanZi_33

命名空间

//  main.cpp
//  C++12_命名空间
//
//  Created by ljj on 8/12/21.
//

#include <iostream>

using namespace::std;

void test(){
    cout << "test()" << endl;
}

namespace XY {
    //全局变量
    int g_no;
 
    class Person {
    public:
        int m_age;
    };
    
    void test(){
        cout << "XY test()" << endl;
    }
}

namespace YY {
    int g_no;

    class Person{
    public:
        int m_age;
    };

    void test(){
        cout << "XY test()" << endl;
    }
}

int main(int argc, const char * argv[]) {
    //使用XY命名空间中的定义
    XY::Person person1;
    person1.m_age = 10;
    
    //使用YY命名空间中的定义
    YY::Person person2;
    person2.m_age = 20;
    
    //注意是全局变量
    XY::g_no = 100;
    YY::g_no = 200;
    
    //函数调用
    test();
    XY::test();
    YY::test();
        
    return 0;
}
#include <iostream>

using namespace::std;

namespace XY {
    //全局变量
    int g_no;
 
    class Person {
    public:
        int m_age;
    };
    
    void test(){
        cout << "XY test()" << endl;
    }
}

int main(int argc, const char * argv[]) {
    
    //表明下面用到的特定元素 是来自XY的命名空间
    using namespace XY;
    Person person;
    person.m_age = 33;
    test();
    
    return 0;
}
#include <iostream>

using namespace::std;

namespace XY {
    //全局变量
    int g_no;
 
    class Person {
    public:
        int m_age;
    };
    
    void test(){
        cout << "XY test()" << endl;
    }
}

int main(int argc, const char * argv[]) {
    
    //表明下面用到的特定元素 是来自XY的命名空间
    using namespace XY;
    Person person;
    person.m_age = 33;
    test();
    g_no = 300;
    
    return 0;
}
#include <iostream>

using namespace::std;

namespace XY {
    //全局变量
    int g_no;
 
    class Person {
    public:
        int m_age;
    };
    
    void test(){
        cout << "XY test()" << endl;
    }
}

int main(int argc, const char * argv[]) {
    
    //Person与g_no是来自于命名空间XY
    using XY::Person;
    using XY::g_no;
    Person person;
    g_no = 30;
    
    return 0;
}
#include <iostream>

using namespace::std;

namespace XY {
    //全局变量
    int g_no;
 
    class Person {
    public:
        int m_age;
    };
    
    void test(){
        cout << "XY test()" << endl;
    }
}

namespace YY {
    int g_no;

    class Person{
    public:
        int m_age;
    };

    void test(){
        cout << "XY test()" << endl;
    }
}

int main(int argc, const char * argv[]) {
    
    using namespace XY;
    test();
    
    using namespace YY;
    test(); //报错
    
    return 0;
}
命名空间的嵌套
#include <iostream>

using namespace::std;

namespace AA {
    namespace BB {
        int g_num;
        class Cat {
        public:
            int m_age;
        };
    }
}

int main(int argc, const char * argv[]) {
    
    AA::BB::g_num = 300;
    
    AA::BB::Cat *c = new AA::BB::Cat();
    c->m_age = 20;

    using namespace AA::BB;
    g_num = 400;

    return 0;
}
命名空间的合并
namespace MM {
    int m_age;
}

namespace MM {
    int m_weight;
}

namespace MM {
    int m_age;
    int m_weight;
}

继承

#include <iostream>

using namespace::std;

struct Person {
    int m_age;
    
    void run(){
        cout << "run()" << endl;
    }
};

struct Student : Person{
    int m_score;
    
    void study(){
        cout << "study()" << endl;
    }
};

struct Worker : Person{
    int m_salary;
    
    void work(){
        cout << "work()" << endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Student student;
    student.m_age = 10;
    student.run();
    student.study();
    
    Worker work;
    work.m_age = 33;
    work.run();
    work.work();
    
    return 0;
}
对象的内存布局
#include <iostream>

using namespace::std;

struct Person {
    int m_age;
};

struct Student : Person {
    int m_no;
};

struct GoodStudent : Student{
    int m_money;
};


int main(int argc, const char * argv[]) {
    
    GoodStudent gs;
    gs.m_age = 20;
    gs.m_no = 1;
    gs.m_money = 2000;
    
    cout << "gs size = " << sizeof(gs) << endl;
    cout << &gs << endl;
    cout << &gs.m_age << endl;
    cout << &gs.m_no << endl;
    cout << &gs.m_money << endl;

    return 0;
}
Snip20210812_141.png

成员的访问权限

#include <iostream>

using namespace::std;

struct Person {
protected:
    int m_age;
    
    void run(){
        
    }
};

struct Student : Person {
    int m_no;
    
    void study(){
        this->m_age = 30;
    }
};

int main(int argc, const char * argv[]) {
    
    Person person;
    person.m_age = 30; //报错
    
    return 0;
}

初始化列表

#include <iostream>

using namespace::std;

struct Person {
    int m_age;
    int m_height;
    
    Person(){
        this->m_age = 0;
        this->m_height = 0;
    }
    
//    Person(int age,int height){
//        this->m_age = age;
//        this->m_height = height;
//    }
    
    Person(int age,int height) :m_age(age),m_height(height){
        
    }    
    
    void display(){
        cout << "m_age = " << this->m_age << endl;
        cout << "m_height = " << this->m_height << endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Person person1;
    person1.display();

    cout << "-----------" << endl;
    
    Person person2(10,20);
    person2.display();
    
    
    return 0;
}

构造函数的相互调用

#include <iostream>

using namespace::std;

struct Person {
    int m_age;
    int m_height;
    
    Person(){
        //0x7ffeefbff3a8
        cout << "Person()" << this << endl;
        //直接调用构造函数,又会创建一个对象
        Person(1,2);
    }
    
    Person(int age,int height){
        //0x7ffeefbff360
        cout << "Person(int age,int height)" << this << endl;
        this->m_age = age;
        this->m_height = height;
    }
    
    void display(){
        cout << "m_age = " << this->m_age << endl;
        cout << "m_height = " << this->m_height << endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Person person1;
    person1.display();
    
    return 0;
}
Snip20210812_142.png
#include <iostream>

using namespace::std;

struct Person {
    int m_age;
    int m_height;
    
    //在初始化列表中 调用其他构造函数
    Person() :Person(1,2){
        cout << "Person()" << this << endl;
        //直接调用构造函数,又会创建一个对象
    }
    
    Person(int age,int height){
        cout << "Person(int age,int height)" << this << endl;
        this->m_age = age;
        this->m_height = height;
    }
   
    void display(){
        cout << "m_age = " << this->m_age << endl;
        cout << "m_height = " << this->m_height << endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Person person1;
    person1.display();
    
    return 0;
}
Snip20210812_143.png
初始化列表与默认参数
#include <iostream>

using namespace::std;

struct Person {
    int m_age;
    int m_height;
    
    Person(int age = 0,int height = 0) : m_age(age),m_height(height){
        cout << "Person(int age,int height)" << this << endl;
    }
    
    void display(){
        cout << "m_age = " << this->m_age << endl;
        cout << "m_height = " << this->m_height << endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Person person1;
    Person person2(100);
    Person person3(10,20);
    person1.display();
    person2.display();
    person3.display();
    
    return 0;
}
#include <iostream>

using namespace::std;

struct Person {
    int m_age;
    int m_height;

    
    Person(int age = 0,int height = 0);
    
    void display(){
        cout << "m_age = " << this->m_age << endl;
        cout << "m_height = " << this->m_height << endl;
    }
};

//默认参数只能写在函数声明
//初始化列表只能写在函数实现
Person::Person(int age,int height) : m_age(age),m_height(height){
    cout << "Person(int age,int height)" << this << endl;
}

int main(int argc, const char * argv[]) {
    
    Person person1;
    Person person2(100);
    Person person3(10,20);
    person1.display();
    person2.display();
    person3.display();

    return 0;
}

父类的构造函数

#include <iostream>

using namespace::std;

class Person {
    
public:
    Person(){
        cout << "Person()" << endl;
    }

    ~Person(){
        cout << "~Person()" << endl;
    }
};

class Student : Person {
        
public:
    Student(){
        cout << "Student()" << endl;
    }

    ~Student(){
        cout << "~Student()" << endl;
    }
};


class GoodStudent : Student {
    
public:
    GoodStudent(){
        cout << "GoodStudent()" << endl;
    }
};


int main(int argc, const char * argv[]) {
    
    GoodStudent gs;
    
    return 0;
}
Snip20210812_144.png
#include <iostream>

using namespace::std;

class Person {
    int m_age;
public:
    Person(){
        cout << "Person()" << endl;
    }
    
    Person(int age) : m_age(age){
        cout << "Person(int age)" << endl;
    }
    
};

class Student : Person {
    int m_score;
public:
    Student(){
        cout << "Student()" << endl;
    }
    //初始化列表 调用父类的构造函数(有参的)
    Student(int age,int score) : m_score(score),Person(age){
        cout << "Student(int score)" << endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Student student(10,100);
    
    return 0;
}
#include <iostream>

using namespace::std;

class Person {
    int m_age;
public:
    Person(int age) : m_age(age){
        cout << "Person(int age)" << endl;
    }
    
};

class Student : Person {
    int m_score;
public:
    //父类没有无参的构造函数;
    //子类的构造函数,必须调用父类的有参构造函数,否则报错;
    Student() : Person(0){
        cout << "Student()" << endl;
    }
    //初始化列表 调用父类的构造函数(有参的)
    Student(int age,int score) : m_score(score),Person(age){
        cout << "Student(int score)" << endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Student student();
    
    return 0;
}

父类指针,子类指针

#include <iostream>

using namespace::std;

class Person {
public:
    int m_age;
    
};

class Student : public Person {
public:
    int m_score;
    
};

int main(int argc, const char * argv[]) {
    
    //父类指针 指向子类对象
    Person *s = new Student();
    s->m_age = 30;
    
    //子类指针 指向父类对象
    Student *p = (Student *)new Person();
    
    return 0;
}
Snip20210812_145.png
上一篇 下一篇

猜你喜欢

热点阅读