继承公有
继承的目的是为了代码重用。就是相当于复制粘贴。
class shop
{
public:
int m_a;
void saledailygood()
{
cout<<"卖日用品"<<endl;
return;
}
};
class supermarket:public shop//继承了上一个类里面的public
{
public:
void salefood()
{
m_a = 10;//因为上一个类里面已经定力m_a所以这里面可以省略一些
cout<<"食品的价格是:"<<m_a<<endl;
return;
}
};
int main()
{
supermarket m;//supermarket的等级比较低,所以用它来初始化
m.saledailygood();
m.salefood();
return 0;
}
继承保护的
class shop
{
protected://继承保护的变量以及函数,这里的别丢了ed;
int m_a;
public:
void saledailygood()
{
cout<<"卖日用品"<<endl;
return;
}
};
class supermarket:public shop
{
public:
void salefood()
{
m_a = 10;
cout<<"食品的价格是:"<<m_a<<endl;
return;
}
};
int main()
{
supermarket m;
m.saledailygood();
m.salefood();
return 0;
}
Paste_Image.png
私有继承:没有太大意义
class shop
{
private:
int m_a;
public:
void setb(int a)
{
m_a=a;
}
int getb()
{
return m_a;
}
void saledailygood()
{
cout<<"卖日用品"<<endl;
return;
}
};
class supermarket:public shop
{
public:
void salefood()
{
// m_a = 10;
setb(20);
cout<<getb()<<"食品的价格是:"<<endl;
return;
}
};
int main()
{
supermarket m;
m.saledailygood();
m.salefood();
return 0;
}
继承可以通过儿子找到父亲
class shop
{
public:
shop()
{
cout<<"c++ is hard to learn"<<endl;
}
};
class supermarket:public shop
{
public:
supermarket()
{
cout<<"you are a good boy"<<endl;
}
};
int main()
{
supermarket m;//这里面只是调用了supermarket类的构造函数,但是它自己因为继承的原因,
所以也找到了shop的构造函数。
return 0;
}
如果父类的构造函数有了形参,可以用手动调用给其赋值
class shop
{
int m_a;
public:
shop(int a)//这里面带了形参
{
m_a=a;
cout<<"c++ is hard to learn"<<endl;
}
};
class supermarket:public shop
{
public:
supermarket():shop(222)//这里是给父类传参-->这里注意一下,这么传参不是太理解。
{
cout<<"you are a good boy"<<endl;
}
};
int main()
{
supermarket m;
return 0;
}
构造函数加析构函数的继承
class shop
{
int m_a;
public:
shop(int a)
{
m_a=a;
cout<<"~基类c++ is hard to learn"<<endl;
}
~shop()
{
cout<<"基类析构"<<endl;
}
};
class test
{
public:
test()
{
cout<<"test===="<<endl;
}
~test()
{
cout<<"~test===="<<endl;
}
};
class supermarket:public shop
{
test m_t;
public:
supermarket():shop(222)
{
cout<<"子类:you are a good boy"<<endl;
}
~supermarket()
{
cout<<"~子类:you are a good boy"<<endl;
}
};
int main()
{
supermarket m;
cout<<"--------------"<<endl;
return 0;
}
如何调用其它类名里的函数
class B0
{
public:
void display()
{
cout<<"B0 display"<<endl;
}
};
class B1:public B0
{
public:
void display()
{
cout<<"B1 display"<<endl;
}
};
class D1:public B1
{
public:
void display()
{
cout<<"D1 display"<<endl;
}
};
int main()
{
B0 b0;
b0.display();
B1 b1;
b1.display();
D1 d1;
d1.display();//它只会显示与类本身最近的函数。
d1.B1::display();//调用其他类里面的函数
return 0;
}
多态:关键字virtual
class B0
{
public:
virtual void display()//多态的关键字放在基类,这个函数是虚函数
{
cout<<"B0 display"<<endl;
}
};
class B1:public B0
{
public:
void display()//和基类有相同的函数名
{
cout<<"B1 display"<<endl;
}
};
class D1:public B1
{
public:
void display()
{
cout<<"D1 display"<<endl;
}
};
int main()
{
B0 b0,*p0;
p0=&b0;
p0->display();
B1 b1;
p0=&b1;
p0->display();//如果没有virtual,那么这个值和b0是一样的。
D1 d1;
d1.display();
d1.B1::display();
return 0;
}
多重继承:
但是现实生活中很多东西不能多重继承,比如名字,身份证号码。
class B0
{
public:
virtual void display()
{
cout<<"B0 display"<<endl;
}
};
class B1:public B0
{
public:
void display()
{
cout<<"B1 display"<<endl;
}
};
class D1:public B1
{
public:
void display()
{
cout<<"D1 display"<<endl;
}
};
int main()
{
B0 b0,*p0;
p0=&b0;
p0->display();
B1 b1;
p0=&b1;
p0->display();
D1 d1;
d1.display();
d1.B1::display();
return 0;
}
多重继承里面用了虚继承
class person
{
public:
int id;
};
class father:virtual public person//如果不用virtual,会编译不过他会告诉你:
对成员'id'的请求是不明确的
{
public:
void money()
{
cout<<"有钱"<<endl;
}
};
class mother:virtual public person
{
public:
void face()
{
cout<<"漂亮"<<endl;
}
};
class son:public father,public mother
{
};
int main(int argc,char **argv)
{
son s;
s.id=3;
person *p=&s;
}
关于虚拟继承的打印顺序问题
class A
{
public:
A()
{
cout<<"A cons---"<<endl;
}
~A()
{
cout<<"----!A"<<endl;
}
};
class B
{
public:
B()
{
cout<<"B cons---"<<endl;
}
~B()
{
cout<<"----!B"<<endl;
}
};
class C
{
public:
C()
{
cout<<"C cons---"<<endl;
}
~C()
{
cout<<"----!C"<<endl;
}
};
class D: public A, public B, virtual public C //这里的C用了虚拟继承,
打印的时候先打印 C的类。
{
public:
D()
{
cout<<"D cons---"<<endl;
}
~D()
{
cout<<"----!D"<<endl;
}
};
int main()
{
D d;
cout<<"============================"<<endl;
}
网友评论