首页 > 编程语言 >C++ 继承和派生的简单应用

C++ 继承和派生的简单应用

时间:2022-11-20 21:13:32浏览次数:37  
标签:string 派生 继承 age Father C++ Son include name

Father.h:

#pragma once
#include <iostream>
#include <string>
using namespace std;
class Father
{
public:
    Father();
    Father(const string &name,int age);
    string getName();
    int getAge();
    string description();
private:
    string name;
    int age;
};

Father.cpp:

#include "Father.h"
#include <string>
#include <sstream>
using namespace std;
Father::Father() {
    name = "小熊猫";
    age = 10;
}
Father::Father(const string& name, int age) {
    cout << __FUNCTION__ << endl;
    this->name = name;
    this->age = age;
}
string Father::getName() {
    return name;
}
int Father::getAge() {
    return age;
}
string Father::description() {
    stringstream ret;
    ret << "姓名:" << name << " 年龄:" << age ;
    return ret.str();
}

Son.h:

#pragma once
#include <string>
#include "Father.h"
using namespace std;
class Son:public Father
{
public:
    Son(const string& name,int age,const string& game);
    string getGame();
    string description();
private:
    string game;
};

Son.cpp:

#include "Son.h"
#include <string>
#include <sstream>
using namespace std;
string Son::getGame() {
    return game;
}
Son::Son(const string &name, int age, const string &game):Father(name,age) {
    //若没有显式的调用父类的构造函数,那么将会自动调用父类的默认构造函数
    cout << __FUNCTION__ << endl;
    this->game = game;
}
string Son::description() {
    stringstream ret;
    ret << "姓名:" << getName() << " 年龄:" << getAge() << " 游戏:" << game;
    return ret.str();
}

main.cpp:

#include <iostream>
#include <string>
#include "Father.h"
#include "Son.h"
int main() {
    Father wjl("老王", 68);
    Son wsc("小王",25,"电竞");
    
    cout << wjl.description() << endl;
    cout << wsc.description() << endl;

    system("pause");
    return 0;
}

运行结果:

标签:string,派生,继承,age,Father,C++,Son,include,name
From: https://www.cnblogs.com/smartlearn/p/16909532.html

相关文章

  • 利用xmake在c++项目中编译与调用webassmebly
    最近在尝试用webassembly替代lua作为c++程序的脚本。刚好xmake也支持了webassembly的编译。下面是踩坑记录。项目需要两个target:一个c++项目、一个webassembly项目。需要......
  • linux 多线程 c++2
    为了在用线程分离的时候保证即使主线程退出了,子线程还能正常运行,可以使用pthread_exit(NULL)去退出主线程,这样地址空间还能正常存在线程分离是为了保证主线程不会被堵塞,还能......
  • C++ Tips:signed main 和 int main 的区别?
    #include<bits/stdc++.h>#defineintlonglongusingnamespacestd;signedmain(){return0;}注意到#defineintlonglong而main函数必须返回一个in......
  • c++:模板
    一、模板的基本概念c++除了面向对象的编程思想之外,还有泛型编程,主要技术是模板。c++提供两种模板机制:函数模板,类模板。函数模板:建立一个通用函数,其函数返回值和形参类型......
  • 102:继承
    ###集成继承是面向对象程序设计的重要特征,也是实现“代码复用”的重要手段。如果一个新类继承自一个设计好的类,就直接具备了已有类的特征,就大大降低了工作难度。已有的类,......
  • C++面向对象程序设计概念梳理
    写在前面:本篇文档是为了《C++面向对象程序设计》课程所写,包含了本课程考试可能会考的所有概念。当然,因为目的只是为了通过考试,所以我将这些概念已经尽量精简。如果你将下......
  • c++:函数的重载
    一、重载的定义对于同一种功能但是参数不一样的函数,使用重载就可以实现功能相同的函数拥有相同的函数名,在调用函数的时候,通过实参的类型,判断调用哪个函数。【不使用重载......
  • c/c++常见的数据类型表示的范围
    首先参考博客 ​​C语言:基本数据类型及表示范围-myrj-这里有个疑问,int和long到底什么区别:看了一些博客:​​在C+中,int和long有什么区别?​​​​C和C++中int和long有什么......
  • Android 编译C++
    Android编译C++项目​​前言​​​​正文​​​​一、基本知识​​​​①要做什么?​​​​②JNI是什么?​​​​③NDK是什么?​​​​二、配置NDK​​​​三、创建新工程......
  • [排序算法] 归并排序 (C++)
    归并排序解释归并排序MergeSort是典型的分治法的应用,其算法步骤完全遵循分治模式。分治法思想分治法思想:将原问题分解为几个规模较小但又保持原问题性质的子问题,......