C++拷贝构造函数,继承,静态

在这里插入图片描述

一.拷贝构造函数

当将一个对象赋值给另外一个对象的时候,只会调用一次构造函数 而此时,会默认再调用一次拷贝构造函数(浅拷贝)

2.自己实现拷贝构造函数,称为深拷贝

// 拷贝构造函数
Student(Student &s)
{
    cout << "&Student" << endl;
} 

//----------------------------------------
demo:
    class Student
    {
    public:
        Student(const char *name)
        {
            cout << "Student" << endl;
            this->name = new char[100];
            strcpy(this->name,name);
            cout << "name = " << this->name << endl;
        }
        // 拷贝构造函数
        // s == Jack
        Student(Student &s)
        {
            cout << "&Student" << endl;
            this->name = new char[100];
            strcpy(this->name,s.name);
            cout << "name = " << this->name << endl;
        }

        void show()
        {
            cout << name << endl;
        }

        ~Student()
        {
            cout << "~Student" << endl;
            delete []this->name;
            this->name = NULL;
        }
    private:
        char *name;

    };
    Student Jack("Tom");
   // Student Rose = Jack;
    Student Rose(Jack);

    Jack.show();
    Rose.show();

总结:对象赋值的时候一定会调用默认拷贝构造函数,所以要自己实现深拷贝,防止内存泄漏 //--------------------------------

二.继承

人的类Person(属性:姓名 性别 年龄 行为:吃饭,睡觉)
学生的类Student(属性:学号 行为:学习)
学生 = 人类 + 学习
继承好处:
1.代码复用
2.升级功能
//---------------------------

继承格式

 继承方式 : public protected private
    class 子类名 :继承方式 父类类名
    {

    }  

继承方式总结:

1.类外访问父类成员
1.子类以公有方式(public)继承父类 子类对象可以访问父类的公有成员

2.子类以保护方式(protected)继承父类 子类对象不可以访问父类的公有成员

3.子类以私有方式(private)继承父类 子类对象不可以访问父类的公有成员

所以所的继承方式都是以公有继承 //------------------------------------

2.子类,类内访问父类成员

1.父类的成员是私有(private)属性 子类无法访问
2.父类的成员是受保护(protected)属性 子类可以访问
3.父类的成员是公有(public)属性 子类可以访问 //------------------------------------

3.子类继承父类后,先调用父类构造函数再调用子类构造函数
4.子类与父类都有相同的方法,会默认调用子类的方法
5.子类与父类方法不能重载,重载只能在同一个类里面
6.子类与父类都有相同的方法,如果想要调用父类的方法 子类对象.父类名::方法(); jack.Person::show(10);

练习:自行设计子类和父类,进行测试

        #include <iostream>
        using namespace std;

        // 父类(基类)
        class Person
        {
        public:
            Person()
            {
                cout << "person" << endl;
            }
            ~Person()
            {
                cout << "~Perosn" << endl;
            }

            void show(int a)
            {
                cout << "person show" << endl;
                cout << a << endl;
            }
        public:
            int sex;
        protected: 
            int age;
        private:
            string name;
            
        };
        // 子类(派生类)
        class Student : public Person
        {
        public:
            Student()
            {
                cout << "student" << endl;
                //string Name = name;
                int Age = age;
                int Sex = sex;
            }
            ~Student()
            {
                cout << "~student" << endl;
            }
            void show()
            {
                cout << "student show" << endl;
            }
        private:
            int number;    
        };

        int main()
        {
            Student jack;
            // 子类可以调用父类的方法
            jack.show();
            jack.Person::show(10);
        }

2.父类构造函数带参

1.父类构造函数参数列表初始化

Person(string name,int age,char sex):name(name),age(age),sex(sex)
{
	cout << "person" << endl;
	cout << this->name << " " << this->age << " " << this->sex << endl;
}
2.子类传参给父类
Student(string name,int age,char sex) : Person(name,age,sex)
{
	cout << "student" << endl;
	//string Name = name;
	int Age = age;
	int Sex = sex;
}

练习 : 学生类继承父类 学生类给父类传递参数,父类把参数打印出来,父子类构造函数类外实现
//--------------------------------

    demo:
            #include <iostream>
            using namespace std;

            // 父类(基类)
            class Person
            {
            public:
                Person(string name,int age,char sex);
                
                ~Person()
                {
                    cout << "~Perosn" << endl;
                }

                void show(int a)
                {
                    cout << "person show" << endl;
                    cout << a << endl;
                }
            public:
                char sex;
            protected: 
                int age;
            private:
                string name;
                
            };
            // 父类构造函数实现
            Person::Person(string name,int age,char sex):name(name),age(age),sex(sex)
            {
                cout << "person" << endl;
                cout << this->name << " " << this->age << " " << this->sex << endl;
            }

            //----------------------------------
            // 子类(派生类)
            class Student : public Person
            {
            public:
                Student(string name,int age,char sex);
                ~Student()
                {
                    cout << "~student" << endl;
                }
                void show()
                {
                    cout << "student show" << endl;
                }
            private:
                int number;    
            };

            //-------------------------------
            Student::Student(string name,int age,char sex) : Person(name,age,sex)
            {
                cout << "student" << endl;
                //string Name = name;
                int Age = age;
                int Sex = sex;
            }

            int main()
            {
                Student jack("Tome",18,'M');
                
            }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qt历险记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值