c++类继承的一些反思
1.在创建对象中,数据被存储,类只有一份
2.在继承中,派生类的空间中有一份基类的,派生类和基类共有基类的数据,所以我们可以用父类初始化
student(int n,int m):person(n){
this->m=m;
}
3.s.print()会输出是父类类型,虽然子类继承了父类,但函数是父类的
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<typeinfo>
using namespace std;
class person{
public:
person(int n){
this->n=n;
}
void print(){
cout<<typeid(this).name();
cout<<this->n;
}
int n;
};
class student:public person{
public:
student(int n,int m):person(n){
this->m=m;
}
void nn(){
cout<<endl;
cout<<typeid(this).name();
cout<<this->n;
}
int m;
};
int main(){
student s(4,2);
s.print();//
}