当前位置: 首页 > news >正文

企业运营与发展形考作业答案网站seo诊断的主要内容

企业运营与发展形考作业答案,网站seo诊断的主要内容,手机网站的域名,中国网站优化公司c虚函数纯虚函数详解加代码解释 一.概念#xff1a;二.虚函数示例及解析#xff1a;三.纯虚函数示例及解析#xff1a;四.验证和实际使用及解析#xff1a;1.子类没有对父类的函数重载#xff0c;mian()函数调用#xff0c;是直接返回父类的值2.子类对父类的函数重载虚函数纯虚函数详解加代码解释 一.概念二.虚函数示例及解析三.纯虚函数示例及解析四.验证和实际使用及解析1.子类没有对父类的函数重载mian()函数调用是直接返回父类的值2.子类对父类的函数重载mian()函数调用是直接返回子类重载后的返回值3.子类对父类的函数重载但子类调用自己函数mian()函数调用时报错是因为类的定义指向父类重新定义子类父类中没有的子类也不能调用。 五.总结补充override关键字作用 一.概念 虚函数和纯虚函数是面向对象编程中的概念用于实现多态性和抽象类特性。 虚函数Virtual Function是在基中声明的函数通过在函数前面加上virtual关键字来标识。虚函数可以在派生类中被重写实现函数的多态性。在运行时根据对象的实际类型来确定调用哪个版本的虚函数。通过基类的指针或引用调用虚函数时会根据指针或引用所指向的对象类型来调用相应的函数。 纯虚函数Pure Virtual Function是在基类中声明的没有实际实现的函数通过在函数声明后面加上 0来标识。纯虚函数用于定义接口要求派生类必须实现函数。有了纯虚函数基类就成为了抽象类无法创建对象只能作为其他类的基类来派生出新的类。 纯虚函数的作用是实现接口的统一和规范确保派生类都实现了基类中定义的接口。纯虚函数可以在派类中重写也可以不重写如果某个派类重写纯虚函数那么该派生类也成为抽象类无法创建对象。 二.虚函数示例及解析 #include iostream using namespace std;class Animal { public:virtual void speak() {cout Animal speaks! endl;} };class Dog : public Animal { public:void override() {cout Dog barks! endl;} };class Cat : public Animal { public:void speak() {cout Cat meows! endl;} };int main() {Animal* animal1 new Dog();Animal* animal2 new Cat();animal1-speak(); // 输出Dog barks!animal2-speak(); // 输出Cat meows!delete animal1;delete animal2;return 0; }虚函数是在基类中声明的具有虚函数特性的函数通过在声明前加上键字virtual来标识。它允许在派生类中重写该函数实现多态性。在运行时根据对象的实际类型来确定调用哪个版本虚函数。 在上述中Animal类是基类其中的speak()函数被声明为虚Dog和Cat类是派生类它们分别重写了speak()函数。 在main()函数中我们通过基类指针分别创建了Dog和Cat对象并调用了speak()函数。由于speak()函数是虚函数所以在运行时会根据对象的实际类型来确定调用哪个版本的函数因此输出结果分别是Dog barks!“和Cat meows!”。 三.纯虚函数示例及解析 #include iostream using namespace std;class Shape { public:virtual double getArea() 0; };class Rectangle : public Shape { private:double length;double width; public:Rectangle(double len, double wid) : length(len), width(wid) {}double getArea() override {cout Rectangle endl;return length*width;} };class Square : public Shape { private:double length; public:Square(double len) : length(len){}double getArea() override {cout Square endl;return length * length;} };class Circle : public Shape { private:double radius;public:Circle(double r) : radius(r){}double getArea() override {cout Circle endl;return 3.14 * radius * radius;} };int main() {Shape* shape1 new Rectangle(5, 4);Shape* shape2 new Circle(3);Shape* shape3 new Square(5);cout Area of rectangle: shape1-getArea() endl;cout Area of circle: shape2-getArea() endl;cout Area of square: shape3-getArea() endl;delete shape1;delete shape2;delete shape3;return 0; }纯虚函数是在基类中声明的没有实际实现的函数通过函数声明后加上 0来标识。它用于定义接口求派生类必须实现该函数。 在上述代码中Shape类是基类其中的getArea()函数被声明为纯虚函数。RectangleCircle类是派生类它们必须实现getArea()函数。 在main()函数中我们通过基类指针分别创建了Rectangle和Circle并调用了getArea()函数。由于getArea()函数是纯虚函数所以基类Shape无法创建对象只能通过派生类来实现具体的功能因此输出结果分别是矩形的面积和圆的面。 四.验证和实际使用及解析 1.子类没有对父类的函数重载mian()函数调用是直接返回父类的值 #include iostream using namespace std;class Shape { public:virtual double getArea() 0;virtual double getperimeter() {cout demo test endl;return 5;}; };class Rectangle : public Shape { private:double length;double width; public:Rectangle(double len, double wid) : length(len), width(wid) {}double getArea() override {cout Rectangle endl;return length * width;} };int main() {Shape* shape1 new Rectangle(5, 4);cout Area of rectangle: shape1-getArea() endl;cout getperimeter of rectangle: shape1-getperimeter() endl;delete shape1;return 0; }2.子类对父类的函数重载mian()函数调用是直接返回子类重载后的返回值 #include iostream using namespace std;class Shape { public:virtual double getArea() 0;virtual double getperimeter() {cout demo test endl;return 5;}; };class Rectangle : public Shape { private:double length;double width; public:Rectangle(double len, double wid) : length(len), width(wid) {}double getArea() override {cout Rectangle endl;return length * width;}double getperimeter(){cout getperimeter:getperimeter endl;return length * 2 width * 2;} };int main() {Shape* shape1 new Rectangle(5, 4);cout Area of rectangle: shape1-getArea() endl;cout getperimeter of rectangle: shape1-getperimeter() endl;delete shape1;return 0; }3.子类对父类的函数重载但子类调用自己函数mian()函数调用时报错是因为类的定义指向父类重新定义子类父类中没有的子类也不能调用。 将Shape* shape1 new Rectangle(5, 4); 修改为Rectangle* shape1 new Rectangle(5, 4);我们才能调用子类中父类没有定义的函数 #include iostream using namespace std;class Shape { public:virtual double getArea() 0; };class Rectangle : public Shape { private:double length;double width; public:Rectangle(double len, double wid) : length(len), width(wid) {}double getArea() override {cout Rectangle endl;return length * width;}double getperimeter(){cout getperimeter:getperimeter endl;return length * 2 width * 2;} };int main() {Rectangle* shape1 new Rectangle(5, 4);cout Area of rectangle: shape1-getArea() endl;cout getperimeter of rectangle: shape1-getperimeter() endl;delete shape1;return 0; }五.总结 虚函数用于实现多性允许在派生类中重写基类的函数。 纯虚函数用于定义接口要求派生必须实现函数可以在派生类中重写或不重写。 补充override关键字作用 如果派生类在虚函数声明时使用了override描述符那么该函数必须重载其基类中的同名函数否则代码将无法通过编译
http://www.hkea.cn/news/14536452/

相关文章:

  • 招商加盟网站推广方案南阳网站营销外包
  • 自己的网站怎么做跳转网站开发简称
  • 加强网站建设管理办法网站后台管理系统的重要技术指标
  • 南京网站建设电话铁路网站建设
  • 心理健康网站建设论文网上的推广
  • 沐川移动网站建设石家庄网站模板建站
  • 网站跳转qq哪家建站公司好
  • 在linux上做网站搭建教育网站案例
  • 购物网站建立手机网页微信
  • 上海尚海整装官方网站网站建设服务哪个便宜啊
  • 余杭建设局网站微信小程序开发制作教程
  • wordpress站点目录晋江网络推广公司
  • 淘宝客网站做一种还是做好几种seo推广是什么
  • 大沥做网站东莞做网站seo优化
  • 江苏省 建设 注册中心网站首页域名收录查询
  • 网站建设价格表金融行业网站建设公司
  • 优秀企业网站的特点flash 网站欣赏
  • 微信上的小说网站是怎么做的佛山定制建站公司推荐
  • 游戏网站怎么做营销qq下载
  • 怎么创建卡密网站工商网核名查询
  • 做吃穿住行网站好品牌设计公司
  • 公司网站搭建费用哈尔滨市建设厅网站
  • 信息产业部网站备案做网站为什么赚钱
  • 专业微信网站建设1688官网首页官网
  • 博物馆门户网站建设方案天猫优惠卷怎么做网站
  • 济南建网站公司报价商城类网站和o2o网站
  • 大尺度做爰后入网站移动端app开发
  • 为什么网站建设要将access数据库文件变成asa网站需求建设书
  • 免费网站加速器建设软件资源网站
  • 建设网站的结束语广州企业注册一网通