c++语法学习--类的继承

1.基础的继承规则:

摘自:https://www.cnblogs.com/metalsteel/p/6280389.html

其中,传入名字的代码,应该加const定义,别的部分没有变化。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

using namespace std;
class Parent
{
protected:
    char *str;
public:
    Parent(const char *stre)
    {
        if(str != NULL){
                //存储这堆中,在类销毁时需要手动释放。
            this->str = new char[strlen(str) + 1];
            strcpy(this->str, str);
        }
        else{
            this->str = NULL;
        }
        std::cout << "father create ... " << std::endl;
    }

    ~Parent()
    {
        if(this->str != NULL)
        {
                //delete 数组类型
            delete[] this->str;
            this->str = NULL;
        }
        std::cout << "father destory ... " << std::endl;
    }
};

class Object
{
private:
    int i;
public:
    Object(int i)
    {
        this->i = i;
        std::cout << "Object create ... " << std::endl;
    }
    ~Object()
    {
            //if need?
        this->i=0;
        std::cout << "Object destory " << std::endl;
    }
};

class Child:public Parent
{
private:
    Object obj;
    char *name;
    int age;
public:
        //在构造子类对象时,要先调用父类的构造函数
    Child():Parent(NULL),obj(10)
        {
            this->name = NULL;
            this->age = 0;
            std::cout <<"child  create, no para" << std::endl;
        }
    Child(const char *name, int age):Parent(name),obj(age)
        {
            this->name = new char [strlen(name) + 1];
            strcpy(this->name , name );
            std::cout << "child create, has para" << std::endl;
        }
    ~Child()
    {
            //c++ 判断字符串是否为空
            // != NULL or !this->name
        if(this->name != NULL)
            {
                delete [] this->name;
                this->name = NULL;
            }
        std::cout << "child destory " << std::endl;
    }
};


int main(int argc, char *argv[])
{
    Child c1;
        //类似这种固定的字符串,我们一定要定义成const, 不然会有warning。
        //char *name="wanggang";
    const char *name ="wanggang";
    Child c2(name, 22);

    return 0;
}

运行结果:

father create ... 

Object create ... 

child  create, no para

father create ... 

Object create ... 

child create, has para

child destory 

Object destory 

father destory ... 

child destory 

Object destory 

father destory ...


二、类的兼容性

class MyChild:public MyParent
{
protected:
    int i;
public:
    MyChild()
    {
        i=0;
        name = "I'am Child";
    }
};

int main(int argc, char *argv[])
{
    MyChild c;
    c.print();

    MyParent p1=c;
    p1.print();

    MyParent *p2 = &c;
    p2->print();

    MyParent &p3 = c;
    p3.print();

    return 0;
}

运行结果:

ame is:= I'am Child

name is:= I'am Child

name is:= I'am Child

name is:= I'am Child

解释:

  • 子类对象可以当做父类对象来使用。
  • 子类对象可以直接赋值给父类对象。
  • 子类对象可以直接初始化父类对象。
  • 父类指针可以直接指向子类对象。
  • 父类引用可以直接引用子类对象。

其实,个人理解就是包含关系,和防止空指针。

多重继承二义性:

#include <iostream>

using namespace std;

class B
{
public:
    int a;
};

class B1 : virtual public B
{

};

class B2 : virtual public B
{

};

class C :public B1, public B2
{

};

int main(int argc, char *argv[])
{
    C c;
        //error                                                                                                             
        //error: cannot use dot operator on a type                                                                          
        //c.B::a = 100;                                                                                                     
    c.B::a = 100;
    #if 1
    printf("C.B::a :=%d \n", c.B::a);
    printf("C.B1::a :=%d \n", c.B1::a);
    printf("C.B2::a :=%d \n", c.B2::a);
    #endif
}

运行结果:

C.B::a :=100 

C.B1::a :=100 

C.B2::a :=100



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++继承是一种面向对象编程的重要概念,它允许一个(称为派生或子)从另一个(称为基或父继承属性和方法。在继承,子可以重写父的方法,或添加自己的方法和属性。 下面是C++继承语法: ``` class DerivedClass : accessSpecifier BaseClass { // derived class members }; ``` 其,`DerivedClass`是派生的名称,`accessSpecifier`是访问修饰符,可以是`public`、`protected`或`private`,`BaseClass`是基的名称。 下面是一个简单的C++继承的实例,其`Animal`是基,`Dog`是派生: ``` #include <iostream> using namespace std; // 基 class Animal { public: void eat() { cout << "Animal can eat!" << endl; } }; // 派生 class Dog: public Animal { public: void bark() { cout << "Dog can bark!" << endl; } }; // 程序的主函数 int main() { // 创建一个对象 Dog d1; // 调用基的成员函数 d1.eat(); // 调用派生的成员函数 d1.bark(); return 0; } ``` 运行结果: ``` Animal can eat! Dog can bark! ``` 在上面的示例,`Dog`从`Animal`继承了`eat()`方法,同时添加了自己的`bark()`方法。在主函数,我们创建了一个`Dog`对象并调用了`eat()`和`bark()`方法,输出了预期的结果。 需要注意的是,派生不能直接访问基的私有成员,但可以通过基的公有和保护成员来访问。此外,派生可以重写基的方法,即在派生实现与基相同名称的方法,以覆盖基的实现。 以上就是C++继承语法和实例详解,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值