zoukankan      html  css  js  c++  java
  • c++ 子类调用父类构造方法 调用父类方法 类声明与实现分离

    Person.h

    #pragma once
    #include "stdafx.h"
    #include<iostream>
    class Person {
    private:
        std::string name;
        int age;
    public:
        void sayHello();
        Person(std::string name, int age);
    };

    Person.cpp

    #include "stdafx.h"
    #include<iostream>
    #include "Person.h"
    #include <string>
    Person::Person(std::string name, int age)
    {
        this->name = name;
        this->age = age;
    }
    void Person::sayHello() {
        std::cout << "hell my name is: " << this->name << std::endl;
    }

    Male.h

    #pragma once
    #include"Person.h"
    #include<string>
    class Male:Person {
    private :
        std::string gender;
    public:
        Male::Male(std::string gender, std::string name, int age) ;
        void sayHello();
    };

    Male.cpp

    #include "stdafx.h"
    #include"Male.h"
    #include<iostream>
    #include<string>
    
    Male::Male(std::string gender, std::string name, int age) :Person(name, age)
    {
        this->gender = gender;
    }
    void Male::sayHello(){
        Person::sayHello();
        std::cout << "hell my gender is: " << this->gender << std::endl;
    }

     ConsoleApplication1.cpp

    // ConsoleApplication1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <math.h>
    #include <string>
    #include "Person.h"
    #include "Male.h"
    int main()
    {
        using namespace std;
        
        Person* person = new Person("martin", 18);
        person->sayHello();
    
        Male* male = new Male("male","wangdada", 26);
        male->sayHello();
    
        cin.get();
        return 0;
    }

    运行结果

  • 相关阅读:
    springMVC中@RequestParam和@RequestBody的作用
    java 中Excel的导入导出
    数据库字段名称与实体类属性不一致的处理措施
    linux下解压命令大全
    linux 下 cat
    mysqli
    解析php mysql 事务处理回滚操作(附实例)
    css 样式(checkbox开关、css按钮)
    cetons 怎么强制卸载 PHP
    yhdsir@function:php
  • 原文地址:https://www.cnblogs.com/heben/p/5458559.html
Copyright © 2011-2022 走看看