首页 > 代码库 > C++中类的继承
C++中类的继承
#include <iostream.h>
using namespace std;
class A {
public:
int a;
protected:
int b;
private:
int c;
public:
void test() {
cout<<"test public a\n";
}
A(int a) {
this->a=a;
}
A() {
}
protected:
void test1() {
cout<<"test1 protected\n";
}
private:
void test2() {
cout<<"test2 private\n";
}
};
class B:public A {
public:
void testb() {
this->test1(); //¼Ì³ÐÏÂÀ´ÁË ¶øÇÒ protected public ¶¼Êǿɼ̳еÄ
}
/*void testb1() {
this->test2();
} */ //²»¿É±»¼Ì³ÐµÄ
void testb2() {
cout<<"test b2 public\n";
}
protected:
void testb3() {
cout<<"test b3 protected\n";
}
};
class C:public A,public B {
//c++ÓïÑÔÀïÃæµÄ¶à¼Ì³Ð
};
class D:public A,protected B {
//¿É¼û B protected·½Ê½¼Ì³ÐÏÂÀ´µÄʱºò Ö»ÄÜÔÚº¯ÊýÀïÃæ·ÃÎÊ ÍâÃæÊDz»ÄÜ·ÃÎʵÄ
public:
void testd() {
this->testb2();
cout<<"----------------";
this->testb3();
}
};
int main() {
A a;
a.test();
B b;
b.testb();
C c;
c.testb2();
D d;
d.testd();
d.test();
return 0;
}
C++中类的继承