首页 > 编程语言 >c++ class 和继承

c++ class 和继承

时间:2024-07-21 22:42:27浏览次数:11  
标签:std const name 继承 param c++ Animal class


c++ class 和继承     #include <iostream> #include <string>   // 基类 class Animal { protected: std::string name; public: Animal(const std::string& name_param) : name(name_param) {} virtual void speak() const = 0; // 纯虚函数,派生类需要实现 };   // 派生类 class Dog : public Animal { public: Dog(const std::string& name_param) : Animal(name_param) {} void speak() const override { std::cout << "Woof! My name is " << name << std::endl; } };   // 派生类 class Cat : public Animal { public: Cat(const std::string& name_param) : Animal(name_param) {} void speak() const override { std::cout << "Meow! My name is " << name << std::endl; } };   int main() { Dog dog("Rex"); Cat cat("Whiskers");   dog.speak(); // 输出: Woof! My name is Rex cat.speak(); // 输出: Meow! My name is Whiskers   return 0; }

这段代码展示了如何在C++中定义一个抽象基类Animal和两个派生类DogCat。这些类都有一个共同的接口speak(),并且通过构造函数传递名字。这是面向对象编程中的一个常见模式,也展示了如何使用继承和多态。

标签:std,const,name,继承,param,c++,Animal,class
From: https://www.cnblogs.com/sexintercourse/p/18315069

相关文章

  • C++标准模板(STL)- 概念库 (C++20) - 指定一个类型派生自另一类型 - (std::derived_from)
    概念库提供基础语言概念的定义,它们能用于进行模板实参的编译时校验,以及基于类型属性的函数派发。这些概念在程序中提供等式推理的基础。标准库中的大多数概念一同加上了语法及语义要求。通常,编译器只能检查语法要求。若在使用点语义要求未得到满足,则程序为病式,不要求诊断。......
  • C++11(2): 通用
    31.继承构造函数structB:A{usingA::A;};32.委派构造函数classA{public: A() :A(1234){}//先调用A(int) A(intn1):A(n1,'C'){}//先调用A(int,char) A(charc1):A(2,c1){}//先调用A(int,char)private: A(intn2,charc2):my_......
  • c++ 引用和指针
      c++引用和指针在C++中,引用和指针是两个非常重要的概念,它们有一些相似之处也有一些不同之处。相似之处:引用和指针都可以指向一个对象。引用一旦被初始化指向一个对象后,就不能再指向其他对象,而指针可以在任何时候指向其他对象。不同之处:引用在......
  • 快速完成VsCode C/C++开发环境配置
    前言本人在配置VsCodeC++开发环境时,查看了很多的博客,内容参差不齐,尤其是关于json文件的配置,绕的人头很晕,最终还是通过阅读官方文档,结合部分博客的指引,完成了环境的配置,在此记录本人的配置过程,希望能帮助到大家。事先声明,本文的内容大量引自VsCode官方的文章:https://code.visual......
  • OpenCV 遍历Mat,像素操作,使用TrackBar 调整图像的亮度和对比度 C++实现
    文章目录1.使用C++遍历Mat,完成颜色反转1.1常规遍历方式1.2迭代器遍历方式1.3指针访问方式遍历(最快)1.4不同遍历方式的时间对比2.图像像素操作,提高图像的亮度3.TrackBar进度条操作3.1使用TrackBar调整图像的亮度3.2使用TrackBar调整图像的对比度1.使用C++遍历M......
  • C++吃豆人~
    #include<cstdio>#include<iostream>#include<ctime>#include<conio.h>#include<windows.h>#include<cstdlib>#include<cstring>usingnamespacestd;constintn=809;structPoint{intx,y;};intdali;intfx[......
  • C++小游戏
    #include<iostream>usingnamespacestd;doubleshengmingli=2000;//定义主角初始生命力intgongjili=150;//定义主角初始攻击力intfangyuli=200;//定义主角初始防御力intmoney=20;//定义主角初始金钱数量boolguoguan;//定义是否通关判定voidwuqidian();//定义武器......
  • c++的一些科技
    pd_ds需要#include<bits/extc++.h>usingnamespace__gnu_pbds;usingnamespace__gnu_cxx__gnu_pbds::priority_queue注意可能会与std::priority_queue冲突。定义方法:__gnu_pbds::priority_queue<T,Compare,Tag,Allocator>T:类型名Compare:严格弱化的比较类型......
  • C++合作开发项目:美术馆1.0
    快乐星空MakerZINCFFO合作入口:CM工作室效果图:代码:(还有几个音乐!)main.cpp#include<bits/stdc++.h>#include<windows.h>#include<conio.h>#include<time.h>#include"music.h"usingnamespacestd;structCITYBLOCK{ stringi......
  • LeetCode题(66,69,35,88)--《c++》
     66.加一////Createdbywxj05on2024/7/20.////法一classSolution{public:vector<int>plusOne(vector<int>&digits){boolcarry=true;//进位标志for(inti=digits.size()-1;i>=0&&carry;--i){......