基类与派生类的对应关系
1.单继承
派生类只从一个基类派生
2.多继承
派生类从多个基类派生
3.多重派生
由一个基类派生出多个不同的派生类
4.多层派生
派生类又作为基类,继续派生新的类
//注释 下面的程序要注意继承时候的继承的方式
*同种访问权限
#include<iostream>
class father
{
private:
int tall;
public:
void seta(int a){tall=a;}
void print1(){std::cout<<"身高="<<tall<<std::endl;}
};
class mother
{
private:
int weight;
public:
void setb(int b){weight=b;}
void print2(){std::cout<<"体重="<<weight<<std::endl;}
};
class son:public father,public mother
{
private:
int age;
public:
void setc(int c){age=c;}
void print3(){print1();print2();std::cout<<"年龄="<<age<<std::endl;}
};
int main()
{
son a;
a.seta(55);
a.setb(80);
a.setc(78);
a.print3();
return 0;
}
多种访问权限
#include<iostream>
class father
{
private:
int tall;
public:
void seta(int a){tall=a;}
void print1(){std::cout<<"身高="<<tall<<std::endl;}
};
class mother
{
private:
int weight;
public:
void setb(int b){weight=b;}
void print2(){std::cout<<"体重="<<weight<<std::endl;}
};
class son:public father,private mother
{
private:
int age;
public:
void setc(int c,int d){age=c;setb(d);}
void print3(){print1();print2();std::cout<<"年龄="<<age<<std::endl;}
};
int main()
{
son a;
a.seta(55);
a.setc(55,56);
a.print3();
return 0;
}
1.单继承
派生类只从一个基类派生
2.多继承
派生类从多个基类派生
3.多重派生
由一个基类派生出多个不同的派生类
4.多层派生
派生类又作为基类,继续派生新的类
//注释 下面的程序要注意继承时候的继承的方式
*同种访问权限
#include<iostream>
class father
{
private:
int tall;
public:
void seta(int a){tall=a;}
void print1(){std::cout<<"身高="<<tall<<std::endl;}
};
class mother
{
private:
int weight;
public:
void setb(int b){weight=b;}
void print2(){std::cout<<"体重="<<weight<<std::endl;}
};
class son:public father,public mother
{
private:
int age;
public:
void setc(int c){age=c;}
void print3(){print1();print2();std::cout<<"年龄="<<age<<std::endl;}
};
int main()
{
son a;
a.seta(55);
a.setb(80);
a.setc(78);
a.print3();
return 0;
}
多种访问权限
#include<iostream>
class father
{
private:
int tall;
public:
void seta(int a){tall=a;}
void print1(){std::cout<<"身高="<<tall<<std::endl;}
};
class mother
{
private:
int weight;
public:
void setb(int b){weight=b;}
void print2(){std::cout<<"体重="<<weight<<std::endl;}
};
class son:public father,private mother
{
private:
int age;
public:
void setc(int c,int d){age=c;setb(d);}
void print3(){print1();print2();std::cout<<"年龄="<<age<<std::endl;}
};
int main()
{
son a;
a.seta(55);
a.setc(55,56);
a.print3();
return 0;
}