• 个人简介

    PING-PONG [杀不死我们的终会让我们 更加强大—— (SUN)我爱乒乓球 。只要心向太阳,哪里都是太阳——(KYM) ] 1. #include using namespace std; int main(){ int n; cin>>n; if (n%4000) { cout<<"yes"<<endl; } else if ((n%40)&&(n%100!=0)){ cout<<"yes"<<endl; } else cout<<"no"<<endl; return 0; } //简单版,全功能版在后面 #include #include // 基类 Animal class Animal { protected: std::string name; // 动物的名字 public: // 构造函数 Animal(const std::string& n) : name(n) {} // 打印动物名字的函数 virtual void printName() const { std::cout << "动物的名字是: " << name << std::endl; } // 动物通常有叫的功能 virtual void makeSound() const { std::cout << "动物在叫:汪汪汪" << std::endl; } // 析构函数 virtual ~Animal() {} };

    // 派生类 Human class Human : public Animal { public: // 构造函数 Human(const std::string& n) : Animal(n) {}

    //重写父类的printName()函数,输出人类的名字 void printName() const override{ std::cout << "人的名字是: " << name << std::endl; }

    // 人类特有的行为:说话 void speak() const { std::cout << "人类在说话:你好" << std::endl; }

    // 重写父类的 makeSound 函数 // 人类也有叫的功能,但叫声与动物不同 void makeSound() const override { std::cout << "人类在叫:啊啊啊" << std::endl; }

    // 析构函数 ~Human() override {} };

    int main() { // 创建一个动物对象 Animal* a = new Animal("小狗"); a->printName(); a->makeSound();

    // 创建一个人类对象 Human* h = new Human("小明"); h->printName(); h->makeSound(); h->speak();

    // 清理内存 delete a; delete h;

    return 0; } #include #include using namespace std; class Person { public: string name; int age,id; public: void show(){ cout<<"[name="<<name<<",age="<<age<<",id="<<id<<"]"<<endl; } }; int main() { Person p= {"zhangfs",12,1}; p.show(); return 0; } /* 1.类的一个特征就是封装,public和private作用就是实现这一目的。所以: 用户代码(类外)可以访问public成员而不能访问private成员;private成员只能由类成员(类内)和友元访问。 2.类的另一个特征就是继承,protected的作用就是实现这一目的。所以: protected成员可以被派生类对象访问,不能被用户代码(类外)访问。 3.深入理解C++中public、protected及private用法,访问以下链接 https://zhuanlan.zhihu.com/p/70758317 */ //全功能版 #include #include using namespace std; // 一、类是一个蓝图,用于创建具有相似特征和行为的对象 class Animal { //创建动物类Animal protected: // 受保护成员 - 只有自己和子类可以访问 string name; // 动物的名字 private: // 私有成员 - 外部不能直接访问 int age; // 动物的年龄 public: // 公有成员 - 外部可以访问 // 构造函数 - 创建对象时自动调用 Animal(const string& n, int a) : name(n), age(a) { cout << "创建了一个Animal对象,名字:" << name << endl; }

    //二、理解成员函数(方法) // 1. 普通成员函数 void introduce() const { cout << "我是一只动物,名字叫" << name << ",今年" << age << "岁" << endl; }

    // 2. 虚函数 - 可以在子类中被重写(多态的基础) virtual void makeSound() const { cout << name << "在叫:汪汪汪" << endl; }

    //三、理解访问器(getter)和修改器(setter) // 获取名字 string getName() const { return name; }

    // 设置名字 void setName(const string& newName) { name = newName; cout << "动物改名为:" << name << endl; }

    // 获取年龄 int getAge() const { return age; }

    // 四、理解析构函数 - 对象销毁时自动调用 virtual ~Animal() { cout << "Animal对象" << name << "被销毁" << endl; } };

    // 五、理解继承 - Human继承自Animal class Human : public Animal {//创建类 人类Human,Human继承自Animal private: string occupation; // 人类特有的属性:职业 public: // 派生类构造函数 - 先调用基类构造函数 Human(const string& n, int a, const string& occ) : Animal(n, a), occupation(occ) { cout << "创建了一个Human对象,职业:" << occupation << endl; }

    //六、理解函数重写(override) void makeSound() const override { //覆盖了父类的函数makeSound() cout << name << "在叫:啊啊啊" << endl; }

    // 七、理解派生类特有的功能 void speak() const { cout << name << "在说话:你好!我是" << occupation << endl; }

    void work() const { cout << name << "正在工作:编程中..." << endl; }

    // 八、理解方法重载(同一个类中的同名函数,参数不同) void speak(const string& content) const { cout << name << "说:" << content << endl; }

    // 析构函数 ~Human() override { cout << "Human对象" << name << "被销毁" << endl; } };

    int main() { cout << "=== 面向对象编程基础教程 ===" << endl << endl;

    cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; } 2273 已递交 2265 已通过 0 题解被赞 题目标签 基础语法749AtCoder358ABC349算法竞赛进阶指南250字符串227其他178动态规划142搜索131二维数组130数组问题126数据结构126循环118一维数组115模拟115递归99分支99顺序87贪心83dp82数论81 状态 评测队列 服务状态 开发 开源 支持 帮助 联系我们 关于 关于 隐私 服务条款 版权申诉 Language 兼容模式 主题 程序:程序是为解决一个信息处理任务而预先编制的工作执行方案,是由一 串 CPU 能够执行的基本指令组成的序列,每一条指令规定了计算机应进行什么 操作(如加、减、乘、判断等)及操作需要的有关数据。例如,从存储器读一个 数送到运算器就是一条指令,从存储器读出一个数并和运算器中原有的数相加也 是一条指令。 C++的程序是结构化程序设计语言。结构化程序包括顺序、选择、循环三种 控制结构。顺序结构就是按照语句的先后顺序依次执行程序的方式。 最简单的程序:输出:Hello world! 参考程序: #include //以符号“#”开头的行称为编译预处理行 using namespace std; //为变量名开辟内存空间 int main() //主程序 { //程序开始 cout<<"Hello word!"; //输出”Hello word!” return 0; //结束 } //程序结束 例:计算正方形的面积和周长 【问题分析】:正方形的面积等于边长乘以边长,周长等于 4 倍的边长。 【算法设计】:正方形的边长用 a 表示,面积用 s 表示,周长用 c 表示,则 s=aa,c=4a。 参考程序: #include using namespace std; int main() { int a,s,c; 一、循环结构功能介绍 计算机在现在社会、生活中应用得非常广泛,之所以有这样强大的功 能,在很大程度上依靠程序语言中的三大结构来实现(顺序结构、选择结 构、循环结构),在三大结构中,循环结构是能够在满足一定条件下使某 段程序重复执行的结构,被重复执行的代码段称为循环体;这样程序能够 让我们从重复的劳动中解脱出来。 循环结构是整个程序设计的重中之重,该部分在整个程序设计中起到 一个枢纽作用,从一定意义上讲,该部分知识掌握的好与坏,将直接影响 对整个程序设计语言的把握。 在 C++语言中,循环有三种结构:for 循环、while 循环、do-while 循 环,本节重点介绍 for 循环结构。 二、for 循环语句格式: for(初始条件表达式;结束条件表达式;循环变量递增/递减表达式) { 循环体; #include #include #include using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; if (a>b)swap(a,b); if (a>c)swap(a,c); if (b>c)swap(b,c); if (a+b>c&&a+c>b&&c+b>a){ if (aa+bbcc) cout<<"Right triangle"<<"\n"; if (aa+bb>cc) cout<<"Acute triangle"<<"\n"; if (aa+bb<c*c) cout<<"Obtuse triangle"<<"\n"; if (ab||ac||bc) cout<<"Isosceles triangle"<<"\n"; if (ab&&bc) cout<<"Equilateral triangle"<<"\n"; } else cout<<"Equilateral triangle"; return 0; } For 语句是循环控制结构中使用最广泛的一种循环控制语句,特别适合已知循 环次数的情况。 例 1:利用 for 循环,计算输出 1+2+…+100 的和 【问题分析】:单纯的 100 个数相加问题。操作数在变,而操作符“+”不变。 【算法设计】:第一个数是 1,最后一个是 100,循环次数为 100 次。 法一: #include using namespace std; int main () { int sum=0; for (int i=1; i<=100 ; ++i) sum+=i;//sum=sum+i; cout << sum; return 0; } 法二: #include using namespace std; int main() { int sum=0,i; for (i=1;i<=100;i++) sum+=i; printf("sum=%d",sum); return 0; } 1、语句格式 2 格式 1 说明:语句 1 是 for 循环语句的循环体,它将在满足条件的情况下被重复执行。 格式 2 说明:循环体部分由多个语句构成,应由一对花括号括起来,构成一个语句块 的形式程序风格提示:写 for 循环语句时,循环体的语句相对于 for 缩进两格。 二、语句执行过程 for 语句的执行过程可由以下 4 步来描述。 (1)执行“控制变量初始化语句”,使控制变量获得一个初值。 (2)判断控制变量是否满足“条件表达式”,若满足条件则执行一遍循环体,否则结 束整个 for 语句,继续执行 for 循环下面的句子。 (3)根据增量表达式,计算出控制变量所得到的新值 (4)自动转到第(2)步。 下面以整型变量 i 和 j 为例,说明 for 语句的执行情况 For (i=1;i<=5;I++) for (j=5;j>=1,j--) Cout<<i<<" "; cout<<j<<" "; 运行结果为: 运行结果为: 1 2 3 4 5 5 4 3 2 1 例 2:编写程序,按字母的顺序和逆序输出字母。 【问题分析】:字母顺序为 abc…xyz,逆序为 zyx…cba。 【算法设计】:循环变量为字符型,从’a’到’z’每次加 1;逆序循环变量从’z’到’a’, 每次减 1。 3 参考程序: 法一: #include using namespace std; int main() { char letter; for (letter='a';letter<='z';letter++) printf("%c ",letter); printf("\n"); 第一部分 基础算法 第1章 贪心算法 第2章 二分与三分 第3章 深搜的剪枝技巧 第4章 广搜的优化技巧 第二部分 字符串算法 第1章 哈希和哈希表 第2章 KMP算法 第3章 Trie字典树 第4章 AC自动机 第三部分 图论 第1章 小生成树 第2章 短路 第3章SPFA算法的优化 第4章 差分约束系统 第5章 强连通分量 第6章 割点和桥 第7章 欧拉回路 第四部分 数据结构 第1章 树状数组 第2章 RMQ问题 第3章 线段树 第4章 倍增求LCA 第5章 树链剖分 第6章 平衡树Treap 第五部分动态规划 第1章 区间类动态规划 第2章 树型动态规划 第3章 数位动态规划 第4章 状态压缩类动态规划 第5章 单调队列优化动态规划 第6章 斜率优化动态规划 第六部分 数学基础 第1章 快速幂 第2章 质数 第3章 约数 第4章 同余问题 第5章 矩阵乘法 第6章 组合数学 第7章 博弈论

    89 已递交 73 已通过 0 题解被赞 题目标签 基础语法48分支22顺序16分支问题13顺序结构11基础问题11循环9简单循环7入门6ABC6AtCoder6NOIP320152市赛2T12山东小学组220171递归1数论1素数判定1 cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0; cout << "【第1部分:创建和使用对象】" << endl; cout << "------------------------" << endl; Animal dog("旺财", 3); dog.introduce(); dog.makeSound();

    cout << "\n【第2部分:使用getter和setter】" << endl; cout << "------------------------" << endl; cout << "当前名字:" << dog.getName() << endl; dog.setName("小黄"); cout << "修改后名字:" << dog.getName() << endl;

    cout << "\n【第3部分:继承和多态】" << endl; cout << "------------------------" << endl; Human xiaoming("小明", 20, "程序员"); xiaoming.introduce(); // 继承自Animal的方法 xiaoming.makeSound(); // 重写的方法 xiaoming.speak(); // Human特有的方法 xiaoming.work();

    cout << "\n【第4部分:方法重载】" << endl; cout << "------------------------" << endl; xiaoming.speak("今天天气真好!");

    cout << "\n【第5部分:使用指针和动态内存】" << endl; cout << "------------------------" << endl; Animal* animalPtr = new Animal("小猫", 2); Human* humanPtr = new Human("小红", 18, "学生");

    cout << "\n【第6部分:多态演示】" << endl; cout << "------------------------" << endl; // 使用基类指针指向派生类对象 Animal* ptr = humanPtr; cout << "通过基类指针调用makeSound():" << endl; ptr->makeSound(); // 调用的是Human版本的makeSound

    cout << "\n【第7部分:清理内存】" << endl; cout << "------------------------" << endl; delete animalPtr; delete humanPtr;

    cout << "\n=== 程序结束,自动调用析构函数 ===" << endl; // dog和xiaoming对象会在main函数结束时自动销毁

    return 0;

  • 通过的题目

  • 最近活动

题目标签

基础语法
64
顺序
24
分支
19
循环
18
基础问题
15
顺序结构
14
分支问题
13
简单循环
11
嵌套循环
7
字符串
6
入门
5
需要找规律的循环
5
ABC
5
AtCoder
5
递归
2
函数
2
数组问题
2
T2
2
普及组一阶测试题
2
2010
1