C++ 覆盖

本文通过C++代码展示了面向对象编程中的子类覆盖基类方法的概念。子类Pig和Turtle继承自Animal类,并重写了eat()方法,同时能够调用基类的eat()方法。在main()函数中,展示了如何实例化这些类并调用它们特有的行为,如Pig的climb()和Turtle的swim(),以及它们各自覆盖后的eat()方法。

覆盖:子类对基类的方法进行覆盖,但是能够调用基类的方法,且子类能够添加自己的内容

#include <iostream>
#include <string>

class Animal
{
public:
    Animal(std::string theName);
    void eat();
    void sleep();

private:
    std::string name;
};

class Pig:public Animal
{
public:
    Pig(std::string theName);
    void climb();
    void eat(); //不同动物吃不一样,在各个动物类下,进行方法的重新声明
};

class Turtle:public Animal
{
public:
    Turtle(std::string theName);
    void swim();
    void eat();
};

Animal::Animal(std::string theName)
{
    name = theName;
}

void Animal::eat()
{
    std::cout<<"基类的吃eating"<<std::endl;
}

void Animal::sleep()
{
    std::cout<<"sleeping"<<std::endl;  
}
//______________________
Pig::Pig(std::string theName):Animal(theName)
{

}
void Pig::climb()
{
    std::cout<<"小猪爬树"<<std::endl;
}

void Pig::eat()//子类覆盖了基类的方法
{
    Animal::eat();//前面声明过,可直接调用
    std::cout<<"子类覆盖了基类的吃,小猪吃鱼"<<std::endl;
}
//____________________
Turtle::Turtle(std::string theName):Animal(theName)
{

}

void Turtle::swim()
{
    std::cout<<"甲鱼游泳"<<std::endl;
}
void Turtle::eat()
{
    Animal::eat();//前面声明过,可直接调用基类的吃
    std::cout<<"甲鱼吃东西"<<std::endl;//子类覆盖了基类的方法
}

int main()
{
    Pig pig("小猪");
    Turtle turtle("甲鱼");
    turtle.eat();
    pig.climb();
    turtle.swim();
    pig.eat();
    return 0;
}

 

在 Visual Studio 社区版中使用 C++ 进行代码覆盖率检测,可参考以下思路。虽然没有直接针对社区版的完整覆盖说明,但结合相关信息可构建方法。 首先,对于代码覆盖率检测的原理和作用,Visual Studio Enterprise 中的代码覆盖率功能可发现测试需要改进的地方,使软件更健壮可靠,社区版也有类似的意义和价值,可借鉴这一理念来推动测试完善 [^1]。 在操作层面,可参考 C++ 代码覆盖率检测的脚本示例。下面是一个示例脚本,展示了对被测 DLL 进行插桩、启动覆盖率数据监控和收集、构建项目以及运行测试的过程: ```batch @set localFolder=D:\shary\NCLIB_ALL\bin\ @set nclibFolder=\\192.168.1.110\test\NCLIB\nmc\20170918_3728 @set codetrunkpath=%localFolder%/../ @set VStools="E:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Performance Tools\x64" @if exist %localFolder%\unittest.coverage del %localFolder%\unittest.coverage @copy %nclibFolder% %localFolder% /y @cd /d %VStools% REM 对被测的DLL进行插桩 vsinstr /coverage "%localFolder%\nclib.dll" REM 启动coverage数据监控和收集 vsperfcmd /start:coverage /output:%localFolder%\unittest.coverage @goto MSBUILD :MSBUILD call "%VS140COMNTOOLS%..\..\VC\vcvarsall.bat" amd64 msbuild %codetrunkpath%Test\NCLibTestFramework\code\NCLibUnitTest.sln /t:rebuild /p:Configuration=Release /p:platform=x64 /m %codetrunkpath%bin\NCLibUnitTest.exe --gtest_filter=*regression* > %codetrunkpath%NCLIB.log ``` 在使用这个脚本时,需要根据实际情况修改路径,确保路径指向正确的项目文件和工具位置 [^4]。 同时,Visual Studio 对 C++ 提供了深度优化的编辑器体验,如智能感知、代码补全、自动类型推断和代码分析工具,以及对现代 C++ 标准(C++11 及以上)的全面支持,这些功能有助于编写高效且可维护的代码,为代码覆盖率检测提供良好的代码基础 [^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

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

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

打赏作者

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

抵扣说明:

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

余额充值