类模板继承

#include <iostream>
using namespace std;

class interface{
public:
    virtual int getAge()=0;
    virtual int getAge1()=0;
};

class person:virtual public interface{
public:
    person(int a){
        age = a;
        std::cout<<"age:"<<age<<endl;
    }
    int getAge(){
        return age;
    }
private:    
    int age;
};

class person1:virtual public interface{
public:
    person1(int a){
        age1 = a;
        std::cout<<"age1:"<<age1<<endl;
    }
    int getAge1(){
        return age1+12;
    }
private:    
    int age1;
};


template<typename ... _AttributeExtensions>
class student
    :virtual public _AttributeExtensions...{
    
    public:
        student(int a);
        ~student(){}
    int n;    
};

template <typename ... _AttributeExtensions>
student<_AttributeExtensions...>::student(int a):n(a),
        _AttributeExtensions(a)... {
}

int main(){
    student<person,person1> s(22);
    std::cout<<"age1:"<<s.getAge1()<<std::endl;
    std::cout<<"age:"<<s.getAge()<<std::endl;
    
return 0;
}
ygy@ygy-VirtualBox:~/work/tmp$ g++ c.cpp 
ygy@ygy-VirtualBox:~/work/tmp$ ./a.out 
age:22
age1:22
age1:34
age:22

 

posted @ 2022-03-10 15:58  于光远  阅读(72)  评论(0编辑  收藏  举报