Where should we start thinking about this and how to write it correctly (language-c + +)

CSDN Q & A 2022-05-15 04:47:35 阅读数:368

startthinkingwritecorrectlylanguage-c

Define a Animal, This class can store the names of animals , And there's one Identity The function is used to display the species of animals . Define two classes Cat and Dog, Inherit from Animal, And overloading Identity function , Not only to show the species of animals , Also show the name of the animal .




Take the answer :
#include<iostream>using namespace std;class Animal {public: string name; string kind; Animal(string n, string k) { name = n; kind = k; } void Identity() { cout << " What kind of animal is it :" << "kind" << endl; }};class Cat : public Animal{ public: Cat(string n , string k) : Animal(n,k) { } void Identity() { cout <<" The animal's name yes :"<< name <<", What kind of animal is it :" << "kind" << endl; }};int main(){ Cat c(" cat ", " Cats "); c.Identity(); }

result :

img


I didn't quite understand the question , For reference , Hope to adopt, thank you


版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://primo.wiki/2022/131/202205041636084076.html