您的位置:首页 > 博客中心 > 互联网 >

静态成员函数和静态成员变量,以及继承

时间:2022-05-11 13:01

Father.hpp

 1 //
 2 // Created by Administrator on 2021/7/15.
 3 //
 4 
 5 #ifndef C__TEST01_FATHER_HPP
 6 #define C__TEST01_FATHER_HPP
 7 
 8 
 9 class Father {
10 public:
11     Father(){
12         this->age = 100;
13     }
14     ~Father(){}
15     //静态成员函数只能访问静态变量
16     static void getName(){
17         name = "niao";
18         cout<<"name = "<

Son.hpp

 1 //
 2 // Created by Administrator on 2021/7/15.
 3 //
 4 
 5 #ifndef C__TEST01_SON_HPP
 6 #define C__TEST01_SON_HPP
 7 
 8 #include "Father.hpp"
 9 
10 class Son: public Father{
11 public:
12     Son(){
13         this->age = 50;
14     }
15     ~Son(){}
16     static void getName(){
17         cout<<"name = "<

main.cpp

 1 #include 
 2 using namespace std;
 3 
 4 //#include "person.cpp"
 5 //类模板里面的成员函数写在cpp只能这么调用
 6 #include "Father.hpp"
 7 #include "Son.hpp"
 8 
 9 int main() {
10     //通过对象名调用
11     Father *father = new Father();
12     Son son;
13     father->getName();
14     father->getAge();
15     son.getName();
16     son.getAge();
17 
18     //通过类名调用
19     //Father::getAge();不是静态的
20     cout<<"通过类名调用"<

 

本类排行

今日推荐

热门手游