继承
class A:public B
子类 : 继承方式 父类
class Phone
{
public:
Phone()
{
frame = "框架";
screen = "屏幕";
battery = "电池";
}
public:
string frame;
string screen;
string battery;
};
class Brand:public Phone
{
public:
Brand(string str)
{
brd = str;
}
public:
string brd;
};
ostream& operator<<(ostream &cout, Brand &p)
{
cout << p.frame << endl;
cout << p.screen<< endl;
cout << p.battery << endl<<endl;
cout << p.brd ;
return cout;
}
void test()
{
Brand b1("水果");
Brand b2("为华");
Brand b3("大米");
cout << b1<< endl;
cout << "------------------------------" << endl;
cout << b2<< endl;
cout << "------------------------------" << endl;
cout << b3 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
继承可以减少代码重复量
继承的同名成员访问
要使用.加作用域访问
class A
{
public:
A()
{
m_a = 100;
}
int m_a;
};
class B : public A
{
public:
B()
{
m_a = 200;
}
int m_a;
};
int main()
{
B b;
cout << "B.m_a= "<<b.m_a << endl;
cout << "A.m_a= " << b.A::m_a << endl;
system("pause");
return 0;
}
多继承
一个儿子认多个爹
class A;
class B;
class C : public A, public B;
由于多继承使用较少只做了解
标签:string,继承,C++,class,Phone,int,public From: https://blog.51cto.com/u_16071993/6506802