刚学c++,遇到问题请大神指点哪错了。谢谢

C++语言 码拜 9年前 (2015-05-11) 1001次浏览 0个评论
 

#include<iostream>
using namespace std;
class A
{
public:
void get(){x=3,y=4,z=5;}
int x;
int fun2(){return z;}
protected:
int y;
private:
int z;
};
class B:public A
{
public:
int fun1(){return y;}
};
void main()
{
B b;
b.get();
cout<<b.x<<endl;
b.fun1();
cout<<b.fun1<<endl;
b.fun2();
cout<<b.fun2<<endl;
}

10分
#include <iostream>
#include <cstdio>

using namespace std;
class A
{
    public:
        void get(){x=3,y=4,z=5;}
        int x;
        int fun2(){return z;}
    protected:
        int y;
    private:
        int z;
};

class B:public A
{
    public:
        int fun1(){return y;}
};

int main(int argc, char *argv[])
{
    B b;
    b.get();
    cout<<b.x<<endl;
    b.fun1();
    b.fun2();

    printf("%p\n", &B::fun1);
    printf("%p\n", &B::fun2);

    return 0;
}
5分
cout<<b.fun1<<endl;
fun1是函数不是成员变量啊
5分
函数调用得使用()

cout << b.fun1() << endl;
cout << b.fun2() << endl;
非常感谢  恍然大悟
如果换成私有继承呢如何访问基类?
#include<iostream>
using namespace std;
class A
{
public:
void get(){x=3, y=4,z=5;}
int x;
protected:
int y;
private:
int z;
};
class B:private A
{
public:
int fun1(){return x;}
    int fun2(){return y;}
};
void main()
{
B b;
b.fun1();
cout<<b.fun1()<<endl;
b.fun2();
cout<<b.fun2()<<endl;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明刚学c++,遇到问题请大神指点哪错了。谢谢
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!