|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <iostream>
- #include <string>
- class A
- {
- public :
- A(std::string thename);
- int x;
- char *str;
- char *get_char();
- std::string name;
- void print();
- private:
- char *p;
- };
- class B:public A
- {
- public:
- B(std::string thename);
- };
- A::A(std::string thename)
- {
- x=2;
- str="QWER";
- name=thename;
- p="ZXCV";
- }
- B::B(std::string thename):A(thename)
- {
- x=3;
- str="1234";
- name=thename;
- }
- void A::print()
- {
- std::cout<<"A puting"<<std::endl;
- }
- /*
- void B::print()
- {
- std::cout<< "B puting"<<std::endl; // 打开这里编译报错,B中没有这个函数
- }
- */
- int main()
- {
- A a("A");
- B b("B");
-
- std::cout<<"x="<<b.x << "---name="<<b.name << "---str="<<b.str<<std::endl;
- std::cout<<"x="<<a.x << "---name="<<a.name << "---str="<<a.str<<std::endl;
- a.print();
- b.print(); //显示A puting
- return 0;
- }
复制代码
|
|